示例#1
0
        internal async Task <bool> InitializeAsync(
            WeixinWelcomePageOptions options,
            HttpContext context,
            ILogger logger,
            IWeixinMessageEncryptor encryptor)
        {
            _options    = options ?? throw new ArgumentNullException(nameof(options));
            HttpContext = context ?? throw new ArgumentNullException(nameof(context));
            _logger     = logger ?? throw new ArgumentNullException(nameof(logger));
            _encryptor  = encryptor ?? throw new ArgumentNullException(nameof(encryptor));

            context.Response.OnStarting(OnStartingCallback, this);
            {
                InitializeResult = await HandleOnceAsync();

                if (InitializeResult?.Handled == true)
                {
                    return(true);
                }
                if (InitializeResult?.Failure != null)
                {
                    _logger.LogWarning(0, InitializeResult.Failure, InitializeResult.Failure.Message);
                }
            }
            return(false);
        }
示例#2
0
 protected async Task <WeixinMessageHandleResult> HandleOnceSafeAsync()
 {
     try
     {
         return(await HandleOnceSafeAsync());
     }
     catch (Exception ex)
     {
         return(WeixinMessageHandleResult.Fail(ex));
     }
 }
示例#3
0
        /// <summary>
        /// 执行微信请求
        /// </summary>
        protected async Task <WeixinMessageHandleResult> HandleAsync()
        {
            bool handled = false;

            var xml = new StreamReader(HttpContext.Request.Body).ReadToEnd();

            _logger.LogDebug("Request Body({0}): {1}", xml?.Length, xml);

            //Try decrypt if needed
            var encryptType = HttpContext.Request.Query["encrypt_type"];             //aes

            _logger.LogDebug("encrypt_type: {0}", encryptType);
            var isEncrypted = encryptType == "aes";

            _logger.LogDebug("isEncrypted: {0}", isEncrypted);
            if (isEncrypted)
            {
                var msg_signature = HttpContext.Request.Query["msg_signature"];
                _logger.LogDebug("msg_signature: {0}", msg_signature);
                var timestamp = HttpContext.Request.Query["timestamp"];
                _logger.LogDebug("timestamp: {0}", timestamp);
                var nonce = HttpContext.Request.Query["nonce"];
                _logger.LogDebug("nonce: {0}", nonce);

                var decryptedXml = _encryptor.Decrypt(msg_signature, timestamp, nonce, xml);
                _logger.LogDebug("Decrypted Request Body({0}): {1}", decryptedXml?.Length, decryptedXml);

                xml = decryptedXml;
            }

            var received = XmlConvert.DeserializeObject <ReceivedEventArgs>(xml);

            switch (received.MsgType)
            {
            case ReceivedMsgType.@event:
            {
                var ev = XmlConvert.DeserializeObject <EventReceivedEventArgs>(xml);
                switch (ev.Event)
                {
                case ReceivedEventType.subscribe:
                {
                    var x   = XmlConvert.DeserializeObject <SubscribeEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <SubscribeEventReceivedEventArgs>(this, x, isEncrypted);
                    handled = await _options.Events.SubscribeEventReceived(ctx);
                }
                break;

                case ReceivedEventType.unsubscribe:
                {
                    var x   = XmlConvert.DeserializeObject <UnsubscribeEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <UnsubscribeEventReceivedEventArgs>(this, x, isEncrypted);
                    handled = await _options.Events.UnsubscribeEventReceived(ctx);
                }
                break;

                case ReceivedEventType.SCAN:
                {
                    var x   = XmlConvert.DeserializeObject <QrscanEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <QrscanEventReceivedEventArgs>(this, x, isEncrypted);
                    handled = await _options.Events.QrscanEventReceived(ctx);
                }
                break;

                case ReceivedEventType.LOCATION:
                {
                    var x   = XmlConvert.DeserializeObject <LocationEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <LocationEventReceivedEventArgs>(this, x, isEncrypted);
                    handled = await _options.Events.LocationEventReceived(ctx);
                }
                break;

                case ReceivedEventType.CLICK:
                {
                    var x   = XmlConvert.DeserializeObject <ClickMenuEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <ClickMenuEventReceivedEventArgs>(this, x, isEncrypted);
                    handled = await _options.Events.ClickMenuEventReceived(ctx);
                }
                break;

                case ReceivedEventType.VIEW:
                {
                    var x   = XmlConvert.DeserializeObject <ViewMenuEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <ViewMenuEventReceivedEventArgs>(this, x, isEncrypted);
                    handled = await _options.Events.ViewMenuEventReceived(ctx);
                }
                break;

                //case ReceivedEventType.ENTER://已确认被腾讯移除!
                //	{
                //		var x = XmlConvert.DeserializeObject<EnterEventReceivedEventArgs>(xml);
                //		var ctx = new WeixinReceivedContext<EnterEventReceivedEventArgs>(this, x, isEncrypted);
                //		handled = await _options.Events.EnterEventReceived(ctx);
                //	}
                //	break;
                default:
                    throw new NotSupportedException($"不支持的事件[{ev.Event.ToString()}]");
                }
            }
            break;

            case ReceivedMsgType.text:
            {
                var x   = XmlConvert.DeserializeObject <TextMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <TextMessageReceivedEventArgs>(this, x, isEncrypted);
                handled = await _options.Events.TextMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.image:
            {
                var x   = XmlConvert.DeserializeObject <ImageMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <ImageMessageReceivedEventArgs>(this, x, isEncrypted);
                handled = await _options.Events.ImageMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.voice:
            {
                var x   = XmlConvert.DeserializeObject <VoiceMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <VoiceMessageReceivedEventArgs>(this, x, isEncrypted);
                handled = await _options.Events.VoiceMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.video:
            {
                var x   = XmlConvert.DeserializeObject <VideoMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <VideoMessageReceivedEventArgs>(this, x, isEncrypted);
                handled = await _options.Events.VideoMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.shortvideo:
            {
                var x   = XmlConvert.DeserializeObject <ShortVideoMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <ShortVideoMessageReceivedEventArgs>(this, x, isEncrypted);
                handled = await _options.Events.ShortVideoMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.location:
            {
                var x   = XmlConvert.DeserializeObject <LocationMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <LocationMessageReceivedEventArgs>(this, x, isEncrypted);
                handled = await _options.Events.LocationMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.link:
            {
                var x   = XmlConvert.DeserializeObject <LinkMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <LinkMessageReceivedEventArgs>(this, x, isEncrypted);
                handled = await _options.Events.LinkMessageReceived(ctx);
            }
            break;

            default:
                throw new NotSupportedException($"不支持的信息类型[{received.MsgType.ToString()}]");
            }

            await Task.FromResult(0);

            return(handled ? WeixinMessageHandleResult.Handle() : WeixinMessageHandleResult.Fail("未处理"));
        }
示例#4
0
        /// <summary>
        /// 执行微信请求
        /// </summary>
        public async Task <WeixinMessageHandleResult> HandleAsync()
        {
            bool handled = false;

            var xml = new StreamReader(_context.Request.Body).ReadToEnd();

            _logger.LogDebug("Request Body({0}): {1}", xml?.Length, xml);

            var received = XmlConvert.DeserializeObject <ReceivedEventArgs>(xml);

            switch (received.MsgType)
            {
            case ReceivedMsgType.@event:
            {
                var ev = XmlConvert.DeserializeObject <EventReceivedEventArgs>(xml);
                switch (ev.Event)
                {
                case ReceivedEventType.subscribe:
                {
                    var x   = XmlConvert.DeserializeObject <SubscribeEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <SubscribeEventReceivedEventArgs>(this, x);
                    handled = await _options.Events.SubscribeEventReceived(ctx);
                }
                break;

                case ReceivedEventType.unsubscribe:
                {
                    var x   = XmlConvert.DeserializeObject <UnsubscribeEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <UnsubscribeEventReceivedEventArgs>(this, x);
                    handled = await _options.Events.UnsubscribeEventReceived(ctx);
                }
                break;

                case ReceivedEventType.SCAN:
                {
                    var x   = XmlConvert.DeserializeObject <QrscanEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <QrscanEventReceivedEventArgs>(this, x);
                    handled = await _options.Events.QrscanEventReceived(ctx);
                }
                break;

                case ReceivedEventType.LOCATION:
                {
                    var x   = XmlConvert.DeserializeObject <LocationEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <LocationEventReceivedEventArgs>(this, x);
                    handled = await _options.Events.LocationEventReceived(ctx);
                }
                break;

                case ReceivedEventType.CLICK:
                {
                    var x   = XmlConvert.DeserializeObject <ClickMenuEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <ClickMenuEventReceivedEventArgs>(this, x);
                    handled = await _options.Events.ClickMenuEventReceived(ctx);
                }
                break;

                case ReceivedEventType.VIEW:
                {
                    var x   = XmlConvert.DeserializeObject <ViewMenuEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <ViewMenuEventReceivedEventArgs>(this, x);
                    handled = await _options.Events.ViewMenuEventReceived(ctx);
                }
                break;

                case ReceivedEventType.ENTER:
                {
                    var x   = XmlConvert.DeserializeObject <EnterEventReceivedEventArgs>(xml);
                    var ctx = new WeixinReceivedContext <EnterEventReceivedEventArgs>(this, x);
                    handled = await _options.Events.EnterEventReceived(ctx);
                }
                break;

                default:
                    throw new NotSupportedException($"不支持的事件[{ev.Event.ToString()}]");
                }
            }
            break;

            case ReceivedMsgType.text:
            {
                var x   = XmlConvert.DeserializeObject <TextMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <TextMessageReceivedEventArgs>(this, x);
                handled = await _options.Events.TextMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.image:
            {
                var x   = XmlConvert.DeserializeObject <ImageMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <ImageMessageReceivedEventArgs>(this, x);
                handled = await _options.Events.ImageMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.link:
            {
                var x   = XmlConvert.DeserializeObject <LinkMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <LinkMessageReceivedEventArgs>(this, x);
                handled = await _options.Events.LinkMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.location:
            {
                var x   = XmlConvert.DeserializeObject <LocationMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <LocationMessageReceivedEventArgs>(this, x);
                handled = await _options.Events.LocationMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.voice:
            {
                var x   = XmlConvert.DeserializeObject <VoiceMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <VoiceMessageReceivedEventArgs>(this, x);
                handled = await _options.Events.VoiceMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.video:
            {
                var x   = XmlConvert.DeserializeObject <VideoMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <VideoMessageReceivedEventArgs>(this, x);
                handled = await _options.Events.VideoMessageReceived(ctx);
            }
            break;

            case ReceivedMsgType.shortvideo:
            {
                var x   = XmlConvert.DeserializeObject <ShortVideoMessageReceivedEventArgs>(xml);
                var ctx = new WeixinReceivedContext <ShortVideoMessageReceivedEventArgs>(this, x);
                handled = await _options.Events.ShortVideoMessageReceived(ctx);
            }
            break;

            default:
                throw new NotSupportedException($"不支持的信息类型[{received.MsgType.ToString()}]");
            }

            await Task.FromResult(0);

            return(handled ? WeixinMessageHandleResult.Handle() : WeixinMessageHandleResult.Fail("未处理"));
        }