|
@@ -0,0 +1,165 @@
|
|
|
+using Microsoft.AspNetCore.Mvc;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using TEAMModelOS.Models;
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
+using System.Text.Json;
|
|
|
+using TEAMModelOS.SDK.Models;
|
|
|
+using Microsoft.AspNetCore.Http;
|
|
|
+using TEAMModelOS.SDK.Extension;
|
|
|
+using Azure.Cosmos;
|
|
|
+using System.Text;
|
|
|
+using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using Azure.Messaging.ServiceBus;
|
|
|
+using Microsoft.Extensions.Configuration;
|
|
|
+using HTEXLib.COMM.Helpers;
|
|
|
+using TEAMModelOS.SDK;
|
|
|
+using System.IdentityModel.Tokens.Jwt;
|
|
|
+using TEAMModelOS.Services;
|
|
|
+using TEAMModelOS.SDK.Models.Service;
|
|
|
+using System.IO;
|
|
|
+using System.Dynamic;
|
|
|
+using Microsoft.AspNetCore.Authorization;
|
|
|
+using Azure.Storage.Blobs.Models;
|
|
|
+using static TEAMModelOS.SDK.Models.Teacher;
|
|
|
+using System.Web;
|
|
|
+using static TEAMModelOS.Controllers.FixDataController;
|
|
|
+using static TEAMModelOS.SDK.SchoolService;
|
|
|
+using Microsoft.AspNetCore.Hosting;
|
|
|
+using DingTalk.Api;
|
|
|
+using DingTalk.Api.Request;
|
|
|
+using DingTalk.Api.Response;
|
|
|
+
|
|
|
+namespace TEAMModelOS.Controllers.Third
|
|
|
+{
|
|
|
+ // <summary>
|
|
|
+ /// 标准OAuth2
|
|
|
+ /// </summary>
|
|
|
+ ///
|
|
|
+ [ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
+ [ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
|
+ [ApiController]
|
|
|
+ [Route("dingding")]
|
|
|
+ public class DingDingController : ControllerBase
|
|
|
+ {
|
|
|
+ private readonly SnowflakeId _snowflakeId;
|
|
|
+ private readonly AzureCosmosFactory _azureCosmos;
|
|
|
+ private readonly DingDing _dingDing;
|
|
|
+ private readonly Option _option;
|
|
|
+ private readonly AzureStorageFactory _azureStorage;
|
|
|
+ private readonly AzureServiceBusFactory _serviceBus;
|
|
|
+ private readonly AzureRedisFactory _azureRedis;
|
|
|
+ private readonly CoreAPIHttpService _coreAPIHttpService;
|
|
|
+ private readonly ThirdApisService _scsApisService;
|
|
|
+ private readonly HttpTrigger _httpTrigger;
|
|
|
+ private readonly IWebHostEnvironment _environment;
|
|
|
+ /// <summary>
|
|
|
+ /// 机构安全码
|
|
|
+ /// </summary>
|
|
|
+ public string _sc_passKey;
|
|
|
+ /// <summary>
|
|
|
+ /// 机构ID
|
|
|
+ /// </summary>
|
|
|
+ public string _sc_trainComID;
|
|
|
+ /// <summary>
|
|
|
+ /// 机构 AES 密钥
|
|
|
+ /// </summary>
|
|
|
+ public string _sc_privateKey;
|
|
|
+ /// <summary>
|
|
|
+ /// 访问地址
|
|
|
+ /// </summary>
|
|
|
+ public string _sc_url;
|
|
|
+ public IConfiguration _configuration { get; set; }
|
|
|
+ public DingDingController(IWebHostEnvironment environment, AzureCosmosFactory azureCosmos, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage,
|
|
|
+ AzureRedisFactory azureRedis, AzureServiceBusFactory serviceBus, IConfiguration configuration, CoreAPIHttpService coreAPIHttpService, ThirdApisService scsApisService, HttpTrigger httpTrigger)
|
|
|
+ {
|
|
|
+ _azureCosmos = azureCosmos;
|
|
|
+ _snowflakeId = snowflakeId;
|
|
|
+ _dingDing = dingDing;
|
|
|
+ _option = option?.Value;
|
|
|
+ _azureStorage = azureStorage;
|
|
|
+ _serviceBus = serviceBus;
|
|
|
+ _configuration = configuration;
|
|
|
+ _azureRedis = azureRedis;
|
|
|
+ _coreAPIHttpService = coreAPIHttpService;
|
|
|
+ _scsApisService = scsApisService;
|
|
|
+ _httpTrigger = httpTrigger;
|
|
|
+ _environment = environment;
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 钉钉扫码登录获取扫码信息
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="jsonElement"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("dingding-login")]
|
|
|
+ public async Task<IActionResult>DingdingLogin(JsonElement jsonElement) {
|
|
|
+ try {
|
|
|
+ string appKey = _configuration["DingDingAuth:appKey"];
|
|
|
+ string appSecret = _configuration["DingDingAuth:appSecret"];
|
|
|
+ if (string.IsNullOrWhiteSpace(appKey) || string.IsNullOrWhiteSpace(appSecret))
|
|
|
+ {
|
|
|
+ return Ok(new { state = 0, message = "请检查配置钉钉的信息" });
|
|
|
+ }
|
|
|
+ //自己传的code
|
|
|
+ if (!jsonElement.TryGetProperty("code", out JsonElement LoginTempCode)) return BadRequest();
|
|
|
+
|
|
|
+ //获取access_token
|
|
|
+ IDingTalkClient tokenClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
|
|
|
+ OapiGettokenRequest tokenRequest = new OapiGettokenRequest() { Appkey = appKey, Appsecret = appSecret };
|
|
|
+ tokenRequest.SetHttpMethod("Get");
|
|
|
+ OapiGettokenResponse tokenRespone = tokenClient.Execute(tokenRequest);
|
|
|
+ if (tokenRespone.IsError)
|
|
|
+ {
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
+
|
|
|
+ string access_token = tokenRespone.AccessToken;
|
|
|
+ //获取临时授权码 获取授权用户的个人信息
|
|
|
+ DefaultDingTalkClient clientinfo = new DefaultDingTalkClient("https://oapi.dingtalk.com/sns/getuserinfo_bycode");
|
|
|
+ OapiSnsGetuserinfoBycodeRequest req = new OapiSnsGetuserinfoBycodeRequest() { TmpAuthCode = $"{LoginTempCode}" }; //通过扫描二维码,跳转到指定的Url后,向Url中追加Code临时授权码
|
|
|
+ OapiSnsGetuserinfoBycodeResponse response = clientinfo.Execute(req, appKey, appSecret);
|
|
|
+
|
|
|
+ if (response.Errcode.Equals(40078))
|
|
|
+ {
|
|
|
+ return Ok(new { state = 0, message = $"state:{response.Errcode};Err{response.Errmsg}/临时授权码过期请重新扫码" });
|
|
|
+ }
|
|
|
+
|
|
|
+ string unionid = response.UserInfo.Unionid;
|
|
|
+ IDingTalkClient client2 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/getbyunionid"); //userid地址
|
|
|
+ OapiUserGetbyunionidRequest byunionidRequest = new OapiUserGetbyunionidRequest() { Unionid = unionid };
|
|
|
+ OapiUserGetbyunionidResponse byunionidResponse = client2.Execute(byunionidRequest, access_token);
|
|
|
+ if (byunionidResponse.IsError)
|
|
|
+ {
|
|
|
+ return Ok(new { state = 0, message = "扫码登录失败" });
|
|
|
+ }
|
|
|
+ // 根据userId获取用户信息
|
|
|
+ string userid = byunionidResponse.Result.Userid;
|
|
|
+ IDingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get");
|
|
|
+ OapiV2UserGetRequest v2GetRequest = new OapiV2UserGetRequest()
|
|
|
+ {
|
|
|
+ Userid = userid,
|
|
|
+ Language = "zh_CN"
|
|
|
+ };
|
|
|
+
|
|
|
+ v2GetRequest.SetHttpMethod("POST");
|
|
|
+ OapiV2UserGetResponse v2GetResponse = client3.Execute(v2GetRequest, access_token);
|
|
|
+ if (v2GetResponse.IsError)
|
|
|
+ {
|
|
|
+ return Ok(new { state = 0, message = "扫码登录失败" });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (Exception ex) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return Ok();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|