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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.Text;
using CommonUtil.Web;
using CommonUtil;
using WeiXinKfSDK.Response;
using RestSharp;
using WeiXinKfSDK.Request;
using Newtonsoft.Json;
using System.Reflection;
 
namespace WeiXinKfSDK
{
    public class WeiXinKfClient
    {
        /// <summary>
        /// 获取消息
        /// </summary>
        /// <param name="token"></param>
        /// <param name="accessToken"></param>
        public static SyncMsgResponse SyncMsg(string token, string cursor, string accessToken)
        {
            string url = $"https://qyapi.weixin.qq.com/cgi-bin/kf/sync_msg?access_token={accessToken}";
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("token", token);
            if (!cursor.IsNullOrEmpty())
            {
                dic.Add("cursor", cursor);
            }
            string resStr = new WebUtil().DoPostWithJson(url, dic, null);
            LogUtil.Info($"[获取消息]token:{token},返回:{resStr}", "接口请求日志");
            if (resStr.IsNullOrEmpty())
            {
                return null;
            }
            SyncMsgResponse response = JSONUtil.JsonToObject<SyncMsgResponse>(resStr);
            return response;
        }
        /// <summary>
        /// 获取微信客服acess_token
        /// </summary>
        /// <returns></returns>
        public static GetAccessTokenResponse GetAccessToken()
        {
            string getUrl = $"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={WeiXinKfSetting._comId}&corpsecret={WeiXinKfSetting._secret}";
            string resStr = new WebUtil().DoGet(getUrl, null);
            LogUtil.Info($"[获取access_token]返回:{resStr}", "接口请求日志");
            if (resStr.IsNullOrEmpty())
            {
                return null;
            }
            GetAccessTokenResponse response = JSONUtil.JsonToObject<GetAccessTokenResponse>(resStr);
            return response;
        }
 
        /// <summary>
        /// 获取公众号acess_token
        /// </summary>
        /// <returns></returns>
        public static GetAccessTokenResponse GetGzhAccessToken()
        {
            string getUrl = $" https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={WeiXinKfSetting._gzhAppid}&secret={WeiXinKfSetting._gzhSecret}";
            string resStr = new WebUtil().DoGet(getUrl, null);
            LogUtil.Info($"[获取access_token]返回:{resStr}", "接口请求日志");
            if (resStr.IsNullOrEmpty())
            {
                return null;
            }
            GetAccessTokenResponse response = JSONUtil.JsonToObject<GetAccessTokenResponse>(resStr);
            return response;
        }
 
        /// <summary>
        /// 发送客服欢迎语
        /// </summary>
        /// <param name="request"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static SendMsgOnEventResponse SendMsgOnEvent(SendMsgOnEventRequest request, string accessToken)
        {
            string url = $"https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg_on_event?access_token={accessToken}";
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("code", request.code);
            dic.Add("msgid", request.msgid);
            dic.Add("msgtype", request.msgtype);
            if (!request.text.IsNull())
            {
                dic.Add("text", request.text);
            }
            if (!request.msgmenu.IsNull())
            {
                dic.Add("msgmenu", request.msgmenu);
            }
            string resStr = new WebUtil().DoPostWithJson(url, dic, null);
            LogUtil.Info($"[发送客服欢迎语]请求:{JSONUtil.ObjectToJson(request)},返回:{resStr}", "接口请求日志");
            if (resStr.IsNullOrEmpty())
            {
                return null;
            }
            SendMsgOnEventResponse response = JSONUtil.JsonToObject<SendMsgOnEventResponse>(resStr);
            return response;
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static SendMsgOnEventResponse SendMsg(SendMsgRequest request, string accessToken)
        {
            string url = $"https://qyapi.weixin.qq.com/cgi-bin/kf/send_msg?access_token={accessToken}";
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("touser", request.touser);
            dic.Add("open_kfid", request.open_kfid);
            if (!request.msgid.IsNullOrEmpty())
            {
                dic.Add("msgid", request.msgid);
            }
            dic.Add("msgtype", request.msgtype);
            if (!request.text.IsNull())
            {
                dic.Add("text", request.text);
            }
            if (!request.image.IsNull())
            {
                dic.Add("image", request.image);
            }
            string resStr = new WebUtil().DoPostWithJson(url, dic, null);
            // LogUtil.Info($"[发消息]请求:{JSONUtil.ObjectToJson(request)},返回:{resStr}", "接口请求日志");
            if (resStr.IsNullOrEmpty())
            {
                return null;
            }
            SendMsgOnEventResponse response = JSONUtil.JsonToObject<SendMsgOnEventResponse>(resStr);
            return response;
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="imgPath"></param>
        /// <param name="imgName"></param>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static MediaUploadResponse ImgMediaUpload(string imgPath, string imgName, string accessToken)
        {
            try
            {
                var client = new RestClient($"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={accessToken}&type=image");
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddParameter("filename", imgName);
                request.AddParameter("Content-Type", "application/octet-stream");
                request.AddFile("filelength", $"{imgPath}");
                IRestResponse response = client.Execute(request);
                MediaUploadResponse res = JSONUtil.JsonToObject<MediaUploadResponse>(response.Content);
                return res;
            }
            catch (Exception e)
            {
                return null;
            }
        }
 
        /// <summary>
        /// 创建公众号菜单
        /// </summary>
        public static CreateMenuResponse CreateGzhMenu(CreateMenuRequest request, string accessToken)
        {
            string url = $"https://api.weixin.qq.com/cgi-bin/menu/create?access_token={accessToken}";
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("button", request.button);
            string resStr = new WebUtil().DoPostWithJson(url, dic, null);
            LogUtil.Info($"[发消息]请求:{JSONUtil.ObjectToJson(request)},返回:{resStr}", "创建公众号菜单日志");
            if (resStr.IsNullOrEmpty())
            {
                return null;
            }
            CreateMenuResponse response = JSONUtil.JsonToObject<CreateMenuResponse>(resStr);
            return response;
        }
 
        /// <summary>
        /// 发送客服消息
        /// </summary>
        /// <returns></returns>
        public static GzhCustomerSendResponse GzhCustomerSend(GzhCustomerSendRequest request, string accessToken)
        {
            string url = $"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={accessToken}";
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("touser", request.touser);
            dic.Add("msgtype", request.msgtype);
            switch (request.msgtype)
            {
                case "text":
                    dic.Add("text", request.text);
                    break;
                case "image":
                    dic.Add("image", request.image);
                    break;
                default: break;
            }
            string resStr = new WebUtil().DoPostWithJson(url, dic, null);
            LogUtil.Info($"[发消息]请求:{JSONUtil.ObjectToJson(request)},返回:{resStr}", "发送客服消息日志");
            if (resStr.IsNullOrEmpty())
            {
                return null;
            }
            GzhCustomerSendResponse response = JSONUtil.JsonToObject<GzhCustomerSendResponse>(resStr);
            return response;
        }
        //100000052
        public static GzhBaseResponse UploadWxMedia(string imgPath, string imgName, string accessToken)
        {
            try
            {
                var client = new RestClient($"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={accessToken}&type=image");
                client.Timeout = -1;
                var request = new RestRequest(Method.POST);
                request.AddParameter("filename", imgName);
                request.AddParameter("type", "image");
                request.AddParameter("Content-Type", "application/octet-stream");
                request.AddFile("filelength", $"{imgPath}");
                IRestResponse response = client.Execute(request);
                GzhBaseResponse res = JSONUtil.JsonToObject<GzhBaseResponse>(response.Content);
                return res;
            }
            catch (Exception e)
            {
                return null;
            }
        }
 
        /// <summary>
        /// 通过code获取access_token,用户授权
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static GetOauth2TokenResponse GetOauth2Token(string code)
        {
            string getUrl = $"https://api.weixin.qq.com/sns/oauth2/access_token?appid={WeiXinKfSetting._gzhAppid}&secret={WeiXinKfSetting._gzhSecret}&code={code}&grant_type=authorization_code";
            string resStr = new WebUtil().DoGet(getUrl, null);
            LogUtil.Info($"[gzh获取access_token]返回:{resStr}", "接口请求日志");
            GetOauth2TokenResponse response = JSONUtil.JsonToObject<GetOauth2TokenResponse>(resStr);
            return response;
        }
 
 
        /// <summary>
        /// 通过accesstoken获取用户信息
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        public static GetGzhUserInfoResonse GetGzhUserInfo(string accessToken, string openId)
        {
            string getUrl = $"https://api.weixin.qq.com/sns/userinfo?access_token={accessToken}&openid={openId}&lang=zh_CN";
            string resStr = new WebUtil().DoGet(getUrl, null);
            LogUtil.Info($"[获取用户信息]返回:{resStr}", "接口请求日志");
            GetGzhUserInfoResonse response = JSONUtil.JsonToObject<GetGzhUserInfoResonse>(resStr);
            return response;
        }
 
        public static string GetGzhRid(string rid, string accessToken)
        {
            string url = $"https://api.weixin.qq.com/cgi-bin/openapi/rid/get?access_token={accessToken}";
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("rid", rid);
            string resStr = new WebUtil().DoPostWithJson(url, dic, null);
            LogUtil.Info($"[发消息]请求:{JSONUtil.ObjectToJson(dic)},返回:{resStr}", "发送客服消息日志");
            return resStr;
        }
        /// <summary>
        /// 发送公众号模板消息
        /// </summary>
        /// <param name="request"></param>
        public static SendTemplateMsgResponse SendTemplateMsg(SendTemplateMsgRequest request, string access_token)
        {
            string url = $"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={access_token}";
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("touser", request.touser);
            dic.Add("template_id", request.template_id);
            dic.Add("data", request.data);
            string resStr = new WebUtil().DoPostWithJson(url, dic, null);
            LogUtil.Info($"[发模板消息]请求:{JSONUtil.ObjectToJson(dic)},返回:{resStr}", "发送模板消息日志");
            SendTemplateMsgResponse res = JSONUtil.JsonToObject<SendTemplateMsgResponse>(resStr);
            return res;
        }
    }
}