QYWXAPI.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using Microsoft.Extensions.Caching.Memory;
  2. using RestSharp;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using static System.Net.WebRequestMethods;
  9. using static XYY.Common.Standard.QYWXWebHook;
  10. namespace XYY.Common.Standard
  11. {
  12. public partial class QYWXAPI
  13. {
  14. IMemoryCache memoryCache;
  15. public QYWXAPI()
  16. {
  17. this.memoryCache = new MemoryCache(new MemoryCacheOptions
  18. {
  19. });
  20. }
  21. public string GetToken(bool CommunicationNews = false)
  22. {
  23. /*
  24. corpid ww6335df98d0bd3b25
  25. corpsecret Kvrqo51WusTEIRlcyfo6vE2U-Kd_cb9eyJbMlTTF2Gg
  26. */
  27. string token = memoryCache.Get<string>("txltoken");
  28. if (token == null)
  29. {
  30. string corpid = "ww6335df98d0bd3b25";
  31. string corpsecret = "Kvrqo51WusTEIRlcyfo6vE2U-Kd_cb9eyJbMlTTF2Gg";
  32. if (CommunicationNews)
  33. {
  34. corpsecret = "0T82vwSIfvjrMnpYSN18f7c9eeCg-tIvrWFIC9SktJk";
  35. }
  36. string url = $@"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}";
  37. RestClient restClient = new RestClient(url);
  38. RestRequest restRequest = new RestRequest(Method.GET);
  39. var response = restClient.Execute(restRequest);
  40. if (response.IsSuccessful)
  41. {
  42. TokenResult result = Newtonsoft.Json.JsonConvert.DeserializeObject<TokenResult>(response.Content);
  43. if (result != null && !string.IsNullOrEmpty(result.access_token))
  44. {
  45. token = result.access_token;
  46. memoryCache.Set<string>("txltoken", token);
  47. }
  48. else
  49. {
  50. throw new Exception(result.errmsg);
  51. }
  52. }
  53. else
  54. {
  55. throw new Exception("获取WXToken失败" + (response.ErrorMessage ?? response.Content));
  56. }
  57. }
  58. return token;
  59. }
  60. public List<UserlistItem> GetUserInfoItemByDepartment(string token, int departmentId)
  61. {
  62. string url = $@"https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={token}&department_id={departmentId}";
  63. RestClient client = new RestClient(url);
  64. RestRequest request = new RestRequest(Method.GET);
  65. var response = client.Execute(request);
  66. if (response.IsSuccessful)
  67. {
  68. UserInfoResult userIdResult = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfoResult>(response.Content);
  69. if (userIdResult.errcode == 0)
  70. {
  71. return userIdResult.userlist;
  72. }
  73. else
  74. {
  75. throw new Exception(userIdResult.errmsg);
  76. }
  77. }
  78. else
  79. {
  80. throw new Exception(response.ErrorMessage ?? response.Content);
  81. }
  82. }
  83. public List<UserlistItem> GetCustomers(string userId)
  84. {
  85. List<UserlistItem> result = new List<UserlistItem>();
  86. string token = GetToken(false);
  87. string url2 = $"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token={token}";
  88. RestClient client2 = new RestClient(url2);
  89. RestRequest restRequest2 = new RestRequest(Method.POST);
  90. restRequest2.AddJsonBody(new { userid_list = new string[] { userId } });
  91. var response2 = client2.Execute(restRequest2);
  92. if (response2.StatusCode != System.Net.HttpStatusCode.OK)
  93. throw new Exception(response2.ErrorMessage ?? response2.Content);
  94. External_contact_Root root = Newtonsoft.Json.JsonConvert.DeserializeObject<External_contact_Root>(response2.Content);
  95. if (root.errcode != 0)
  96. throw new Exception(root.errmsg);
  97. result.AddRange(root.external_contact_list.Select(x => new UserlistItem
  98. {
  99. name = x.external_contact.name,
  100. position = x.external_contact.position,
  101. corp_name = x.external_contact.corp_name,
  102. avatar = x.external_contact.avatar,
  103. userid = x.external_contact.external_userid
  104. }));
  105. return result;
  106. }
  107. public List<UserlistItem> GetUserInfos()
  108. {
  109. List<UserlistItem> result = new List<UserlistItem>();
  110. string token = GetToken(false);
  111. string url = $"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={token}";
  112. RestClient client = new RestClient(url);
  113. RestRequest request = new RestRequest(Method.GET);
  114. var response = client.Execute(request);
  115. if (response.IsSuccessful)
  116. {
  117. DepartmentItemResult userIdResult = Newtonsoft.Json.JsonConvert.DeserializeObject<DepartmentItemResult>(response.Content);
  118. if (userIdResult.errcode == 0)
  119. {
  120. userIdResult.department.ForEach(x => result.AddRange(GetUserInfoItemByDepartment(token, x.id)));
  121. return result;
  122. }
  123. else
  124. {
  125. throw new Exception(userIdResult.errmsg);
  126. }
  127. }
  128. else
  129. {
  130. throw new Exception(response.ErrorMessage ?? response.Content);
  131. }
  132. }
  133. public class WXUserInfo
  134. {
  135. public string userid { get; set; }
  136. public string userName { get; set; }
  137. public string department { get; set; }
  138. }
  139. //如果好用,请收藏地址,帮忙分享。
  140. public class TokenResult
  141. {
  142. /// <summary>
  143. ///
  144. /// </summary>
  145. public int errcode { get; set; }
  146. /// <summary>
  147. ///
  148. /// </summary>
  149. public string errmsg { get; set; }
  150. /// <summary>
  151. ///
  152. /// </summary>
  153. public string access_token { get; set; }
  154. /// <summary>
  155. ///
  156. /// </summary>
  157. public int expires_in { get; set; }
  158. }
  159. //如果好用,请收藏地址,帮忙分享。
  160. public class DepartmentItem
  161. {
  162. /// <summary>
  163. ///
  164. /// </summary>
  165. public int id { get; set; }
  166. /// <summary>
  167. /// 广州研发中心
  168. /// </summary>
  169. public string name { get; set; }
  170. /// <summary>
  171. ///
  172. /// </summary>
  173. public string name_en { get; set; }
  174. /// <summary>
  175. ///
  176. /// </summary>
  177. public List<string> department_leader { get; set; }
  178. /// <summary>
  179. ///
  180. /// </summary>
  181. public int parentid { get; set; }
  182. /// <summary>
  183. ///
  184. /// </summary>
  185. public int order { get; set; }
  186. }
  187. //如果好用,请收藏地址,帮忙分享。
  188. public class GetCustomerListResult
  189. {
  190. /// <summary>
  191. ///
  192. /// </summary>
  193. public int errcode { get; set; }
  194. /// <summary>
  195. ///
  196. /// </summary>
  197. public string errmsg { get; set; }
  198. /// <summary>
  199. ///
  200. /// </summary>
  201. public List<string> external_userid { get; set; }
  202. }
  203. public class DepartmentItemResult
  204. {
  205. /// <summary>
  206. ///
  207. /// </summary>
  208. public int errcode { get; set; }
  209. /// <summary>
  210. ///
  211. /// </summary>
  212. public string errmsg { get; set; }
  213. /// <summary>
  214. ///
  215. /// </summary>
  216. public List<DepartmentItem> department { get; set; }
  217. }
  218. //如果好用,请收藏地址,帮忙分享。
  219. public class Text
  220. {
  221. /// <summary>
  222. /// 文本
  223. /// </summary>
  224. public string value { get; set; }
  225. }
  226. public class AttrsItem
  227. {
  228. /// <summary>
  229. ///
  230. /// </summary>
  231. public int type { get; set; }
  232. /// <summary>
  233. /// 文本名称
  234. /// </summary>
  235. public string name { get; set; }
  236. /// <summary>
  237. ///
  238. /// </summary>
  239. public Text text { get; set; }
  240. }
  241. public class Extattr
  242. {
  243. /// <summary>
  244. ///
  245. /// </summary>
  246. public List<AttrsItem> attrs { get; set; }
  247. }
  248. public class Wechat_channels
  249. {
  250. /// <summary>
  251. /// 视频号名称
  252. /// </summary>
  253. public string nickname { get; set; }
  254. /// <summary>
  255. ///
  256. /// </summary>
  257. public int status { get; set; }
  258. }
  259. public class External_attrItem
  260. {
  261. /// <summary>
  262. ///
  263. /// </summary>
  264. public int type { get; set; }
  265. /// <summary>
  266. /// 文本名称
  267. /// </summary>
  268. public string name { get; set; }
  269. /// <summary>
  270. ///
  271. /// </summary>
  272. public Text text { get; set; }
  273. }
  274. public class External_profile
  275. {
  276. /// <summary>
  277. /// 企业简称
  278. /// </summary>
  279. public string external_corp_name { get; set; }
  280. /// <summary>
  281. ///
  282. /// </summary>
  283. public Wechat_channels wechat_channels { get; set; }
  284. /// <summary>
  285. ///
  286. /// </summary>
  287. public List<External_attrItem> external_attr { get; set; }
  288. }
  289. public class UserlistItem
  290. {
  291. /// <summary>
  292. ///
  293. /// </summary>
  294. public string userid { get; set; }
  295. /// <summary>
  296. /// 李四
  297. /// </summary>
  298. public string name { get; set; }
  299. /// <summary>
  300. ///
  301. /// </summary>
  302. public List<int> department { get; set; }
  303. /// <summary>
  304. ///
  305. /// </summary>
  306. public List<int> order { get; set; }
  307. /// <summary>
  308. /// 后台工程师
  309. /// </summary>
  310. public string position { get; set; }
  311. /// <summary>
  312. ///
  313. /// </summary>
  314. public string mobile { get; set; }
  315. /// <summary>
  316. ///
  317. /// </summary>
  318. public string gender { get; set; }
  319. /// <summary>
  320. ///
  321. /// </summary>
  322. public string email { get; set; }
  323. /// <summary>
  324. ///
  325. /// </summary>
  326. public string biz_mail { get; set; }
  327. /// <summary>
  328. ///
  329. /// </summary>
  330. public List<int> is_leader_in_dept { get; set; }
  331. /// <summary>
  332. ///
  333. /// </summary>
  334. public List<string> direct_leader { get; set; }
  335. /// <summary>
  336. ///
  337. /// </summary>
  338. public string avatar { get; set; }
  339. /// <summary>
  340. ///
  341. /// </summary>
  342. public string thumb_avatar { get; set; }
  343. /// <summary>
  344. ///
  345. /// </summary>
  346. public string telephone { get; set; }
  347. /// <summary>
  348. ///
  349. /// </summary>
  350. public string @alias { get; set; }
  351. /// <summary>
  352. ///
  353. /// </summary>
  354. public int status { get; set; }
  355. /// <summary>
  356. /// 广州市海珠区新港中路
  357. /// </summary>
  358. public string address { get; set; }
  359. /// <summary>
  360. ///
  361. /// </summary>
  362. public string english_name { get; set; }
  363. /// <summary>
  364. ///
  365. /// </summary>
  366. public string open_userid { get; set; }
  367. /// <summary>
  368. ///
  369. /// </summary>
  370. public int main_department { get; set; }
  371. /// <summary>
  372. ///
  373. /// </summary>
  374. public Extattr extattr { get; set; }
  375. /// <summary>
  376. ///
  377. /// </summary>
  378. public string qr_code { get; set; }
  379. /// <summary>
  380. /// 产品经理
  381. /// </summary>
  382. public string external_position { get; set; }
  383. public string corp_name { get; set; }
  384. /// <summary>
  385. ///
  386. /// </summary>
  387. public External_profile external_profile { get; set; }
  388. }
  389. public class UserInfoResult
  390. {
  391. /// <summary>
  392. ///
  393. /// </summary>
  394. public int errcode { get; set; }
  395. /// <summary>
  396. ///
  397. /// </summary>
  398. public string errmsg { get; set; }
  399. /// <summary>
  400. ///
  401. /// </summary>
  402. public List<UserlistItem> userlist { get; set; }
  403. }
  404. }
  405. }