/// <summary> /// 核心执行 过程的 验签和解密 /// </summary> /// <param name="recXml">消息内容</param> /// <param name="signature">微信加密签名</param> /// <param name="timestamp">时间戳</param> /// <param name="nonce">随机数</param> /// <returns>验证结果及相应的消息内容体 (如果加密模式,返回的是解密后的明文)</returns> private ResultMo <string> PrepareExecute(string recXml, string signature, string timestamp, string nonce) { if (string.IsNullOrEmpty(recXml)) { return(new ResultMo <string>(ResultTypes.ObjectNull, "接收的消息体为空!")); } var resCheck = WxMsgHelper.CheckSignature(ApiConfig.Token, signature, timestamp, nonce); if (!resCheck.IsSuccess()) { return(resCheck.ConvertToResult <string>()); } if (ApiConfig.SecurityType == WxSecurityType.None) { return(new ResultMo <string>(recXml)); } var dirs = WxMsgHelper.ChangXmlToDir(recXml, out XmlDocument xmlDoc); if (dirs == null || !dirs.TryGetValue("Encrypt", out var encryStr) || string.IsNullOrEmpty(encryStr)) { return(new ResultMo <string>(ResultTypes.ObjectNull, "加密消息为空")); } var recMsgXml = Cryptography.WxAesDecrypt(encryStr, ApiConfig.EncodingAesKey); return(new ResultMo <string>(recMsgXml)); }
/// <summary> /// 核心执行方法 == 执行方法 /// </summary> /// <param name="recMsgXml">传入消息的xml</param> protected ResultMo <MsgContext> ProcessExecute(string recMsgXml) { XmlDocument xmlDoc = null; var recMsgDirs = WxMsgHelper.ChangXmlToDir(recMsgXml, ref xmlDoc); if (!recMsgDirs.ContainsKey("MsgType")) { return(new ResultMo <MsgContext>(ResultTypes.ParaError, "消息数据中未发现 消息类型(MsgType)字段!")); } string msgType = recMsgDirs["MsgType"].ToLower(); if (msgType == "event") { if (!recMsgDirs.ContainsKey("Event")) { return(new ResultMo <MsgContext>(ResultTypes.ParaError, "事件消息数据中未发现 事件类型(Event)字段!")); } } var context = ProcessExecute_BasicMsg(xmlDoc, msgType, recMsgDirs) ?? ProcessExecute_AdvancedMsg(xmlDoc, msgType, recMsgDirs) ?? ExecuteBasicMsgHandler(xmlDoc, recMsgDirs, UnknowHandler); return(new ResultMo <MsgContext>(context)); }
/// <summary> /// 核心执行方法 过程中的 委托方代码执行 /// </summary> /// <param name="recMsgXml">传入消息的xml</param> /// <returns></returns> protected virtual ResultMo <WxMsgContext> Execute(string recMsgXml) { var recMsgDirs = WxMsgHelper.ChangXmlToDir(recMsgXml, out XmlDocument xmlDoc); recMsgDirs.TryGetValue("MsgType", out var msgType);// recMsgDirs["MsgType"].ToLower(); string eventName = null; if (msgType == "event") { if (!recMsgDirs.TryGetValue("Event", out eventName)) { return(new ResultMo <WxMsgContext>(ResultTypes.ParaError, "事件消息数据中未发现 事件类型(Event)字段!")); } } var processor = GetBasicMsgProcessor(msgType, eventName); if (!(processor?.CanExecute).HasValue) { processor = GetCustomProcessor(msgType, eventName, recMsgDirs); if (!(processor?.CanExecute).HasValue) { processor = GetRegProcessor(msgType, eventName); } } var context = processor != null && processor.CanExecute ? ExecuteProcessor(xmlDoc, recMsgDirs, processor.CreateNewInstance(), processor.Execute) : ExecuteProcessor(xmlDoc, recMsgDirs, new WxBaseRecMsg(), null); ExecuteEnd(context); return(new ResultMo <WxMsgContext>(context)); }
/// <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) { XmlDocument xmlDoc = null; var dirs = WxMsgHelper.ChangXmlToDir(recXml, ref xmlDoc); 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>()); }