DDStructController.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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 static DingTalk.Api.Response.OapiV2UserGetResponse;
  18. using static TEAMModelOS.SDK.Models.Teacher;
  19. namespace TEAMModeBI.Controllers.DingDingStruc
  20. {
  21. [Route("dd")]
  22. [ApiController]
  23. public class DDStructController : ControllerBase
  24. {
  25. private readonly IConfiguration _configuration;
  26. //数据容器
  27. private readonly AzureCosmosFactory _azureCosmos;
  28. //文件容器
  29. private readonly AzureStorageFactory _azureStorage;
  30. //钉钉提示信息
  31. private readonly DingDing _dingDing;
  32. private readonly Option _option;
  33. string type = "ddteammodel";
  34. public DDStructController(IConfiguration configuration, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, CoreAPIHttpService aoreAPIHttpService)
  35. {
  36. _configuration = configuration;
  37. _azureCosmos = azureCosmos;
  38. _azureStorage = azureStorage;
  39. _dingDing = dingDing;
  40. _option = option?.Value;
  41. }
  42. /// <summary>
  43. /// 获取组织架构列表
  44. /// </summary>
  45. /// <returns></returns>
  46. [ProducesDefaultResponseType]
  47. [HttpPost("ddminstruc")]
  48. public async Task<IActionResult> DDMainStruc()
  49. {
  50. string str_appKey = _configuration["DingDingAuth:appKey"];
  51. string str_appSecret = _configuration["DingDingAuth:appSecret"];
  52. //获取企业内部应用的accessToken
  53. IDingTalkClient Iclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  54. OapiGettokenRequest request = new OapiGettokenRequest();
  55. request.Appkey = str_appKey;
  56. request.Appsecret = str_appSecret;
  57. request.SetHttpMethod("GET");
  58. OapiGettokenResponse tokenResponse = Iclient.Execute(request);
  59. if (tokenResponse.IsError)
  60. {
  61. return Ok(new { status = 0, message = "请检查配置" });
  62. }
  63. string access_token1 = tokenResponse.AccessToken;
  64. IDingTalkClient dingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/org/union/trunk/get");
  65. OapiOrgUnionTrunkGetRequest oapiOrgUnionTrunkGetRequest = new OapiOrgUnionTrunkGetRequest();
  66. OapiOrgUnionTrunkGetResponse oapiOrgUnionTrunkGetResponse = dingTalkClient.Execute(oapiOrgUnionTrunkGetRequest, tokenResponse.AccessToken);
  67. return Ok(new { oapiOrgUnionTrunkGetResponse.RequestId , oapiOrgUnionTrunkGetResponse.Body, oapiOrgUnionTrunkGetResponse.Result });
  68. }
  69. /// <summary>
  70. /// 获取分支组织列表信息
  71. /// </summary>
  72. /// <returns></returns>
  73. [ProducesDefaultResponseType]
  74. [HttpPost("ddbranchstruc")]
  75. public async Task<IActionResult> DDBranchStruc()
  76. {
  77. string str_appKey = _configuration["DingDingAuth:appKey"];
  78. string str_appSecret = _configuration["DingDingAuth:appSecret"];
  79. //获取企业内部应用的accessToken
  80. IDingTalkClient Iclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  81. OapiGettokenRequest request = new OapiGettokenRequest();
  82. request.Appkey = str_appKey;
  83. request.Appsecret = str_appSecret;
  84. request.SetHttpMethod("GET");
  85. OapiGettokenResponse tokenResponse = Iclient.Execute(request);
  86. if (tokenResponse.IsError)
  87. {
  88. return Ok(new { status = 0, message = "请检查配置" });
  89. }
  90. IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/org/union/branch/get");
  91. OapiOrgUnionBranchGetRequest req = new OapiOrgUnionBranchGetRequest();
  92. OapiOrgUnionBranchGetResponse rsp = client.Execute(req, tokenResponse.AccessToken);
  93. return Ok(new { Result = rsp.Result, Body = rsp.Body, RequestId = rsp.RequestId, SubErrCode = rsp.SubErrCode, Success = rsp.Success });
  94. }
  95. /// <summary>
  96. /// 获取企业部门列表
  97. /// </summary>
  98. [ProducesDefaultResponseType]
  99. [HttpPost("get-deptlist")]
  100. public async Task<IActionResult> GetDeptList()
  101. {
  102. try
  103. {
  104. string appKey = _configuration["DingDingAuth:appKey"];
  105. string appSecret = _configuration["DingDingAuth:appSecret"];
  106. //获取access_token
  107. DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  108. OapiGettokenRequest request = new OapiGettokenRequest();
  109. request.Appkey = appKey;
  110. request.Appsecret = appSecret;
  111. request.SetHttpMethod("Get");
  112. OapiGettokenResponse response = client.Execute(request);
  113. if (response.IsError)
  114. {
  115. return BadRequest();
  116. }
  117. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  118. string access_token = response.AccessToken;
  119. //获取一级部门列表
  120. IDingTalkClient v2ListsubClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
  121. OapiV2DepartmentListsubRequest reqlistsub1 = new OapiV2DepartmentListsubRequest() { DeptId = 1L, Language = "zh_CN" };
  122. OapiV2DepartmentListsubResponse rsplistsub1 = v2ListsubClient1.Execute(reqlistsub1, access_token);
  123. List<DeptInfo> templsit = new List<DeptInfo>();
  124. if (rsplistsub1.Result != null)
  125. {
  126. foreach (var deptList in rsplistsub1.Result)
  127. {
  128. DeptInfo deptInfo = new DeptInfo();
  129. deptInfo.deptId = deptList.DeptId;
  130. deptInfo.deptName = deptList.Name;
  131. deptInfo.parentId = deptList.ParentId;
  132. //获取一级部门用户列表
  133. IDingTalkClient userListClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
  134. OapiUserListidRequest reqUserList1 = new OapiUserListidRequest() { DeptId = deptList.DeptId };
  135. OapiUserListidResponse rspUserList1 = userListClient1.Execute(reqUserList1, access_token);
  136. if (rspUserList1.Result != null)
  137. {
  138. deptInfo.ddUserList = rspUserList1.Result.UseridList;
  139. }
  140. //获取用户详细信息
  141. IDingTalkClient v2UserListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
  142. OapiV2UserGetRequest reqv2UserList = new OapiV2UserGetRequest();
  143. OapiV2UserGetResponse rspv2UserList = v2UserListClient.Execute(reqv2UserList, access_token);
  144. //获取二级部门列表
  145. OapiV2DepartmentListsubRequest reqlistsub = new OapiV2DepartmentListsubRequest() { DeptId = deptList.DeptId, Language = "zh_CN" };
  146. OapiV2DepartmentListsubResponse rsplistsub = v2ListsubClient1.Execute(reqlistsub, access_token);
  147. List<DeptBaseResponseDomain> deptBaseResponseDomainList = new List<DeptBaseResponseDomain>();
  148. if (rsplistsub.Result != null)
  149. {
  150. foreach (var deptlist2 in rsplistsub.Result)
  151. {
  152. //添加二级部门
  153. DeptBaseResponseDomain deptBaseResponseDomain2 = new DeptBaseResponseDomain();
  154. deptBaseResponseDomain2.deptId = deptlist2.DeptId;
  155. deptBaseResponseDomain2.Name = deptlist2.Name;
  156. deptBaseResponseDomain2.ParentId = deptlist2.ParentId;
  157. //获取三级部门用户列表
  158. OapiUserListidRequest reqUserList2 = new OapiUserListidRequest() { DeptId = deptlist2.DeptId };
  159. OapiUserListidResponse rspUserList2 = userListClient1.Execute(reqUserList2, access_token);
  160. if (rspUserList2.Result != null)
  161. {
  162. //添加三级部门用户
  163. deptBaseResponseDomain2.ddUserList = rspUserList2.Result.UseridList;
  164. }
  165. //获取三级部门列表
  166. OapiV2DepartmentListsubRequest reqlistsub3 = new OapiV2DepartmentListsubRequest() { DeptId = deptlist2.DeptId, Language = "zh_CN" };
  167. OapiV2DepartmentListsubResponse rsplistsub3 = v2ListsubClient1.Execute(reqlistsub3, access_token);
  168. List<DeptBaseResponseDomain> deptBaseResponseDomain3List = new List<DeptBaseResponseDomain>();
  169. if (rsplistsub3.Result != null)
  170. {
  171. foreach (var dept3List in rsplistsub3.Result)
  172. {
  173. //添加三级部门
  174. DeptBaseResponseDomain deptBaseResponseDomain3 = new DeptBaseResponseDomain();
  175. deptBaseResponseDomain3.deptId = dept3List.DeptId;
  176. deptBaseResponseDomain3.Name = dept3List.Name;
  177. deptBaseResponseDomain3.ParentId = dept3List.ParentId;
  178. //获取部门用户列表
  179. OapiUserListidRequest reqUserList3 = new OapiUserListidRequest() { DeptId = dept3List.DeptId };
  180. OapiUserListidResponse rspUserList3 = userListClient1.Execute(reqUserList3, access_token);
  181. if (rspUserList3.Result != null)
  182. {
  183. //添加三级部门的用户
  184. deptBaseResponseDomain3.ddUserList = rspUserList3.Result.UseridList;
  185. }
  186. //获取部门列表 四级目录
  187. OapiV2DepartmentListsubRequest reqlistsub4 = new OapiV2DepartmentListsubRequest() { DeptId = dept3List.DeptId, Language = "zh_CN" };
  188. OapiV2DepartmentListsubResponse rsplistsu4 = v2ListsubClient1.Execute(reqlistsub4, access_token);
  189. List<DeptBaseResponseDomain> deptBaseResponseDomain4List = new List<DeptBaseResponseDomain>();
  190. if (rsplistsu4.Result != null)
  191. {
  192. foreach (var dept4List in rsplistsu4.Result)
  193. {
  194. DeptBaseResponseDomain deptBaseResponseDomain4 = new DeptBaseResponseDomain();
  195. deptBaseResponseDomain4.deptId = dept4List.DeptId;
  196. deptBaseResponseDomain4.Name = dept4List.Name;
  197. deptBaseResponseDomain4.ParentId = dept4List.ParentId;
  198. deptBaseResponseDomain4List.Add(deptBaseResponseDomain4);
  199. //获取四级部门用户列表
  200. OapiUserListidRequest reqUserList4 = new OapiUserListidRequest() { DeptId = dept4List.DeptId };
  201. OapiUserListidResponse rspUserList4 = userListClient1.Execute(reqUserList4, access_token);
  202. if (rspUserList4.Result != null)
  203. {
  204. //添加四级部门的用户
  205. deptBaseResponseDomain4.ddUserList = rspUserList4.Result.UseridList;
  206. }
  207. }
  208. }
  209. //添加四级部门列表
  210. deptBaseResponseDomain3.LowerDeip_List = deptBaseResponseDomain4List;
  211. deptBaseResponseDomain3List.Add(deptBaseResponseDomain3);
  212. }
  213. }
  214. //添加三级部门列表
  215. deptBaseResponseDomain2.LowerDeip_List = deptBaseResponseDomain3List;
  216. deptBaseResponseDomainList.Add(deptBaseResponseDomain2);
  217. }
  218. }
  219. //添加二级部门列表
  220. deptInfo.deptList = deptBaseResponseDomainList;
  221. templsit.Add(deptInfo);
  222. }
  223. }
  224. return Ok(new { state = 200, deptlist = templsit });
  225. }
  226. catch (Exception ex)
  227. {
  228. return Ok(new { state = 1, message=$"查询失败!:状态:{ex.StackTrace}错误:{ex.Message}" }) ;
  229. }
  230. }
  231. /// <summary>
  232. /// 获取当前用户的父级集合
  233. /// </summary>
  234. /// <param name="jsonElement"></param>
  235. /// <returns></returns>
  236. [ProducesDefaultResponseType]
  237. [HttpPost("get-parentdept")]
  238. public async Task<IActionResult> GetParentDept(JsonElement jsonElement)
  239. {
  240. try
  241. {
  242. if (!jsonElement.TryGetProperty("userId", out JsonElement userId)) return Ok(new { state = 1, message = "参数错误!" });
  243. string appKey = _configuration["DingDingAuth:appKey"];
  244. string appSecret = _configuration["DingDingAuth:appSecret"];
  245. //获取access_token
  246. DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  247. OapiGettokenRequest request = new OapiGettokenRequest();
  248. request.Appkey = appKey;
  249. request.Appsecret = appSecret;
  250. request.SetHttpMethod("Get");
  251. OapiGettokenResponse response = client.Execute(request);
  252. if (response.IsError)
  253. {
  254. return BadRequest();
  255. }
  256. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  257. string access_token = response.AccessToken;
  258. IDingTalkClient v2DeartDeptClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listparentbyuser");
  259. OapiV2DepartmentListparentbyuserRequest reqDeartDept = new OapiV2DepartmentListparentbyuserRequest() { Userid = userId.ToString() };
  260. OapiV2DepartmentListparentbyuserResponse rspDeartDept = v2DeartDeptClient.Execute(reqDeartDept, access_token);
  261. if (rspDeartDept.Result != null)
  262. {
  263. List<long> userParentDept = new List<long>();
  264. //var parentDept = rspDeartDept.Result.ParentList;
  265. foreach (var temp in rspDeartDept.Result.ParentList)
  266. {
  267. foreach (var deptTemp in temp.ParentDeptIdList)
  268. {
  269. userParentDept.Add(deptTemp);
  270. }
  271. }
  272. return Ok(new { state = 200, parentList = userParentDept });
  273. }
  274. return Ok(new { state = 2, message = "访问失败!" });
  275. }
  276. catch (Exception ex)
  277. {
  278. return Ok(new { state = 2, message = $"访问失败!状态:{ex.StackTrace} 错误:{ex.Message}" });
  279. }
  280. }
  281. /// <summary>
  282. /// 依据钉钉UserID查询钉钉用户信息
  283. /// </summary>
  284. /// <param name="jsonElement"></param>
  285. /// <returns></returns>
  286. [ProducesDefaultResponseType]
  287. [HttpPost("get-dduserinfo")]
  288. public async Task<IActionResult> GetDDUserInfo(JsonElement jsonElement)
  289. {
  290. try
  291. {
  292. if (!jsonElement.TryGetProperty("userids", out JsonElement userIds)) return Ok(new { state = 1, message = "参数问题" });
  293. List<UserGetResponseDomain> ddUserInfos = new List<UserGetResponseDomain>();
  294. string appKey = _configuration["DingDingAuth:appKey"];
  295. string appSecret = _configuration["DingDingAuth:appSecret"];
  296. //获取access_token
  297. DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
  298. OapiGettokenRequest request = new OapiGettokenRequest();
  299. request.Appkey = appKey;
  300. request.Appsecret = appSecret;
  301. request.SetHttpMethod("Get");
  302. OapiGettokenResponse response = client.Execute(request);
  303. if (response.IsError)
  304. {
  305. return BadRequest();
  306. }
  307. //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
  308. string access_token = response.AccessToken;
  309. IDingTalkClient userInfoClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
  310. if (!string.IsNullOrEmpty($"{userIds}"))
  311. {
  312. List<string> str_userids = userIds.ToObject<List<string>>();
  313. foreach (var tempid in str_userids)
  314. {
  315. OapiV2UserGetRequest reqUserInfo = new OapiV2UserGetRequest() { Userid = $"{tempid}", Language = "zh_CN" };
  316. OapiV2UserGetResponse rspUserInfo = userInfoClient.Execute(reqUserInfo, access_token);
  317. if (rspUserInfo.Result != null)
  318. {
  319. ddUserInfos.Add(rspUserInfo.Result);
  320. }
  321. else return Ok(new { state = 2, message = "访问失败!" });
  322. }
  323. }
  324. return Ok(new { state = 200, ddUserInfos = ddUserInfos });
  325. }
  326. catch (Exception ex)
  327. {
  328. await _dingDing.SendBotMsg($"BI,{_option.Location},dd/get-dduserinfo \n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  329. return BadRequest();
  330. }
  331. }
  332. public record DeptInfo
  333. {
  334. /// <summary>
  335. /// 部门ID
  336. /// </summary>
  337. public long deptId { get; set; }
  338. /// <summary>
  339. /// 部门名称
  340. /// </summary>
  341. public string deptName { get; set; }
  342. /// <summary>
  343. /// 父部门id,根部门为1
  344. /// </summary>
  345. public long parentId { get; set; }
  346. /// <summary>
  347. /// 部门集合
  348. /// </summary>
  349. public List<DeptBaseResponseDomain> deptList { get; set; }
  350. /// <summary>
  351. /// 钉钉用户列表
  352. /// </summary>
  353. public List<string> ddUserList { get; set; }
  354. }
  355. public record DeptBaseResponseDomain
  356. {
  357. /// <summary>
  358. /// 部门ID
  359. /// </summary>
  360. public long deptId { get; set; }
  361. /// <summary>
  362. /// 部门名称
  363. /// </summary>
  364. public string Name { get; set; }
  365. /// <summary>
  366. /// 父部门ID
  367. /// </summary>
  368. public long ParentId { get; set; }
  369. /// <summary>
  370. /// 下级列表
  371. /// </summary>
  372. public List<DeptBaseResponseDomain> LowerDeip_List { get; set; }
  373. /// <summary>
  374. /// 钉钉用户列表
  375. /// </summary>
  376. public List<string> ddUserList { get; set; }
  377. }
  378. }
  379. }