/// <summary> /// 根据Post参数获取对应处理事件 /// </summary> /// <param name="postParamater"></param> /// <returns></returns> public static WeChatHandle GetWeChatHandle(U_WeChatCallbackParameter wcp, Stream inputStream) { string postParamater = WeChatCallbackLogic.GetPostParameter(wcp, inputStream); XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(new System.IO.MemoryStream(System.Text.Encoding.GetEncoding("GB2312").GetBytes(postParamater))); XmlNode MsgType = xmldoc.SelectSingleNode("/xml/MsgType"); WeChatHandle weChatHandle = null; if (MsgType != null) { switch (MsgType.InnerText) { case "event": weChatHandle = new WeChatEventHandle(wcp, xmldoc); //事件处理 break; case "text": weChatHandle = new WeChatTextHandle(wcp, xmldoc); //事件处理 break; default: break; } } return(weChatHandle); }
public static string GetPostParameter(U_WeChatCallbackParameter wcp, Stream inputStream) { Byte[] postBytes = new Byte[inputStream.Length]; inputStream.Read(postBytes, 0, (Int32)inputStream.Length); string postString = Encoding.UTF8.GetString(postBytes); string postParmater = WeChatCallbackLogic.DecryptMessage(wcp, postString); return(postParmater); }
/// <summary> /// 被动响应给微信的数据加密 /// </summary> /// <param name="wcp"></param> /// <param name="postString"></param> /// <param name="message"></param> /// <returns></returns> public static string EncryptMessage(U_WeChatCallbackParameter wcp, string postString, string message) { WXBizMsgCrypt wxcpt = new WXBizMsgCrypt(ConfigSugar.GetAppString("WeChatCallbackToken"), ConfigSugar.GetAppString("WeChatCallbackEncodingAESKey"), ConfigSugar.GetAppString("CorpID")); string result = postString; int errorCode = wxcpt.EncryptMsg(message, wcp.timestamp, wcp.nonce, ref result); if (errorCode != 0) { //错误记录日志 LogHelper.WriteLog(errorCode.ToString()); } return(result); }
/// <summary> /// 开启微信回调模式 /// </summary> /// <returns></returns> public string OpenWeChatCallbackMode(U_WeChatCallbackParameter wcp) { //数据不为空,表示验证开启微信回调模式 //数据为空,表示微信数据正常回调 if (!string.IsNullOrEmpty(wcp.echostr)) { string corpid = ""; string echostr = Cryptography.AES_decrypt(wcp.echostr, ConfigSugar.GetAppString("WeChatCallbackEncodingAESKey"), ref corpid); return(echostr); } else { WeChatHandle wch = WeChatEventFactory.GetWeChatHandle(wcp, Request.InputStream); return(wch.ExecuteEventHandle()); } }
/// <summary> /// 构造一个新的微信事件处理类型 /// </summary> /// <param name="xmldoc"></param> public WeChatEventHandle(U_WeChatCallbackParameter wcp, XmlDocument xmldoc) { _wcp = wcp; AnalysisParameter(xmldoc); }