Browse Source

update dingding登录

CrazyIter_Bin 3 years ago
parent
commit
7288fe8cca

+ 6 - 1
TEAMModelOS.SDK/Models/Cosmos/Common/PaperCond.cs

@@ -18,7 +18,12 @@ namespace TEAMModelOS.SDK.Models
         }
         public int count { get; set; }
         public List<GradeCount> grades { get; set; } = new List<GradeCount>();
-        public List<SubjectItemCount> subjects { get; set; } = new List<SubjectItemCount>();
+        public List<SubjectPaperCount> subjects { get; set; } = new List<SubjectPaperCount>();
 
     }
+    public class SubjectPaperCount
+    {
+        public string id { get; set; }
+        public int count { get; set; }
+    }
 }

+ 20 - 0
TEAMModelOS.SDK/Models/Service/PaperService.cs

@@ -0,0 +1,20 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace TEAMModelOS.SDK.Models.Service
+{
+    public class PaperService
+    { /// <summary>
+      /// 计算题目的条件数量变化
+      /// </summary>
+      /// <param name="new></param>
+      /// <param name="odl"></param>
+      /// <param name="cond"></param>
+        public static void CountItemCond(ItemInfo newItem, ItemInfo odlItem, ItemCond cond) { 
+        
+        }
+    }
+}

+ 165 - 0
TEAMModelOS/Controllers/Third/DingDingController.cs

@@ -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();
+        }
+
+    }
+}

BIN
TEAMModelOS/Lib/topsdk-net-core.dll


+ 6 - 1
TEAMModelOS/TEAMModelOS.csproj

@@ -17,6 +17,7 @@
   </ItemGroup>
   <ItemGroup>
     <Folder Include="logfile\" />
+    <Folder Include="Lib\" />
   </ItemGroup>
 
   <ItemGroup>
@@ -25,7 +26,11 @@
   <ItemGroup>
     <ProjectReference Include="..\TEAMModelOS.SDK\TEAMModelOS.SDK.csproj" />
   </ItemGroup>
-
+  <ItemGroup>
+    <Reference Include="topsdk-net-core">
+      <HintPath>Lib\topsdk-net-core.dll</HintPath>
+    </Reference>
+  </ItemGroup>
   <PropertyGroup>
     <!-- Typescript/Javascript Client Configuration -->
     <SpaRoot>ClientApp\</SpaRoot>

+ 6 - 0
TEAMModelOS/appsettings.Development.json

@@ -65,6 +65,12 @@
       "delnotification": "https://api2.teammodel.net/service/delnotification"
     }
   },
+  "DingDingAuth": {
+    "Agentld": "1290158212",
+    "appKey": "dingrucgsnt8p13rfbgd",
+    "appSecret": "Gyx_N57yZslhQOAhAPlvmCwOp_qTm1DScKbd5OoOE0URAW4eViYA2Sk_ZxKb-8WG",
+    "getuserinfo_bycode": "https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey=xxx&timestamp=xxx&signature=xxx"
+  },
   "Third": {
     //"scsyxpt": {
     //  "passKey": "VgEQfEjwzfvFn8my", //机构安全码