Example #1
0
        /// <summary>
        /// 执行过滤器
        /// </summary>
        /// <param name="input">微信的请求数据</param>
        /// <param name="signature">微信请求时携带的signature</param>
        /// <param name="nonce">微信请求时携带的nonce</param>
        /// <param name="timestamp">微信请求时携带的timestamp</param>
        /// <returns></returns>
        public static MiddlewareParameter Execute(string input, string signature, string nonce, string timestamp)
        {
            if (Configurations.Current.EnumMessageMode == Configurations.TransferMode.Cipher)
            {
                WXBizMsgCrypt crypt = new WXBizMsgCrypt();
                var           items = crypt.DecryptMsg(signature, timestamp, nonce, input);
                if (!items.Item1)
                {
                    throw new Exception(items.Item2);
                }
                input = items.Item2;
            }

            var requestModel = input.ExXmlToEntity <RequestBase>();

            requestModel.Nonce     = nonce;
            requestModel.Timestamp = timestamp;
            requestModel.Signature = signature;

            var middleInput = new MiddlewareParameter
            {
                Input = new MiddlewareInput(input, requestModel)
            };

            return(Execute(middleInput));
        }
Example #2
0
        /// <summary>
        /// 执行过滤器
        /// </summary>
        /// <returns></returns>
        private static MiddlewareParameter Execute(MiddlewareParameter middlareResult)
        {
            //全局开始过滤器
            GlobalPreFilters.ForEach(filter => filter(middlareResult));

            //处理文本消息
            if (middlareResult.Input.RequestBaseModel.MsgType == Configurations.Current.MessageType.Text)
            {
                var textReq = middlareResult.Input.GetRequestModel <TextRequest>();
                TextFilters.Execute(textReq, middlareResult);
            }

            //处理图片消息
            if (middlareResult.Input.RequestBaseModel.MsgType == Configurations.Current.MessageType.Image)
            {
                var imageReq = middlareResult.Input.GetRequestModel <ImageRequest>();
                ImageFilters.Execute(imageReq, middlareResult);
            }

            //位置信息
            if (middlareResult.Input.RequestBaseModel.MsgType == Configurations.Current.MessageType.Location)
            {
                var locationReq = middlareResult.Input.GetRequestModel <LocationRequest>();
                LocationFilters.Execute(locationReq, middlareResult);
            }

            //事件
            if (middlareResult.Input.RequestBaseModel.MsgType == Configurations.Current.MessageType.Event)
            {
                var eb = middlareResult.Input.GetRequestModel <EventBase>();
                //普通过滤器
                EventFileters.Execute(eb, middlareResult);

                if (eb.Event == Configurations.Current.EventType.Click)
                {
                    //单击菜单事件
                    var clickReq = middlareResult.Input.GetRequestModel <ClickMenuRequest>();
                    ClickFilters.Execute(clickReq, middlareResult);
                }
                if (eb.Event == Configurations.Current.EventType.Scan)
                {
                    //扫描二维码事件
                    var scanReq = middlareResult.Input.GetRequestModel <ScanQrRequest>();
                    ScanQrFilters.Execute(scanReq, middlareResult);
                }
                if (eb.Event == Configurations.Current.EventType.Location)
                {
                    //位置事件
                    var positionReq = middlareResult.Input.GetRequestModel <PositionRequest>();
                    PositionFilter.Execute(positionReq, middlareResult);
                }
            }
            //执行 语音识别结果 过滤器
            if (middlareResult.Input.RequestBaseModel.MsgType == Configurations.Current.MessageType.Voice)
            {
                var voiceReq = middlareResult.Input.GetRequestModel <VoiceRequest>();
                VoiceFilters.Execute(voiceReq, middlareResult);
            }

            //全局结束过滤器
            GlobalEndFilters.ForEach(filter => filter(middlareResult));

            return(middlareResult);
        }
 /// <summary>
 /// 执行过滤器
 /// </summary>
 /// <param name="req"></param>
 /// <param name="middleware"></param>
 public void Execute(TRequest req, MiddlewareParameter middleware)
 {
     this._filters.ForEach(filter => filter(req, middleware));
 }