| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using DingDingDemo.Common;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Caching.Distributed;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.Logging;
- namespace DingDingDemo.Controllers
- {
- /// <summary>
- /// 用户
- /// </summary>
- [Produces("application/json")]
- [Route("api/[controller]")]
- public class UserController : BaseController
- {
-
- private readonly ILogger<UserController> _logger;
- private readonly IDistributedCache _cache;
- private readonly IConfiguration _configuration;
- public UserController(IDistributedCache cache, IConfiguration configuration, ILogger<UserController> logger)
- {
- _cache = cache;
- _logger = logger;
- _configuration = configuration;
- }
- /// <summary>
- /// 获取列表
- /// </summary>
- /// <param name="deptid"></param>
- /// <returns></returns>
- [HttpGet("getlist")]
- public async Task<IActionResult> GetList(int deptid = 0)
- {
- ArrayList arrresult = new ArrayList();
- TokenHelper th = new TokenHelper(_cache, _configuration);
- if (deptid == 0 )
- {
- var strresult = await HttpHelper.HttpGetAsync(string.Format(_configuration["DingTalkSettings:GetDepartmentList"], th.GetAccessToken()));
- var result = strresult.ToJObject();
- if (result["errcode"].ToString() == "0")
- {
- var rtlist = result["department"].ToJson().ToList<Dictionary<string, object>>();
- foreach (var rt in rtlist)
- {
- arrresult.AddRange(GetDeptUserList(th.GetAccessToken(), Int32.Parse(rt["id"].ToString()), 0, 100));
- }
- }
- }
- else
- {
- arrresult.AddRange(GetDeptUserList(th.GetAccessToken(), deptid, 0, 100));
- }
- return Success("成功", arrresult);
- }
- /// <summary>
- /// 获取部门用户列表
- /// </summary>
- /// <param name="token"></param>
- /// <param name="deptid"></param>
- /// <param name="index"></param>
- /// <param name="size"></param>
- /// <returns></returns>
- [ApiExplorerSettings(IgnoreApi = true)]
- public ArrayList GetDeptUserList(string token, int deptid = 1, int index = 0, int size = 10)
- {
- ArrayList arrresult = new ArrayList();
- string url = string.Format(_configuration["DingTalkSettings:GetEmployeeListByDepartmentId"], token, deptid);
- url += "&offset=" + index + "&size=" + size;
- var strresult = HttpHelper.HttpGet(url);
- var result = strresult.ToJObject();
- if (result["errcode"].ToString() == "0")
- {
- var rtlist = result["userlist"].ToJson().ToList<Dictionary<string, object>>();
- arrresult.AddRange(rtlist);
- if (result["hasMore"].ToString().ToLower() == "true")
- {
- arrresult.AddRange(GetDeptUserList(token, deptid, index + size, size));
- }
- }
- return arrresult;
- }
- /// <summary>
- /// 获取详情
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [HttpGet("getsingle")]
- public async Task<IActionResult> GetSingle(string id)
- {
- TokenHelper th = new TokenHelper(_cache, _configuration);
- var strresult = await HttpHelper.HttpGetAsync(string.Format(_configuration["DingTalkSettings:GetEmployee"], th.GetAccessToken(), id));
- var result = strresult.ToJObject();
- if (result["errcode"].ToString() == "0")
- {
- return Success("成功", result);
- }
- else
- {
- return Error(result["errmsg"].ToString());
- }
- }
- }
- }
|