123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- using System;
- using System.Collections.Generic;
- using System.Xml;
- using System.Web;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using ZcPeng.PublicLibrary;
- using JCSoft.WX.Framework.Models.Requests;
- using JCSoft.WX.Framework.Models;
- namespace Common.Wechat
- {
- /// <summary>
- /// RequestMessage:请求消息辅助类
- /// </summary>
- public class RequestMessageHelper
- {
- /// <summary>
- /// 将xml节点CDATA转换成json之后的键名
- /// </summary>
- private const string CDATA_KEY = "#cdata-section";
- ///// <summary>
- ///// HTTP请求
- ///// </summary>
- //private HttpRequest request;
- /// <summary>
- /// 请求消息
- /// </summary>
- private RequestMessage message;
- ///// <summary>
- ///// 加密类型
- ///// </summary>
- //private MessageEncryptTypeEnum encryptType;
- /// <summary>
- /// 获取是否有请求消息
- /// </summary>
- public bool HasRequestMessage
- {
- get
- {
- return message != null;
- }
- }
- /// <summary>
- /// 获取消息类型
- /// </summary>
- public MsgType MessageType
- {
- get
- {
- if (message != null)
- return message.MsgType;
- else
- throw new NotImplementedException("请求消息为空,无法获取消息类型。");
- }
- }
- /// <summary>
- /// 获取请求消息
- /// </summary>
- public RequestMessage Message
- {
- get
- {
- return message;
- }
- }
- /// <summary>
- /// 获取消息的加密类型
- /// </summary>
- //public MessageEncryptTypeEnum EncryptType
- //{
- // get
- // {
- // return encryptType;
- // }
- //}
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="request">HTTP请求</param>
- public RequestMessageHelper(string content,ref string resultMsg)
- {
- //获取消息加密类型
- //encryptType = GetEncryptType();
- //如果已加密,解密消息
- string rawContent;
- //if (encryptType == MessageEncryptTypeEnum.aes)
- // rawContent = DecryptMessage(content);
- //else
- rawContent = content;
- //解析消息
- if (!TryParse(rawContent, out message))
- {
- //xrwang.weixin.PublicAccount.Message.Insert(new Message(xrwang.weixin.PublicAccount.MessageType.Exception,
- // string.Format("解析消息失败。\r\n地址:{0}\r\n未解密的消息:{1}\r\n解密之后的消息:{2}",
- // "request.RawUrl", content, rawContent)));
- }
- else
- {
- //xrwang.weixin.PublicAccount.Message.Insert(new Message(xrwang.weixin.PublicAccount.MessageType.Exception,
- // string.Format("解析消息成功。\r\n地址:{0}\r\n未解密的消息:{1}\r\n解密之后的消息:{2}",
- // "request.RawUrl", content, message)));
- switch (message.MsgType)
- {
- //当消息为文本时
- case MsgType.Text:
- RequestTextMessage textmsg = (RequestTextMessage)message;
- if (textmsg.Content == "采购")
- {
- int nowtime = ConvertDateTimeInt(DateTime.Now);
- string msg = "欢迎关注亮健好药--供应商采购协同微信公众号:点击链接:http://119.27.191.247/web/supply/purchaseorder 查看采购需求。";
- string resxml = "<xml><ToUserName><![CDATA["
- + textmsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA["
- + textmsg.ToUserName + "]]></FromUserName><CreateTime>"
- + nowtime + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA["
- + msg + "]]></Content><FuncFlag>0</FuncFlag></xml>";
- resultMsg = resxml;
- }
- if (textmsg.Content == "供货")
- {
- int nowtime = ConvertDateTimeInt(DateTime.Now);
- string msg = "欢迎关注亮健好药--供应商采购协同微信公众号:点击链接:http://119.27.191.247/web/supply/index 填写供货。";
- string resxml = "<xml><ToUserName><![CDATA["
- + textmsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA["
- + textmsg.ToUserName + "]]></FromUserName><CreateTime>"
- + nowtime + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA["
- + msg + "]]></Content><FuncFlag>0</FuncFlag></xml>";
- resultMsg = resxml;
- }
- break;
- case MsgType.Event:
- RequestEventMessage eventmsg = (RequestEventMessage)message;
- if (eventmsg.Event == Event.Subscribe)
- {
- //刚关注时的时间,用于欢迎词
- int nowtime = ConvertDateTimeInt(DateTime.Now);
- string msg = "欢迎关注亮健好药--供应商采购协同微信公众号:点击链接:http://119.27.191.247/web/supply/purchaseorder 查看采购情况。";
- string resxml = "<xml><ToUserName><![CDATA["
- + eventmsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA["
- + eventmsg.ToUserName + "]]></FromUserName><CreateTime>"
- + nowtime + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA["
- + msg + "]]></Content><FuncFlag>0</FuncFlag></xml>";
- resultMsg = resxml;
- }
- break;
- default:
- break;
- }
- }
- }
- /// <summary>
- /// 得到消息加密类型
- /// </summary>
- /// <returns>返回消息加密类型</returns>
- //private MessageEncryptTypeEnum GetEncryptType()
- //{
- // string encrypt_type = RequestEx.TryGetQueryString("encrypt_type", MessageEncryptTypeEnum.raw.ToString("g"), request);
- // return (MessageEncryptTypeEnum)Enum.Parse(typeof(MessageEncryptTypeEnum), encrypt_type, true);
- //}
- /// <summary>
- /// 解密消息
- /// </summary>
- /// <returns>返回解密之后的消息</returns>
- //private string DecryptMessage(string content)
- //{
- // string msg = "";
- // XmlDocument doc = new XmlDocument();
- // doc.LoadXml(content);
- // XmlNode root = doc.FirstChild;
- // string userName = root["ToUserName"].InnerText;
- // AccountInfo account = AccountInfoCollection.GetAccountInfo(userName);
- // if (account == null)
- // return msg;
- // Tencent.WXBizMsgCrypt wxcpt = new Tencent.WXBizMsgCrypt(account.Token, account.EncodingAESKey, account.AppId);
- // string msg_signature = RequestEx.TryGetQueryString("msg_signature", "", request);
- // string timestamp = RequestEx.TryGetQueryString("timestamp", "", request);
- // string nonce = RequestEx.TryGetQueryString("nonce", "", request);
- // wxcpt.DecryptMsg(msg_signature, timestamp, nonce, content, ref msg);
- // return msg;
- //}
- /// <summary>
- /// 从xml字符串解析消息
- /// </summary>
- /// <param name="xml">xml字符串</param>
- /// <returns>返回消息</returns>
- public static RequestMessage Parse(string xml)
- {
- RequestMessage msg = null;
- //将xml字符串解析成JObject对象
- //XmlDocument doc = new XmlDocument();
- //doc.LoadXml(xml);
- XmlTextReader reader = new XmlTextReader(xml, XmlNodeType.Document, null);
- msg = RequestMessageFactory.CreateRequestMessage(reader);
-
- //返回
- return msg;
- }
- /// <summary>
- /// 尝试从xml字符串解析消息
- /// </summary>
- /// <param name="xml">xml字符串</param>
- /// <param name="msg">如果解析成功,返回消息;否则,返回null。</param>
- /// <returns>返回解析是否成功</returns>
- private static bool TryParse(string xml, out RequestMessage msg)
- {
- bool success = false;
- msg = null;
- try
- {
- msg = Parse(xml);
- success = true;
- }
- catch { }
- return success;
- }
- #region 将datetime.now 转换为 int类型的秒
- /// <summary>
- /// datetime转换为unixtime
- /// </summary>
- /// <param name="time"></param>
- /// <returns></returns>
- private int ConvertDateTimeInt(System.DateTime time)
- {
- //System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
- System.DateTime startTime = TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1),TimeZoneInfo.Local);
- return (int)(time - startTime).TotalSeconds;
- }
- private int converDateTimeInt(System.DateTime time)
- {
- System.DateTime startTime = TimeZoneInfo.ConvertTime(new System.DateTime(1970, 1, 1),TimeZoneInfo.Local);
- return (int)(time - startTime).TotalSeconds;
- }
- #endregion
-
- }
-
- }
|