IES5OAuthController.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using Azure.Cosmos;
  2. using Azure.Storage.Blobs;
  3. using DocumentFormat.OpenXml.Spreadsheet;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Options;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelBI.Filter;
  13. using TEAMModelBI.Tool.Extension;
  14. using TEAMModelOS.Models;
  15. using TEAMModelOS.SDK.Context.Constant;
  16. using TEAMModelOS.SDK.DI;
  17. using TEAMModelOS.SDK.Extension;
  18. using TEAMModelOS.SDK.Models;
  19. using TEAMModelOS.SDK.Models.Table;
  20. namespace TEAMModelBI.Controllers.BITable
  21. {
  22. [Route("iesoauth")]
  23. [ApiController]
  24. public class IES5OAuthController : ControllerBase
  25. {
  26. public readonly AzureStorageFactory _azureStorage;
  27. private readonly AzureCosmosFactory _azureCosmos;
  28. public readonly DingDing _dingDing;
  29. public readonly Option _option;
  30. public IES5OAuthController(AzureStorageFactory azureStorage, AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option)
  31. {
  32. _azureStorage = azureStorage;
  33. _azureCosmos = azureCosmos;
  34. _dingDing = dingDing;
  35. _option = option?.Value;
  36. }
  37. /// <summary>
  38. /// 获取信息授权信息
  39. /// </summary>
  40. /// <param name="jsonElement"></param>
  41. /// <returns></returns>
  42. [HttpPost("get-oauthshows")]
  43. public async Task<IActionResult> GetOAuthShows(JsonElement jsonElement)
  44. {
  45. jsonElement.TryGetProperty("type", out JsonElement type);
  46. jsonElement.TryGetProperty("scope", out JsonElement scope);
  47. jsonElement.TryGetProperty("code", out JsonElement code);
  48. jsonElement.TryGetProperty("rowKey", out JsonElement rowKey);
  49. List<OAuthShow> oAouthShows = new();
  50. var table = _azureStorage.GetCloudTableClient().GetTableReference("IESOAuth");
  51. StringBuilder tableSql = new(" PartitionKey eq 'OAuthShow'");
  52. if (!string.IsNullOrEmpty($"{type}"))
  53. tableSql.Append($" and Type eq '{type}'");
  54. if (!string.IsNullOrEmpty($"{scope}"))
  55. tableSql.Append($" and Scope eq '{scope}'");
  56. if (!string.IsNullOrEmpty($"{scope}"))
  57. tableSql.Append($" and Code eq '{code}'");
  58. if (!string.IsNullOrEmpty($"{scope}"))
  59. tableSql.Append($" and RowKey eq '{rowKey}'");
  60. //lambda 表达式排序
  61. oAouthShows = await table.QueryWhereString<OAuthShow>(tableSql.ToString());
  62. return Ok(new { state = RespondCode.Ok, oAouthShows });
  63. }
  64. /// <summary>
  65. /// 添加和修改权限
  66. /// </summary>
  67. /// <param name="oAuthShow"></param>
  68. /// <returns></returns>
  69. [AuthToken(Roles = "admin,rdc,sales")]
  70. [HttpPost("set-oauthshow")]
  71. public async Task<IActionResult> UpOAuthShow(OAuthShow oAuthShow)
  72. {
  73. var table = _azureStorage.GetCloudTableClient().GetTableReference("IESOAuth");
  74. var (_tmdId, _tmdName, _, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  75. var cosmosClinet = _azureCosmos.GetCosmosClient();
  76. var tableClient = _azureStorage.GetCloudTableClient();
  77. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  78. StringBuilder msg = new($"{_tmdName}[{_tmdId}]");
  79. List<OAuthShow> oAuthShows = new();
  80. List<OAuthShow> queryOs = new();
  81. ArtSetting artSetting = new();
  82. if (!string.IsNullOrEmpty(oAuthShow.RowKey))
  83. {
  84. string osSql = $" PartitionKey eq 'OAuthShow' and RowKey eq '{oAuthShow.RowKey}'";
  85. queryOs = await table.QueryWhereString<OAuthShow>(osSql);
  86. msg.Append($"修改RowKy为{oAuthShow.RowKey}授权信息");
  87. if (queryOs.Count > 0)
  88. {
  89. foreach (var item in queryOs)
  90. {
  91. item.Type = oAuthShow.Type;
  92. item.Scope = oAuthShow.Scope;
  93. item.Code = oAuthShow.Code;
  94. item.Domain = oAuthShow.Domain;
  95. OAuthShow upOs = await table.SaveOrUpdate<OAuthShow>(item);
  96. oAuthShows.Add(upOs);
  97. }
  98. }
  99. else
  100. return Ok(new { state = RespondCode.NotFound, msg = "未找到需要修改的授权。" });
  101. }
  102. else
  103. {
  104. string osSql = $" PartitionKey eq 'OAuthShow' and RowKey eq '{oAuthShow.Type}-{oAuthShow.Domain}-{oAuthShow.Code}'";
  105. queryOs = await table.QueryWhereString<OAuthShow>(osSql);
  106. if (queryOs.Count == 0)
  107. {
  108. oAuthShow.PartitionKey = "OAuthShow";
  109. oAuthShow.Status = 1;
  110. oAuthShow.RowKey = $"{oAuthShow.Type}-{oAuthShow.Domain}-{oAuthShow.Code}";
  111. msg.Append($"添加RowKy为{oAuthShow.RowKey}授权信息");
  112. oAuthShow = await table.SaveOrUpdate<OAuthShow>(oAuthShow);
  113. oAuthShows.Add(oAuthShow);
  114. if (oAuthShow.Type.Equals("art"))
  115. {
  116. var isExist = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemStreamAsync($"{oAuthShow.Code}", new PartitionKey("ArtSetting"));
  117. if (isExist.Status != 200)
  118. {
  119. artSetting = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<ArtSetting>("default", new PartitionKey("ArtSetting"));
  120. artSetting.id = oAuthShow.Code;
  121. artSetting = await cosmosClinet.GetContainer(Constant.TEAMModelOS, "Normal").CreateItemAsync<ArtSetting>(artSetting, new PartitionKey("ArtSetting"));
  122. }
  123. else
  124. {
  125. //保存操作记录
  126. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "schoolTeacher-add", msg.ToString(), _dingDing, httpContext: HttpContext);
  127. return Ok(new { state = RespondCode.Created, msg = "已授权,区级艺术设置文件已存在!" });
  128. }
  129. }
  130. }
  131. else
  132. return Ok(new { state = RespondCode.Conflict, msg = "已存在该授权。" });
  133. }
  134. //保存操作记录
  135. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "schoolTeacher-add", msg.ToString(), _dingDing, httpContext: HttpContext);
  136. return Ok(new { state = RespondCode.Ok, oAuthShows });
  137. }
  138. }
  139. }