123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- using DingTalk.Api;
- using DingTalk.Api.Request;
- using DingTalk.Api.Response;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Models.Service;
- namespace TEAMModeBI.Controllers.DingDingStruc
- {
- [Route("dd")]
- [ApiController]
- public class DDStructController : ControllerBase
- {
- private readonly IConfiguration _configuration;
- //数据容器
- private readonly AzureCosmosFactory _azureCosmos;
- //文件容器
- private readonly AzureStorageFactory _azureStorage;
- //钉钉提示信息
- private readonly DingDing _dingDing;
- private readonly Option _option;
- public DDStructController(IConfiguration configuration, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, CoreAPIHttpService aoreAPIHttpService)
- {
- _configuration = configuration;
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- _dingDing = dingDing;
- _option = option?.Value;
- }
- /// <summary>
- /// 获取组织架构列表
- /// </summary>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("ddminstruc")]
- public async Task<IActionResult> DDMainStruc()
- {
- string str_appKey = _configuration["DingDingAuth:appKey"];
- string str_appSecret = _configuration["DingDingAuth:appSecret"];
- //获取企业内部应用的accessToken
- IDingTalkClient Iclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
- OapiGettokenRequest request = new OapiGettokenRequest();
- request.Appkey = str_appKey;
- request.Appsecret = str_appSecret;
- request.SetHttpMethod("GET");
- OapiGettokenResponse tokenResponse = Iclient.Execute(request);
- if (tokenResponse.IsError)
- {
- return Ok(new { status = 0, message = "请检查配置" });
- }
- string access_token1 = tokenResponse.AccessToken;
- IDingTalkClient dingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/org/union/trunk/get");
- OapiOrgUnionTrunkGetRequest oapiOrgUnionTrunkGetRequest = new OapiOrgUnionTrunkGetRequest();
- OapiOrgUnionTrunkGetResponse oapiOrgUnionTrunkGetResponse = dingTalkClient.Execute(oapiOrgUnionTrunkGetRequest, tokenResponse.AccessToken);
- return Ok(new { oapiOrgUnionTrunkGetResponse.RequestId , oapiOrgUnionTrunkGetResponse.Body, oapiOrgUnionTrunkGetResponse.Result });
- }
- /// <summary>
- /// 获取分支组织列表信息
- /// </summary>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("ddbranchstruc")]
- public async Task<IActionResult> DDBranchStruc()
- {
- string str_appKey = _configuration["DingDingAuth:appKey"];
- string str_appSecret = _configuration["DingDingAuth:appSecret"];
- //获取企业内部应用的accessToken
- IDingTalkClient Iclient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
- OapiGettokenRequest request = new OapiGettokenRequest();
- request.Appkey = str_appKey;
- request.Appsecret = str_appSecret;
- request.SetHttpMethod("GET");
- OapiGettokenResponse tokenResponse = Iclient.Execute(request);
- if (tokenResponse.IsError)
- {
- return Ok(new { status = 0, message = "请检查配置" });
- }
- IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/org/union/branch/get");
- OapiOrgUnionBranchGetRequest req = new OapiOrgUnionBranchGetRequest();
- OapiOrgUnionBranchGetResponse rsp = client.Execute(req, tokenResponse.AccessToken);
- return Ok(new { Result = rsp.Result, Body = rsp.Body, RequestId = rsp.RequestId, SubErrCode = rsp.SubErrCode, Success = rsp.Success });
- }
- /// <summary>
- /// 获取企业部门列表
- /// </summary>
- [ProducesDefaultResponseType]
- [HttpPost("get-deptlist")]
- public async Task<IActionResult> GetDeptList()
- {
- try
- {
- string appKey = _configuration["DingDingAuth:appKey"];
- string appSecret = _configuration["DingDingAuth:appSecret"];
- //获取access_token
- DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
- OapiGettokenRequest request = new OapiGettokenRequest();
- request.Appkey = appKey;
- request.Appsecret = appSecret;
- request.SetHttpMethod("Get");
- OapiGettokenResponse response = client.Execute(request);
- if (response.IsError)
- {
- return BadRequest();
- }
- //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
- string access_token = response.AccessToken;
- //获取一级部门列表
- IDingTalkClient v2ListsubClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
- OapiV2DepartmentListsubRequest reqlistsub1 = new OapiV2DepartmentListsubRequest() { DeptId = 1L, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rsplistsub1 = v2ListsubClient1.Execute(reqlistsub1, access_token);
- List<DeptInfo> templsit = new List<DeptInfo>();
- if (rsplistsub1.Result != null)
- {
- foreach (var deptList in rsplistsub1.Result)
- {
- DeptInfo deptInfo = new DeptInfo();
- deptInfo.deptId = deptList.DeptId;
- deptInfo.deptName = deptList.Name;
- deptInfo.parentId = deptList.ParentId;
- //获取一级部门用户列表
- IDingTalkClient userListClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
- OapiUserListidRequest reqUserList1 = new OapiUserListidRequest() { DeptId = deptList.DeptId };
- OapiUserListidResponse rspUserList1 = userListClient1.Execute(reqUserList1, access_token);
- if (rspUserList1.Result != null)
- {
- deptInfo.ddUserList = rspUserList1.Result.UseridList;
- }
- //获取用户详细信息
- IDingTalkClient v2UserListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
- OapiV2UserGetRequest reqv2UserList = new OapiV2UserGetRequest();
- OapiV2UserGetResponse rspv2UserList = v2UserListClient.Execute(reqv2UserList, access_token);
- //获取二级部门列表
- OapiV2DepartmentListsubRequest reqlistsub = new OapiV2DepartmentListsubRequest() { DeptId = deptList.DeptId, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rsplistsub = v2ListsubClient1.Execute(reqlistsub, access_token);
- List<DeptBaseResponseDomain> deptBaseResponseDomainList = new List<DeptBaseResponseDomain>();
- if (rsplistsub.Result != null)
- {
- foreach (var deptlist2 in rsplistsub.Result)
- {
- //添加二级部门
- DeptBaseResponseDomain deptBaseResponseDomain2 = new DeptBaseResponseDomain();
- deptBaseResponseDomain2.deptId = deptlist2.DeptId;
- deptBaseResponseDomain2.Name = deptlist2.Name;
- deptBaseResponseDomain2.ParentId = deptlist2.ParentId;
- //获取三级部门用户列表
- OapiUserListidRequest reqUserList2 = new OapiUserListidRequest() { DeptId = deptlist2.DeptId };
- OapiUserListidResponse rspUserList2 = userListClient1.Execute(reqUserList2, access_token);
- if (rspUserList2.Result != null)
- {
- //添加三级部门用户
- deptBaseResponseDomain2.ddUserList = rspUserList2.Result.UseridList;
- }
- //获取三级部门列表
- OapiV2DepartmentListsubRequest reqlistsub3 = new OapiV2DepartmentListsubRequest() { DeptId = deptlist2.DeptId, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rsplistsub3 = v2ListsubClient1.Execute(reqlistsub3, access_token);
- List<DeptBaseResponseDomain> deptBaseResponseDomain3List = new List<DeptBaseResponseDomain>();
- if (rsplistsub3.Result != null)
- {
- foreach (var dept3List in rsplistsub3.Result)
- {
- //添加三级部门
- DeptBaseResponseDomain deptBaseResponseDomain3 = new DeptBaseResponseDomain();
- deptBaseResponseDomain3.deptId = dept3List.DeptId;
- deptBaseResponseDomain3.Name = dept3List.Name;
- deptBaseResponseDomain3.ParentId = dept3List.ParentId;
- //获取部门用户列表
- OapiUserListidRequest reqUserList3 = new OapiUserListidRequest() { DeptId = dept3List.DeptId };
- OapiUserListidResponse rspUserList3 = userListClient1.Execute(reqUserList3, access_token);
- if (rspUserList3.Result != null)
- {
- //添加三级部门的用户
- deptBaseResponseDomain3.ddUserList = rspUserList3.Result.UseridList;
- }
- //获取部门列表 四级目录
- OapiV2DepartmentListsubRequest reqlistsub4 = new OapiV2DepartmentListsubRequest() { DeptId = dept3List.DeptId, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rsplistsu4 = v2ListsubClient1.Execute(reqlistsub4, access_token);
- List<DeptBaseResponseDomain> deptBaseResponseDomain4List = new List<DeptBaseResponseDomain>();
- if (rsplistsu4.Result != null)
- {
- foreach (var dept4List in rsplistsu4.Result)
- {
- DeptBaseResponseDomain deptBaseResponseDomain4 = new DeptBaseResponseDomain();
- deptBaseResponseDomain4.deptId = dept4List.DeptId;
- deptBaseResponseDomain4.Name = dept4List.Name;
- deptBaseResponseDomain4.ParentId = dept4List.ParentId;
- deptBaseResponseDomain4List.Add(deptBaseResponseDomain4);
- //获取三级部门用户列表
- OapiUserListidRequest reqUserList4 = new OapiUserListidRequest() { DeptId = dept4List.DeptId };
- OapiUserListidResponse rspUserList4 = userListClient1.Execute(reqUserList4, access_token);
- if (rspUserList4.Result != null)
- {
- //添加四级部门的用户
- deptBaseResponseDomain4.ddUserList = rspUserList4.Result.UseridList;
- }
- }
- }
- //添加四级部门列表
- deptBaseResponseDomain3.LowerDeip_List = deptBaseResponseDomain4List;
- deptBaseResponseDomain3List.Add(deptBaseResponseDomain3);
- }
- }
- //添加三级部门列表
- deptBaseResponseDomain2.LowerDeip_List = deptBaseResponseDomain3List;
- deptBaseResponseDomainList.Add(deptBaseResponseDomain2);
- }
- }
- //添加二级部门列表
- deptInfo.deptList = deptBaseResponseDomainList;
- templsit.Add(deptInfo);
- }
- }
- return Ok(new { state = 200, deptlist = templsit });
- }
- catch (Exception ex)
- {
- return Ok(new { state = 1, message=$"查询失败!:状态:{ex.StackTrace}错误:{ex.Message}" }) ;
- }
- }
- /// <summary>
- /// 获取当前用户的父级集合
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpPost("get-parentdept")]
- public async Task<IActionResult> GetParentDept(JsonElement jsonElement)
- {
- try
- {
- if (!jsonElement.TryGetProperty("userId", out JsonElement userId)) return Ok(new { state = 1, message = "参数错误!" });
- string appKey = _configuration["DingDingAuth:appKey"];
- string appSecret = _configuration["DingDingAuth:appSecret"];
- //获取access_token
- DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
- OapiGettokenRequest request = new OapiGettokenRequest();
- request.Appkey = appKey;
- request.Appsecret = appSecret;
- request.SetHttpMethod("Get");
- OapiGettokenResponse response = client.Execute(request);
- if (response.IsError)
- {
- return BadRequest();
- }
- //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
- string access_token = response.AccessToken;
- IDingTalkClient v2DeartDeptClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listparentbyuser");
- OapiV2DepartmentListparentbyuserRequest reqDeartDept = new OapiV2DepartmentListparentbyuserRequest() { Userid = userId.ToString() };
- OapiV2DepartmentListparentbyuserResponse rspDeartDept = v2DeartDeptClient.Execute(reqDeartDept, access_token);
- if (rspDeartDept.Result != null)
- {
- List<long> userParentDept = new List<long>();
- //var parentDept = rspDeartDept.Result.ParentList;
- foreach (var temp in rspDeartDept.Result.ParentList)
- {
- foreach (var deptTemp in temp.ParentDeptIdList)
- {
- userParentDept.Add(deptTemp);
- }
- }
- return Ok(new { state = 200, parentList = userParentDept });
- }
- return Ok(new { state = 2, message = "访问失败!" });
- }
- catch (Exception ex)
- {
- return Ok(new { state = 2, message = $"访问失败!状态:{ex.StackTrace} 错误:{ex.Message}" });
- }
- }
- /// <summary>
- ///
- /// </summary>
- /// <returns></returns>
- public async Task<IActionResult> SetDeptList(JsonElement jsonElement)
- {
- try
- {
- string appKey = _configuration["DingDingAuth:appKey"];
- string appSecret = _configuration["DingDingAuth:appSecret"];
- //获取access_token
- DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
- OapiGettokenRequest request = new OapiGettokenRequest();
- request.Appkey = appKey;
- request.Appsecret = appSecret;
- request.SetHttpMethod("Get");
- OapiGettokenResponse response = client.Execute(request);
- if (response.IsError)
- {
- return BadRequest();
- }
- //access_token的有效期为7200秒(2小时),有效期内重复获取会返回相同结果并自动续期,过期后获取会返回新的access_token
- string access_token = response.AccessToken;
- //获取部门列表 一级目录
- IDingTalkClient v2ListsubClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/department/listsub");
- OapiV2DepartmentListsubRequest reqlistsub1 = new OapiV2DepartmentListsubRequest() { DeptId = 1L, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rsplistsub1 = v2ListsubClient1.Execute(reqlistsub1, access_token);
- List<DeptInfo> templsit = new List<DeptInfo>();
- if (rsplistsub1.Result != null)
- {
- foreach (var deptList in rsplistsub1.Result)
- {
- DeptInfo deptInfo = new DeptInfo();
- deptInfo.deptId = deptList.DeptId;
- deptInfo.deptName = deptList.Name;
- deptInfo.parentId = deptList.ParentId;
- //获取一级部门用户列表
- IDingTalkClient userListClient1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
- OapiUserListidRequest reqUserList1 = new OapiUserListidRequest() { DeptId = deptList.DeptId };
- OapiUserListidResponse rspUserList1 = userListClient1.Execute(reqUserList1, access_token);
- if (rspUserList1.Result != null)
- {
- deptInfo.ddUserList = rspUserList1.Result.UseridList;
- }
- //获取用户详细信息
- IDingTalkClient v2UserListClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
- OapiV2UserGetRequest reqv2UserList = new OapiV2UserGetRequest();
- OapiV2UserGetResponse rspv2UserList = v2UserListClient.Execute(reqv2UserList, access_token);
- //获取二级部门列表
- OapiV2DepartmentListsubRequest reqlistsub = new OapiV2DepartmentListsubRequest() { DeptId = deptList.DeptId, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rsplistsub = v2ListsubClient1.Execute(reqlistsub, access_token);
- List<DeptBaseResponseDomain> deptBaseResponseDomainList = new List<DeptBaseResponseDomain>();
- if (rsplistsub.Result != null)
- {
- foreach (var deptlist2 in rsplistsub.Result)
- {
- //添加二级部门
- DeptBaseResponseDomain deptBaseResponseDomain2 = new DeptBaseResponseDomain();
- deptBaseResponseDomain2.deptId = deptlist2.DeptId;
- deptBaseResponseDomain2.Name = deptlist2.Name;
- deptBaseResponseDomain2.ParentId = deptlist2.ParentId;
- //获取三级部门用户列表
- OapiUserListidRequest reqUserList2 = new OapiUserListidRequest() { DeptId = deptlist2.DeptId };
- OapiUserListidResponse rspUserList2 = userListClient1.Execute(reqUserList2, access_token);
- if (rspUserList2.Result != null)
- {
- //添加三级部门用户
- deptBaseResponseDomain2.ddUserList = rspUserList2.Result.UseridList;
- }
- //获取三级部门列表
- OapiV2DepartmentListsubRequest reqlistsub3 = new OapiV2DepartmentListsubRequest() { DeptId = deptlist2.DeptId, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rsplistsub3 = v2ListsubClient1.Execute(reqlistsub3, access_token);
- List<DeptBaseResponseDomain> deptBaseResponseDomain3List = new List<DeptBaseResponseDomain>();
- if (rsplistsub3.Result != null)
- {
- foreach (var dept3List in rsplistsub3.Result)
- {
- //添加三级部门
- DeptBaseResponseDomain deptBaseResponseDomain3 = new DeptBaseResponseDomain();
- deptBaseResponseDomain3.deptId = dept3List.DeptId;
- deptBaseResponseDomain3.Name = dept3List.Name;
- deptBaseResponseDomain3.ParentId = dept3List.ParentId;
- //获取部门用户列表
- OapiUserListidRequest reqUserList3 = new OapiUserListidRequest() { DeptId = dept3List.DeptId };
- OapiUserListidResponse rspUserList3 = userListClient1.Execute(reqUserList3, access_token);
- if (rspUserList3.Result != null)
- {
- //添加三级部门的用户
- deptBaseResponseDomain3.ddUserList = rspUserList3.Result.UseridList;
- }
- //获取部门列表 四级目录
- OapiV2DepartmentListsubRequest reqlistsub4 = new OapiV2DepartmentListsubRequest() { DeptId = dept3List.DeptId, Language = "zh_CN" };
- OapiV2DepartmentListsubResponse rsplistsu4 = v2ListsubClient1.Execute(reqlistsub4, access_token);
- List<DeptBaseResponseDomain> deptBaseResponseDomain4List = new List<DeptBaseResponseDomain>();
- if (rsplistsu4.Result != null)
- {
- foreach (var dept4List in rsplistsu4.Result)
- {
- DeptBaseResponseDomain deptBaseResponseDomain4 = new DeptBaseResponseDomain();
- deptBaseResponseDomain4.deptId = dept4List.DeptId;
- deptBaseResponseDomain4.Name = dept4List.Name;
- deptBaseResponseDomain4.ParentId = dept4List.ParentId;
- deptBaseResponseDomain4List.Add(deptBaseResponseDomain4);
- //获取三级部门用户列表
- OapiUserListidRequest reqUserList4 = new OapiUserListidRequest() { DeptId = dept4List.DeptId };
- OapiUserListidResponse rspUserList4 = userListClient1.Execute(reqUserList4, access_token);
- if (rspUserList4.Result != null)
- {
- //添加四级部门的用户
- deptBaseResponseDomain4.ddUserList = rspUserList4.Result.UseridList;
- }
- }
- }
- //添加四级部门列表
- deptBaseResponseDomain3.LowerDeip_List = deptBaseResponseDomain4List;
- deptBaseResponseDomain3List.Add(deptBaseResponseDomain3);
- }
- }
- //添加三级部门列表
- deptBaseResponseDomain2.LowerDeip_List = deptBaseResponseDomain3List;
- deptBaseResponseDomainList.Add(deptBaseResponseDomain2);
- }
- }
- //添加二级部门列表
- deptInfo.deptList = deptBaseResponseDomainList;
- templsit.Add(deptInfo);
- }
- }
- return Ok(new { state = 200, deptlist = templsit });
- }
- catch (Exception ex)
- {
- return Ok(new { state = 1, message = $"查询失败!:状态:{ex.StackTrace}错误:{ex.Message}" });
- }
- }
- public record DeptInfo
- {
- /// <summary>
- /// 部门ID
- /// </summary>
- public long deptId { get; set; }
- /// <summary>
- /// 部门名称
- /// </summary>
- public string deptName { get; set; }
- /// <summary>
- /// 父部门id,根部门为1
- /// </summary>
- public long parentId { get; set; }
- /// <summary>
- /// 部门集合
- /// </summary>
- public List<DeptBaseResponseDomain> deptList { get; set; }
- /// <summary>
- /// 钉钉用户列表
- /// </summary>
- public List<string> ddUserList { get; set; }
- }
- public record DeptBaseResponseDomain
- {
- /// <summary>
- /// 部门ID
- /// </summary>
- public long deptId { get; set; }
- /// <summary>
- /// 部门名称
- /// </summary>
- public string Name { get; set; }
- /// <summary>
- /// 父部门ID
- /// </summary>
- public long ParentId { get; set; }
- /// <summary>
- /// 下级列表
- /// </summary>
- public List<DeptBaseResponseDomain> LowerDeip_List { get; set; }
- /// <summary>
- /// 钉钉用户列表
- /// </summary>
- public List<string> ddUserList { get; set; }
- }
- }
- }
|