123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using Azure.Cosmos;
- using Azure.Storage.Blobs.Models;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using System;
- using System.Collections.Generic;
- using System.IdentityModel.Tokens.Jwt;
- using System.Linq;
- using System.Threading.Tasks;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Models;
- namespace TEAMModeBI.Controllers.DingDingLogin
- {
- [Route("ddbind")]
- [ApiController]
- public class DDBindController : ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmos;
- private readonly DingDing _dingDing;
- private readonly Option _option;
- private readonly AzureStorageFactory _azureStorage;
- public readonly string type = "ddteammodel";
- public DDBindController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, Option option)
- {
- _azureCosmos = azureCosmos;
- _dingDing = dingDing;
- _azureStorage = azureStorage;
- _option = option;
- }
- /// <summary>
- /// 钉钉绑定醍摩豆教师信息
- /// </summary>
- /// <param name="ddrcord"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [HttpGet("bind")]
- [AllowAnonymous]
- public async Task<IActionResult> Bind(ddrcord ddrcord)
- {
- try
- {
- Teacher teacher = null;
- var jwt = new JwtSecurityToken(ddrcord.id_token);
- if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.OrdinalIgnoreCase)) return BadRequest();
- var id = jwt.Payload.Sub;
- jwt.Payload.TryGetValue("name", out object name);
- jwt.Payload.TryGetValue("picture", out object picture);
- var client = _azureCosmos.GetCosmosClient();
- teacher = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>(id, new PartitionKey("Base"));
- string sql = $"SELECT distinct value(c) FROM c join A1 in c.ddbinds where A1.userid='{1}' and A1.loginid='{1}'";
- await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sql,
- requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
- {
- teacher = item;
- break;
- }
- if (teacher != null)
- {
- if (teacher.id.Equals(id))
- {
- var ddbind = teacher.ddbinds.Find(x => x.userid.Equals($"{ddrcord.userid}") && x.loginid.Equals($"{ddrcord.loginid}"));
- if (ddbind == null)
- {
- teacher.ddbinds = new List<Teacher.DingDingBind> { new Teacher.DingDingBind { type = type, loginid = $"{ddrcord.loginid}", userid = $"{ddrcord.userid}", userName = $"{ddrcord.userName}", Mobile = $"{ddrcord.mobile}", email = $"{ddrcord.email}", sourceid = ddrcord.sourceid } };
- await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey(teacher.code));
- }
- }
- else
- {
- return Ok(new
- {
- location = _option.Location,
- //账号已被别的醍摩豆id绑定
- status = 3,
- tmdid = teacher.id,
- name = teacher.name,
- userid = ddrcord.userid,
- ddname = ddrcord.userName
- });
- }
- }
- else
- {
- teacher = new Teacher
- {
- id = id,
- pk = "Base",
- code = "Base",
- name = name?.ToString(),
- picture = picture?.ToString(),
- //创建账号并第一次登录IES5则默认赠送1G
- size = 1,
- defaultSchool = null,
- schools = new List<Teacher.TeacherSchool>(),
- ddbinds = new List<Teacher.DingDingBind> { new Teacher.DingDingBind { type = type, loginid = $"{ddrcord.loginid.ToString()}", userid = $"{ddrcord.userid.ToString()}", userName = $"{ddrcord.userName}", Mobile = $"{ddrcord.mobile}", email = $"{ddrcord.email}", sourceid = ddrcord.sourceid } }
- };
- var container = _azureStorage.GetBlobContainerClient(id);
- await container.CreateIfNotExistsAsync(PublicAccessType.None); //嘗試創建Teacher私有容器,如存在則不做任何事,保障容器一定存在
- teacher = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
- }
- return Ok(new
- {
- location = _option.Location,
- status = 200,
- });
- }
- catch (Exception)
- {
- return Ok(new
- {
- location = _option.Location,
- status = 2
- });
- }
- }
- public record ddrcord
- {
- /// <summary>
- /// 绑定类型 ddteammodel
- /// </summary>
- public string type { get; set; }
- /// <summary>
- /// 用户来源
- /// </summary>
- public string loginid { get; set; }
- /// <summary>
- /// 钉钉ID
- /// </summary>
- public string userid { get; set; }
- /// <summary>
- /// 钉钉用户名
- /// </summary>
- public string userName { get; set; }
- /// <summary>
- /// 钉钉手机号
- /// </summary>
- public string mobile { get; set; }
- /// <summary>
- /// 邮箱
- /// </summary>
- public string email { get; set; }
- public HashSet<string> sourceid { get; set; } = new HashSet<string>();
- /// <summary>
- /// 登录编号
- /// </summary>
- public string id_token { get; set; }
- }
- }
- }
|