using System.Text; using System.Xml; using WeiXinKfSDK.Response; using CommonUtil; using WeiXinKfSDK; using System.Linq; using Operater.DAL; using Operater.DbModel; using Operater.DTO.TopMessage; using System; using WeiXinKfSDK.Request; using System.Threading; namespace TradeInterflow.Handle { public class GzhHandle { public static WxKfToken _wxkfToken = new WxKfToken(); public static void HandleExecute(string xmlMessage) { Console.WriteLine("公众号消息"); XmlDocument doc = new XmlDocument(); XmlNode root; doc.LoadXml(xmlMessage); root = doc.FirstChild; //根据token获取 信息内容 string accessToken = GetGzhToken(false); if (accessToken.IsNullOrEmpty()) { return; } string msgtype = root["MsgType"].InnerText; switch (msgtype) { case "event"://事件消息 EventHandle(root); break; case "text": default: break; } } public static void TextHandle(XmlNode root) { string text = root["Content"].InnerText; if (text == "9990") { } } /// /// 事件消息 /// /// public static void EventHandle(XmlNode root) { string eventType = root["Event"].InnerText; switch (eventType) { case "CLICK": case "subscribe": ClickEvent(root); break; } } /// /// 点击菜单事件 /// /// public static void ClickEvent(XmlNode root) { string key = root["EventKey"].InnerText; if (key == "V1001_YOUHUI_HELP"|| root["Event"].InnerText== "subscribe") {//优惠小助手 SyncMsgListDto msgitem = new SyncMsgListDto { open_kfid = root["ToUserName"].InnerText, external_userid = root["FromUserName"].InnerText }; GzhCustomerSendRequest sendReq = new GzhCustomerSendRequest() { touser = root["FromUserName"].InnerText, msgtype = "text" }; GzhCustomerSendRequestTxt txt = new GzhCustomerSendRequestTxt() { content = "[庆祝]每天几块钱喝蜜雪冰城和瑞幸\n超低价外卖每日享,只差最后一步啦!\n🧧点击可领取20 - 10大额红包[红包]\n仅限今日\n👇👇👇👇👇👇👇👇👇" }; //发送第一段文案 sendReq.text = txt; WeiXinKfClient.GzhCustomerSend(sendReq, _wxkfToken.AccessToken); //发送图片 sendReq.msgtype = "image"; GzhCustomerSendRequestImage img = new GzhCustomerSendRequestImage() { media_id = "w3SCuv94XNIOR9go2n-QX0wCZjcuFduv2bfi7G1YJ6oN4P54FNm9hfm4YzBeP5tA" }; sendReq.image = img; WeiXinKfClient.GzhCustomerSend(sendReq, _wxkfToken.AccessToken); Thread.Sleep(1000); //发送第二段文案 sendReq = new GzhCustomerSendRequest() { touser = root["FromUserName"].InnerText, msgtype = "text" }; txt = new GzhCustomerSendRequestTxt() { content = "👆👆👆👆👆👆👆👆👆\n对了,添加了还可以参加0元撸!!!电动牙刷、螺蛳粉、抽纸、洗手液、零食等几十样商品!只邀一次,仅限今天哦~" }; sendReq.text = txt; WeiXinKfClient.GzhCustomerSend(sendReq, _wxkfToken.AccessToken); } } #region 获取公众号accesstoken public static string GetGzhToken(bool isRestart) { if (isRestart) { _wxkfToken = null; } if (_wxkfToken.IsNull() || _wxkfToken.AccessToken.IsNullOrEmpty() || _wxkfToken.ExpressTime < DateTime.Now) {//从接口获取 var topToken = WeiXinKfClient.GetGzhAccessToken(); if (topToken.IsNull()) { return null; } if (topToken.errcode != 0) { LogUtil.Info($"获取accesstoken失败:{topToken.errmsg}", "公众号error"); return null; } _wxkfToken = new WxKfToken(); _wxkfToken.AccessToken = topToken.access_token; _wxkfToken.ExpressTime = DateTime.Now.AddHours(2); LogUtil.Info(topToken.access_token, "公众号token"); return topToken.access_token; } return _wxkfToken.AccessToken; } #endregion } }