微信公众号开发小计(二)实现底部菜单的时间
在上一篇文章中我们创建了WrchatMain的一般处理程序
底部菜单中每一个时间的点击都是一个post提交所以我们会在else中加入以下代码
CreateLog.SaveLogs("获取到微信发来的消息\r\n\t", 1); Stream wxdata = context.Request.InputStream; //将字节流转化为输出流 byte[] wxdatabyte = new byte[wxdata.Length]; wxdata.Read(wxdatabyte, 0, (int)wxdata.Length); string postdata = Encoding.UTF8.GetString(wxdatabyte); //CreateLog.SaveLogs(postdata, 1); if (!string.IsNullOrEmpty(postdata)) { //参数不为空则获取相关参数 returnStr = MessageCommon.ReqMessage(postdata); }
在WeChatBussiness中创建Article类用于指定微信推荐信息的数据格式
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Travel.WeChat.WeChatBussiness { public class Article { private string _title; private string _description; private string _picUrl; private string _url; /// <summary> /// 图文消息名称 /// </summary> public string Title { get { return _title; } set { _title = value; } } /// <summary> /// 图文消息描述 /// </summary> public string Description { get { return _description; } set { _description = value; } } /// <summary> /// 图片链接,支持JPG、PNG格式,较好的效果为大图640*320,小图80*80,限制图片链接的域名需要与开发者填写的基本资料中的Url一致 /// </summary> public string PicUrl { get { return _picUrl; } set { _picUrl = value; } } /// <summary> /// 点击图文消息跳转链接 /// </summary> public string Url { get { return _url; } set { _url = value; } } } }
创建MessageCommon用于接受微信的请求,从而进行处理
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml; using Travel.WeChat.WeChatBussiness; namespace Travel.WeChat.WeCahtCode { public class MessageCommon { public MessageCommon() { } /// <summary> /// 消息处理 /// </summary> /// <param name="document">消息字符串</param> /// <returns></returns> public static string ReqMessage(string document) { string returnstr = ""; //开始把字符串变成XML对象 XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml(document); //获得xml的根节点 XmlElement roolelement = xmldoc.DocumentElement; //开始需要知道我获得的消息是什么? //获取MsgType 消息类型 string MsgType = roolelement.SelectSingleNode("MsgType").InnerText; if (MsgType == "text") { //文本消息 //CreateLog.SaveLogs("文本信息:"+document+"\r\n\t", 1); return ""; } else if (MsgType == "event") { //事件 //CreateLog.SaveLogs("事件:" + document + "\r\n\t", 1); //需要知道是什么事件,通过Xml中的Event节点来判别。 string keyNode = roolelement.SelectSingleNode("Event").InnerText; if (keyNode == "CLICK") { //获得事件的标识字符串,如:菜单节点中的key值 string EventKey = roolelement.SelectSingleNode("EventKey").InnerText; //开始组织一个返回结果的对象,对象类型是文本。 RespTextMessage rtm = new RespTextMessage(); rtm.ToUserName = roolelement.SelectSingleNode("FromUserName").InnerText; rtm.FromUserName = roolelement.SelectSingleNode("ToUserName").InnerText; rtm.CreateTime = WeChatInit.datetimestr(); rtm.MsgType = "text"; RespNewsMessage newsObj = new RespNewsMessage(); newsObj.ToUserName = roolelement.SelectSingleNode("FromUserName").InnerText; newsObj.FromUserName = roolelement.SelectSingleNode("ToUserName").InnerText; newsObj.CreateTime = WeChatInit.datetimestr(); newsObj.MsgType = "news"; ProductBussiness infor = new ProductBussiness(); // CreateLog.SaveLogs("点击事件:EventKey=" + EventKey + ";ToUserName=" + rtm.ToUserName + ";FromUserName=" + rtm.FromUserName + ";CreateTime=" + roolelement.SelectSingleNode("CreateTime").InnerText, 1); // CreateLog.SaveLogs(EventKey == "NewProduct" ? "1" : "2", 1); switch (EventKey) { case "NewProduct": //最新产品 newsObj.Articles = infor.GetProduct(); newsObj.ArticleCount = newsObj.Articles.Count; returnstr = newsObj.SendContent(); break; case "NewSale": //最新特卖 newsObj.Articles = infor.GetSaleProduct(); newsObj.ArticleCount = newsObj.Articles.Count; returnstr = newsObj.SendContent(); break; case "NewSelling": //当季畅销 newsObj.Articles = infor.GetSalingProduct(); newsObj.ArticleCount = newsObj.Articles.Count; returnstr = newsObj.SendContent(); break; default: break; } return returnstr; } else if (keyNode == "VIEW") { string EventKey = roolelement.SelectSingleNode("EventKey").InnerText; string ToUserName = roolelement.SelectSingleNode("ToUserName").InnerText; string FromUserName = roolelement.SelectSingleNode("FromUserName").InnerText; string CreateTime = roolelement.SelectSingleNode("CreateTime").InnerText; string MenuID = roolelement.SelectSingleNode("MenuID").InnerText; CreateLog.SaveLogs("点击事件:EventKey=" + EventKey + ";ToUserName=" + ToUserName + ";FromUserName=" + FromUserName + ";MenuID=" + MenuID + ";CreateTime=" + CreateTime, 1); } return ""; } else if (MsgType == "Page") { //CreateLog.SaveLogs("页面:" + document + "\r\n\t", 1); return ""; } return returnstr; } } }
这样我们的菜单就创建完成了