|
@@ -1,6 +1,10 @@
|
|
|
using Azure.Cosmos;
|
|
|
+using DingTalk.Api;
|
|
|
+using DingTalk.Api.Request;
|
|
|
+using DingTalk.Api.Response;
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
+using Microsoft.Extensions.Configuration;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Linq;
|
|
@@ -17,30 +21,81 @@ namespace TEAMModeBI.Controllers
|
|
|
[ApiController]
|
|
|
public class LoginController : ControllerBase
|
|
|
{
|
|
|
- private readonly AzureCosmosFactory _azureCosmos;
|
|
|
- public LoginController(AzureCosmosFactory azureCosmos)
|
|
|
+ private readonly IConfiguration _configuration;
|
|
|
+ public LoginController(IConfiguration configuration)
|
|
|
{
|
|
|
- _azureCosmos = azureCosmos;
|
|
|
-
|
|
|
+ _configuration = configuration;
|
|
|
}
|
|
|
|
|
|
- /// <summary>
|
|
|
- /// 修改教师信息
|
|
|
- /// </summary>
|
|
|
- /// <param name="request"></param>
|
|
|
- /// <returns></returns>
|
|
|
+ /// <summary>
|
|
|
+ /// 钉钉扫描登录
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="loginTmpCode"></param>
|
|
|
+ /// <returns></returns>
|
|
|
[ProducesDefaultResponseType]
|
|
|
- [HttpPost("dingding")]
|
|
|
- public async Task<IActionResult> SetTeacherInfo(JsonElement request) {
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
- if (!request.TryGetProperty("code", out JsonElement _code))
|
|
|
+ [HttpGet("dingding")]
|
|
|
+ public IActionResult DingDingLogin(string loginTmpCode)
|
|
|
+ {
|
|
|
+ string appKey = _configuration["appKey"];
|
|
|
+ string appSecret = _configuration["appSecret"];
|
|
|
+ string getuserinfo_bycode = _configuration["getuserinfo_bycode"];
|
|
|
+ //判断参数是否为空
|
|
|
+ if (string.IsNullOrEmpty(loginTmpCode))
|
|
|
+ {
|
|
|
+ return BadRequest("temp code error");
|
|
|
+ }
|
|
|
+ //获取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();
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- Teacher teacher = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>($"{_code}", new PartitionKey("Base"));
|
|
|
- return Ok(new { teacher = teacher });
|
|
|
+ string access_token = response.AccessToken;
|
|
|
+ //获取临时授权码 获取授权用户的个人信息
|
|
|
+ DefaultDingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
|
|
|
+ OapiSnsGetuserinfoBycodeRequest bycodeRequest = new OapiSnsGetuserinfoBycodeRequest()
|
|
|
+ {
|
|
|
+ //通过扫描二维码,跳转到指定的Url后,向Url中追加Code临时授权码
|
|
|
+ TmpAuthCode = loginTmpCode
|
|
|
+ };
|
|
|
+ OapiSnsGetuserinfoBycodeResponse bycodeResponse = client1.Execute(bycodeRequest, appKey, appSecret);
|
|
|
+ if (bycodeResponse.IsError)
|
|
|
+ {
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+ //根据unionid获取userid
|
|
|
+ string unionid = bycodeResponse.UserInfo.Unionid;
|
|
|
+ DefaultDingTalkClient clientDingTalkClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid");
|
|
|
+ OapiUserGetbyunionidRequest byunionidRequest = new OapiUserGetbyunionidRequest()
|
|
|
+ {
|
|
|
+ Unionid = unionid
|
|
|
+ };
|
|
|
+ OapiUserGetbyunionidResponse byunionidResponse = clientDingTalkClient.Execute(byunionidRequest, access_token);
|
|
|
+ if (byunionidResponse.IsError)
|
|
|
+ {
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+ string userid = byunionidResponse.Result.Userid;
|
|
|
+ //根据userId获取用户信息
|
|
|
+ DefaultDingTalkClient clientDingTalkClient2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
|
|
|
+ OapiV2UserGetRequest getRequest = new OapiV2UserGetRequest()
|
|
|
+ {
|
|
|
+ Userid = userid,
|
|
|
+ Language="zh_CN"
|
|
|
+ };
|
|
|
+ getRequest.SetHttpMethod("Get");
|
|
|
+ OapiV2UserGetResponse getResponse = clientDingTalkClient2.Execute(getRequest, access_token);
|
|
|
+ if (getResponse.IsError)
|
|
|
+ {
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+ return Ok(getResponse);
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
}
|