DDStructController.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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() { { "PartitionKey", "authority-bi" } };
  228. var table = _azureStorage.GetCloudTableClient().GetTableReference("SchoolSetting");
  229. List<Authority> authorityBIList = await table.FindListByDict<Authority>(dic);
  230. //获取access_token
  231. DefaultDingTalkClient client = new("https://oapi.dingtalk.com/gettoken");
  232. OapiGettokenRequest request = new();
  233. request.Appkey = appKey;
  234. request.Appsecret = appSecret;
  235. request.SetHttpMethod("Get");
  236. OapiGettokenResponse response = client.Execute(request);
  237. if (response.IsError) return BadRequest();
  238. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  239. string access_token = response.AccessToken;
  240. IDingTalkClient userListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/list");
  241. OapiV2UserListRequest reqUserList1 = new()
  242. {
  243. DeptId = long.Parse($"{tempDeptId}"),
  244. Cursor = 0L,
  245. Size = 50L,
  246. ContainAccessLimit = false,
  247. OrderField = "custom",
  248. Language = "zh_CN"
  249. };
  250. reqUserList1.SetHttpMethod("GET");
  251. OapiV2UserListResponse rspV2UserList1 = userListClient.Execute(reqUserList1, access_token);
  252. List<DDUserInfoAndTMD> dDAndTmdInfos = new();
  253. List<DingDingUserInfo> ddUserInfos = new();
  254. var tabledd = _azureStorage.GetCloudTableClient().GetTableReference("DDUserInfo");
  255. if (rspV2UserList1.Result.List != null)
  256. {
  257. foreach (var itemUser in rspV2UserList1.Result.List)
  258. {
  259. List<DingDingUserInfo> temp = await tabledd.FindListByDict<DingDingUserInfo>(new Dictionary<string, object> { { "RowKey", $"{itemUser.Userid}" } });
  260. foreach (var item in temp)
  261. {
  262. ddUserInfos.Add(item);
  263. }
  264. //DDUserInfoAndTMD dDAndTmdInfo = new DDUserInfoAndTMD();
  265. //dDAndTmdInfo.unionid = itemUser.Unionid;
  266. //dDAndTmdInfo.userid = itemUser.Userid;
  267. //dDAndTmdInfo.title = itemUser.Title;
  268. //dDAndTmdInfo.name = itemUser.Name;
  269. //dDAndTmdInfo.mobile = itemUser.Mobile;
  270. //dDAndTmdInfo.jobNumber = itemUser.JobNumber;
  271. //dDAndTmdInfo.avatar = itemUser.Avatar;
  272. //dDAndTmdInfo.depts = itemUser.DeptIdList;
  273. //List<string> roles = new List<string>();//角色列表
  274. //List<string> power = new List<string>();//权限列表
  275. //string sqltxt = $"select distinct value(c) from c join A1 in c.ddbinds where A1.userid ='{itemUser.Userid}'";
  276. //try
  277. //{
  278. // await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  279. // {
  280. // dDAndTmdInfo.tmdId = item.id;
  281. // dDAndTmdInfo.isexist = true;
  282. // if (!string.IsNullOrEmpty($"{item.defaultSchool}"))
  283. // {
  284. // var schoolRoles = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(item.id, new PartitionKey($"Teacher-{item.defaultSchool}"));
  285. // if (schoolRoles.Status == 200)
  286. // {
  287. // using var json = await JsonDocument.ParseAsync(schoolRoles.ContentStream);
  288. // if (json.RootElement.TryGetProperty("roles", out JsonElement _roles) && _roles.ValueKind != JsonValueKind.Null)
  289. // {
  290. // foreach (var obj in _roles.EnumerateArray())
  291. // {
  292. // //初始定义顾问的assistant 更改为assist
  293. // if (obj.GetString().Equals($"assist"))
  294. // {
  295. // roles.Add(obj.GetString());
  296. // }
  297. // }
  298. // }
  299. // if (json.RootElement.TryGetProperty("permissions", out JsonElement _permissions) && _permissions.ValueKind != JsonValueKind.Null)
  300. // {
  301. // foreach (var obj in _permissions.EnumerateArray())
  302. // {
  303. // //显示BI权限
  304. // foreach (var aut in authorityBIList)
  305. // {
  306. // if (aut.RowKey.Equals(obj.GetString()))
  307. // {
  308. // power.Add(obj.GetString());
  309. // }
  310. // }
  311. // }
  312. // }
  313. // }
  314. // dDAndTmdInfo.tmdroles = roles;
  315. // dDAndTmdInfo.tmdpower = power;
  316. // }
  317. // }
  318. //}
  319. //catch { }
  320. }
  321. }
  322. if (!string.IsNullOrEmpty($"{tempDeptId}"))
  323. {
  324. //获取下级部门列表
  325. IDingTalkClient v2DeptListClient2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
  326. OapiV2DepartmentListsubRequest reqDeptList2 = new() { DeptId = long.Parse($"{tempDeptId}"), Language = "zh_CN" };
  327. OapiV2DepartmentListsubResponse rspDeptLis2 = v2DeptListClient2.Execute(reqDeptList2, access_token);
  328. if (rspDeptLis2.Result != null)
  329. {
  330. foreach (var tempDept in rspDeptLis2.Result)
  331. {
  332. OapiV2UserListRequest reqUserList2 = new OapiV2UserListRequest();
  333. reqUserList2.DeptId = long.Parse($"{tempDept.DeptId}");
  334. reqUserList2.Cursor = 0L;
  335. reqUserList2.Size = 50L;
  336. reqUserList2.ContainAccessLimit = false;
  337. reqUserList2.OrderField = "custom";
  338. reqUserList2.Language = "zh_CN";
  339. reqUserList2.SetHttpMethod("GET");
  340. OapiV2UserListResponse rspV2UserList2 = userListClient.Execute(reqUserList2, access_token);
  341. if (rspV2UserList2.Result.List != null)
  342. {
  343. foreach (var itemUser2 in rspV2UserList2.Result.List)
  344. {
  345. var tempInfo = dDAndTmdInfos.Find(x => x.unionid.Equals(itemUser2.Unionid));
  346. if (string.IsNullOrEmpty($"{tempInfo}"))
  347. {
  348. DDUserInfoAndTMD dDAndTmdInfo2 = new DDUserInfoAndTMD();
  349. dDAndTmdInfo2.unionid = itemUser2.Unionid;
  350. dDAndTmdInfo2.userid = itemUser2.Userid;
  351. dDAndTmdInfo2.title = itemUser2.Title;
  352. dDAndTmdInfo2.name = itemUser2.Name;
  353. dDAndTmdInfo2.mobile = itemUser2.Mobile;
  354. dDAndTmdInfo2.jobNumber = itemUser2.JobNumber;
  355. dDAndTmdInfo2.avatar = itemUser2.Avatar;
  356. dDAndTmdInfo2.depts = itemUser2.DeptIdList;
  357. List<string> roles = new List<string>();//角色列表
  358. List<string> power = new List<string>();//权限列表
  359. string sqltxt = $"select distinct value(c) from c join A1 in c.ddbinds where A1.userid ='{itemUser2.Userid}'";
  360. try
  361. {
  362. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  363. {
  364. dDAndTmdInfo2.tmdId = item.id;
  365. dDAndTmdInfo2.isexist = true;
  366. if (!string.IsNullOrEmpty($"{item.defaultSchool}"))
  367. {
  368. var schoolRoles = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(item.id, new PartitionKey($"Teacher-{item.defaultSchool}"));
  369. if (schoolRoles.Status == 200)
  370. {
  371. using var json = await JsonDocument.ParseAsync(schoolRoles.ContentStream);
  372. if (json.RootElement.TryGetProperty("roles", out JsonElement _roles) && _roles.ValueKind != JsonValueKind.Null)
  373. {
  374. foreach (var obj in _roles.EnumerateArray())
  375. {
  376. //初始定义顾问的assistant 更改为assist
  377. if (obj.GetString().Equals($"assist"))
  378. {
  379. roles.Add(obj.GetString());
  380. }
  381. }
  382. }
  383. if (json.RootElement.TryGetProperty("permissions", out JsonElement _permissions) && _permissions.ValueKind != JsonValueKind.Null)
  384. {
  385. foreach (var obj in _permissions.EnumerateArray())
  386. {
  387. //显示BI权限
  388. foreach (var aut in authorityBIList)
  389. {
  390. if (aut.RowKey.Equals(obj.GetString()))
  391. {
  392. power.Add(obj.GetString());
  393. }
  394. }
  395. }
  396. }
  397. }
  398. dDAndTmdInfo2.tmdroles = roles;
  399. dDAndTmdInfo2.tmdpower = power;
  400. }
  401. }
  402. }
  403. catch { }
  404. dDAndTmdInfos.Add(dDAndTmdInfo2);
  405. }
  406. }
  407. }
  408. }
  409. }
  410. }
  411. return Ok(new { state = 200, count = ddUserInfos.Count, ddUserInfos }) ;
  412. }
  413. catch (Exception ex)
  414. {
  415. await _dingDing.SendBotMsg($"BI,{_option.Location} dd/get-tmdandddusers \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  416. return BadRequest();
  417. }
  418. }
  419. /// <summary>
  420. /// 依据钉钉UserID查询钉钉用户信息
  421. /// </summary>
  422. /// <param name="jsonElement"></param>
  423. /// <returns></returns>
  424. [ProducesDefaultResponseType]
  425. [HttpPost("get-dduserinfo")]
  426. public async Task<IActionResult> GetDDUserInfo(JsonElement jsonElement)
  427. {
  428. try
  429. {
  430. if (!jsonElement.TryGetProperty("userids", out JsonElement userIds)) return Ok(new { state = 1, message = "参数问题" });
  431. List<DDUserInfoAndTMD> dDUserInfoAndTMDs = new List<DDUserInfoAndTMD>();//返回钉钉信息和查询的醍摩豆信息
  432. string appKey = _configuration["DingDingAuth:appKey"];
  433. string appSecret = _configuration["DingDingAuth:appSecret"];
  434. //获取access_token
  435. DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  436. OapiGettokenRequest request = new OapiGettokenRequest();
  437. request.Appkey = appKey;
  438. request.Appsecret = appSecret;
  439. request.SetHttpMethod("Get");
  440. OapiGettokenResponse response = client.Execute(request);
  441. if (response.IsError)
  442. {
  443. return BadRequest();
  444. }
  445. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  446. string access_token = response.AccessToken;
  447. IDingTalkClient userInfoClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
  448. Dictionary<string, object> dic = new Dictionary<string, object> { { "PartitionKey", "authority-bi" } };
  449. var table = _azureStorage.GetCloudTableClient().GetTableReference("SchoolSetting");
  450. List<Authority> authorityBIList = await table.FindListByDict<Authority>(dic);
  451. if (!string.IsNullOrEmpty($"{userIds}"))
  452. {
  453. //List<string> str_userids = userIds.ToObject<List<string>>().Distinct().ToList();//通过数组的Equals实现去重
  454. List<string> str_userids1 = userIds.ToObject<List<string>>();
  455. List<string> str_userids = str_userids1.Where((x, i) => str_userids1.FindIndex(z => z == x) == i).ToList();//Lambda表达式去重
  456. foreach (var tempid in str_userids)
  457. {
  458. OapiV2UserGetRequest reqUserInfo = new OapiV2UserGetRequest() { Userid = $"{tempid}", Language = "zh_CN" };
  459. OapiV2UserGetResponse rspUserInfo = userInfoClient.Execute(reqUserInfo, access_token);
  460. if (rspUserInfo.Result != null)
  461. {
  462. List<string> roles = new List<string>();//角色列表
  463. List<string> power = new List<string>();//权限列表
  464. DDUserInfoAndTMD dDUserInfoAndTMD = new DDUserInfoAndTMD();
  465. dDUserInfoAndTMD.unionid = rspUserInfo.Result.Unionid;
  466. dDUserInfoAndTMD.title = rspUserInfo.Result.Title;
  467. dDUserInfoAndTMD.userid = rspUserInfo.Result.Userid;
  468. dDUserInfoAndTMD.jobNumber = rspUserInfo.Result.JobNumber;
  469. dDUserInfoAndTMD.name = rspUserInfo.Result.Name;
  470. dDUserInfoAndTMD.depts = rspUserInfo.Result.DeptIdList;
  471. dDUserInfoAndTMD.mobile = rspUserInfo.Result.Mobile;
  472. dDUserInfoAndTMD.avatar = rspUserInfo.Result.Avatar;
  473. string sqltxt = $"select distinct value(c) from c join A1 in c.ddbinds where A1.userid ='{tempid}'";
  474. try
  475. {
  476. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  477. {
  478. dDUserInfoAndTMD.tmdId = item.id;
  479. dDUserInfoAndTMD.isexist = true;
  480. if (!string.IsNullOrEmpty($"{item.defaultSchool}"))
  481. {
  482. var schoolRoles = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(item.id, new PartitionKey($"Teacher-{item.defaultSchool}"));
  483. if (schoolRoles.Status == 200)
  484. {
  485. using var json = await JsonDocument.ParseAsync(schoolRoles.ContentStream);
  486. if (json.RootElement.TryGetProperty("roles", out JsonElement _roles) && _roles.ValueKind != JsonValueKind.Null)
  487. {
  488. foreach (var obj in _roles.EnumerateArray())
  489. {
  490. //初始定义顾问的assistant 更改为assist
  491. if (obj.GetString().Equals($"assist"))
  492. {
  493. roles.Add(obj.GetString());
  494. }
  495. }
  496. }
  497. if (json.RootElement.TryGetProperty("permissions", out JsonElement _permissions) && _permissions.ValueKind != JsonValueKind.Null)
  498. {
  499. foreach (var obj in _permissions.EnumerateArray())
  500. {
  501. //显示BI权限
  502. foreach (var aut in authorityBIList)
  503. {
  504. if (aut.RowKey.Equals(obj.GetString()))
  505. {
  506. power.Add(obj.GetString());
  507. }
  508. }
  509. }
  510. }
  511. }
  512. dDUserInfoAndTMD.tmdroles = roles;
  513. dDUserInfoAndTMD.tmdpower = power;
  514. }
  515. }
  516. }
  517. catch { }
  518. dDUserInfoAndTMDs.Add(dDUserInfoAndTMD);
  519. }
  520. else return Ok(new { state = 2, message = "访问失败!" });
  521. }
  522. }
  523. return Ok(new { state = 200, ddUserInfos = dDUserInfoAndTMDs});
  524. }
  525. catch (Exception ex)
  526. {
  527. await _dingDing.SendBotMsg($"BI,{_option.Location},dd/get-dduserinfo \n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  528. return BadRequest();
  529. }
  530. }
  531. /// <summary>
  532. /// 返回钉钉和能查询到醍摩豆信息
  533. /// </summary>
  534. public class DDUserInfoAndTMD
  535. {
  536. /// <summary>
  537. /// 是否存在醍摩豆账户
  538. /// </summary>
  539. public bool isexist { get; set; }
  540. /// <summary>
  541. /// 绑定的醍摩豆账户
  542. /// </summary>
  543. public string tmdId { get; set; }
  544. /// <summary>
  545. /// 醍摩豆角色
  546. /// </summary>
  547. public List<string> tmdroles { get; set; }
  548. /// <summary>
  549. /// 醍摩豆角色权限
  550. /// </summary>
  551. public List<string> tmdpower { get; set; }
  552. /// <summary>
  553. /// 员工在当前开发者企业账号范围内的唯一标识
  554. /// </summary>
  555. public string unionid { get; set; }
  556. /// <summary>
  557. /// 用户ID
  558. /// </summary>
  559. public string userid { get; set; }
  560. /// <summary>
  561. /// 员工名称
  562. /// </summary>
  563. public string name { get; set; }
  564. /// <summary>
  565. /// 职位
  566. /// </summary>
  567. public string title { get; set; }
  568. /// <summary>
  569. /// 手机号
  570. /// </summary>
  571. public string mobile { get; set; }
  572. /// <summary>
  573. /// 员工工号
  574. /// </summary>
  575. public string jobNumber { get; set; }
  576. /// <summary>
  577. /// 所属部门
  578. /// </summary>
  579. public long deptId { get; set; }
  580. /// <summary>
  581. /// 部门名称
  582. /// </summary>
  583. public string deptName { get; set; }
  584. /// <summary>
  585. /// 所属部门id列表
  586. /// </summary>
  587. public List<long> depts { get; set; }
  588. /// <summary>
  589. /// 钉钉头像
  590. /// </summary>
  591. public string avatar { get; set; }
  592. }
  593. /// <summary>
  594. /// 部门信息
  595. /// </summary>
  596. public record DeptInfo
  597. {
  598. /// <summary>
  599. /// 部门ID
  600. /// </summary>
  601. public long deptId { get; set; }
  602. /// <summary>
  603. /// 部门名称
  604. /// </summary>
  605. public string deptName { get; set; }
  606. /// <summary>
  607. /// 父部门id,根部门为1
  608. /// </summary>
  609. public long parentId { get; set; }
  610. /// <summary>
  611. /// 部门集合
  612. /// </summary>
  613. public List<DeptBaseResponseDomain> deptList { get; set; }
  614. /// <summary>
  615. /// 钉钉用户列表
  616. /// </summary>
  617. public List<string> ddUserList { get; set; }
  618. }
  619. public record DeptBaseResponseDomain
  620. {
  621. /// <summary>
  622. /// 部门ID
  623. /// </summary>
  624. public long deptId { get; set; }
  625. /// <summary>
  626. /// 部门名称
  627. /// </summary>
  628. public string Name { get; set; }
  629. /// <summary>
  630. /// 父部门ID
  631. /// </summary>
  632. public long ParentId { get; set; }
  633. /// <summary>
  634. /// 下级列表
  635. /// </summary>
  636. public List<DeptBaseResponseDomain> LowerDeip_List { get; set; }
  637. /// <summary>
  638. /// 钉钉用户列表
  639. /// </summary>
  640. public List<string> ddUserList { get; set; }
  641. }
  642. }
  643. }