Beispiel #1
0
        /// <summary>
        /// 核心执行方法   ==  执行方法
        /// </summary>
        /// <param name="recMsgXml">传入消息的xml</param>
        protected ResultMo <MsgContext> ProcessExecute(string recMsgXml)
        {
            var recMsgDirs = WxMsgHelper.ChangXmlToDir(recMsgXml);

            if (!recMsgDirs.ContainsKey("MsgType"))
            {
                return(new ResultMo <MsgContext>(ResultTypes.ParaNotMeet, "消息数据中未发现 消息类型(MsgType)字段!"));
            }

            string msgType = recMsgDirs["MsgType"].ToLower();

            if (msgType == "event")
            {
                if (!recMsgDirs.ContainsKey("Event"))
                {
                    return(new ResultMo <MsgContext>(ResultTypes.ParaNotMeet, "事件消息数据中未发现 事件类型(Event)字段!"));
                }
            }

            var context = ProcessExecute_BasicMsg(recMsgXml, msgType, recMsgDirs)
                          ?? ProcessExecute_AdvancedMsg(recMsgXml, msgType, recMsgDirs)
                          ?? ExecuteBasicMsgHandler(recMsgXml, recMsgDirs, UnknowHandler);

            return(new ResultMo <MsgContext>(context));
        }
Beispiel #2
0
        /// <summary>
        /// 核心执行方法    ==    验证签名和消息体信息解密处理部分
        /// </summary>
        /// <param name="recXml">消息内容</param>
        /// <param name="signature">微信加密签名</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="nonce">随机数</param>
        /// <returns>验证结果及相应的消息内容体 (如果加密模式,返回的是解密后的明文)</returns>
        protected ResultMo <string> ProcessBegin(string recXml, string signature,
                                                 string timestamp, string nonce)
        {
            if (string.IsNullOrEmpty(recXml))
            {
                return(new ResultMo <string>(ResultTypes.ObjectNull, "接收的消息体为空!"));
            }

            var resCheck = WxMsgHelper.CheckSignature(m_Config.Token, signature, timestamp, nonce);

            if (resCheck.IsSuccess)
            {
                if (m_Config.SecurityType != WxSecurityType.None)
                {
                    var dirs = WxMsgHelper.ChangXmlToDir(recXml);
                    if (dirs == null || !dirs.ContainsKey("Encrypt"))
                    {
                        return(new ResultMo <string>(ResultTypes.ObjectNull, "加密消息为空"));
                    }

                    var recMsgXml = Cryptography.WxAesDecrypt(dirs["Encrypt"], m_Config.EncodingAesKey);
                    return(new ResultMo <string>(recMsgXml));
                }
                return(new ResultMo <string>(recXml));
            }
            return(resCheck.ConvertToResultOnly <string>());
        }
Beispiel #3
0
        /// <summary>
        /// 核心执行方法
        /// </summary>
        /// <param name="contentXml">内容信息</param>
        /// <param name="signature">签名信息</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="nonce">随机字符创</param>
        /// <param name="echostr">验证服务器参数,如果存在则只进行签名验证,并将在结果Data中返回</param>
        /// <returns>消息结果,Data为响应微信数据,如果出错Message为错误信息</returns>
        public ResultMo <string> Process(string contentXml, string signature, string timestamp, string nonce,
                                         string echostr)
        {
            // 一.  检查是否是服务器验证
            if (!string.IsNullOrEmpty(echostr))
            {
                return(CheckServerValid(signature, timestamp, nonce, echostr));
            }

            // 二.  正常消息处理
            {
                var checkRes = ProcessBegin(contentXml, signature, timestamp, nonce);
                if (!checkRes.IsSuccess)
                {
                    return(checkRes.ConvertToResultOnly <string>());
                }

                var contextRes = ProcessExecute(checkRes.Data);
                if (!contextRes.IsSuccess)
                {
                    return(contextRes.ConvertToResultOnly <string>());
                }

                ProcessEnd(contextRes.Data);

                var resultString = contextRes.Data.ReplyMsg.ToReplyXml();
                if (m_Config.SecurityType != WxSecurityType.None &&
                    !string.IsNullOrEmpty(contextRes.Data.ReplyMsg.MsgType))
                {
                    return(WxMsgHelper.EncryptMsg(resultString, m_Config));
                }
                return(new ResultMo <string>(resultString));
            }
        }
Beispiel #4
0
        /// <summary>
        ///  服务器验证
        /// </summary>
        /// <param name="signature"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce"></param>
        /// <param name="echostr"></param>
        /// <returns></returns>
        public ResultMo <string> CheckServerValid(string signature, string timestamp, string nonce, string echostr)
        {
            var checkSignRes = WxMsgHelper.CheckSignature(m_Config.Token, signature, timestamp, nonce);
            var resultRes    = checkSignRes.ConvertToResultOnly <string>();

            resultRes.Data = resultRes.IsSuccess ? echostr : string.Empty;
            return(resultRes);
        }
Beispiel #5
0
        /// <summary>
        ///  根据具体的消息类型执行相关的消息委托方法(基础消息)
        /// </summary>
        /// <typeparam name="TRecMsg"></typeparam>
        /// <param name="recMsgXml"></param>
        /// <param name="recMsgDirs"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        private static MsgContext ExecuteBasicMsgHandler <TRecMsg>(string recMsgXml,
                                                                   IDictionary <string, string> recMsgDirs, Func <TRecMsg, BaseReplyMsg> func)
            where TRecMsg : BaseRecMsg, new()
        {
            var msgContext = new MsgContext();

            var recMsg = WxMsgHelper.GetMsg <TRecMsg>(recMsgDirs);

            recMsg.RecMsgXml = recMsgXml;

            msgContext.ReplyMsg = ExecuteHandler(recMsg, func);
            msgContext.RecMsg   = recMsg;

            return(msgContext);
        }