|
@@ -0,0 +1,235 @@
|
|
|
|
+using OASystem.Domain.Dtos.QiYeWeChat;
|
|
|
|
+using OASystem.Domain.ViewModels.JuHeExchangeRate;
|
|
|
|
+using OASystem.Domain.ViewModels.QiYeWeChat;
|
|
|
|
+using Org.BouncyCastle.Ocsp;
|
|
|
|
+using System.Text.Json;
|
|
|
|
+using Ubiety.Dns.Core;
|
|
|
|
+
|
|
|
|
+namespace OASystem.API.OAMethodLib.QiYeWeChatAPI
|
|
|
|
+{
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 聚合Api 服务
|
|
|
|
+ /// </summary>
|
|
|
|
+ public class QiYeWeChatApiService: IQiYeWeChatApiService
|
|
|
|
+ {
|
|
|
|
+ private readonly HttpClient _httpClient;
|
|
|
|
+ private readonly string CorpId = "wwe978bef5495a0728"; //企业Id
|
|
|
|
+ private readonly string PersonnelAssistant_AgentId = "3010011"; //人事助手Id
|
|
|
|
+ private readonly string PersonnelAssistant_Corpsecret = "ig--IJd6TxWDMJ1wT4e-RDRcRX12v5GjB359DNATwJ4"; //人事助手凭证密钥
|
|
|
|
+ private readonly string PunchCard_AgentId = "3010185"; //打卡Id
|
|
|
|
+ private readonly string PunchCard_Corpsecret = "Xhrl37GOqlAjsu0VzUSJECaJdjzkDXQLbvrzRsZQb8M"; //打卡凭证密钥
|
|
|
|
+ private readonly string Email_AgentId = "1000004"; //E-Mail Id
|
|
|
|
+ private readonly string Email_Corpsecret = "NA1zbJM15GmgjPYwDOqz59dIo1Wnug-MbU107MeUemc"; //E-Mail 凭证密钥
|
|
|
|
+ private readonly string AddressBook_Corpsecret = "Y1tnjh7j-BvbqAytAoXZPUbmDR6dqLTL6mXtc6PZ7fo"; //通讯录同步 凭证密钥
|
|
|
|
+ private readonly JobPostRepository _jobPostRep;
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 初始化
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="clientFactory"></param>
|
|
|
|
+ /// <param name="jobPostRep"></param>
|
|
|
|
+ public QiYeWeChatApiService(IHttpClientFactory clientFactory, JobPostRepository jobPostRep)
|
|
|
|
+ {
|
|
|
|
+ _httpClient = clientFactory.CreateClient("PublicQiYeWeChatApi"); ;
|
|
|
|
+ _jobPostRep = jobPostRep;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 获取access_token
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="applicationType">
|
|
|
|
+ /// 1:人事助手
|
|
|
|
+ /// 2:打卡
|
|
|
|
+ /// 3:邮件
|
|
|
|
+ /// 4:通讯录同步
|
|
|
|
+ /// </param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ private async Task<Access_TokenView> GetTokenAsync(int applicationType)
|
|
|
|
+ {
|
|
|
|
+ Access_TokenView tokenResult = new Access_TokenView();
|
|
|
|
+ Access_Token_Request access_Token = new Access_Token_Request();
|
|
|
|
+ string cacheName = string.Empty;
|
|
|
|
+
|
|
|
|
+ if (applicationType == 1)
|
|
|
|
+ {
|
|
|
|
+ access_Token.corpsecret = PersonnelAssistant_Corpsecret; //人事助手
|
|
|
|
+ cacheName = "PersonnelAssistant_Access_Token";
|
|
|
|
+ }
|
|
|
|
+ else if (applicationType == 2)
|
|
|
|
+ {
|
|
|
|
+ access_Token.corpsecret = PunchCard_Corpsecret; //打卡
|
|
|
|
+ cacheName = "PunchCard_Access_Token";
|
|
|
|
+ }
|
|
|
|
+ else if (applicationType == 3)
|
|
|
|
+ {
|
|
|
|
+ access_Token.corpsecret = Email_Corpsecret; //E-Mail
|
|
|
|
+ cacheName = "Enail_Access_Token";
|
|
|
|
+ }
|
|
|
|
+ else if (applicationType == 4)
|
|
|
|
+ {
|
|
|
|
+ access_Token.corpsecret = AddressBook_Corpsecret; //通讯录同步
|
|
|
|
+ cacheName = "AddressBook_Access_Token";
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ tokenResult.errmsg = "未识别应用类型Id!";
|
|
|
|
+ return tokenResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ string access_token = await RedisRepository.RedisFactory
|
|
|
|
+ .CreateRedisRepository()
|
|
|
|
+ .StringGetAsync<string>(cacheName);//string 取
|
|
|
|
+
|
|
|
|
+ if (string.IsNullOrEmpty(access_token))
|
|
|
|
+ {
|
|
|
|
+ string access_token_url = string.Format(@"/cgi-bin/gettoken?corpid={0}&corpsecret={1}", access_Token.corpid, access_Token.corpsecret);
|
|
|
|
+
|
|
|
|
+ var access_tokenReq = await _httpClient.GetAsync(access_token_url);
|
|
|
|
+ if (access_tokenReq.IsSuccessStatusCode)
|
|
|
|
+ {
|
|
|
|
+ var stringResponse = await access_tokenReq.Content.ReadAsStringAsync();
|
|
|
|
+
|
|
|
|
+ tokenResult = System.Text.Json.JsonSerializer.Deserialize<Access_TokenView>(stringResponse,
|
|
|
|
+ new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
|
|
|
|
+
|
|
|
|
+ if (tokenResult.errcode == 0)
|
|
|
|
+ {
|
|
|
|
+ TimeSpan ts = DateTime.Now.AddMinutes(118) - DateTime.Now; //设置redis 过期时间 118分钟
|
|
|
|
+ await RedisRepository
|
|
|
|
+ .RedisFactory
|
|
|
|
+ .CreateRedisRepository()
|
|
|
|
+ .StringSetAsync<string>(cacheName, tokenResult.access_token, ts);//string 存
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ tokenResult.errmsg = "企业微信Token未获取到!";
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ tokenResult.errcode = 0;
|
|
|
|
+ tokenResult.access_token = access_token;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return tokenResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 获取部门列表
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="access_token"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ private async Task<DepartmentListView> GetDepartmentAsync(string access_token)
|
|
|
|
+ {
|
|
|
|
+ DepartmentListView result = new DepartmentListView();
|
|
|
|
+
|
|
|
|
+ string dep_url = string.Format("/cgi-bin/department/list?access_token={0}", access_token);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ var depReq = await _httpClient.GetAsync(dep_url);
|
|
|
|
+ if (depReq.IsSuccessStatusCode)
|
|
|
|
+ {
|
|
|
|
+ var stringResponse = await depReq.Content.ReadAsStringAsync();
|
|
|
|
+
|
|
|
|
+ result = System.Text.Json.JsonSerializer.Deserialize<DepartmentListView>(stringResponse,
|
|
|
|
+ new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ result.errcode = -1;
|
|
|
|
+ result.errmsg = "企业微信部门列表未获取到!";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 创建员工
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="create_Request"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public async Task<ResponseBase> CreateAsync(Create_Request create_Request)
|
|
|
|
+ {
|
|
|
|
+ ResponseBase responseBase = new ResponseBase();
|
|
|
|
+
|
|
|
|
+ #region 职位
|
|
|
|
+ string depName = string.Empty,
|
|
|
|
+ jobName = string.Empty;
|
|
|
|
+
|
|
|
|
+ List<Sys_JobPostI> jobs = await _jobPostRep.QueryJobPost(string.Empty);
|
|
|
|
+
|
|
|
|
+ Sys_JobPostI jobPost = jobs.Where(it => it.IsDel == 0 && it.Id == Convert.ToInt32(create_Request.position)).FirstOrDefault();
|
|
|
|
+
|
|
|
|
+ if (jobPost != null)
|
|
|
|
+ {
|
|
|
|
+ depName = jobPost.DepName;
|
|
|
|
+ jobName = jobPost.JobName;
|
|
|
|
+ }
|
|
|
|
+ create_Request.position = jobName;
|
|
|
|
+
|
|
|
|
+ if (string.IsNullOrEmpty(create_Request.biz_mail))
|
|
|
|
+ {
|
|
|
|
+ create_Request.biz_mail = string.Format("{0}@pan-american-intl.com", create_Request.userid);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
|
|
+ Access_TokenView access_Token = new Access_TokenView();
|
|
|
|
+ access_Token = await GetTokenAsync(4);
|
|
|
|
+ if (access_Token.errcode == 0)
|
|
|
|
+ {
|
|
|
|
+ create_Request.access_token = access_Token.access_token;
|
|
|
|
+
|
|
|
|
+ #region 处理部门
|
|
|
|
+ DepartmentListView depData = await GetDepartmentAsync(access_Token.access_token);
|
|
|
|
+
|
|
|
|
+ if (depData.errcode == 0)
|
|
|
|
+ {
|
|
|
|
+ var depData1 = depData.department.Where(x => x.name == depName).FirstOrDefault();
|
|
|
|
+ if (depData1 != null)
|
|
|
|
+ {
|
|
|
|
+ create_Request.department = new List<long> { depData1.id };
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ responseBase.errcode = -1;
|
|
|
|
+ responseBase.errmsg = "未在企业微信上找到OA内设置的部门";
|
|
|
|
+ return responseBase;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ responseBase.errcode = depData.errcode;
|
|
|
|
+ responseBase.errmsg = depData.errmsg;
|
|
|
|
+ return responseBase;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ #endregion
|
|
|
|
+
|
|
|
|
+ create_Request.access_token = access_Token.access_token;
|
|
|
|
+ string create_url = string.Format("/cgi-bin/user/create?access_token={0}", create_Request.access_token);
|
|
|
|
+
|
|
|
|
+ var json = System.Text.Json.JsonSerializer.Serialize(create_Request);
|
|
|
|
+ var content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
+ var create_Req = await _httpClient.PostAsync(create_url, content);
|
|
|
|
+ var stringResponse = await create_Req.Content.ReadAsStringAsync();
|
|
|
|
+
|
|
|
|
+ responseBase = System.Text.Json.JsonSerializer.Deserialize<ResponseBase>(stringResponse,
|
|
|
|
+ new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ responseBase.errcode = access_Token.errcode;
|
|
|
|
+ responseBase.errmsg = access_Token.errmsg;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return responseBase;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|