zhaojs
2023-09-15 fc13938ff90213060532d99a600dea4a84456885
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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")
            {
 
            }
        }
 
        /// <summary>
        /// 事件消息
        /// </summary>
        /// <param name="root"></param>
        public static void EventHandle(XmlNode root)
        {
            string eventType = root["Event"].InnerText;
            switch (eventType)
            {
                case "CLICK":
                case "subscribe":
                    ClickEvent(root);
                    break;
            }
        }
 
        /// <summary>
        /// 点击菜单事件
        /// </summary>
        /// <param name="key"></param>
        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
    }
}