DDStructController.cs 38 KB

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