QiYeWeChatApiService.cs 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. using EyeSoft.Extensions;
  2. using OASystem.Domain.Dtos.QiYeWeChat;
  3. using OASystem.Domain.ViewModels.QiYeWeChat;
  4. using System.Diagnostics;
  5. using System.Text.Json;
  6. namespace OASystem.API.OAMethodLib.QiYeWeChatAPI
  7. {
  8. /// <summary>
  9. /// 聚合Api 服务
  10. /// </summary>
  11. public class QiYeWeChatApiService : IQiYeWeChatApiService
  12. {
  13. private readonly HttpClient _httpClient;
  14. private readonly string CorpId = "wwe978bef5495a0728"; //企业Id
  15. private readonly string PersonnelAssistant_AgentId = "3010011"; //人事助手Id
  16. private readonly string PersonnelAssistant_Corpsecret = "ig--IJd6TxWDMJ1wT4e-RDRcRX12v5GjB359DNATwJ4"; //人事助手凭证密钥
  17. private readonly string PunchCard_AgentId = "3010185"; //打卡Id
  18. private readonly string PunchCard_Corpsecret = "Xhrl37GOqlAjsu0VzUSJECaJdjzkDXQLbvrzRsZQb8M"; //打卡凭证密钥
  19. private readonly string Email_AgentId = "1000004"; //E-Mail Id
  20. private readonly string Email_Corpsecret = "NA1zbJM15GmgjPYwDOqz59dIo1Wnug-MbU107MeUemc"; //E-Mail 凭证密钥
  21. private readonly string AddressBook_Corpsecret = "Y1tnjh7j-BvbqAytAoXZPUbmDR6dqLTL6mXtc6PZ7fo"; //通讯录同步 凭证密钥
  22. private readonly string Approve_AgentId = "3010040"; //审批 Id
  23. private readonly string Approve_Corpsecret = "k_Jo69Jw9Hqg_in-Rypbs30PNbxOYa1t4e-dxYuT-kw"; //审批 凭证密钥
  24. private readonly string GroupStatus_AgentId = "1000008"; //OA通知 Id
  25. private readonly string GroupStatus_Corpsecret = "7J_ST3jTPzbZpFwl7ttToTVufjEx6O2wuApvKHxt2Ak"; //OA通知Secret
  26. private readonly DateTime _1970 = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  27. private readonly JobPostRepository _jobPostRep;
  28. /// <summary>
  29. /// 初始化
  30. /// </summary>
  31. /// <param name="clientFactory"></param>
  32. /// <param name="jobPostRep"></param>
  33. public QiYeWeChatApiService(IHttpClientFactory clientFactory, JobPostRepository jobPostRep)
  34. {
  35. _httpClient = clientFactory.CreateClient("PublicQiYeWeChatApi"); ;
  36. _jobPostRep = jobPostRep;
  37. }
  38. /// <summary>
  39. /// 获取access_token
  40. /// </summary>
  41. /// <param name="applicationType">
  42. /// 1:人事助手
  43. /// 2:打卡
  44. /// 3:邮件
  45. /// 4:通讯录同步
  46. /// 5:审批
  47. /// 6:团组状态通知
  48. /// </param>
  49. /// <returns></returns>
  50. private async Task<Access_TokenView> GetTokenAsync(int applicationType)
  51. {
  52. Access_TokenView tokenResult = new Access_TokenView();
  53. Access_Token_Request access_Token = new Access_Token_Request();
  54. string cacheName = string.Empty;
  55. if (applicationType == 1)
  56. {
  57. access_Token.corpsecret = PersonnelAssistant_Corpsecret; //人事助手
  58. cacheName = "PersonnelAssistant_Access_Token";
  59. }
  60. else if (applicationType == 2)
  61. {
  62. access_Token.corpsecret = PunchCard_Corpsecret; //打卡
  63. cacheName = "PunchCard_Access_Token";
  64. }
  65. else if (applicationType == 3)
  66. {
  67. access_Token.corpsecret = Email_Corpsecret; //E-Mail
  68. cacheName = "Enail_Access_Token";
  69. }
  70. else if (applicationType == 4) //通讯录同步
  71. {
  72. access_Token.corpsecret = AddressBook_Corpsecret;
  73. cacheName = "AddressBook_Access_Token";
  74. }
  75. else if (applicationType == 5) //审批
  76. {
  77. access_Token.corpsecret = Approve_Corpsecret;
  78. cacheName = "Approve_Access_Token";
  79. }
  80. else if (applicationType == 6) //团组状态通知
  81. {
  82. access_Token.corpsecret = GroupStatus_Corpsecret;
  83. cacheName = "GroupStatus_Access_Token";
  84. }
  85. else
  86. {
  87. tokenResult.errmsg = "未识别应用类型Id!";
  88. return tokenResult;
  89. }
  90. string access_token = await RedisRepository.RedisFactory
  91. .CreateRedisRepository()
  92. .StringGetAsync<string>(cacheName);//string 取
  93. if (string.IsNullOrEmpty(access_token))
  94. {
  95. string access_token_url = string.Format(@"/cgi-bin/gettoken?corpid={0}&corpsecret={1}", access_Token.corpid, access_Token.corpsecret);
  96. var access_tokenReq = await _httpClient.GetAsync(access_token_url);
  97. if (access_tokenReq.IsSuccessStatusCode)
  98. {
  99. var stringResponse = await access_tokenReq.Content.ReadAsStringAsync();
  100. tokenResult = System.Text.Json.JsonSerializer.Deserialize<Access_TokenView>(stringResponse,
  101. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  102. if (tokenResult.errcode == 0)
  103. {
  104. TimeSpan ts = DateTime.Now.AddMinutes(118) - DateTime.Now; //设置redis 过期时间 118分钟
  105. await RedisRepository
  106. .RedisFactory
  107. .CreateRedisRepository()
  108. .StringSetAsync<string>(cacheName, tokenResult.access_token, ts);//string 存
  109. }
  110. }
  111. else
  112. {
  113. tokenResult.errmsg = "企业微信Token未获取到!";
  114. }
  115. }
  116. else
  117. {
  118. tokenResult.errcode = 0;
  119. tokenResult.access_token = access_token;
  120. }
  121. return tokenResult;
  122. }
  123. #region 添加员工
  124. /// <summary>
  125. /// 获取成员ID列表
  126. /// </summary>
  127. /// <returns></returns>
  128. public async Task<UserIdListView> GetUserIdListAsync()
  129. {
  130. UserIdListView userIdListView = new UserIdListView();
  131. Access_TokenView access_Token = await GetTokenAsync(4);
  132. if (access_Token.errcode != 0)
  133. {
  134. userIdListView.errcode = access_Token.errcode;
  135. userIdListView.errmsg = access_Token.errmsg;
  136. return userIdListView;
  137. }
  138. string url = string.Format("/cgi-bin/user/list_id?access_token={0}&debug=1", access_Token.access_token);
  139. var jsonData = new
  140. {
  141. access_token = access_Token.access_token, //调用接口凭证
  142. };
  143. var json = System.Text.Json.JsonSerializer.Serialize(jsonData);
  144. var content = new StringContent(json, Encoding.UTF8, "application/json");
  145. var create_Req = await _httpClient.PostAsync(url, content);
  146. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  147. userIdListView = System.Text.Json.JsonSerializer.Deserialize<UserIdListView>(stringResponse,
  148. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  149. return userIdListView;
  150. }
  151. /// <summary>
  152. /// 获取成员信息
  153. /// </summary>
  154. /// <returns></returns>
  155. public async Task<QYWX_UserInfosView> GetUserInfosAsync()
  156. {
  157. QYWX_UserInfosView infoViews = new QYWX_UserInfosView();
  158. //获取员工useridList
  159. UserIdListView useridView = await GetUserIdListAsync();
  160. if (useridView.errcode != 0)
  161. {
  162. infoViews.errcode = useridView.errcode;
  163. infoViews.errmsg = useridView.errmsg;
  164. return infoViews;
  165. }
  166. Access_TokenView access_Token = await GetTokenAsync(4);
  167. if (access_Token.errcode != 0)
  168. {
  169. infoViews.errcode = access_Token.errcode;
  170. infoViews.errmsg = access_Token.errmsg;
  171. return infoViews;
  172. }
  173. List<QYWX_UserInfo> items = new List<QYWX_UserInfo>();
  174. string urlTitle = string.Format("/cgi-bin/user/get?access_token={0}", access_Token.access_token);
  175. foreach (var item in useridView.dept_user)
  176. {
  177. if (!string.IsNullOrEmpty(item.userid))
  178. {
  179. QYWX_UserInfoView _UserInfoView = new QYWX_UserInfoView();
  180. string url = string.Format("{0}&userid={1}", urlTitle, item.userid);
  181. var create_Req = await _httpClient.GetAsync(url);
  182. if (create_Req.IsSuccessStatusCode)
  183. {
  184. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  185. _UserInfoView = System.Text.Json.JsonSerializer.Deserialize<QYWX_UserInfoView>(stringResponse,
  186. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  187. if (_UserInfoView.errcode == 0)
  188. {
  189. items.Add(new QYWX_UserInfo()
  190. {
  191. userid = _UserInfoView.QYWX_User.userid,
  192. name = _UserInfoView.QYWX_User.name,
  193. mobile = _UserInfoView.QYWX_User.mobile,
  194. position = _UserInfoView.QYWX_User.position,
  195. email = _UserInfoView.QYWX_User.email,
  196. biz_mail = _UserInfoView.QYWX_User.biz_mail,
  197. });
  198. }
  199. }
  200. }
  201. }
  202. infoViews.errcode = 0;
  203. infoViews.errmsg = "操作成功!";
  204. infoViews.QYWX_Users = items;
  205. return infoViews;
  206. }
  207. /// <summary>
  208. /// 获取部门列表
  209. /// </summary>
  210. /// <param name="access_token"></param>
  211. /// <returns></returns>
  212. private async Task<DepartmentListView> GetDepartmentAsync(string access_token)
  213. {
  214. DepartmentListView result = new DepartmentListView();
  215. string dep_url = string.Format("/cgi-bin/department/list?access_token={0}&debug=1", access_token);
  216. var depReq = await _httpClient.GetAsync(dep_url);
  217. if (depReq.IsSuccessStatusCode)
  218. {
  219. var stringResponse = await depReq.Content.ReadAsStringAsync();
  220. result = System.Text.Json.JsonSerializer.Deserialize<DepartmentListView>(stringResponse,
  221. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  222. }
  223. else
  224. {
  225. result.errcode = -1;
  226. result.errmsg = "企业微信部门列表未获取到!";
  227. }
  228. return result;
  229. }
  230. /// <summary>
  231. /// 创建员工
  232. /// </summary>
  233. /// <param name="create_Request"></param>
  234. /// <returns></returns>
  235. public async Task<ResponseBase> CreateAsync(Create_Request create_Request)
  236. {
  237. ResponseBase responseBase = new ResponseBase();
  238. #region 职位
  239. string depName = string.Empty,
  240. jobName = string.Empty;
  241. List<Sys_JobPostI> jobs = await _jobPostRep.QueryJobPost(string.Empty);
  242. Sys_JobPostI jobPost = jobs.Where(it => it.IsDel == 0 && it.Id == Convert.ToInt32(create_Request.position)).FirstOrDefault();
  243. if (jobPost != null)
  244. {
  245. depName = jobPost.DepName;
  246. jobName = jobPost.JobName;
  247. }
  248. create_Request.position = jobName;
  249. if (string.IsNullOrEmpty(create_Request.biz_mail))
  250. {
  251. create_Request.biz_mail = string.Format("{0}@pan-american-intl.com", create_Request.userid);
  252. }
  253. #endregion
  254. Access_TokenView access_Token = new Access_TokenView();
  255. access_Token = await GetTokenAsync(4);
  256. if (access_Token.errcode == 0)
  257. {
  258. create_Request.access_token = access_Token.access_token;
  259. #region 处理部门
  260. Access_TokenView dep_Access_Token = await GetTokenAsync(1);
  261. if (dep_Access_Token.errcode != 0)
  262. {
  263. responseBase.errcode = dep_Access_Token.errcode;
  264. responseBase.errmsg = dep_Access_Token.errmsg;
  265. }
  266. DepartmentListView depData = await GetDepartmentAsync(dep_Access_Token.access_token);
  267. if (depData.errcode == 0)
  268. {
  269. var depData1 = depData.department.Where(x => x.name == depName).FirstOrDefault();
  270. if (depData1 != null)
  271. {
  272. create_Request.department = new List<long> { depData1.id };
  273. }
  274. else
  275. {
  276. responseBase.errcode = -1;
  277. responseBase.errmsg = "未在企业微信上找到OA内设置的部门";
  278. return responseBase;
  279. }
  280. }
  281. else
  282. {
  283. responseBase.errcode = depData.errcode;
  284. responseBase.errmsg = depData.errmsg;
  285. return responseBase;
  286. }
  287. #endregion
  288. create_Request.access_token = access_Token.access_token;
  289. string create_url = string.Format("/cgi-bin/user/create?access_token={0}", create_Request.access_token);
  290. var json = System.Text.Json.JsonSerializer.Serialize(create_Request);
  291. var content = new StringContent(json, Encoding.UTF8, "application/json");
  292. var create_Req = await _httpClient.PostAsync(create_url, content);
  293. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  294. responseBase = System.Text.Json.JsonSerializer.Deserialize<ResponseBase>(stringResponse,
  295. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  296. }
  297. else
  298. {
  299. responseBase.errcode = access_Token.errcode;
  300. responseBase.errmsg = access_Token.errmsg;
  301. }
  302. return responseBase;
  303. }
  304. #endregion
  305. #region 打卡
  306. /// <summary>
  307. /// 获取企业所有打卡规则
  308. /// </summary>
  309. /// <returns></returns>
  310. public async Task<CorpCheckInRuleView> GetCheckIn_CorpCheckInOptionAsync()
  311. {
  312. CorpCheckInRuleView corpCheckInRuleView = new CorpCheckInRuleView();
  313. //获取打卡数据 token
  314. Access_TokenView access_Token = await GetTokenAsync(2);
  315. if (access_Token.errcode != 0)
  316. {
  317. corpCheckInRuleView.errcode = access_Token.errcode;
  318. corpCheckInRuleView.errmsg = string.Format("【企业微信】【获取月打卡数据】【Token】【Msg】{0}", access_Token.errmsg);
  319. return corpCheckInRuleView;
  320. }
  321. string url = string.Format("/cgi-bin/checkin/getcorpcheckinoption?access_token={0}", access_Token.access_token);
  322. var content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
  323. var create_Req = await _httpClient.PostAsync(url, content);
  324. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  325. corpCheckInRuleView = System.Text.Json.JsonSerializer.Deserialize<CorpCheckInRuleView>(stringResponse,
  326. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  327. return corpCheckInRuleView;
  328. }
  329. /// <summary>
  330. /// 获取月打卡记录数据
  331. /// </summary>
  332. /// <param name="startDt"></param>
  333. /// <param name="endDt"></param>
  334. /// <returns></returns>
  335. public async Task<CheckInView> GetCheckin_MonthDataAsync1(DateTime startDt, DateTime endDt)
  336. {
  337. CheckInView checkInView = new CheckInView();
  338. //获取员工Id
  339. UserIdListView userIdListView = await GetUserIdListAsync();
  340. if (userIdListView.errcode != 0)
  341. {
  342. checkInView.errcode = userIdListView.errcode;
  343. checkInView.errmsg = string.Format("【企业微信】【获取员工IdList】【Msg】{0}", userIdListView.errmsg);
  344. return checkInView;
  345. }
  346. if (userIdListView.dept_user == null || userIdListView.dept_user.Count <= 0)
  347. {
  348. checkInView.errmsg = string.Format("【企业微信】【获取员工IdList】【Msg】未查出员工Id");
  349. return checkInView;
  350. }
  351. //获取打卡数据 token
  352. Access_TokenView access_Token = await GetTokenAsync(2);
  353. if (access_Token.errcode != 0)
  354. {
  355. checkInView.errcode = access_Token.errcode;
  356. checkInView.errmsg = string.Format("【企业微信】【获取月打卡数据】【Token】【Msg】{0}", access_Token.errmsg);
  357. return checkInView;
  358. }
  359. string url = string.Format("/cgi-bin/checkin/getcheckin_monthdata?access_token={0}", access_Token.access_token);
  360. Checkin_MonthData_Request checkInReq = new Checkin_MonthData_Request();
  361. checkInReq.access_token = access_Token.access_token;
  362. checkInReq.starttime = (uint)(startDt - _1970).TotalSeconds;
  363. checkInReq.endtime = (uint)(endDt - _1970).TotalSeconds;
  364. checkInReq.useridlist = userIdListView.dept_user.Select(it => it.userid).ToList();
  365. var json = System.Text.Json.JsonSerializer.Serialize(checkInReq);
  366. var content = new StringContent(json, Encoding.UTF8, "application/json");
  367. var create_Req = await _httpClient.PostAsync(url, content);
  368. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  369. checkInView = System.Text.Json.JsonSerializer.Deserialize<CheckInView>(stringResponse,
  370. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  371. return checkInView;
  372. }
  373. /// <summary>
  374. /// 获取月打卡记录数据
  375. /// </summary>
  376. /// <param name="startDt"></param>
  377. /// <param name="endDt"></param>
  378. /// <returns></returns>
  379. public async Task<CheckInView> GetCheckin_MonthDataAsync(DateTime startDt, DateTime endDt)
  380. {
  381. CheckInView checkInView = new CheckInView();
  382. //获取员工Id
  383. UserIdListView userIdListView = await GetUserIdListAsync();
  384. if (userIdListView.errcode != 0)
  385. {
  386. checkInView.errcode = userIdListView.errcode;
  387. checkInView.errmsg = string.Format("【企业微信】【获取员工IdList】【Msg】{0}", userIdListView.errmsg);
  388. return checkInView;
  389. }
  390. if (userIdListView.dept_user == null || userIdListView.dept_user.Count <= 0)
  391. {
  392. checkInView.errmsg = string.Format("【企业微信】【获取员工IdList】【Msg】未查出员工Id");
  393. return checkInView;
  394. }
  395. //获取打卡数据 token
  396. Access_TokenView access_Token = await GetTokenAsync(2);
  397. if (access_Token.errcode != 0)
  398. {
  399. checkInView.errcode = access_Token.errcode;
  400. checkInView.errmsg = string.Format("【企业微信】【获取月打卡数据】【Token】【Msg】{0}", access_Token.errmsg);
  401. return checkInView;
  402. }
  403. #region 处理超31天的月报表数据
  404. string url = string.Format("/cgi-bin/checkin/getcheckin_monthdata?access_token={0}", access_Token.access_token);
  405. Checkin_MonthData_Request checkInReq = new Checkin_MonthData_Request();
  406. checkInReq.access_token = access_Token.access_token;
  407. checkInReq.useridlist = userIdListView.dept_user.Select(it => it.userid).ToList();
  408. uint starttimeOne, endtimeOne, starttimeTwo, endtimeTwo;
  409. int days = (int)(endDt - startDt).TotalDays;
  410. if (days > 32)
  411. {
  412. starttimeOne = (uint)((startDt.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
  413. endtimeOne = (uint)((startDt.AddDays(31).ToUniversalTime().Ticks - 621355968000000000) / 10000000);
  414. starttimeTwo = (uint)((startDt.AddDays(32).ToUniversalTime().Ticks - 621355968000000000) / 10000000);
  415. endtimeTwo = (uint)((endDt.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
  416. checkInReq.starttime = starttimeOne;
  417. checkInReq.endtime = endtimeOne;
  418. CheckInView checkInViewOne = new CheckInView();
  419. var jsonOne = System.Text.Json.JsonSerializer.Serialize(checkInReq);
  420. var contentOne = new StringContent(jsonOne, Encoding.UTF8, "application/json");
  421. var create_ReqOne = await _httpClient.PostAsync(url, contentOne);
  422. var stringResponseOne = await create_ReqOne.Content.ReadAsStringAsync();
  423. checkInViewOne = System.Text.Json.JsonSerializer.Deserialize<CheckInView>(stringResponseOne,
  424. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  425. if (checkInViewOne.errcode != 0)
  426. {
  427. return checkInViewOne;
  428. }
  429. checkInReq.starttime = starttimeTwo;
  430. checkInReq.endtime = endtimeTwo;
  431. CheckInView checkInViewTwo = new CheckInView();
  432. var jsonTwo = System.Text.Json.JsonSerializer.Serialize(checkInReq);
  433. var contentTwo = new StringContent(jsonTwo, Encoding.UTF8, "application/json");
  434. var create_ReqTwo = await _httpClient.PostAsync(url, contentTwo);
  435. var stringResponseTwo = await create_ReqTwo.Content.ReadAsStringAsync();
  436. checkInViewTwo = System.Text.Json.JsonSerializer.Deserialize<CheckInView>(stringResponseTwo,
  437. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  438. if (checkInViewTwo.errcode != 0)
  439. {
  440. return checkInViewTwo;
  441. }
  442. #region 整合月报表数据
  443. foreach (var item_one in checkInViewOne.datas)
  444. {
  445. foreach (var item_two in checkInViewTwo.datas)
  446. {
  447. if (item_one.base_info.acctid.Equals(item_two.base_info.acctid))
  448. {
  449. #region summary_info 汇总信息整合
  450. item_one.summary_info.work_days += item_two.summary_info.work_days; //应打卡天数
  451. item_one.summary_info.regular_days += item_two.summary_info.regular_days; //正常天数
  452. item_one.summary_info.except_days += item_two.summary_info.except_days; //异常天数
  453. item_one.summary_info.regular_work_sec += item_two.summary_info.regular_work_sec; //实际工作时长,为统计周期每日实际工作时长之和
  454. item_one.summary_info.standard_work_sec += item_two.summary_info.standard_work_sec; //标准工作时长,为统计周期每日标准工作时长之和
  455. #endregion
  456. #region exception_infos 异常状态统计信息 整合
  457. item_one.exception_infos.AddRange(item_two.exception_infos);
  458. #endregion
  459. #region sp_items 假勤统计信息整合
  460. item_one.sp_items.AddRange(item_two.sp_items);
  461. #endregion
  462. #region overwork_info 加班情况 整合
  463. item_one.overwork_info.workday_over_sec += item_two.overwork_info.workday_over_sec; //工作日加班时长
  464. item_one.overwork_info.holidays_over_sec += item_two.overwork_info.holidays_over_sec; //节假日加班时长
  465. item_one.overwork_info.restdays_over_sec += item_two.overwork_info.restdays_over_sec; //休息日加班时长
  466. #endregion
  467. break;
  468. }
  469. }
  470. }
  471. #endregion
  472. checkInView = checkInViewOne;
  473. }
  474. else
  475. {
  476. checkInReq.starttime = (uint)((startDt.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
  477. checkInReq.endtime = (uint)((endDt.ToUniversalTime().Ticks - 621355968000000000) / 10000000);
  478. var json = System.Text.Json.JsonSerializer.Serialize(checkInReq);
  479. var content = new StringContent(json, Encoding.UTF8, "application/json");
  480. var create_Req = await _httpClient.PostAsync(url, content);
  481. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  482. checkInView = System.Text.Json.JsonSerializer.Deserialize<CheckInView>(stringResponse,
  483. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  484. }
  485. #endregion
  486. return checkInView;
  487. }
  488. /// <summary>
  489. /// 获取月打卡数据(redis缓存)
  490. /// </summary>
  491. /// <param name="startDt"></param>
  492. /// <param name="endDt"></param>
  493. /// <returns></returns>
  494. public async Task<CheckInView> GetCheckin_MonthDataRedisAsync(DateTime startDt, DateTime endDt)
  495. {
  496. CheckInView checkInView = new CheckInView();
  497. string checkInDatastring = await RedisRepository.RedisFactory
  498. .CreateRedisRepository()
  499. .StringGetAsync<string>("Checkin_MonthData");//List<T> 取
  500. if (!string.IsNullOrEmpty(checkInDatastring))
  501. {
  502. checkInView = JsonConvert.DeserializeObject<CheckInView>(checkInDatastring);
  503. return checkInView;
  504. }
  505. //获取员工Id
  506. UserIdListView userIdListView = await GetUserIdListAsync();
  507. if (userIdListView.errcode != 0)
  508. {
  509. checkInView.errcode = userIdListView.errcode;
  510. checkInView.errmsg = string.Format("【企业微信】【获取员工IdList】【Msg】{0}", userIdListView.errmsg);
  511. return checkInView;
  512. }
  513. if (userIdListView.dept_user == null || userIdListView.dept_user.Count <= 0)
  514. {
  515. checkInView.errmsg = string.Format("【企业微信】【获取员工IdList】【Msg】未查出员工Id");
  516. return checkInView;
  517. }
  518. //获取月打卡数据 token
  519. Access_TokenView access_Token = await GetTokenAsync(2);
  520. if (access_Token.errcode != 0)
  521. {
  522. checkInView.errcode = access_Token.errcode;
  523. checkInView.errmsg = string.Format("【企业微信】【获取月打卡数据】【Token】【Msg】{0}", access_Token.errmsg);
  524. return checkInView;
  525. }
  526. string url = string.Format("/cgi-bin/checkin/getcheckin_monthdata?access_token={0}", access_Token.access_token);
  527. Checkin_MonthData_Request checkInReq = new Checkin_MonthData_Request();
  528. checkInReq.access_token = access_Token.access_token;
  529. checkInReq.starttime = (uint)(startDt - _1970).TotalSeconds;
  530. checkInReq.endtime = (uint)(endDt - _1970).TotalSeconds;
  531. checkInReq.useridlist = userIdListView.dept_user.Select(it => it.userid).ToList();
  532. var json = System.Text.Json.JsonSerializer.Serialize(checkInReq);
  533. var content = new StringContent(json, Encoding.UTF8, "application/json");
  534. var create_Req = await _httpClient.PostAsync(url, content);
  535. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  536. checkInView = System.Text.Json.JsonSerializer.Deserialize<CheckInView>(stringResponse,
  537. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  538. if (checkInView.errcode == 0)
  539. {
  540. TimeSpan ts = DateTime.Now.AddMinutes(60) - DateTime.Now; //设置redis 过期时间 60分钟
  541. await RedisRepository
  542. .RedisFactory
  543. .CreateRedisRepository()
  544. .StringSetAsync<string>("Checkin_MonthData", JsonConvert.SerializeObject(checkInView), ts);//List<T> 存
  545. }
  546. return checkInView;
  547. }
  548. public async Task<object> QueryAssessmentByUser(DateTime startDt, DateTime endDt, List<string> qiyeUidList)
  549. {
  550. AssessmentByUserListView assessmentByUserListView = new AssessmentByUserListView();
  551. List<AssessmentByUserView> AssessmentByUserView = new List<AssessmentByUserView>();
  552. assessmentByUserListView.Data = AssessmentByUserView;
  553. assessmentByUserListView.errcode = 0;
  554. assessmentByUserListView.errmsg = "成功";
  555. CheckInView? checkInView = new CheckInView();
  556. //获取打卡数据 token
  557. Access_TokenView access_Token = await GetTokenAsync(2);
  558. if (access_Token.errcode != 0)
  559. {
  560. assessmentByUserListView.errcode = access_Token.errcode;
  561. assessmentByUserListView.errmsg = string.Format("【企业微信】【获取月打卡数据】【Token】【Msg】{0}", access_Token.errmsg);
  562. return assessmentByUserListView;
  563. }
  564. string url = string.Format("/cgi-bin/checkin/getcheckin_monthdata?access_token={0}", access_Token.access_token);
  565. Checkin_MonthData_Request checkInReq = new Checkin_MonthData_Request();
  566. checkInReq.access_token = access_Token.access_token;
  567. checkInReq.starttime = (uint)(startDt - _1970).TotalSeconds;
  568. checkInReq.endtime = (uint)(endDt - _1970).TotalSeconds;
  569. checkInReq.useridlist = qiyeUidList;
  570. var json = System.Text.Json.JsonSerializer.Serialize(checkInReq);
  571. var content = new StringContent(json, Encoding.UTF8, "application/json");
  572. var create_Req = await _httpClient.PostAsync(url, content);
  573. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  574. checkInView = System.Text.Json.JsonSerializer.Deserialize<CheckInView>(stringResponse,
  575. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  576. if (checkInView == null)
  577. {
  578. assessmentByUserListView.errcode = -1;
  579. assessmentByUserListView.errmsg = "checkInView Deserialize Error";
  580. return assessmentByUserListView;
  581. }
  582. if (checkInView.errcode != 0)
  583. {
  584. assessmentByUserListView.errcode = checkInView.errcode;
  585. assessmentByUserListView.errmsg = checkInView.errmsg;
  586. return assessmentByUserListView;
  587. }
  588. var errorUserList = new List<string>();
  589. //遍历用户数据
  590. foreach (var item in checkInView.datas)
  591. {
  592. //有异常状态的情况下获取具体异常信息
  593. if (item.summary_info.except_days > 0)
  594. {
  595. errorUserList.Add(item.base_info.acctid);
  596. }
  597. assessmentByUserListView.Data.Add(new Domain.ViewModels.QiYeWeChat.AssessmentByUserView
  598. {
  599. Accid = item.base_info.acctid,
  600. Except_days = item.summary_info.except_days,
  601. Name = item.base_info.name,
  602. Regular_days = item.summary_info.regular_days,
  603. Exception_info_List = new List<Exception_info>(),
  604. Work_days = item.summary_info.work_days
  605. });
  606. }
  607. if (errorUserList.Count > 0) {
  608. var dayInfo = await GetCheckinDataAsync(errorUserList, 3, startDt, endDt);
  609. if (dayInfo.errcode != 0)
  610. {
  611. assessmentByUserListView.errcode = dayInfo.errcode;
  612. assessmentByUserListView.errmsg = dayInfo.errmsg;
  613. assessmentByUserListView.Data = new List<AssessmentByUserView>();
  614. return assessmentByUserListView;
  615. }
  616. var dic = new Dictionary<ErrorType, List<string>>()
  617. {
  618. { ErrorType.迟到 , new List<string>{ "时间异常" } },
  619. { ErrorType.缺卡 , new List<string>{ "未打卡" } },
  620. };
  621. foreach (var item in dayInfo.checkindata)
  622. {
  623. foreach (var user in assessmentByUserListView.Data)
  624. {
  625. if (user.Accid == item.userid)
  626. {
  627. //遍历异常信息
  628. if (!item.exception_type.IsNullOrWhiteSpace())
  629. {
  630. var linqFind = dic.First(x => x.Value.Contains(item.exception_type));
  631. var errinfo = new Exception_info
  632. {
  633. Date = item.checkin_time_dt,
  634. Type = linqFind.Key,
  635. Info = item.exception_type
  636. };
  637. user.Exception_info_List.Add(errinfo);
  638. }
  639. }
  640. }
  641. }
  642. }
  643. return assessmentByUserListView;
  644. }
  645. /// <summary>
  646. /// 获取打卡记录数据
  647. /// </summary>
  648. /// <param name="useridlist">需要获取打卡记录的用户列表</param>
  649. /// <param name="opencheckindatatype">打卡类型。1:上下班打卡;2:外出打卡;3:全部打卡</param>
  650. /// <param name="startDt">获取打卡记录的开始时间。Unix时间戳</param>
  651. /// <param name="endDt">获取打卡记录的结束时间。Unix时间戳</param>
  652. /// <returns></returns>
  653. public async Task<CheckInDataView> GetCheckinDataAsync(List<string> useridlist, int opencheckindatatype, DateTime startDt, DateTime endDt)
  654. {
  655. CheckInDataView checkInDataView = new CheckInDataView();
  656. //参数处理
  657. if (useridlist.Count <= 0)
  658. {
  659. checkInDataView.errmsg = "请填写企业微信用户id,可为多个!";
  660. return checkInDataView;
  661. }
  662. if (useridlist.Count > 100)
  663. {
  664. checkInDataView.errmsg = "用户列表不超过100个。若用户超过100个,请分批获取!";
  665. return checkInDataView;
  666. }
  667. //获取打卡数据 token
  668. Access_TokenView access_Token = await GetTokenAsync(2);
  669. if (access_Token.errcode != 0)
  670. {
  671. checkInDataView.errcode = access_Token.errcode;
  672. checkInDataView.errmsg = string.Format("【企业微信】【获取打卡数据】【Token】【Msg】{0}", access_Token.errmsg);
  673. return checkInDataView;
  674. }
  675. string url = string.Format("/cgi-bin/checkin/getcheckindata?access_token={0}", access_Token.access_token);
  676. DateTime centerDt = startDt.AddDays(30);
  677. long startTs = (long)(startDt - _1970).TotalSeconds;
  678. long centerTs = (long)(centerDt - _1970).TotalSeconds;
  679. long endTs = (long)(endDt - _1970).TotalSeconds;
  680. CheckInData_Request checkInData_Req = new CheckInData_Request()
  681. {
  682. access_token = access_Token.access_token,
  683. opencheckindatatype = opencheckindatatype,
  684. useridlist = useridlist,
  685. starttime = startTs,
  686. endtime = centerTs
  687. };
  688. var json = System.Text.Json.JsonSerializer.Serialize(checkInData_Req);
  689. var content = new StringContent(json, Encoding.UTF8, "application/json");
  690. var create_Req = await _httpClient.PostAsync(url, content);
  691. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  692. checkInDataView = System.Text.Json.JsonSerializer.Deserialize<CheckInDataView>(stringResponse,
  693. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  694. if (centerDt < endDt)
  695. {
  696. checkInData_Req.starttime = centerTs;
  697. checkInData_Req.endtime = endTs;
  698. var json1 = System.Text.Json.JsonSerializer.Serialize(checkInData_Req);
  699. var content1 = new StringContent(json1, Encoding.UTF8, "application/json");
  700. var create_Req1 = await _httpClient.PostAsync(url, content1);
  701. var stringResponse1 = await create_Req1.Content.ReadAsStringAsync();
  702. CheckInDataView checkInDataView1 = System.Text.Json.JsonSerializer.Deserialize<CheckInDataView>(stringResponse1,
  703. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  704. if (checkInDataView1.errcode != 0)
  705. {
  706. return checkInDataView1;
  707. }
  708. checkInDataView.checkindata.AddRange(checkInDataView1.checkindata);
  709. return checkInDataView;
  710. }
  711. return checkInDataView;
  712. }
  713. /// <summary>
  714. /// 获取打卡日报数据
  715. /// </summary>
  716. /// <param name="useridlist"></param>
  717. /// <param name="startDt"></param>
  718. /// <param name="endDt"></param>
  719. /// <returns></returns>
  720. public async Task<CheckInDayDataView> GetCheckInDayDataAsync1(List<string> useridlist, DateTime startDt, DateTime endDt)
  721. {
  722. CheckInDayDataView checkInDayDataView = new CheckInDayDataView();
  723. //参数处理
  724. if (useridlist.Count <= 0)
  725. {
  726. checkInDayDataView.errmsg = "请填写企业微信用户id,可为多个!";
  727. return checkInDayDataView;
  728. }
  729. //获取打卡数据 token
  730. Access_TokenView access_Token = await GetTokenAsync(2);
  731. if (access_Token.errcode != 0)
  732. {
  733. checkInDayDataView.errcode = access_Token.errcode;
  734. checkInDayDataView.errmsg = string.Format("【企业微信】【获取打卡日报数据】【Token】【Msg】{0}", access_Token.errmsg);
  735. return checkInDayDataView;
  736. }
  737. string url = string.Format("/cgi-bin/checkin/getcheckin_daydata?access_token={0}&debug=1", access_Token.access_token);
  738. long startTs = (startDt.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
  739. long endTs = (endDt.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
  740. var checkInData_Req = new
  741. {
  742. access_token = access_Token.access_token,
  743. useridlist = useridlist,
  744. starttime = startTs,
  745. endtime = endTs
  746. };
  747. var json = System.Text.Json.JsonSerializer.Serialize(checkInData_Req);
  748. var content = new StringContent(json, Encoding.UTF8, "application/json");
  749. var create_Req = await _httpClient.PostAsync(url, content);
  750. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  751. checkInDayDataView = System.Text.Json.JsonSerializer.Deserialize<CheckInDayDataView>(stringResponse,
  752. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  753. return checkInDayDataView;
  754. }
  755. /// <summary>
  756. /// 获取打卡日报数据
  757. /// </summary>
  758. /// <param name="useridlist"></param>
  759. /// <param name="startDt"></param>
  760. /// <param name="endDt"></param>
  761. /// <returns></returns>
  762. public async Task<CheckInDayDataView> GetCheckInDayDataAsync(List<string> useridlist, DateTime startDt, DateTime endDt)
  763. {
  764. CheckInDayDataView checkInDayDataView = new CheckInDayDataView();
  765. //参数处理
  766. if (useridlist.Count <= 0)
  767. {
  768. checkInDayDataView.errmsg = "请填写企业微信用户id,可为多个!";
  769. return checkInDayDataView;
  770. }
  771. //获取打卡数据 token
  772. Access_TokenView access_Token = await GetTokenAsync(2);
  773. if (access_Token.errcode != 0)
  774. {
  775. checkInDayDataView.errcode = access_Token.errcode;
  776. checkInDayDataView.errmsg = string.Format("【企业微信】【获取打卡日报数据】【Token】【Msg】{0}", access_Token.errmsg);
  777. return checkInDayDataView;
  778. }
  779. string url = string.Format("/cgi-bin/checkin/getcheckin_daydata?access_token={0}&debug=1", access_Token.access_token);
  780. int days = (int)(endDt - startDt).TotalDays;
  781. if (days > 31)
  782. {
  783. long startTs_One = (startDt.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
  784. long endTs_One = (startDt.AddDays(31).ToUniversalTime().Ticks - 621355968000000000) / 10000000;
  785. long startTs_Two = (startDt.AddDays(31).ToUniversalTime().Ticks - 621355968000000000) / 10000000;
  786. long endTs_Two = (endDt.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
  787. var checkInData_ReqOne = new
  788. {
  789. access_token = access_Token.access_token,
  790. useridlist = useridlist,
  791. starttime = startTs_One,
  792. endtime = endTs_One
  793. };
  794. var jsonOne = System.Text.Json.JsonSerializer.Serialize(checkInData_ReqOne);
  795. var contentOne = new StringContent(jsonOne, Encoding.UTF8, "application/json");
  796. var create_ReqOne = await _httpClient.PostAsync(url, contentOne);
  797. var stringResponseOne = await create_ReqOne.Content.ReadAsStringAsync();
  798. CheckInDayDataView checkInDayDataViewOne = System.Text.Json.JsonSerializer.Deserialize<CheckInDayDataView>(stringResponseOne,
  799. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  800. if (checkInDayDataViewOne.errcode != 0)
  801. {
  802. return checkInDayDataViewOne;
  803. }
  804. var checkInData_ReqTwo = new
  805. {
  806. access_token = access_Token.access_token,
  807. useridlist = useridlist,
  808. starttime = startTs_Two,
  809. endtime = endTs_Two
  810. };
  811. var jsonTwo = System.Text.Json.JsonSerializer.Serialize(checkInData_ReqTwo);
  812. var contentTwo = new StringContent(jsonTwo, Encoding.UTF8, "application/json");
  813. var create_ReqTwo = await _httpClient.PostAsync(url, contentTwo);
  814. var stringResponseTwo = await create_ReqTwo.Content.ReadAsStringAsync();
  815. CheckInDayDataView checkInDayDataViewTwo = System.Text.Json.JsonSerializer.Deserialize<CheckInDayDataView>(stringResponseTwo,
  816. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  817. if (checkInDayDataViewTwo.errcode != 0)
  818. {
  819. return checkInDayDataViewTwo;
  820. }
  821. checkInDayDataViewOne.datas.AddRange(checkInDayDataViewTwo.datas);
  822. return checkInDayDataViewOne;
  823. }
  824. else
  825. {
  826. long startTs = (startDt.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
  827. long endTs = (endDt.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
  828. var checkInData_Req = new
  829. {
  830. access_token = access_Token.access_token,
  831. useridlist = useridlist,
  832. starttime = startTs,
  833. endtime = endTs
  834. };
  835. var json = System.Text.Json.JsonSerializer.Serialize(checkInData_Req);
  836. var content = new StringContent(json, Encoding.UTF8, "application/json");
  837. var create_Req = await _httpClient.PostAsync(url, content);
  838. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  839. checkInDayDataView = System.Text.Json.JsonSerializer.Deserialize<CheckInDayDataView>(stringResponse,
  840. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  841. }
  842. return checkInDayDataView;
  843. }
  844. #endregion
  845. #region 审批
  846. /// <summary>
  847. /// 获取审批数据(旧)
  848. /// </summary>
  849. /// <param name="startDt"></param>
  850. /// <param name="endDt"></param>
  851. /// <returns></returns>
  852. public async Task<ApprovalDataView> GetApprovalDataAsync(DateTime startDt, DateTime endDt)
  853. {
  854. ApprovalDataView approvalDataView = new ApprovalDataView();
  855. //获取审批数据 token
  856. Access_TokenView access_Token = await GetTokenAsync(5);
  857. if (access_Token.errcode != 0)
  858. {
  859. approvalDataView.errcode = access_Token.errcode;
  860. approvalDataView.errmsg = string.Format("【企业微信】【获取审批数据】【Token】【Msg】{0}", access_Token.errmsg);
  861. return approvalDataView;
  862. }
  863. string url = string.Format("/cgi-bin/corp/getapprovaldata?access_token={0}", access_Token.access_token);
  864. ApprovalData_Request approvalDataReq = new ApprovalData_Request();
  865. approvalDataReq.access_token = access_Token.access_token;
  866. approvalDataReq.starttime = (uint)(startDt - _1970).TotalSeconds;
  867. approvalDataReq.endtime = (uint)(endDt - _1970).TotalSeconds;
  868. var json = System.Text.Json.JsonSerializer.Serialize(approvalDataReq);
  869. var content = new StringContent(json, Encoding.UTF8, "application/json");
  870. var create_Req = await _httpClient.PostAsync(url, content);
  871. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  872. approvalDataView = System.Text.Json.JsonSerializer.Deserialize<ApprovalDataView>(stringResponse,
  873. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  874. List<Sp_Info> sp_datas = new List<Sp_Info>();
  875. sp_datas.AddRange(approvalDataView.data);
  876. int index = 0;
  877. //多次访问审批接口
  878. if (approvalDataView.total >= 100)
  879. {
  880. approvalDataView.total -= 100;
  881. int forTotal = approvalDataView.total % 100 == 0 ? approvalDataView.total / 100 : approvalDataView.total / 100 + 1;
  882. long? next_spnum = approvalDataView.next_spnum;
  883. approvalDataReq.next_spnum = next_spnum;
  884. for (int i = 0; i < forTotal; i++)
  885. {
  886. index++;
  887. var for_json = System.Text.Json.JsonSerializer.Serialize(approvalDataReq);
  888. var for_content = new StringContent(for_json, Encoding.UTF8, "application/json");
  889. var for_create_Req = await _httpClient.PostAsync(url, for_content);
  890. var for_stringResponse = await for_create_Req.Content.ReadAsStringAsync();
  891. ApprovalDataView for_approvalDataView = System.Text.Json.JsonSerializer.Deserialize<ApprovalDataView>(for_stringResponse,
  892. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  893. approvalDataReq.next_spnum = for_approvalDataView.next_spnum; //重新定义游标
  894. sp_datas.AddRange(for_approvalDataView.data); //追加数据
  895. }
  896. approvalDataView.total += 100;
  897. }
  898. approvalDataView.data = sp_datas;
  899. return approvalDataView;
  900. }
  901. /// <summary>
  902. /// 获取审批数据(旧)(redis缓存)
  903. /// </summary>
  904. /// <param name="startDt"></param>
  905. /// <param name="endDt"></param>
  906. /// <returns></returns>
  907. public async Task<List<Sp_Info>> GetApprovalDatasAsync(DateTime startDt, DateTime endDt)
  908. {
  909. List<Sp_Info> sp_Infos = new List<Sp_Info>();
  910. //获取所有打卡补卡,审批 数据 前后范围增加10天
  911. DateTime sp_startDt = startDt.AddDays(-10);
  912. DateTime sp_centerDt = sp_startDt.AddDays(30);
  913. DateTime sp_endDt = endDt.AddDays(10);
  914. ApprovalDataView approvalData_1 = await GetApprovalDataAsync(sp_startDt, sp_centerDt); //时间段内所有 审批数据
  915. ApprovalDataView approvalData_2 = await GetApprovalDataAsync(sp_centerDt, sp_endDt); //时间段内所有 审批数据
  916. if (approvalData_1.errcode != 0)
  917. {
  918. Log.Error("企业微信 获取 " + sp_startDt + " - " + sp_centerDt + " 内审批 Msg:" + approvalData_1.errmsg);
  919. return sp_Infos;
  920. }
  921. sp_Infos.AddRange(approvalData_1.data);
  922. if (approvalData_2.errcode != 0)
  923. {
  924. Log.Error("企业微信 获取 " + sp_centerDt + " - " + sp_endDt + " 内审批 Msg:" + approvalData_2.errmsg);
  925. return sp_Infos;
  926. }
  927. sp_Infos.AddRange(approvalData_2.data);
  928. sp_Infos = sp_Infos.Where(it => it.sp_status == 2).ToList(); //存储已审核的数据
  929. return sp_Infos;
  930. }
  931. /// <summary>
  932. /// 获取审批数据(旧)(redis缓存)
  933. /// </summary>
  934. /// <param name="startDt"></param>
  935. /// <param name="endDt"></param>
  936. /// <returns></returns>
  937. public async Task<List<Sp_Info>> GetApprovalDatasRedisAsync(DateTime startDt, DateTime endDt)
  938. {
  939. List<Sp_Info> sp_Infos = new List<Sp_Info>();
  940. //获取所有打卡补卡,审批 数据 前后范围增加10天
  941. DateTime sp_startDt = startDt.AddDays(-10);
  942. DateTime sp_centerDt = sp_startDt.AddDays(30);
  943. DateTime sp_endDt = endDt.AddDays(10);
  944. string redisName = "ApprovalData" + sp_startDt.ToString("yyyyMMdd") + "-" + sp_endDt.ToString("yyyyMMdd");
  945. string sp_InfosString = string.Empty;
  946. //sp_InfosString = await RedisRepository.RedisFactory
  947. // .CreateRedisRepository()
  948. // .StringGetAsync<string>(redisName);//string 取
  949. if (string.IsNullOrEmpty(sp_InfosString))
  950. {
  951. ApprovalDataView approvalData_1 = await GetApprovalDataAsync(sp_startDt, sp_centerDt); //时间段内所有 审批数据
  952. ApprovalDataView approvalData_2 = await GetApprovalDataAsync(sp_centerDt, sp_endDt); //时间段内所有 审批数据
  953. if (approvalData_1.errcode != 0)
  954. {
  955. Log.Error("企业微信 获取 " + sp_startDt + " - " + sp_centerDt + " 内审批 Msg:" + approvalData_1.errmsg);
  956. return sp_Infos;
  957. }
  958. sp_Infos.AddRange(approvalData_1.data);
  959. if (approvalData_2.errcode != 0)
  960. {
  961. Log.Error("企业微信 获取 " + sp_centerDt + " - " + sp_endDt + " 内审批 Msg:" + approvalData_2.errmsg);
  962. return sp_Infos;
  963. }
  964. sp_Infos.AddRange(approvalData_2.data);
  965. sp_Infos = sp_Infos.Where(it => it.sp_status == 2).ToList(); //存储已审核的数据
  966. TimeSpan ts = DateTime.Now.AddMinutes(60) - DateTime.Now; //设置redis 过期时间 60分钟
  967. await RedisRepository
  968. .RedisFactory
  969. .CreateRedisRepository()
  970. .StringSetAsync<string>(redisName, JsonConvert.SerializeObject(sp_Infos), ts);//string 存
  971. }
  972. else
  973. {
  974. sp_Infos = JsonConvert.DeserializeObject<List<Sp_Info>>(sp_InfosString);
  975. }
  976. return sp_Infos;
  977. }
  978. /// <summary>
  979. /// 批量获取审批单号
  980. /// </summary>
  981. /// <param name="startDt"></param>
  982. /// <param name="endDt"></param>
  983. /// <param name="creator">
  984. /// 申请人
  985. /// </param>
  986. /// <param name="sp_status">
  987. /// sp_status-审批单状态(1-审批中;2-已通过;3-已驳回;4-已撤销;6-通过后撤销;7-已删除;10-已支付)
  988. /// </param>
  989. /// <param name="record_type">
  990. /// record_type == 0 ALL 有值 请填写对应值
  991. /// record_type - 审批单类型属性,1-请假;2-打卡补卡;3-出差;4-外出;5-加班; 6- 调班;7-会议室预定;8-退款审批;9-红包报销审批
  992. /// </param>
  993. /// <returns></returns>
  994. private async Task<ApprovalInfoView> GetApprovalInfoAsync(DateTime startDt, DateTime endDt, string creator, int sp_status, int record_type)
  995. {
  996. Stopwatch sw = new Stopwatch();
  997. sw.Start();
  998. ApprovalInfoView approvalInfoView = new ApprovalInfoView();
  999. //获取所有打卡补卡,审批 数据 向前增加30天 向后范围增加15天
  1000. DateTime sp_startDt = startDt.AddDays(-30);
  1001. DateTime sp_endDt = endDt.AddDays(15);
  1002. //获取审批数据 token
  1003. Access_TokenView access_Token = await GetTokenAsync(5);
  1004. if (access_Token.errcode != 0)
  1005. {
  1006. approvalInfoView.errcode = access_Token.errcode;
  1007. approvalInfoView.errmsg = string.Format("【企业微信】【批量获取审批单号】【Token】【Msg】{0}", access_Token.errmsg);
  1008. return approvalInfoView;
  1009. }
  1010. string url = string.Format("/cgi-bin/oa/getapprovalinfo?access_token={0}", access_Token.access_token);
  1011. List<Dic> filters = new List<Dic> {
  1012. new Dic() { key = "creator", value = creator }, //筛选条件 申请人
  1013. new Dic() { key = "sp_status", value = sp_status.ToString() }, //筛选条件 审核状态 2 已同意
  1014. };
  1015. if (record_type != 0)
  1016. {
  1017. filters.Add(new Dic() { key = "record_type", value = record_type.ToString() }); //筛选条件 审批单类型 请假 1
  1018. }
  1019. int days = (sp_endDt - sp_startDt).Days;
  1020. decimal runCount = days % 30;
  1021. int runCount1 = 0;
  1022. if (runCount == 0) runCount1 = (int)(days % 30.00M);
  1023. else runCount1 = (int)Math.Ceiling(days / 30.00M);
  1024. List<string> sp_no_lists = new List<string>();
  1025. for (int i = 0; i < runCount1; i++)
  1026. {
  1027. DateTime for_sp_startDt = sp_startDt;
  1028. DateTime for_sp_endDt = sp_startDt.AddDays(30);
  1029. sp_startDt = sp_startDt.AddDays(30);
  1030. if (for_sp_endDt >= sp_endDt)
  1031. {
  1032. for_sp_endDt = sp_endDt;
  1033. }
  1034. ApprovalInfo_Request approvalInfoReq = new ApprovalInfo_Request()
  1035. {
  1036. access_token = access_Token.access_token,
  1037. starttime = (uint)ConvertToTimeSpan(for_sp_startDt),
  1038. endtime = (uint)ConvertToTimeSpan(for_sp_endDt),
  1039. filters = filters.ToList(),
  1040. };
  1041. var json = System.Text.Json.JsonSerializer.Serialize(approvalInfoReq);
  1042. var content = new StringContent(json, Encoding.UTF8, "application/json");
  1043. var create_Req = await _httpClient.PostAsync(url, content);
  1044. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  1045. ApprovalInfoView approvalInfoView1 = System.Text.Json.JsonSerializer.Deserialize<ApprovalInfoView>(stringResponse,
  1046. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  1047. #region 分页调用 new_next_cursor != null
  1048. string new_cursor = approvalInfoView1.new_next_cursor;
  1049. while (!string.IsNullOrEmpty(new_cursor))
  1050. {
  1051. approvalInfoReq.new_cursor = new_cursor;
  1052. var json1 = System.Text.Json.JsonSerializer.Serialize(approvalInfoReq);
  1053. var content1 = new StringContent(json, Encoding.UTF8, "application/json");
  1054. var create_Req1 = await _httpClient.PostAsync(url, content1);
  1055. var stringResponse1 = await create_Req1.Content.ReadAsStringAsync();
  1056. ApprovalInfoView approvalInfoView11 = System.Text.Json.JsonSerializer.Deserialize<ApprovalInfoView>(stringResponse1,
  1057. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  1058. if (approvalInfoView11.errcode != 0)
  1059. {
  1060. return approvalInfoView1;
  1061. }
  1062. new_cursor = approvalInfoView11.new_next_cursor;
  1063. if (approvalInfoView11.sp_no_list.Count > 0 || approvalInfoView11.sp_no_list != null)
  1064. {
  1065. sp_no_lists.AddRange(approvalInfoView1.sp_no_list);
  1066. }
  1067. }
  1068. #endregion
  1069. if (approvalInfoView1.errcode != 0)
  1070. {
  1071. return approvalInfoView1;
  1072. }
  1073. if (approvalInfoView1.sp_no_list.Count > 0 || approvalInfoView1.sp_no_list != null)
  1074. {
  1075. sp_no_lists.AddRange(approvalInfoView1.sp_no_list);
  1076. }
  1077. }
  1078. approvalInfoView.errcode = 0;
  1079. if (sp_no_lists.Count > 0)
  1080. {
  1081. sp_no_lists = sp_no_lists.Distinct().ToList();
  1082. }
  1083. approvalInfoView.sp_no_list = sp_no_lists;
  1084. sw.Stop();
  1085. approvalInfoView.errmsg = approvalInfoView.errmsg + " 耗时:" + sw.Elapsed.TotalMilliseconds + "ms";
  1086. return approvalInfoView;
  1087. }
  1088. /// <summary>
  1089. /// 获取审批申请详情
  1090. /// </summary>
  1091. /// <param name="sp_no"></param>
  1092. /// <returns></returns>
  1093. public async Task<ApprovalDetailView> GetApprovalDetailAsync(string sp_no)
  1094. {
  1095. ApprovalDetailView ApprovalDetailView = new ApprovalDetailView();
  1096. if (string.IsNullOrEmpty(sp_no))
  1097. {
  1098. return ApprovalDetailView;
  1099. }
  1100. //获取审批数据 token
  1101. Access_TokenView access_Token = await GetTokenAsync(5);
  1102. if (access_Token.errcode != 0)
  1103. {
  1104. ApprovalDetailView.errcode = access_Token.errcode;
  1105. ApprovalDetailView.errmsg = string.Format("【企业微信】【审批申请详情】【Token】【Msg】{0}", access_Token.errmsg);
  1106. return ApprovalDetailView;
  1107. }
  1108. string url = string.Format("/cgi-bin/oa/getapprovaldetail?access_token={0}", access_Token.access_token);
  1109. ApprovalDetail_Request approvalDetail_Req = new ApprovalDetail_Request()
  1110. {
  1111. access_token = access_Token.access_token,
  1112. sp_no = sp_no,
  1113. };
  1114. var json = System.Text.Json.JsonSerializer.Serialize(approvalDetail_Req);
  1115. var content = new StringContent(json, Encoding.UTF8, "application/json");
  1116. var create_Req = await _httpClient.PostAsync(url, content);
  1117. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  1118. ApprovalDetailView = System.Text.Json.JsonSerializer.Deserialize<ApprovalDetailView>(stringResponse,
  1119. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  1120. return ApprovalDetailView;
  1121. }
  1122. /// <summary>
  1123. /// 批量获取审批详情
  1124. /// </summary>
  1125. /// <param name="startDt"></param>
  1126. /// <param name="endDt"></param>
  1127. /// <param name="creator">
  1128. /// 申请人
  1129. /// </param>
  1130. /// <param name="sp_status">
  1131. /// sp_status-审批单状态(1-审批中;2-已通过;3-已驳回;4-已撤销;6-通过后撤销;7-已删除;10-已支付)
  1132. /// </param>
  1133. /// <param name="record_type">
  1134. /// record_type - 审批单类型属性,1-请假;2-打卡补卡;3-出差;4-外出;5-加班; 6- 调班;7-会议室预定;8-退款审批;9-红包报销审批
  1135. /// </param>
  1136. /// <returns></returns>
  1137. public async Task<List<Sp_Detail>> GetApprovalDetailsAsync(DateTime startDt, DateTime endDt, string creator, int sp_status, int record_type)
  1138. {
  1139. Stopwatch sw = new Stopwatch();
  1140. sw.Start();
  1141. List<Sp_Detail> details = new List<Sp_Detail>();
  1142. if (string.IsNullOrEmpty(creator))
  1143. {
  1144. return details;
  1145. }
  1146. ApprovalInfoView approvalInfoView = new ApprovalInfoView();
  1147. approvalInfoView = await GetApprovalInfoAsync(startDt, endDt, creator, sp_status, record_type);
  1148. if (approvalInfoView.errcode != 0)
  1149. {
  1150. return details;
  1151. }
  1152. //循环查询详情
  1153. foreach (var item in approvalInfoView.sp_no_list)
  1154. {
  1155. ApprovalDetailView approvalDetailView = new ApprovalDetailView();
  1156. approvalDetailView = await GetApprovalDetailAsync(item);
  1157. if (approvalDetailView.errcode != 0)
  1158. {
  1159. Log.Error("【GetApprovalDetailsAsync】 record_type:" + record_type + " ErrorMsg:" + approvalDetailView.errmsg);
  1160. break;
  1161. }
  1162. details.Add(approvalDetailView.info);
  1163. }
  1164. sw.Stop();
  1165. double hs = sw.Elapsed.TotalMilliseconds;
  1166. return details;
  1167. }
  1168. /// <summary>
  1169. /// 获取审批模板详情
  1170. /// </summary>
  1171. /// <param name="template_id">模板Id</param>
  1172. /// <returns></returns>
  1173. public async Task<TemplateDetailView> GetTemplateDetailAsync(string template_id)
  1174. {
  1175. TemplateDetailView templateDetailView = new TemplateDetailView();
  1176. if (string.IsNullOrEmpty(template_id))
  1177. {
  1178. templateDetailView.errmsg = "template_id为空!";
  1179. return templateDetailView;
  1180. }
  1181. //获取审批数据 token
  1182. Access_TokenView access_Token = await GetTokenAsync(5);
  1183. if (access_Token.errcode != 0)
  1184. {
  1185. templateDetailView.errcode = access_Token.errcode;
  1186. templateDetailView.errmsg = string.Format("【企业微信】【审批申请详情】【Token】【Msg】{0}", access_Token.errmsg);
  1187. return templateDetailView;
  1188. }
  1189. string url = string.Format("/cgi-bin/oa/gettemplatedetail?access_token={0}", access_Token.access_token);
  1190. var approvalDetail_Req = new
  1191. {
  1192. access_token = access_Token.access_token,
  1193. template_id = template_id,
  1194. };
  1195. var json = System.Text.Json.JsonSerializer.Serialize(approvalDetail_Req);
  1196. var content = new StringContent(json, Encoding.UTF8, "application/json");
  1197. var create_Req = await _httpClient.PostAsync(url, content);
  1198. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  1199. templateDetailView = System.Text.Json.JsonSerializer.Deserialize<TemplateDetailView>(stringResponse,
  1200. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  1201. return templateDetailView;
  1202. }
  1203. #endregion
  1204. #region hook机器人
  1205. /// <summary>
  1206. /// 发送团组信息给财务群
  1207. /// </summary>
  1208. /// <param name="msg"></param>
  1209. /// <returns></returns>
  1210. /// <exception cref="NotImplementedException"></exception>
  1211. public async Task<ResponseBase> RobotSendMsg_GroupInfo(string msg)
  1212. {
  1213. string key = "b4fe152f-a97a-48b1-830f-ab447f6d2f5f";
  1214. string url = string.Format("/cgi-bin/webhook/send?key={0}", key);
  1215. RobotSendMsg_GroupInfo_Content contentStr = new RobotSendMsg_GroupInfo_Content() { content = msg };
  1216. RobotSendMsg_GroupInfo_Param reqJson = new RobotSendMsg_GroupInfo_Param()
  1217. {
  1218. msgtype = "markdown",
  1219. markdown = contentStr
  1220. };
  1221. var json = System.Text.Json.JsonSerializer.Serialize(reqJson);
  1222. var content = new StringContent(json, Encoding.UTF8, "application/json");
  1223. var create_Req = await _httpClient.PostAsync(url, content);
  1224. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  1225. ResponseBase sendMsgView = System.Text.Json.JsonSerializer.Deserialize<ResponseBase>(stringResponse,
  1226. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  1227. return sendMsgView;
  1228. }
  1229. #endregion
  1230. #region 团组状态通知
  1231. /// <summary>
  1232. /// 获取部门成员
  1233. /// </summary>
  1234. /// <param name="departmentId"></param>
  1235. /// <returns></returns>
  1236. public async Task<GroupStatus_UserSimplelistView> GroupStatus_GetUserList(int departmentId = 8402038)
  1237. {
  1238. GroupStatus_UserSimplelistView result = new GroupStatus_UserSimplelistView();
  1239. Access_TokenView access_Token = await GetTokenAsync(6);
  1240. if (access_Token.errcode != 0)
  1241. {
  1242. result.errcode = access_Token.errcode;
  1243. result.errmsg = string.Format("【企业微信】【团组状态通知】【Token】【Msg】{0}", access_Token.errmsg);
  1244. return result;
  1245. }
  1246. string url = string.Format("/cgi-bin/user/simplelist?access_token={0}&department_id={1}", access_Token.access_token, departmentId);
  1247. var simplelist_Req = await _httpClient.GetAsync(url);
  1248. if (simplelist_Req.IsSuccessStatusCode)
  1249. {
  1250. var stringResponse = await simplelist_Req.Content.ReadAsStringAsync();
  1251. result = System.Text.Json.JsonSerializer.Deserialize<GroupStatus_UserSimplelistView>(stringResponse,
  1252. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  1253. }
  1254. else
  1255. {
  1256. result.errcode = -1;
  1257. result.errmsg = "企业微信部门列表未获取到!";
  1258. }
  1259. return result;
  1260. }
  1261. /// <summary>
  1262. /// 创建群聊
  1263. /// </summary>
  1264. /// <param name="chatName"></param>
  1265. /// <param name="owner"></param>
  1266. /// <param name="userList"></param>
  1267. /// <param name="chatId"></param>
  1268. /// <returns></returns>
  1269. public async Task<GroupStatus_CreateChatView> GroupStatus_CreateChat(string chatName, string owner, List<string> userList, string chatId)
  1270. {
  1271. GroupStatus_CreateChatView result = new GroupStatus_CreateChatView();
  1272. Access_TokenView access_Token = await GetTokenAsync(6);
  1273. if (access_Token.errcode != 0)
  1274. {
  1275. result.errcode = access_Token.errcode;
  1276. result.errmsg = string.Format("【企业微信】【团组状态通知】【Token】【Msg】{0}", access_Token.errmsg);
  1277. return result;
  1278. }
  1279. GroupStatus_CreateChat reqJson = new GroupStatus_CreateChat()
  1280. {
  1281. chatid = chatId,
  1282. name = chatName,
  1283. owner = owner,
  1284. userlist = userList
  1285. };
  1286. string url = string.Format("/cgi-bin/appchat/create?access_token={0}&debug=1", access_Token.access_token);
  1287. var json = System.Text.Json.JsonSerializer.Serialize(reqJson);
  1288. var content = new StringContent(json, Encoding.UTF8, "application/json");
  1289. var create_Req = await _httpClient.PostAsync(url, content);
  1290. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  1291. result = System.Text.Json.JsonSerializer.Deserialize<GroupStatus_CreateChatView>(stringResponse,
  1292. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  1293. return result;
  1294. }
  1295. /// <summary>
  1296. /// 向群聊发送信息
  1297. /// </summary>
  1298. /// <param name="chatId"></param>
  1299. /// <param name="msgContent"></param>
  1300. /// <param name="msgType"></param>
  1301. /// <returns></returns>
  1302. /// <exception cref="NotImplementedException"></exception>
  1303. public async Task<ResponseBase> GroupStatus_SendChatMsg_Markdown(string chatId, string msgContent, string msgType = "markdown")
  1304. {
  1305. ResponseBase result = new ResponseBase();
  1306. Access_TokenView access_Token = await GetTokenAsync(6);
  1307. if (access_Token.errcode != 0)
  1308. {
  1309. result.errcode = access_Token.errcode;
  1310. result.errmsg = string.Format("【企业微信】【团组状态通知】【Token】【Msg】{0}", access_Token.errmsg);
  1311. return result;
  1312. }
  1313. RobotSendMsg_GroupInfo_Content markdown = new RobotSendMsg_GroupInfo_Content() { content = msgContent };
  1314. GroupStatus_AppChatSend_Markdown reqJson = new GroupStatus_AppChatSend_Markdown()
  1315. {
  1316. chatid = chatId,
  1317. msgtype = msgType,
  1318. markdown = markdown
  1319. };
  1320. string url = string.Format("/cgi-bin/appchat/send?access_token={0}", access_Token.access_token);
  1321. var json = System.Text.Json.JsonSerializer.Serialize(reqJson);
  1322. var content = new StringContent(json, Encoding.UTF8, "application/json");
  1323. var create_Req = await _httpClient.PostAsync(url, content);
  1324. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  1325. result = System.Text.Json.JsonSerializer.Deserialize<ResponseBase>(stringResponse,
  1326. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  1327. return result;
  1328. }
  1329. /// <summary>
  1330. /// 向用户发送Markdown信息
  1331. /// </summary>
  1332. /// <param name="toUser"></param>
  1333. /// <param name="msgContent"></param>
  1334. /// <param name="msgType"></param>
  1335. /// <returns></returns>
  1336. /// <exception cref="NotImplementedException"></exception>
  1337. public async Task<GroupStatus_SendMessageView> GroupStatus_SendMessage_ToUser_Markdown(List<string> toUser, string msgContent, string msgType = "markdown")
  1338. {
  1339. GroupStatus_SendMessageView result = new GroupStatus_SendMessageView();
  1340. Access_TokenView access_Token = await GetTokenAsync(6);
  1341. if (access_Token.errcode != 0)
  1342. {
  1343. result.errcode = access_Token.errcode;
  1344. result.errmsg = string.Format("【企业微信】【团组状态通知】【Token】【Msg】{0}", access_Token.errmsg);
  1345. return result;
  1346. }
  1347. RobotSendMsg_GroupInfo_Content markdown = new RobotSendMsg_GroupInfo_Content() { content = msgContent };
  1348. string userIds = "";
  1349. toUser.ForEach(s => userIds += s + "|");
  1350. userIds = userIds.TrimEnd('|');
  1351. GroupStatus_AppSend_Markdown reqJson = new GroupStatus_AppSend_Markdown()
  1352. {
  1353. markdown = markdown,
  1354. touser = userIds
  1355. };
  1356. string url = string.Format("/cgi-bin/message/send?access_token={0}&debug=1", access_Token.access_token);
  1357. var json = System.Text.Json.JsonSerializer.Serialize(reqJson);
  1358. var content = new StringContent(json, Encoding.UTF8, "application/json");
  1359. var create_Req = await _httpClient.PostAsync(url, content);
  1360. var stringResponse = await create_Req.Content.ReadAsStringAsync();
  1361. result = System.Text.Json.JsonSerializer.Deserialize<GroupStatus_SendMessageView>(stringResponse,
  1362. new JsonSerializerOptions() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
  1363. return result;
  1364. }
  1365. #endregion
  1366. public long ConvertToTimeSpan(DateTime dt)
  1367. {
  1368. return (dt.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
  1369. }
  1370. }
  1371. }