|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+using Newtonsoft.Json;
|
|
|
2
|
+using System;
|
|
|
3
|
+using System.Collections.Generic;
|
|
|
4
|
+using System.IO;
|
|
|
5
|
+using System.Linq;
|
|
|
6
|
+using System.Net;
|
|
|
7
|
+using System.Net.Http;
|
|
|
8
|
+using System.Net.Http.Headers;
|
|
|
9
|
+using System.Text;
|
|
|
10
|
+
|
|
|
11
|
+namespace CallCenter.Utility
|
|
|
12
|
+{
|
|
|
13
|
+ public class FeiShuiHelper
|
|
|
14
|
+ {
|
|
|
15
|
+ static string app_id = Configs.GetValue("FeiShui_AppId");
|
|
|
16
|
+ static string app_secret = Configs.GetValue("FeiShui_AppSecret");
|
|
|
17
|
+ //获取tenant_access_token
|
|
|
18
|
+ private static string GetTenantAccessToken()
|
|
|
19
|
+ {
|
|
|
20
|
+ string result = string.Empty;
|
|
|
21
|
+ var param = new
|
|
|
22
|
+ {
|
|
|
23
|
+ app_id = app_id,
|
|
|
24
|
+ app_secret = app_secret,
|
|
|
25
|
+
|
|
|
26
|
+ };
|
|
|
27
|
+ var strresult = HttpMethods.HttpPost("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal", param.ToJson(),"application/json; charset=utf-8");
|
|
|
28
|
+ if (!string.IsNullOrEmpty(strresult))
|
|
|
29
|
+ {
|
|
|
30
|
+ var resultobj = strresult.ToJObject();
|
|
|
31
|
+ if (resultobj["code"].ToString() != "0")
|
|
|
32
|
+ {
|
|
|
33
|
+ LogFactory.GetLogger("tenant_access_token").Error(strresult);
|
|
|
34
|
+ result = resultobj["msg"].ToString();
|
|
|
35
|
+ }
|
|
|
36
|
+ else
|
|
|
37
|
+ {
|
|
|
38
|
+ result = resultobj["tenant_access_token"].ToString();
|
|
|
39
|
+ }
|
|
|
40
|
+ }
|
|
|
41
|
+ return result;
|
|
|
42
|
+ }
|
|
|
43
|
+ //获取user_access_token
|
|
|
44
|
+ private static string GetUserAccessToken(string code)
|
|
|
45
|
+ {
|
|
|
46
|
+ string result = string.Empty;
|
|
|
47
|
+ var param = new
|
|
|
48
|
+ {
|
|
|
49
|
+ grant_type = "authorization_code",
|
|
|
50
|
+ client_id = app_id,
|
|
|
51
|
+ client_secret=app_secret,
|
|
|
52
|
+ code= code
|
|
|
53
|
+
|
|
|
54
|
+ };
|
|
|
55
|
+ var strresult = HttpMethods.HttpPost("https://open.feishu.cn/open-apis/authen/v2/oauth/token", param.ToJson(), "application/json; charset=utf-8");
|
|
|
56
|
+ if (!string.IsNullOrEmpty(strresult))
|
|
|
57
|
+ {
|
|
|
58
|
+ var resultobj = strresult.ToJObject();
|
|
|
59
|
+ if (resultobj["code"].ToString() != "0")
|
|
|
60
|
+ {
|
|
|
61
|
+ LogFactory.GetLogger("error").Error(strresult);
|
|
|
62
|
+ result = resultobj["error"].ToString();
|
|
|
63
|
+ }
|
|
|
64
|
+ else
|
|
|
65
|
+ {
|
|
|
66
|
+ result = resultobj["access_token"].ToString();
|
|
|
67
|
+ }
|
|
|
68
|
+ }
|
|
|
69
|
+ return result;
|
|
|
70
|
+ }
|
|
|
71
|
+ //通过手机号获取用户id
|
|
|
72
|
+ private static string GetUserid(string mobile)
|
|
|
73
|
+ {
|
|
|
74
|
+ string result = string.Empty;
|
|
|
75
|
+
|
|
|
76
|
+ //Dictionary<string, object> param = new Dictionary<string, object>();
|
|
|
77
|
+ //param.Add("mobiles", new string[] { mobile });
|
|
|
78
|
+ //param.Add("include_resigned", true);
|
|
|
79
|
+
|
|
|
80
|
+ var postData = new { mobiles = new string[] { mobile }, include_resigned = true };
|
|
|
81
|
+ var strresult = SendPostRequest
|
|
|
82
|
+ ("https://open.feishu.cn/open-apis/contact/v3/users/batch_get_id?user_id_type=user_id", postData);
|
|
|
83
|
+ if (!string.IsNullOrEmpty(strresult))
|
|
|
84
|
+ {
|
|
|
85
|
+ generalResult<userslist> resultobj = JsonConvert.
|
|
|
86
|
+ DeserializeObject<generalResult<userslist>>(strresult);
|
|
|
87
|
+ if (resultobj.code != "0")
|
|
|
88
|
+ {
|
|
|
89
|
+ LogFactory.GetLogger("GetUserid").Error(resultobj.msg);
|
|
|
90
|
+ result = resultobj.msg;
|
|
|
91
|
+ }
|
|
|
92
|
+ else
|
|
|
93
|
+ {
|
|
|
94
|
+ if (resultobj.data!=null&& resultobj.data.user_list.Count>0)
|
|
|
95
|
+ result = resultobj.data.user_list[0].user_id;
|
|
|
96
|
+ }
|
|
|
97
|
+ }
|
|
|
98
|
+ return result;
|
|
|
99
|
+ }
|
|
|
100
|
+ //发送消息
|
|
|
101
|
+ public static string SendMsg(string mobile,string msg)
|
|
|
102
|
+ {
|
|
|
103
|
+ string userid = GetUserid(mobile);
|
|
|
104
|
+ if (string.IsNullOrEmpty(mobile))
|
|
|
105
|
+ return "";
|
|
|
106
|
+ Guid guid = Guid.NewGuid();
|
|
|
107
|
+ string uuid = guid.ToString();
|
|
|
108
|
+ string result = string.Empty;
|
|
|
109
|
+ //Dictionary<string, object> param = new Dictionary<string, object>();
|
|
|
110
|
+ //param.Add("receive_id", userid);
|
|
|
111
|
+ //param.Add("msg_type", "text");
|
|
|
112
|
+ //param.Add( "content", "{\"text\":\"" + msg + "\"}");
|
|
|
113
|
+ //param.Add("uuid", uuid);
|
|
|
114
|
+
|
|
|
115
|
+ var postData = new { receive_id = userid, msg_type = "post",
|
|
|
116
|
+ content = "{\"zh_cn\":{\"title\":\""+ msg + "\",\"content\":[[{\"tag\":\"text\",\"text\":\"地址 :\"},{\"tag\":\"a\",\"href\":\"http://www.feishu.cn\",\"text\":\"超链接\"}]]}}",
|
|
|
117
|
+ uuid = uuid,
|
|
|
118
|
+ };
|
|
|
119
|
+ var strresult = SendPostRequest
|
|
|
120
|
+ ("https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=user_id", postData);
|
|
|
121
|
+ if (!string.IsNullOrEmpty(strresult))
|
|
|
122
|
+ {
|
|
|
123
|
+ var resultobj = strresult.ToJObject();
|
|
|
124
|
+ if (resultobj["code"].ToString() != "0")
|
|
|
125
|
+ {
|
|
|
126
|
+ LogFactory.GetLogger("SendMsg").Error(strresult);
|
|
|
127
|
+ result = resultobj["msg"].ToString();
|
|
|
128
|
+ }
|
|
|
129
|
+ else
|
|
|
130
|
+ {
|
|
|
131
|
+ result = resultobj.ToJson() ;
|
|
|
132
|
+ }
|
|
|
133
|
+ }
|
|
|
134
|
+ return result;
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ //获取单个用户
|
|
|
138
|
+ public static string GetUser(string code)
|
|
|
139
|
+ {
|
|
|
140
|
+ string token = GetUserAccessToken(code);
|
|
|
141
|
+
|
|
|
142
|
+ Guid guid = Guid.NewGuid();
|
|
|
143
|
+ string uuid = guid.ToString();
|
|
|
144
|
+ string result = string.Empty;
|
|
|
145
|
+ var strresult = SendGetRequest("https://open.feishu.cn/open-apis/authen/v1/user_info",token);
|
|
|
146
|
+ if (!string.IsNullOrEmpty(strresult))
|
|
|
147
|
+ {
|
|
|
148
|
+ generalResult<user> resultobj = JsonConvert.DeserializeObject<generalResult<user>>(strresult);
|
|
|
149
|
+ if (resultobj.code != "0")
|
|
|
150
|
+ {
|
|
|
151
|
+ LogFactory.GetLogger("GetUser").Error(resultobj.msg);
|
|
|
152
|
+ result = resultobj.msg;
|
|
|
153
|
+ }
|
|
|
154
|
+ else
|
|
|
155
|
+ {
|
|
|
156
|
+ if (resultobj.data != null )
|
|
|
157
|
+ result = resultobj.data.mobile;
|
|
|
158
|
+ }
|
|
|
159
|
+ }
|
|
|
160
|
+ return result;
|
|
|
161
|
+ }
|
|
|
162
|
+ public static string SendGetRequest(string url, string authToken)
|
|
|
163
|
+ {
|
|
|
164
|
+ // 创建请求对象
|
|
|
165
|
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
|
166
|
+ request.Method = "GET";
|
|
|
167
|
+ request.ContentType = "application/json; charset=utf-8"; // 设置 Content-Type
|
|
|
168
|
+ request.Headers.Add("Authorization", $"Bearer {authToken}");
|
|
|
169
|
+
|
|
|
170
|
+ // 如果需要忽略 SSL 证书验证(仅测试环境使用)
|
|
|
171
|
+ // ServicePointManager.ServerCertificateValidationCallback +=
|
|
|
172
|
+ // (sender, certificate, chain, sslPolicyErrors) => true;
|
|
|
173
|
+
|
|
|
174
|
+ try
|
|
|
175
|
+ {
|
|
|
176
|
+ // 获取响应
|
|
|
177
|
+ using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
|
|
178
|
+ using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
|
|
|
179
|
+ {
|
|
|
180
|
+ return reader.ReadToEnd();
|
|
|
181
|
+ }
|
|
|
182
|
+ }
|
|
|
183
|
+ catch (WebException ex)
|
|
|
184
|
+ {
|
|
|
185
|
+ // 处理错误响应
|
|
|
186
|
+ if (ex.Response is HttpWebResponse errorResponse)
|
|
|
187
|
+ {
|
|
|
188
|
+ using (Stream errorStream = errorResponse.GetResponseStream())
|
|
|
189
|
+ using (StreamReader reader = new StreamReader(errorStream, Encoding.UTF8))
|
|
|
190
|
+ {
|
|
|
191
|
+ string errorContent = reader.ReadToEnd();
|
|
|
192
|
+ throw new Exception($"HTTP Error {(int)errorResponse.StatusCode}: {errorContent}");
|
|
|
193
|
+ }
|
|
|
194
|
+ }
|
|
|
195
|
+ throw;
|
|
|
196
|
+ }
|
|
|
197
|
+ }
|
|
|
198
|
+
|
|
|
199
|
+ public static string HttpGet(string url, string token)
|
|
|
200
|
+ {
|
|
|
201
|
+ ServicePointManager.Expect100Continue = true;
|
|
|
202
|
+ ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
|
|
|
203
|
+ ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
|
|
|
204
|
+ HttpClient client = new HttpClient();
|
|
|
205
|
+ client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json; charset=utf-8"));
|
|
|
206
|
+ client.DefaultRequestHeaders.Authorization = new
|
|
|
207
|
+ AuthenticationHeaderValue
|
|
|
208
|
+ ("Bearer", token);
|
|
|
209
|
+ var result = client.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
|
|
|
210
|
+
|
|
|
211
|
+ return result;
|
|
|
212
|
+ }
|
|
|
213
|
+ public static string SendPostRequest(string url, object data)
|
|
|
214
|
+ {
|
|
|
215
|
+ string authToken = GetTenantAccessToken();
|
|
|
216
|
+ // 创建请求对象
|
|
|
217
|
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
|
218
|
+ request.Method = "POST";
|
|
|
219
|
+ request.ContentType = "application/json; charset=utf-8";
|
|
|
220
|
+
|
|
|
221
|
+ // 添加 Authorization 头
|
|
|
222
|
+ request.Headers.Add("Authorization", $"Bearer {authToken}");
|
|
|
223
|
+
|
|
|
224
|
+ // 如果需要忽略 SSL 证书验证(仅测试环境使用)
|
|
|
225
|
+ // ServicePointManager.ServerCertificateValidationCallback +=
|
|
|
226
|
+ // (sender, certificate, chain, sslPolicyErrors) => true;
|
|
|
227
|
+
|
|
|
228
|
+ try
|
|
|
229
|
+ {
|
|
|
230
|
+ // 序列化数据为 JSON
|
|
|
231
|
+ string json = JsonConvert.SerializeObject(data);
|
|
|
232
|
+ byte[] postData = Encoding.UTF8.GetBytes(json);
|
|
|
233
|
+
|
|
|
234
|
+ // 写入请求体
|
|
|
235
|
+ request.ContentLength = postData.Length;
|
|
|
236
|
+ using (Stream stream = request.GetRequestStream())
|
|
|
237
|
+ {
|
|
|
238
|
+ stream.Write(postData, 0, postData.Length);
|
|
|
239
|
+ }
|
|
|
240
|
+
|
|
|
241
|
+ // 获取响应
|
|
|
242
|
+ using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
|
|
243
|
+ using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
|
|
|
244
|
+ {
|
|
|
245
|
+ return reader.ReadToEnd();
|
|
|
246
|
+ }
|
|
|
247
|
+ }
|
|
|
248
|
+ catch (WebException ex)
|
|
|
249
|
+ {
|
|
|
250
|
+ // 处理错误响应
|
|
|
251
|
+ if (ex.Response is HttpWebResponse errorResponse)
|
|
|
252
|
+ {
|
|
|
253
|
+ using (Stream errorStream = errorResponse.GetResponseStream())
|
|
|
254
|
+ using (StreamReader reader = new StreamReader(errorStream, Encoding.UTF8))
|
|
|
255
|
+ {
|
|
|
256
|
+ string errorContent = reader.ReadToEnd();
|
|
|
257
|
+ throw new Exception($"HTTP Error {(int)errorResponse.StatusCode}: {errorContent}");
|
|
|
258
|
+ }
|
|
|
259
|
+ }
|
|
|
260
|
+ throw;
|
|
|
261
|
+ }
|
|
|
262
|
+ }
|
|
|
263
|
+
|
|
|
264
|
+ public static string HttpPost(string url, Dictionary<string, object> dic)
|
|
|
265
|
+ {
|
|
|
266
|
+ string result = ""; string token = GetTenantAccessToken();
|
|
|
267
|
+ try
|
|
|
268
|
+ {
|
|
|
269
|
+ // 创建 HttpWebRequest 对象
|
|
|
270
|
+ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
|
271
|
+ // 设置请求方法为 POST
|
|
|
272
|
+ request.Method = "POST";
|
|
|
273
|
+ // 设置 Content-Type
|
|
|
274
|
+ request.ContentType = "application/json; charset=utf-8";
|
|
|
275
|
+ // 设置 Authorization 头部
|
|
|
276
|
+ request.Headers.Add("Authorization", token);
|
|
|
277
|
+ var d = JsonConvert.SerializeObject(dic);
|
|
|
278
|
+ // 将请求体数据转换为字节数组
|
|
|
279
|
+ byte[] requestBodyBytes = Encoding.UTF8.GetBytes(d);
|
|
|
280
|
+ // 设置请求体的长度
|
|
|
281
|
+ request.ContentLength = requestBodyBytes.Length;
|
|
|
282
|
+
|
|
|
283
|
+ // 获取请求流
|
|
|
284
|
+ using (Stream requestStream = request.GetRequestStream())
|
|
|
285
|
+ {
|
|
|
286
|
+ // 将请求体数据写入请求流
|
|
|
287
|
+ requestStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
|
|
|
288
|
+ }
|
|
|
289
|
+
|
|
|
290
|
+ // 发送请求并获取响应
|
|
|
291
|
+ using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
|
|
292
|
+ {
|
|
|
293
|
+ // 检查响应状态码
|
|
|
294
|
+ if (response.StatusCode == HttpStatusCode.OK)
|
|
|
295
|
+ {
|
|
|
296
|
+ // 获取响应流
|
|
|
297
|
+ using (Stream responseStream = response.GetResponseStream())
|
|
|
298
|
+ {
|
|
|
299
|
+ if (responseStream != null)
|
|
|
300
|
+ {
|
|
|
301
|
+ // 创建 StreamReader 用于读取响应流
|
|
|
302
|
+ using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))
|
|
|
303
|
+ {
|
|
|
304
|
+ // 读取响应内容
|
|
|
305
|
+ string responseBody = reader.ReadToEnd();
|
|
|
306
|
+ Console.WriteLine("请求成功,响应内容:");
|
|
|
307
|
+ return responseBody;
|
|
|
308
|
+ }
|
|
|
309
|
+ }
|
|
|
310
|
+ }
|
|
|
311
|
+ }
|
|
|
312
|
+ else
|
|
|
313
|
+ {
|
|
|
314
|
+ return $"请求失败,状态码:{response.StatusCode}";
|
|
|
315
|
+ }
|
|
|
316
|
+ }
|
|
|
317
|
+ }
|
|
|
318
|
+ catch (WebException ex)
|
|
|
319
|
+ {
|
|
|
320
|
+ if (ex.Status == WebExceptionStatus.ProtocolError)
|
|
|
321
|
+ {
|
|
|
322
|
+ HttpWebResponse errorResponse = (HttpWebResponse)ex.Response;
|
|
|
323
|
+ return $"请求失败,状态码:{errorResponse.StatusCode}";
|
|
|
324
|
+ }
|
|
|
325
|
+ else
|
|
|
326
|
+ {
|
|
|
327
|
+ return $"发生网络异常:{ex.Message}";
|
|
|
328
|
+
|
|
|
329
|
+ }
|
|
|
330
|
+ }
|
|
|
331
|
+ catch (Exception ex)
|
|
|
332
|
+ {
|
|
|
333
|
+ return $"发生异常:{ex.Message}";
|
|
|
334
|
+ }
|
|
|
335
|
+
|
|
|
336
|
+ return result;
|
|
|
337
|
+ }
|
|
|
338
|
+ private class generalResult<T> where T : class
|
|
|
339
|
+ {
|
|
|
340
|
+ public string code { get; set; }
|
|
|
341
|
+ public string msg { get; set; }
|
|
|
342
|
+ public T data { get; set; }
|
|
|
343
|
+ }
|
|
|
344
|
+ private class userlist
|
|
|
345
|
+ {
|
|
|
346
|
+ public string user_id { get; set; }
|
|
|
347
|
+ }
|
|
|
348
|
+ private class userslist
|
|
|
349
|
+ {
|
|
|
350
|
+ public List<userlist> user_list { get; set; }
|
|
|
351
|
+ }
|
|
|
352
|
+ private class user
|
|
|
353
|
+ {
|
|
|
354
|
+ public string mobile { get; set; }
|
|
|
355
|
+ }
|
|
|
356
|
+
|
|
|
357
|
+ }
|
|
|
358
|
+}
|