DDStructController.cs 39 KB

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