private void textCase(ExmlMsg xmlMsg) { long nowtime = DateTime.Now.Ticks; string msg = ""; msg = getText(xmlMsg); string resxml = "<xml><ToUserName><![CDATA[" + xmlMsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + xmlMsg.ToUserName + "]]></FromUserName><CreateTime>" + nowtime + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + msg + "]]></Content><FuncFlag>0</FuncFlag></xml>"; Response.WriteAsync(resxml); }
private string getText(ExmlMsg xmlMsg) { //string con = xmlMsg.Content.Trim(); System.Text.StringBuilder retsb = new StringBuilder(200); retsb.Append("四川红领巾少儿艺术团"); //retsb.Append("接收到的消息:" + xmlMsg.Content); //retsb.Append("用户的OPEANID:" + xmlMsg.FromUserName); Log.WriteLog("APPSECRET:" + WxPayConfig.APPSECRET); return(retsb.ToString()); }
public void CheckWeChat()// 服务器响应微信请求 { try { string weixin = Common.ReadStream2String(Request.Body); Log.WriteLog("weixin:" + weixin); XmlDocument doc = new XmlDocument(); doc.LoadXml(weixin);//读取xml字符串 XmlElement root = doc.DocumentElement; ExmlMsg xmlMsg = GetExmlMsg(root); //XmlNode MsgType = root.SelectSingleNode("MsgType"); //string messageType = MsgType.InnerText; string messageType = xmlMsg.MsgType;//获取收到的消息类型。文本(text),图片(image),语音等。 switch (messageType) { //当消息为文本时 case "text": textCase(xmlMsg); break; case "event": break; case "image": break; case "voice": break; case "vedio": break; case "location": break; case "link": break; default: break; } } catch (Exception e) { Log.WriteLog(e.Message); } }
private ExmlMsg GetExmlMsg(XmlElement root) { ExmlMsg xmlMsg = new ExmlMsg() { FromUserName = root.SelectSingleNode("FromUserName").InnerText, ToUserName = root.SelectSingleNode("ToUserName").InnerText, CreateTime = root.SelectSingleNode("CreateTime").InnerText, MsgType = root.SelectSingleNode("MsgType").InnerText, EventKey = root.SelectSingleNode("EventKey").InnerText }; if (xmlMsg.MsgType.Trim().ToLower() == "text") { xmlMsg.Content = root.SelectSingleNode("Content").InnerText; } else if (xmlMsg.MsgType.Trim().ToLower() == "event") { xmlMsg.EventName = root.SelectSingleNode("Event").InnerText; } return(xmlMsg); }
public void CheckWeChat()// 服务器响应微信请求 { try { string weixin = Common.ReadStream2String(Request.Body); Log.WriteLog("weixin:" + weixin); XmlDocument doc = new XmlDocument(); doc.LoadXml(weixin);//读取xml字符串 XmlElement root = doc.DocumentElement; ExmlMsg xmlMsg = GetExmlMsg(root); //XmlNode MsgType = root.SelectSingleNode("MsgType"); //string messageType = MsgType.InnerText; string messageType = xmlMsg.MsgType;//获取收到的消息类型。文本(text),图片(image),语音等。 switch (messageType) { //当消息为文本时 case "text": textCase(xmlMsg); break; case "event": Log.WriteLog("xmlMsg.EventName:" + xmlMsg.EventName.Trim()); if (!string.IsNullOrEmpty(xmlMsg.EventName) && xmlMsg.EventName.Trim() == "subscribe") { //刚关注时的时间,用于欢迎词 long nowtime = DateTime.Now.Ticks; string msg = "欢迎来到恭城六艺教育,随便点点试试吧~~~"; string resxml = "<xml><ToUserName><![CDATA[" + xmlMsg.FromUserName + "]]></ToUserName><FromUserName><![CDATA[" + xmlMsg.ToUserName + "]]></FromUserName><CreateTime>" + nowtime + "</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[" + msg + "]]></Content><FuncFlag>0</FuncFlag></xml>"; Log.WriteLog(xmlMsg.ToUserName); //关注公众号 注册或者用户信息 object token = CacheHelper.Get("tokens"); string access_token = token != null?token.ToString() : ""; if (string.IsNullOrEmpty(access_token)) { Log.WriteLog("access_token:no"); GetToken(WxPayConfig.APPID, WxPayConfig.APPSECRET, xmlMsg.FromUserName); } else { Log.WriteLog("access_token:" + access_token); userinfo(xmlMsg.FromUserName, access_token); } Response.WriteAsync(resxml); } else if (!string.IsNullOrEmpty(xmlMsg.EventName) && xmlMsg.EventName.Trim() == "CLICK") { User user = db.Users.FirstOrDefault(c => c.LoginName == xmlMsg.FromUserName); string userid = ""; if (user != null) { userid = "&userid=" + user.Id; } //刚关注时的时间,用于欢迎词 long nowtime = DateTime.Now.Ticks; string PicUrl = "http://www.bellali.cn/Files/Image/de9a0931-ef73-4e4d-977d-6d10351ca56b.png"; string resxml = ""; if (xmlMsg.EventKey == "-101") { string url = "http://www.bellali.cn/Files/app/volunteer.html?openid=" + xmlMsg.FromUserName + userid; resxml = ReArticle(xmlMsg.FromUserName, xmlMsg.ToUserName, "志愿者报名", "做公益啦", PicUrl, url); Response.WriteAsync(resxml, Encoding.UTF8); } else if (Convert.ToInt32(xmlMsg.EventKey) > 0) { string url = "http://www.bellali.cn/files/app/activedetail.html?id=" + xmlMsg.EventKey + "&openid=" + xmlMsg.FromUserName + userid; resxml = ReArticle(xmlMsg.FromUserName, xmlMsg.ToUserName, "书房活动", "书房活动", PicUrl, url); Response.WriteAsync(resxml, Encoding.UTF8); } else if (xmlMsg.EventKey == "-100") { string url = "http://www.bellali.cn/Files/app/myactive.html?openid=" + xmlMsg.FromUserName + userid; resxml = ReArticle(xmlMsg.FromUserName, xmlMsg.ToUserName, "我的活动", "我的活动", PicUrl, url); Response.WriteAsync(resxml, Encoding.UTF8); } } break; case "image": break; case "voice": break; case "vedio": break; case "location": break; case "link": break; default: break; } } catch (Exception e) { Log.WriteLog(e.Message); } }