using Microsoft.Extensions.Caching.Memory; using RestSharp; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static System.Net.WebRequestMethods; using static XYY.Common.Standard.QYWXWebHook; namespace XYY.Common.Standard { public partial class QYWXAPI { IMemoryCache memoryCache; public QYWXAPI() { this.memoryCache = new MemoryCache(new MemoryCacheOptions { }); } public string GetToken(bool CommunicationNews = false) { /* corpid ww6335df98d0bd3b25 corpsecret Kvrqo51WusTEIRlcyfo6vE2U-Kd_cb9eyJbMlTTF2Gg */ string token = memoryCache.Get("txltoken"); if (token == null) { string corpid = "ww6335df98d0bd3b25"; string corpsecret = "Kvrqo51WusTEIRlcyfo6vE2U-Kd_cb9eyJbMlTTF2Gg"; if (CommunicationNews) { corpsecret = "0T82vwSIfvjrMnpYSN18f7c9eeCg-tIvrWFIC9SktJk"; } string url = $@"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={corpid}&corpsecret={corpsecret}"; RestClient restClient = new RestClient(url); RestRequest restRequest = new RestRequest(Method.GET); var response = restClient.Execute(restRequest); if (response.IsSuccessful) { TokenResult result = Newtonsoft.Json.JsonConvert.DeserializeObject(response.Content); if (result != null && !string.IsNullOrEmpty(result.access_token)) { token = result.access_token; memoryCache.Set("txltoken", token); } else { throw new Exception(result.errmsg); } } else { throw new Exception("获取WXToken失败" + (response.ErrorMessage ?? response.Content)); } } return token; } public List GetUserInfoItemByDepartment(string token, int departmentId) { string url = $@"https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={token}&department_id={departmentId}"; RestClient client = new RestClient(url); RestRequest request = new RestRequest(Method.GET); var response = client.Execute(request); if (response.IsSuccessful) { UserInfoResult userIdResult = Newtonsoft.Json.JsonConvert.DeserializeObject(response.Content); if (userIdResult.errcode == 0) { return userIdResult.userlist; } else { throw new Exception(userIdResult.errmsg); } } else { throw new Exception(response.ErrorMessage ?? response.Content); } } public List GetCustomers(string userId) { List result = new List(); string token = GetToken(false); string url2 = $"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/batch/get_by_user?access_token={token}"; RestClient client2 = new RestClient(url2); RestRequest restRequest2 = new RestRequest(Method.POST); restRequest2.AddJsonBody(new { userid_list = new string[] { userId } }); var response2 = client2.Execute(restRequest2); if (response2.StatusCode != System.Net.HttpStatusCode.OK) throw new Exception(response2.ErrorMessage ?? response2.Content); External_contact_Root root = Newtonsoft.Json.JsonConvert.DeserializeObject(response2.Content); if (root.errcode != 0) throw new Exception(root.errmsg); result.AddRange(root.external_contact_list.Select(x => new UserlistItem { name = x.external_contact.name, position = x.external_contact.position, corp_name = x.external_contact.corp_name, avatar = x.external_contact.avatar, userid = x.external_contact.external_userid })); return result; } public List GetUserInfos() { List result = new List(); string token = GetToken(false); string url = $"https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={token}"; RestClient client = new RestClient(url); RestRequest request = new RestRequest(Method.GET); var response = client.Execute(request); if (response.IsSuccessful) { DepartmentItemResult userIdResult = Newtonsoft.Json.JsonConvert.DeserializeObject(response.Content); if (userIdResult.errcode == 0) { userIdResult.department.ForEach(x => result.AddRange(GetUserInfoItemByDepartment(token, x.id))); return result; } else { throw new Exception(userIdResult.errmsg); } } else { throw new Exception(response.ErrorMessage ?? response.Content); } } public class WXUserInfo { public string userid { get; set; } public string userName { get; set; } public string department { get; set; } } //如果好用,请收藏地址,帮忙分享。 public class TokenResult { /// /// /// public int errcode { get; set; } /// /// /// public string errmsg { get; set; } /// /// /// public string access_token { get; set; } /// /// /// public int expires_in { get; set; } } //如果好用,请收藏地址,帮忙分享。 public class DepartmentItem { /// /// /// public int id { get; set; } /// /// 广州研发中心 /// public string name { get; set; } /// /// /// public string name_en { get; set; } /// /// /// public List department_leader { get; set; } /// /// /// public int parentid { get; set; } /// /// /// public int order { get; set; } } //如果好用,请收藏地址,帮忙分享。 public class GetCustomerListResult { /// /// /// public int errcode { get; set; } /// /// /// public string errmsg { get; set; } /// /// /// public List external_userid { get; set; } } public class DepartmentItemResult { /// /// /// public int errcode { get; set; } /// /// /// public string errmsg { get; set; } /// /// /// public List department { get; set; } } //如果好用,请收藏地址,帮忙分享。 public class Text { /// /// 文本 /// public string value { get; set; } } public class AttrsItem { /// /// /// public int type { get; set; } /// /// 文本名称 /// public string name { get; set; } /// /// /// public Text text { get; set; } } public class Extattr { /// /// /// public List attrs { get; set; } } public class Wechat_channels { /// /// 视频号名称 /// public string nickname { get; set; } /// /// /// public int status { get; set; } } public class External_attrItem { /// /// /// public int type { get; set; } /// /// 文本名称 /// public string name { get; set; } /// /// /// public Text text { get; set; } } public class External_profile { /// /// 企业简称 /// public string external_corp_name { get; set; } /// /// /// public Wechat_channels wechat_channels { get; set; } /// /// /// public List external_attr { get; set; } } public class UserlistItem { /// /// /// public string userid { get; set; } /// /// 李四 /// public string name { get; set; } /// /// /// public List department { get; set; } /// /// /// public List order { get; set; } /// /// 后台工程师 /// public string position { get; set; } /// /// /// public string mobile { get; set; } /// /// /// public string gender { get; set; } /// /// /// public string email { get; set; } /// /// /// public string biz_mail { get; set; } /// /// /// public List is_leader_in_dept { get; set; } /// /// /// public List direct_leader { get; set; } /// /// /// public string avatar { get; set; } /// /// /// public string thumb_avatar { get; set; } /// /// /// public string telephone { get; set; } /// /// /// public string @alias { get; set; } /// /// /// public int status { get; set; } /// /// 广州市海珠区新港中路 /// public string address { get; set; } /// /// /// public string english_name { get; set; } /// /// /// public string open_userid { get; set; } /// /// /// public int main_department { get; set; } /// /// /// public Extattr extattr { get; set; } /// /// /// public string qr_code { get; set; } /// /// 产品经理 /// public string external_position { get; set; } public string corp_name { get; set; } /// /// /// public External_profile external_profile { get; set; } } public class UserInfoResult { /// /// /// public int errcode { get; set; } /// /// /// public string errmsg { get; set; } /// /// /// public List userlist { get; set; } } } }