DDBindController.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using Azure.Cosmos;
  2. using Azure.Storage.Blobs.Models;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IdentityModel.Tokens.Jwt;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.Models;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Models;
  14. namespace TEAMModeBI.Controllers.DingDingLogin
  15. {
  16. [Route("ddbind")]
  17. [ApiController]
  18. public class DDBindController : ControllerBase
  19. {
  20. private readonly AzureCosmosFactory _azureCosmos;
  21. private readonly DingDing _dingDing;
  22. private readonly Option _option;
  23. private readonly AzureStorageFactory _azureStorage;
  24. public readonly string type = "ddteammodel";
  25. public DDBindController(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage, Option option)
  26. {
  27. _azureCosmos = azureCosmos;
  28. _dingDing = dingDing;
  29. _azureStorage = azureStorage;
  30. _option = option;
  31. }
  32. /// <summary>
  33. /// 钉钉绑定醍摩豆教师信息
  34. /// </summary>
  35. /// <param name="ddrcord"></param>
  36. /// <returns></returns>
  37. [ProducesDefaultResponseType]
  38. [HttpGet("bind")]
  39. [AllowAnonymous]
  40. public async Task<IActionResult> Bind(ddrcord ddrcord)
  41. {
  42. try
  43. {
  44. Teacher teacher = null;
  45. var jwt = new JwtSecurityToken(ddrcord.id_token);
  46. if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.OrdinalIgnoreCase)) return BadRequest();
  47. var id = jwt.Payload.Sub;
  48. jwt.Payload.TryGetValue("name", out object name);
  49. jwt.Payload.TryGetValue("picture", out object picture);
  50. var client = _azureCosmos.GetCosmosClient();
  51. teacher = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Teacher>(id, new PartitionKey("Base"));
  52. string sql = $"SELECT distinct value(c) FROM c join A1 in c.ddbinds where A1.userid='{1}' and A1.loginid='{1}'";
  53. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: sql,
  54. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  55. {
  56. teacher = item;
  57. break;
  58. }
  59. if (teacher != null)
  60. {
  61. if (teacher.id.Equals(id))
  62. {
  63. var ddbind = teacher.ddbinds.Find(x => x.userid.Equals($"{ddrcord.userid}") && x.loginid.Equals($"{ddrcord.loginid}"));
  64. if (ddbind == null)
  65. {
  66. 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 } };
  67. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Teacher>(teacher, teacher.id, new PartitionKey(teacher.code));
  68. }
  69. }
  70. else
  71. {
  72. return Ok(new
  73. {
  74. location = _option.Location,
  75. //账号已被别的醍摩豆id绑定
  76. status = 3,
  77. tmdid = teacher.id,
  78. name = teacher.name,
  79. userid = ddrcord.userid,
  80. ddname = ddrcord.userName
  81. });
  82. }
  83. }
  84. else
  85. {
  86. teacher = new Teacher
  87. {
  88. id = id,
  89. pk = "Base",
  90. code = "Base",
  91. name = name?.ToString(),
  92. picture = picture?.ToString(),
  93. //创建账号并第一次登录IES5则默认赠送1G
  94. size = 1,
  95. defaultSchool = null,
  96. schools = new List<Teacher.TeacherSchool>(),
  97. 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 } }
  98. };
  99. var container = _azureStorage.GetBlobContainerClient(id);
  100. await container.CreateIfNotExistsAsync(PublicAccessType.None); //嘗試創建Teacher私有容器,如存在則不做任何事,保障容器一定存在
  101. teacher = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
  102. }
  103. return Ok(new
  104. {
  105. location = _option.Location,
  106. status = 200,
  107. });
  108. }
  109. catch (Exception)
  110. {
  111. return Ok(new
  112. {
  113. location = _option.Location,
  114. status = 2
  115. });
  116. }
  117. }
  118. public record ddrcord
  119. {
  120. /// <summary>
  121. /// 绑定类型 ddteammodel
  122. /// </summary>
  123. public string type { get; set; }
  124. /// <summary>
  125. /// 用户来源
  126. /// </summary>
  127. public string loginid { get; set; }
  128. /// <summary>
  129. /// 钉钉ID
  130. /// </summary>
  131. public string userid { get; set; }
  132. /// <summary>
  133. /// 钉钉用户名
  134. /// </summary>
  135. public string userName { get; set; }
  136. /// <summary>
  137. /// 钉钉手机号
  138. /// </summary>
  139. public string mobile { get; set; }
  140. /// <summary>
  141. /// 邮箱
  142. /// </summary>
  143. public string email { get; set; }
  144. public HashSet<string> sourceid { get; set; } = new HashSet<string>();
  145. /// <summary>
  146. /// 登录编号
  147. /// </summary>
  148. public string id_token { get; set; }
  149. }
  150. }
  151. }