DDStructController.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. using DingTalk.Api;
  2. using DingTalk.Api.Request;
  3. using DingTalk.Api.Response;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.Options;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Text.Json;
  12. using System.Threading.Tasks;
  13. using TEAMModelOS.Models;
  14. using TEAMModelOS.SDK.DI;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Models.Service;
  17. using Azure.Cosmos;
  18. using TEAMModelOS.SDK.Models;
  19. using TEAMModelBI.Controllers.BISchool;
  20. using TEAMModelOS.SDK.Models.Cosmos.BI;
  21. using Microsoft.Azure.Cosmos.Table;
  22. using TEAMModelOS.SDK;
  23. namespace TEAMModelBI.Controllers.DingDingStruc
  24. {
  25. [ProducesResponseType(StatusCodes.Status200OK)]
  26. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  27. [Route("dd")]
  28. [ApiController]
  29. public class DDStructController : ControllerBase
  30. {
  31. private readonly IConfiguration _configuration;
  32. //数据容器
  33. private readonly AzureCosmosFactory _azureCosmos;
  34. //文件容器
  35. private readonly AzureStorageFactory _azureStorage;
  36. //钉钉提示信息
  37. private readonly DingDing _dingDing;
  38. //雪花ID
  39. private readonly SnowflakeId _snowflakeId;
  40. private readonly Option _option;
  41. public DDStructController(IConfiguration configuration, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, CoreAPIHttpService aoreAPIHttpService, SnowflakeId snowflakeId)
  42. {
  43. _configuration = configuration;
  44. _azureCosmos = azureCosmos;
  45. _azureStorage = azureStorage;
  46. _dingDing = dingDing;
  47. _option = option?.Value;
  48. _snowflakeId = snowflakeId;
  49. }
  50. /// <summary>
  51. /// 获取分支组织列表信息
  52. /// </summary>
  53. /// <returns></returns>
  54. [ProducesDefaultResponseType]
  55. [HttpPost("ddbranchstruc")]
  56. public async Task<IActionResult> DDBranchStruc()
  57. {
  58. string str_appKey = _configuration["DingDingAuth:appKey"];
  59. string str_appSecret = _configuration["DingDingAuth:appSecret"];
  60. //获取企业内部应用的accessToken
  61. IDingTalkClient Iclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  62. OapiGettokenRequest request = new OapiGettokenRequest();
  63. request.Appkey = str_appKey;
  64. request.Appsecret = str_appSecret;
  65. request.SetHttpMethod("GET");
  66. OapiGettokenResponse tokenResponse = Iclient.Execute(request);
  67. if (tokenResponse.IsError)
  68. {
  69. return Ok(new { state = 0, message = "请检查配置" });
  70. }
  71. IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/org/union/branch/get");
  72. OapiOrgUnionBranchGetRequest req = new OapiOrgUnionBranchGetRequest();
  73. OapiOrgUnionBranchGetResponse rsp = client.Execute(req, tokenResponse.AccessToken);
  74. return Ok(new { Result = rsp.Result, Body = rsp.Body, RequestId = rsp.RequestId, SubErrCode = rsp.SubErrCode, Success = rsp.Success });
  75. }
  76. /// <summary>
  77. /// 获取企业部门列表
  78. /// </summary>
  79. [ProducesDefaultResponseType]
  80. [HttpPost("get-deptlist")]
  81. public async Task<IActionResult> GetDeptList()
  82. {
  83. try
  84. {
  85. string appKey = _configuration["DingDingAuth:appKey"];
  86. string appSecret = _configuration["DingDingAuth:appSecret"];
  87. //获取access_token
  88. DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  89. OapiGettokenRequest request = new OapiGettokenRequest();
  90. request.Appkey = appKey;
  91. request.Appsecret = appSecret;
  92. request.SetHttpMethod("Get");
  93. OapiGettokenResponse response = client.Execute(request);
  94. if (response.IsError)
  95. {
  96. return BadRequest();
  97. }
  98. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  99. string access_token = response.AccessToken;
  100. //获取一级部门列表
  101. IDingTalkClient v2ListsubClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
  102. OapiV2DepartmentListsubRequest reqlistsub1 = new OapiV2DepartmentListsubRequest() { DeptId = 1L, Language = "zh_CN" };
  103. OapiV2DepartmentListsubResponse rsplistsub1 = v2ListsubClient1.Execute(reqlistsub1, access_token);
  104. List<DeptInfo> templsit = new List<DeptInfo>();
  105. if (rsplistsub1.Result != null)
  106. {
  107. foreach (var deptList in rsplistsub1.Result)
  108. {
  109. DeptInfo deptInfo = new DeptInfo();
  110. deptInfo.deptId = deptList.DeptId;
  111. deptInfo.deptName = deptList.Name;
  112. deptInfo.parentId = deptList.ParentId;
  113. //获取一级部门用户列表
  114. IDingTalkClient userListClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
  115. OapiUserListidRequest reqUserList1 = new OapiUserListidRequest() { DeptId = deptList.DeptId };
  116. OapiUserListidResponse rspUserList1 = userListClient1.Execute(reqUserList1, access_token);
  117. if (rspUserList1.Result != null)
  118. {
  119. deptInfo.ddUserList = rspUserList1.Result.UseridList;
  120. }
  121. //获取用户详细信息
  122. IDingTalkClient v2UserListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
  123. OapiV2UserGetRequest reqv2UserList = new OapiV2UserGetRequest();
  124. OapiV2UserGetResponse rspv2UserList = v2UserListClient.Execute(reqv2UserList, access_token);
  125. //获取二级部门列表
  126. OapiV2DepartmentListsubRequest reqlistsub = new OapiV2DepartmentListsubRequest() { DeptId = deptList.DeptId, Language = "zh_CN" };
  127. OapiV2DepartmentListsubResponse rsplistsub = v2ListsubClient1.Execute(reqlistsub, access_token);
  128. List<DeptBaseResponseDomain> deptBaseResponseDomainList = new List<DeptBaseResponseDomain>();
  129. if (rsplistsub.Result != null)
  130. {
  131. foreach (var deptlist2 in rsplistsub.Result)
  132. {
  133. //添加二级部门
  134. DeptBaseResponseDomain deptBaseResponseDomain2 = new DeptBaseResponseDomain();
  135. deptBaseResponseDomain2.deptId = deptlist2.DeptId;
  136. deptBaseResponseDomain2.Name = deptlist2.Name;
  137. deptBaseResponseDomain2.ParentId = deptlist2.ParentId;
  138. //获取三级部门用户列表
  139. OapiUserListidRequest reqUserList2 = new OapiUserListidRequest() { DeptId = deptlist2.DeptId };
  140. OapiUserListidResponse rspUserList2 = userListClient1.Execute(reqUserList2, access_token);
  141. if (rspUserList2.Result != null)
  142. {
  143. //添加三级部门用户
  144. deptBaseResponseDomain2.ddUserList = rspUserList2.Result.UseridList;
  145. }
  146. //获取三级部门列表
  147. OapiV2DepartmentListsubRequest reqlistsub3 = new OapiV2DepartmentListsubRequest() { DeptId = deptlist2.DeptId, Language = "zh_CN" };
  148. OapiV2DepartmentListsubResponse rsplistsub3 = v2ListsubClient1.Execute(reqlistsub3, access_token);
  149. List<DeptBaseResponseDomain> deptBaseResponseDomain3List = new List<DeptBaseResponseDomain>();
  150. if (rsplistsub3.Result != null)
  151. {
  152. foreach (var dept3List in rsplistsub3.Result)
  153. {
  154. //添加三级部门
  155. DeptBaseResponseDomain deptBaseResponseDomain3 = new DeptBaseResponseDomain();
  156. deptBaseResponseDomain3.deptId = dept3List.DeptId;
  157. deptBaseResponseDomain3.Name = dept3List.Name;
  158. deptBaseResponseDomain3.ParentId = dept3List.ParentId;
  159. //获取部门用户列表
  160. OapiUserListidRequest reqUserList3 = new OapiUserListidRequest() { DeptId = dept3List.DeptId };
  161. OapiUserListidResponse rspUserList3 = userListClient1.Execute(reqUserList3, access_token);
  162. if (rspUserList3.Result != null)
  163. {
  164. //添加三级部门的用户
  165. deptBaseResponseDomain3.ddUserList = rspUserList3.Result.UseridList;
  166. }
  167. //获取部门列表 四级目录
  168. OapiV2DepartmentListsubRequest reqlistsub4 = new OapiV2DepartmentListsubRequest() { DeptId = dept3List.DeptId, Language = "zh_CN" };
  169. OapiV2DepartmentListsubResponse rsplistsu4 = v2ListsubClient1.Execute(reqlistsub4, access_token);
  170. List<DeptBaseResponseDomain> deptBaseResponseDomain4List = new List<DeptBaseResponseDomain>();
  171. if (rsplistsu4.Result != null)
  172. {
  173. foreach (var dept4List in rsplistsu4.Result)
  174. {
  175. DeptBaseResponseDomain deptBaseResponseDomain4 = new DeptBaseResponseDomain();
  176. deptBaseResponseDomain4.deptId = dept4List.DeptId;
  177. deptBaseResponseDomain4.Name = dept4List.Name;
  178. deptBaseResponseDomain4.ParentId = dept4List.ParentId;
  179. deptBaseResponseDomain4List.Add(deptBaseResponseDomain4);
  180. //获取四级部门用户列表
  181. OapiUserListidRequest reqUserList4 = new OapiUserListidRequest() { DeptId = dept4List.DeptId };
  182. OapiUserListidResponse rspUserList4 = userListClient1.Execute(reqUserList4, access_token);
  183. if (rspUserList4.Result != null)
  184. {
  185. //添加四级部门的用户
  186. deptBaseResponseDomain4.ddUserList = rspUserList4.Result.UseridList;
  187. }
  188. }
  189. }
  190. //添加四级部门列表
  191. deptBaseResponseDomain3.LowerDeip_List = deptBaseResponseDomain4List;
  192. deptBaseResponseDomain3List.Add(deptBaseResponseDomain3);
  193. }
  194. }
  195. //添加三级部门列表
  196. deptBaseResponseDomain2.LowerDeip_List = deptBaseResponseDomain3List;
  197. deptBaseResponseDomainList.Add(deptBaseResponseDomain2);
  198. }
  199. }
  200. //添加二级部门列表
  201. deptInfo.deptList = deptBaseResponseDomainList;
  202. templsit.Add(deptInfo);
  203. }
  204. }
  205. return Ok(new { state = 200, deptlist = templsit });
  206. }
  207. catch (Exception ex)
  208. {
  209. return Ok(new { state = 1, message=$"查询失败!:状态:{ex.StackTrace}错误:{ex.Message}" }) ;
  210. }
  211. }
  212. /// <summary>
  213. /// 查询钉钉的研发中心B人员
  214. /// </summary>
  215. /// <param name="jsonElement"></param>
  216. /// <returns></returns>
  217. [ProducesDefaultResponseType]
  218. [HttpPost("get-tmdandddusers")]
  219. public async Task<IActionResult> GetTmdAndDdUsers(JsonElement jsonElement)
  220. {
  221. try
  222. {
  223. jsonElement.TryGetProperty("deptId", out JsonElement deptId);
  224. string tempDeptId = string.IsNullOrEmpty($"{deptId}") ? "67863053" : $"{deptId}";
  225. string appKey = _configuration["DingDingAuth:appKey"];
  226. string appSecret = _configuration["DingDingAuth:appSecret"];
  227. Dictionary<string, object> dic = new Dictionary<string, object> { { "PartitionKey", "authority-bi" } };
  228. List<Authority> authorityBIList = await _azureStorage.FindListByDict<Authority>(dic);
  229. //获取access_token
  230. DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  231. OapiGettokenRequest request = new OapiGettokenRequest();
  232. request.Appkey = appKey;
  233. request.Appsecret = appSecret;
  234. request.SetHttpMethod("Get");
  235. OapiGettokenResponse response = client.Execute(request);
  236. if (response.IsError)
  237. {
  238. return BadRequest();
  239. }
  240. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  241. string access_token = response.AccessToken;
  242. IDingTalkClient userListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
  243. OapiV2UserListRequest reqUserList1 = new OapiV2UserListRequest();
  244. reqUserList1.DeptId = long.Parse($"{tempDeptId}");
  245. reqUserList1.Cursor = 0L;
  246. reqUserList1.Size = 50L;
  247. reqUserList1.ContainAccessLimit = false;
  248. reqUserList1.OrderField = "custom";
  249. reqUserList1.Language = "zh_CN";
  250. reqUserList1.SetHttpMethod("GET");
  251. OapiV2UserListResponse rspV2UserList1 = userListClient.Execute(reqUserList1, access_token);
  252. List<DDUserInfoAndTMD> dDAndTmdInfos = new List<DDUserInfoAndTMD>();
  253. List<DingDingUserInfo> ddUserInfos = new List<DingDingUserInfo>();
  254. if (rspV2UserList1.Result.List != null)
  255. {
  256. foreach (var itemUser in rspV2UserList1.Result.List)
  257. {
  258. List<DingDingUserInfo> temp = await _azureStorage.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "RowKey", $"{itemUser.Userid}" } });
  259. foreach (var item in temp)
  260. {
  261. ddUserInfos.Add(item);
  262. }
  263. //DDUserInfoAndTMD dDAndTmdInfo = new DDUserInfoAndTMD();
  264. //dDAndTmdInfo.unionid = itemUser.Unionid;
  265. //dDAndTmdInfo.userid = itemUser.Userid;
  266. //dDAndTmdInfo.title = itemUser.Title;
  267. //dDAndTmdInfo.name = itemUser.Name;
  268. //dDAndTmdInfo.mobile = itemUser.Mobile;
  269. //dDAndTmdInfo.jobNumber = itemUser.JobNumber;
  270. //dDAndTmdInfo.avatar = itemUser.Avatar;
  271. //dDAndTmdInfo.depts = itemUser.DeptIdList;
  272. //List<string> roles = new List<string>();//角色列表
  273. //List<string> power = new List<string>();//权限列表
  274. //string sqltxt = $"select distinct value(c) from c join A1 in c.ddbinds where A1.userid ='{itemUser.Userid}'";
  275. //try
  276. //{
  277. // await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  278. // {
  279. // dDAndTmdInfo.tmdId = item.id;
  280. // dDAndTmdInfo.isexist = true;
  281. // if (!string.IsNullOrEmpty($"{item.defaultSchool}"))
  282. // {
  283. // var schoolRoles = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(item.id, new PartitionKey($"Teacher-{item.defaultSchool}"));
  284. // if (schoolRoles.Status == 200)
  285. // {
  286. // using var json = await JsonDocument.ParseAsync(schoolRoles.ContentStream);
  287. // if (json.RootElement.TryGetProperty("roles", out JsonElement _roles) && _roles.ValueKind != JsonValueKind.Null)
  288. // {
  289. // foreach (var obj in _roles.EnumerateArray())
  290. // {
  291. // //初始定义顾问的assistant 更改为assist
  292. // if (obj.GetString().Equals($"assist"))
  293. // {
  294. // roles.Add(obj.GetString());
  295. // }
  296. // }
  297. // }
  298. // if (json.RootElement.TryGetProperty("permissions", out JsonElement _permissions) && _permissions.ValueKind != JsonValueKind.Null)
  299. // {
  300. // foreach (var obj in _permissions.EnumerateArray())
  301. // {
  302. // //显示BI权限
  303. // foreach (var aut in authorityBIList)
  304. // {
  305. // if (aut.RowKey.Equals(obj.GetString()))
  306. // {
  307. // power.Add(obj.GetString());
  308. // }
  309. // }
  310. // }
  311. // }
  312. // }
  313. // dDAndTmdInfo.tmdroles = roles;
  314. // dDAndTmdInfo.tmdpower = power;
  315. // }
  316. // }
  317. //}
  318. //catch { }
  319. }
  320. }
  321. if (!string.IsNullOrEmpty($"{tempDeptId}"))
  322. {
  323. //获取下级部门列表
  324. IDingTalkClient v2DeptListClient2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
  325. OapiV2DepartmentListsubRequest reqDeptList2 = new OapiV2DepartmentListsubRequest() { DeptId = long.Parse($"{tempDeptId}"), Language = "zh_CN" };
  326. OapiV2DepartmentListsubResponse rspDeptLis2 = v2DeptListClient2.Execute(reqDeptList2, access_token);
  327. if (rspDeptLis2.Result != null)
  328. {
  329. foreach (var tempDept in rspDeptLis2.Result)
  330. {
  331. OapiV2UserListRequest reqUserList2 = new OapiV2UserListRequest();
  332. reqUserList2.DeptId = long.Parse($"{tempDept.DeptId}");
  333. reqUserList2.Cursor = 0L;
  334. reqUserList2.Size = 50L;
  335. reqUserList2.ContainAccessLimit = false;
  336. reqUserList2.OrderField = "custom";
  337. reqUserList2.Language = "zh_CN";
  338. reqUserList2.SetHttpMethod("GET");
  339. OapiV2UserListResponse rspV2UserList2 = userListClient.Execute(reqUserList2, access_token);
  340. if (rspV2UserList2.Result.List != null)
  341. {
  342. foreach (var itemUser2 in rspV2UserList2.Result.List)
  343. {
  344. var tempInfo = dDAndTmdInfos.Find(x => x.unionid.Equals(itemUser2.Unionid));
  345. if (string.IsNullOrEmpty($"{tempInfo}"))
  346. {
  347. DDUserInfoAndTMD dDAndTmdInfo2 = new DDUserInfoAndTMD();
  348. dDAndTmdInfo2.unionid = itemUser2.Unionid;
  349. dDAndTmdInfo2.userid = itemUser2.Userid;
  350. dDAndTmdInfo2.title = itemUser2.Title;
  351. dDAndTmdInfo2.name = itemUser2.Name;
  352. dDAndTmdInfo2.mobile = itemUser2.Mobile;
  353. dDAndTmdInfo2.jobNumber = itemUser2.JobNumber;
  354. dDAndTmdInfo2.avatar = itemUser2.Avatar;
  355. dDAndTmdInfo2.depts = itemUser2.DeptIdList;
  356. List<string> roles = new List<string>();//角色列表
  357. List<string> power = new List<string>();//权限列表
  358. string sqltxt = $"select distinct value(c) from c join A1 in c.ddbinds where A1.userid ='{itemUser2.Userid}'";
  359. try
  360. {
  361. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  362. {
  363. dDAndTmdInfo2.tmdId = item.id;
  364. dDAndTmdInfo2.isexist = true;
  365. if (!string.IsNullOrEmpty($"{item.defaultSchool}"))
  366. {
  367. var schoolRoles = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(item.id, new PartitionKey($"Teacher-{item.defaultSchool}"));
  368. if (schoolRoles.Status == 200)
  369. {
  370. using var json = await JsonDocument.ParseAsync(schoolRoles.ContentStream);
  371. if (json.RootElement.TryGetProperty("roles", out JsonElement _roles) && _roles.ValueKind != JsonValueKind.Null)
  372. {
  373. foreach (var obj in _roles.EnumerateArray())
  374. {
  375. //初始定义顾问的assistant 更改为assist
  376. if (obj.GetString().Equals($"assist"))
  377. {
  378. roles.Add(obj.GetString());
  379. }
  380. }
  381. }
  382. if (json.RootElement.TryGetProperty("permissions", out JsonElement _permissions) && _permissions.ValueKind != JsonValueKind.Null)
  383. {
  384. foreach (var obj in _permissions.EnumerateArray())
  385. {
  386. //显示BI权限
  387. foreach (var aut in authorityBIList)
  388. {
  389. if (aut.RowKey.Equals(obj.GetString()))
  390. {
  391. power.Add(obj.GetString());
  392. }
  393. }
  394. }
  395. }
  396. }
  397. dDAndTmdInfo2.tmdroles = roles;
  398. dDAndTmdInfo2.tmdpower = power;
  399. }
  400. }
  401. }
  402. catch { }
  403. dDAndTmdInfos.Add(dDAndTmdInfo2);
  404. }
  405. }
  406. }
  407. }
  408. }
  409. }
  410. return Ok(new { state = 200, count = ddUserInfos.Count, ddUserInfos }) ;
  411. }
  412. catch (Exception ex)
  413. {
  414. await _dingDing.SendBotMsg($"BI,{_option.Location} dd/get-tmdandddusers \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  415. return BadRequest();
  416. }
  417. }
  418. /// <summary>
  419. /// 依据钉钉UserID查询钉钉用户信息
  420. /// </summary>
  421. /// <param name="jsonElement"></param>
  422. /// <returns></returns>
  423. [ProducesDefaultResponseType]
  424. [HttpPost("get-dduserinfo")]
  425. public async Task<IActionResult> GetDDUserInfo(JsonElement jsonElement)
  426. {
  427. try
  428. {
  429. if (!jsonElement.TryGetProperty("userids", out JsonElement userIds)) return Ok(new { state = 1, message = "参数问题" });
  430. List<DDUserInfoAndTMD> dDUserInfoAndTMDs = new List<DDUserInfoAndTMD>();//返回钉钉信息和查询的醍摩豆信息
  431. string appKey = _configuration["DingDingAuth:appKey"];
  432. string appSecret = _configuration["DingDingAuth:appSecret"];
  433. //获取access_token
  434. DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  435. OapiGettokenRequest request = new OapiGettokenRequest();
  436. request.Appkey = appKey;
  437. request.Appsecret = appSecret;
  438. request.SetHttpMethod("Get");
  439. OapiGettokenResponse response = client.Execute(request);
  440. if (response.IsError)
  441. {
  442. return BadRequest();
  443. }
  444. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  445. string access_token = response.AccessToken;
  446. IDingTalkClient userInfoClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
  447. Dictionary<string, object> dic = new Dictionary<string, object> { { "PartitionKey", "authority-bi" } };
  448. List<Authority> authorityBIList = await _azureStorage.FindListByDict<Authority>(dic);
  449. if (!string.IsNullOrEmpty($"{userIds}"))
  450. {
  451. //List<string> str_userids = userIds.ToObject<List<string>>().Distinct().ToList();//通过数组的Equals实现去重
  452. List<string> str_userids1 = userIds.ToObject<List<string>>();
  453. List<string> str_userids = str_userids1.Where((x, i) => str_userids1.FindIndex(z => z == x) == i).ToList();//Lambda表达式去重
  454. foreach (var tempid in str_userids)
  455. {
  456. OapiV2UserGetRequest reqUserInfo = new OapiV2UserGetRequest() { Userid = $"{tempid}", Language = "zh_CN" };
  457. OapiV2UserGetResponse rspUserInfo = userInfoClient.Execute(reqUserInfo, access_token);
  458. if (rspUserInfo.Result != null)
  459. {
  460. List<string> roles = new List<string>();//角色列表
  461. List<string> power = new List<string>();//权限列表
  462. DDUserInfoAndTMD dDUserInfoAndTMD = new DDUserInfoAndTMD();
  463. dDUserInfoAndTMD.unionid = rspUserInfo.Result.Unionid;
  464. dDUserInfoAndTMD.title = rspUserInfo.Result.Title;
  465. dDUserInfoAndTMD.userid = rspUserInfo.Result.Userid;
  466. dDUserInfoAndTMD.jobNumber = rspUserInfo.Result.JobNumber;
  467. dDUserInfoAndTMD.name = rspUserInfo.Result.Name;
  468. dDUserInfoAndTMD.depts = rspUserInfo.Result.DeptIdList;
  469. dDUserInfoAndTMD.mobile = rspUserInfo.Result.Mobile;
  470. dDUserInfoAndTMD.avatar = rspUserInfo.Result.Avatar;
  471. string sqltxt = $"select distinct value(c) from c join A1 in c.ddbinds where A1.userid ='{tempid}'";
  472. try
  473. {
  474. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  475. {
  476. dDUserInfoAndTMD.tmdId = item.id;
  477. dDUserInfoAndTMD.isexist = true;
  478. if (!string.IsNullOrEmpty($"{item.defaultSchool}"))
  479. {
  480. var schoolRoles = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(item.id, new PartitionKey($"Teacher-{item.defaultSchool}"));
  481. if (schoolRoles.Status == 200)
  482. {
  483. using var json = await JsonDocument.ParseAsync(schoolRoles.ContentStream);
  484. if (json.RootElement.TryGetProperty("roles", out JsonElement _roles) && _roles.ValueKind != JsonValueKind.Null)
  485. {
  486. foreach (var obj in _roles.EnumerateArray())
  487. {
  488. //初始定义顾问的assistant 更改为assist
  489. if (obj.GetString().Equals($"assist"))
  490. {
  491. roles.Add(obj.GetString());
  492. }
  493. }
  494. }
  495. if (json.RootElement.TryGetProperty("permissions", out JsonElement _permissions) && _permissions.ValueKind != JsonValueKind.Null)
  496. {
  497. foreach (var obj in _permissions.EnumerateArray())
  498. {
  499. //显示BI权限
  500. foreach (var aut in authorityBIList)
  501. {
  502. if (aut.RowKey.Equals(obj.GetString()))
  503. {
  504. power.Add(obj.GetString());
  505. }
  506. }
  507. }
  508. }
  509. }
  510. dDUserInfoAndTMD.tmdroles = roles;
  511. dDUserInfoAndTMD.tmdpower = power;
  512. }
  513. }
  514. }
  515. catch { }
  516. dDUserInfoAndTMDs.Add(dDUserInfoAndTMD);
  517. }
  518. else return Ok(new { state = 2, message = "访问失败!" });
  519. }
  520. }
  521. return Ok(new { state = 200, ddUserInfos = dDUserInfoAndTMDs});
  522. }
  523. catch (Exception ex)
  524. {
  525. await _dingDing.SendBotMsg($"BI,{_option.Location},dd/get-dduserinfo \n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  526. return BadRequest();
  527. }
  528. }
  529. /// <summary>
  530. /// 返回钉钉和能查询到醍摩豆信息
  531. /// </summary>
  532. public class DDUserInfoAndTMD
  533. {
  534. /// <summary>
  535. /// 是否存在醍摩豆账户
  536. /// </summary>
  537. public bool isexist { get; set; }
  538. /// <summary>
  539. /// 绑定的醍摩豆账户
  540. /// </summary>
  541. public string tmdId { get; set; }
  542. /// <summary>
  543. /// 醍摩豆角色
  544. /// </summary>
  545. public List<string> tmdroles { get; set; }
  546. /// <summary>
  547. /// 醍摩豆角色权限
  548. /// </summary>
  549. public List<string> tmdpower { get; set; }
  550. /// <summary>
  551. /// 员工在当前开发者企业账号范围内的唯一标识
  552. /// </summary>
  553. public string unionid { get; set; }
  554. /// <summary>
  555. /// 用户ID
  556. /// </summary>
  557. public string userid { get; set; }
  558. /// <summary>
  559. /// 员工名称
  560. /// </summary>
  561. public string name { get; set; }
  562. /// <summary>
  563. /// 职位
  564. /// </summary>
  565. public string title { get; set; }
  566. /// <summary>
  567. /// 手机号
  568. /// </summary>
  569. public string mobile { get; set; }
  570. /// <summary>
  571. /// 员工工号
  572. /// </summary>
  573. public string jobNumber { get; set; }
  574. /// <summary>
  575. /// 所属部门
  576. /// </summary>
  577. public long deptId { get; set; }
  578. /// <summary>
  579. /// 部门名称
  580. /// </summary>
  581. public string deptName { get; set; }
  582. /// <summary>
  583. /// 所属部门id列表
  584. /// </summary>
  585. public List<long> depts { get; set; }
  586. /// <summary>
  587. /// 钉钉头像
  588. /// </summary>
  589. public string avatar { get; set; }
  590. }
  591. /// <summary>
  592. /// 部门信息
  593. /// </summary>
  594. public record DeptInfo
  595. {
  596. /// <summary>
  597. /// 部门ID
  598. /// </summary>
  599. public long deptId { get; set; }
  600. /// <summary>
  601. /// 部门名称
  602. /// </summary>
  603. public string deptName { get; set; }
  604. /// <summary>
  605. /// 父部门id,根部门为1
  606. /// </summary>
  607. public long parentId { get; set; }
  608. /// <summary>
  609. /// 部门集合
  610. /// </summary>
  611. public List<DeptBaseResponseDomain> deptList { get; set; }
  612. /// <summary>
  613. /// 钉钉用户列表
  614. /// </summary>
  615. public List<string> ddUserList { get; set; }
  616. }
  617. public record DeptBaseResponseDomain
  618. {
  619. /// <summary>
  620. /// 部门ID
  621. /// </summary>
  622. public long deptId { get; set; }
  623. /// <summary>
  624. /// 部门名称
  625. /// </summary>
  626. public string Name { get; set; }
  627. /// <summary>
  628. /// 父部门ID
  629. /// </summary>
  630. public long ParentId { get; set; }
  631. /// <summary>
  632. /// 下级列表
  633. /// </summary>
  634. public List<DeptBaseResponseDomain> LowerDeip_List { get; set; }
  635. /// <summary>
  636. /// 钉钉用户列表
  637. /// </summary>
  638. public List<string> ddUserList { get; set; }
  639. }
  640. }
  641. }