DDStructController.cs 39 KB

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