IES5OAuthController.cs 6.8 KB

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