Ver código fonte

钉钉扫码登录

Li 3 anos atrás
pai
commit
27f70bd04c

+ 72 - 17
TEAMModeBI/Controllers/LoginController.cs

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

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


+ 10 - 0
TEAMModeBI/TEAMModeBI.csproj

@@ -25,6 +25,16 @@
     <ProjectReference Include="..\TEAMModelOS.SDK\TEAMModelOS.SDK.csproj" />
   </ItemGroup>
 
+  <ItemGroup>
+    <Folder Include="Lib\" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <Reference Include="topsdk-net-core">
+      <HintPath>Lib\topsdk-net-core.dll</HintPath>
+    </Reference>
+  </ItemGroup>
+
   <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
     <!-- Ensure Node.js is installed -->
     <Exec Command="node --version" ContinueOnError="true">

+ 5 - 0
TEAMModeBI/appsettings.Development.json

@@ -7,6 +7,11 @@
     }
   },
   "AllowedHosts": "*",
+  "DingDingAuth": {
+    "appKey": "dingrucgsnt8p13rfbgd",
+    "appSecret": "Gyx_N57yZslhQOAhAPlvmCwOp_qTm1DScKbd5OoOE0URAW4eViYA2Sk_ZxKb-8WG",
+    "getuserinfo_bycode": "https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey=xxx&timestamp=xxx&signature=xxx"
+  },
   "Option": {
     "Location": "China-Dep",
     "LocationNum": "1",

+ 37 - 0
TEAMModelOS.SDK/Models/Cosmos/BI/DingDingUser.cs

@@ -0,0 +1,37 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace TEAMModelOS.SDK.Models.Cosmos.BI
+{
+   public class DingDingUser:CosmosEntity
+    {
+        public string name { get; set; }
+        public string picture { get; set; }
+        public List<School> schools { get; set; }
+        public class School
+        {
+            public string schoolId { get; set; }
+            public string name { get; set; }
+            public string status { get; set; }
+            public long time { get; set; }
+        }
+        public string phone { get; set; }
+        public string tmdid { get; set; }
+        public string stateCode { get; set; }
+        public string jobNumber { get; set; }
+        public string email { get; set; }
+        public string deptPositionListDeptId { get; set; }
+        public string deptPositionListTitle { get; set; }
+        public string hiredDate { get; set; }
+        public bool active { get; set; }
+        public List<rolelist> roleList { get; set; }
+        public class rolelist
+        {
+            public string id { get; set; }
+            public string name { get; set; }
+            public string groupName { get; set; } 
+        }
+
+    }
+}

+ 0 - 6
TEAMModelOS.SDK/TEAMModelOS.SDK.csproj

@@ -33,10 +33,4 @@
     <PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="2.0.0-preview" />
     <PackageReference Include="System.Net.Http.Json" Version="5.0.0" />
   </ItemGroup>
-
-
-
-  <ItemGroup>
-    <Folder Include="Models\Cosmos\Bi\" />
-  </ItemGroup>
 </Project>