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
{
///
/// RequestMessage:请求消息辅助类
///
public class RequestMessageHelper
{
///
/// 将xml节点CDATA转换成json之后的键名
///
private const string CDATA_KEY = "#cdata-section";
/////
///// HTTP请求
/////
//private HttpRequest request;
///
/// 请求消息
///
private RequestMessage message;
/////
///// 加密类型
/////
//private MessageEncryptTypeEnum encryptType;
///
/// 获取是否有请求消息
///
public bool HasRequestMessage
{
get
{
return message != null;
}
}
///
/// 获取消息类型
///
public MsgType MessageType
{
get
{
if (message != null)
return message.MsgType;
else
throw new NotImplementedException("请求消息为空,无法获取消息类型。");
}
}
///
/// 获取请求消息
///
public RequestMessage Message
{
get
{
return message;
}
}
///
/// 获取消息的加密类型
///
//public MessageEncryptTypeEnum EncryptType
//{
// get
// {
// return encryptType;
// }
//}
///
/// 构造函数
///
/// HTTP请求
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 = ""
+ nowtime + "0";
resultMsg = resxml;
}
if (textmsg.Content == "供货")
{
int nowtime = ConvertDateTimeInt(DateTime.Now);
string msg = "欢迎关注亮健好药--供应商采购协同微信公众号:点击链接:http://119.27.191.247/web/supply/index 填写供货。";
string resxml = ""
+ nowtime + "0";
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 = ""
+ nowtime + "0";
resultMsg = resxml;
}
break;
default:
break;
}
}
}
///
/// 得到消息加密类型
///
/// 返回消息加密类型
//private MessageEncryptTypeEnum GetEncryptType()
//{
// string encrypt_type = RequestEx.TryGetQueryString("encrypt_type", MessageEncryptTypeEnum.raw.ToString("g"), request);
// return (MessageEncryptTypeEnum)Enum.Parse(typeof(MessageEncryptTypeEnum), encrypt_type, true);
//}
///
/// 解密消息
///
/// 返回解密之后的消息
//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;
//}
///
/// 从xml字符串解析消息
///
/// xml字符串
/// 返回消息
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;
}
///
/// 尝试从xml字符串解析消息
///
/// xml字符串
/// 如果解析成功,返回消息;否则,返回null。
/// 返回解析是否成功
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类型的秒
///
/// datetime转换为unixtime
///
///
///
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
}
}