RequestMessageHelper.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using System.Web;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. using ZcPeng.PublicLibrary;
  8. using JCSoft.WX.Framework.Models.Requests;
  9. using JCSoft.WX.Framework.Models;
  10. namespace Common.Wechat
  11. {
  12. /// <summary>
  13. /// RequestMessage:请求消息辅助类
  14. /// </summary>
  15. public class RequestMessageHelper
  16. {
  17. /// <summary>
  18. /// 将xml节点CDATA转换成json之后的键名
  19. /// </summary>
  20. private const string CDATA_KEY = "#cdata-section";
  21. ///// <summary>
  22. ///// HTTP请求
  23. ///// </summary>
  24. //private HttpRequest request;
  25. /// <summary>
  26. /// 请求消息
  27. /// </summary>
  28. private RequestMessage message;
  29. ///// <summary>
  30. ///// 加密类型
  31. ///// </summary>
  32. //private MessageEncryptTypeEnum encryptType;
  33. /// <summary>
  34. /// 获取是否有请求消息
  35. /// </summary>
  36. public bool HasRequestMessage
  37. {
  38. get
  39. {
  40. return message != null;
  41. }
  42. }
  43. /// <summary>
  44. /// 获取消息类型
  45. /// </summary>
  46. public MsgType MessageType
  47. {
  48. get
  49. {
  50. if (message != null)
  51. return message.MsgType;
  52. else
  53. throw new NotImplementedException("请求消息为空,无法获取消息类型。");
  54. }
  55. }
  56. /// <summary>
  57. /// 获取请求消息
  58. /// </summary>
  59. public RequestMessage Message
  60. {
  61. get
  62. {
  63. return message;
  64. }
  65. }
  66. /// <summary>
  67. /// 获取消息的加密类型
  68. /// </summary>
  69. //public MessageEncryptTypeEnum EncryptType
  70. //{
  71. // get
  72. // {
  73. // return encryptType;
  74. // }
  75. //}
  76. /// <summary>
  77. /// 构造函数
  78. /// </summary>
  79. /// <param name="request">HTTP请求</param>
  80. public RequestMessageHelper(string content,ref string resultMsg)
  81. {
  82. //获取消息加密类型
  83. //encryptType = GetEncryptType();
  84. //如果已加密,解密消息
  85. string rawContent;
  86. //if (encryptType == MessageEncryptTypeEnum.aes)
  87. // rawContent = DecryptMessage(content);
  88. //else
  89. rawContent = content;
  90. //解析消息
  91. if (!TryParse(rawContent, out message))
  92. {
  93. //xrwang.weixin.PublicAccount.Message.Insert(new Message(xrwang.weixin.PublicAccount.MessageType.Exception,
  94. // string.Format("解析消息失败。\r\n地址:{0}\r\n未解密的消息:{1}\r\n解密之后的消息:{2}",
  95. // "request.RawUrl", content, rawContent)));
  96. }
  97. else
  98. {
  99. //xrwang.weixin.PublicAccount.Message.Insert(new Message(xrwang.weixin.PublicAccount.MessageType.Exception,
  100. // string.Format("解析消息成功。\r\n地址:{0}\r\n未解密的消息:{1}\r\n解密之后的消息:{2}",
  101. // "request.RawUrl", content, message)));
  102. switch (message.MsgType)
  103. {
  104. //当消息为文本时
  105. case MsgType.Text:
  106. RequestTextMessage textmsg = (RequestTextMessage)message;
  107. if (textmsg.Content == "采购")
  108. {
  109. int nowtime = ConvertDateTimeInt(DateTime.Now);
  110. string msg = "欢迎关注亮健好药--供应商采购协同微信公众号:点击链接:http://119.27.191.247/web/supply/purchaseorder 查看采购需求。";
  111. string resxml = "<xml><ToUserName><![CDATA["
  112. + textmsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA["
  113. + textmsg.ToUserName + "]]></FromUserName><CreateTime>"
  114. + nowtime + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA["
  115. + msg + "]]></Content><FuncFlag>0</FuncFlag></xml>";
  116. resultMsg = resxml;
  117. }
  118. if (textmsg.Content == "供货")
  119. {
  120. int nowtime = ConvertDateTimeInt(DateTime.Now);
  121. string msg = "欢迎关注亮健好药--供应商采购协同微信公众号:点击链接:http://119.27.191.247/web/supply/index 填写供货。";
  122. string resxml = "<xml><ToUserName><![CDATA["
  123. + textmsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA["
  124. + textmsg.ToUserName + "]]></FromUserName><CreateTime>"
  125. + nowtime + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA["
  126. + msg + "]]></Content><FuncFlag>0</FuncFlag></xml>";
  127. resultMsg = resxml;
  128. }
  129. break;
  130. case MsgType.Event:
  131. RequestEventMessage eventmsg = (RequestEventMessage)message;
  132. if (eventmsg.Event == Event.Subscribe)
  133. {
  134. //刚关注时的时间,用于欢迎词
  135. int nowtime = ConvertDateTimeInt(DateTime.Now);
  136. string msg = "欢迎关注亮健好药--供应商采购协同微信公众号:点击链接:http://119.27.191.247/web/supply/purchaseorder 查看采购情况。";
  137. string resxml = "<xml><ToUserName><![CDATA["
  138. + eventmsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA["
  139. + eventmsg.ToUserName + "]]></FromUserName><CreateTime>"
  140. + nowtime + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA["
  141. + msg + "]]></Content><FuncFlag>0</FuncFlag></xml>";
  142. resultMsg = resxml;
  143. }
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// 得到消息加密类型
  152. /// </summary>
  153. /// <returns>返回消息加密类型</returns>
  154. //private MessageEncryptTypeEnum GetEncryptType()
  155. //{
  156. // string encrypt_type = RequestEx.TryGetQueryString("encrypt_type", MessageEncryptTypeEnum.raw.ToString("g"), request);
  157. // return (MessageEncryptTypeEnum)Enum.Parse(typeof(MessageEncryptTypeEnum), encrypt_type, true);
  158. //}
  159. /// <summary>
  160. /// 解密消息
  161. /// </summary>
  162. /// <returns>返回解密之后的消息</returns>
  163. //private string DecryptMessage(string content)
  164. //{
  165. // string msg = "";
  166. // XmlDocument doc = new XmlDocument();
  167. // doc.LoadXml(content);
  168. // XmlNode root = doc.FirstChild;
  169. // string userName = root["ToUserName"].InnerText;
  170. // AccountInfo account = AccountInfoCollection.GetAccountInfo(userName);
  171. // if (account == null)
  172. // return msg;
  173. // Tencent.WXBizMsgCrypt wxcpt = new Tencent.WXBizMsgCrypt(account.Token, account.EncodingAESKey, account.AppId);
  174. // string msg_signature = RequestEx.TryGetQueryString("msg_signature", "", request);
  175. // string timestamp = RequestEx.TryGetQueryString("timestamp", "", request);
  176. // string nonce = RequestEx.TryGetQueryString("nonce", "", request);
  177. // wxcpt.DecryptMsg(msg_signature, timestamp, nonce, content, ref msg);
  178. // return msg;
  179. //}
  180. /// <summary>
  181. /// 从xml字符串解析消息
  182. /// </summary>
  183. /// <param name="xml">xml字符串</param>
  184. /// <returns>返回消息</returns>
  185. public static RequestMessage Parse(string xml)
  186. {
  187. RequestMessage msg = null;
  188. //将xml字符串解析成JObject对象
  189. //XmlDocument doc = new XmlDocument();
  190. //doc.LoadXml(xml);
  191. XmlTextReader reader = new XmlTextReader(xml, XmlNodeType.Document, null);
  192. msg = RequestMessageFactory.CreateRequestMessage(reader);
  193. //返回
  194. return msg;
  195. }
  196. /// <summary>
  197. /// 尝试从xml字符串解析消息
  198. /// </summary>
  199. /// <param name="xml">xml字符串</param>
  200. /// <param name="msg">如果解析成功,返回消息;否则,返回null。</param>
  201. /// <returns>返回解析是否成功</returns>
  202. private static bool TryParse(string xml, out RequestMessage msg)
  203. {
  204. bool success = false;
  205. msg = null;
  206. try
  207. {
  208. msg = Parse(xml);
  209. success = true;
  210. }
  211. catch { }
  212. return success;
  213. }
  214. #region 将datetime.now 转换为 int类型的秒
  215. /// <summary>
  216. /// datetime转换为unixtime
  217. /// </summary>
  218. /// <param name="time"></param>
  219. /// <returns></returns>
  220. private int ConvertDateTimeInt(System.DateTime time)
  221. {
  222. //System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
  223. System.DateTime startTime = TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1),TimeZoneInfo.Local);
  224. return (int)(time - startTime).TotalSeconds;
  225. }
  226. private int converDateTimeInt(System.DateTime time)
  227. {
  228. System.DateTime startTime = TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1),TimeZoneInfo.Local);
  229. return (int)(time - startTime).TotalSeconds;
  230. }
  231. #endregion
  232. }
  233. }