BIOpenApiController.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelBI.Filter;
  11. using TEAMModelBI.Tool.Extension;
  12. using TEAMModelOS.Models;
  13. using TEAMModelOS.SDK.Context.BI;
  14. using TEAMModelOS.SDK.DI;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Models.Cosmos.BI;
  17. namespace TEAMModelBI.Controllers.BITable
  18. {
  19. [Route("openapi")]
  20. [ApiController]
  21. public class BIOpenApiController : ControllerBase
  22. {
  23. public readonly AzureCosmosFactory _azureCosmos;
  24. public readonly AzureStorageFactory _azureStorage;
  25. public readonly DingDing _dingDing;
  26. public readonly Option _option;
  27. public BIOpenApiController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
  28. {
  29. _azureCosmos = azureCosmos;
  30. _azureStorage = azureStorage;
  31. _dingDing = dingDing;
  32. _option = option?.Value;
  33. }
  34. /// <summary>
  35. /// 查询所有开放API列表
  36. /// </summary>
  37. /// <param name="jsonElement"></param>
  38. /// <returns></returns>
  39. [ProducesDefaultResponseType]
  40. [AuthToken(Roles = "admin,rdc,assist,sales,company")]
  41. [HttpPost("get-infos")]
  42. public async Task<IActionResult> GetOpenApi(JsonElement jsonElement)
  43. {
  44. jsonElement.TryGetProperty("id", out JsonElement id);
  45. jsonElement.TryGetProperty("site", out JsonElement site);
  46. var cosmosClient = _azureCosmos.GetCosmosClient();
  47. if ($"{site}".Equals(BIConst.Global))
  48. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  49. StringBuilder sqlTxt = new("select c.id,c.code,c.pk,c.name,c.rw,c.method,c.url,c.descr,c.type,c.descrUrl,c.createTime from c where c.pk='Api' ");
  50. if (!string.IsNullOrEmpty($"{id}"))
  51. {
  52. sqlTxt.Append($" and c.id='{id}'");
  53. }
  54. List<ReadApi> openApis = new();
  55. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryStreamIterator(queryText:sqlTxt.ToString(),requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Api")}))
  56. {
  57. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  58. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt32() > 0)
  59. {
  60. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  61. {
  62. ReadApi readApi = new()
  63. {
  64. id = obj.GetProperty("id").GetString(),
  65. pk = obj.GetProperty("pk").GetString(),
  66. code = obj.GetProperty("code").GetString(),
  67. name = obj.GetProperty("name").GetString(),
  68. rw = obj.GetProperty("rw").GetString(),
  69. method= obj.GetProperty("method").GetString(),
  70. url = obj.GetProperty("url").GetString(),
  71. descr = obj.GetProperty("descr").GetString(),
  72. type = obj.GetProperty("type").GetInt32(),
  73. descrUrl=obj.GetProperty("descrUrl").GetString(),
  74. createTime = obj.GetProperty("createTime").GetInt64()
  75. };
  76. openApis.Add(readApi);
  77. }
  78. }
  79. }
  80. return Ok(new { state = 200, openApis });
  81. }
  82. /// <summary>
  83. /// 新增和修改Api信息
  84. /// </summary>
  85. /// <param name="openApi"></param>
  86. /// <returns></returns>
  87. [ProducesDefaultResponseType]
  88. [AuthToken(Roles = "admin,rdc,assist,sales,company")]
  89. [HttpPost("set-api")]
  90. public async Task<IActionResult> SetOpenApi(BIOpenApi openApi, [FromHeader] string site)
  91. {
  92. try
  93. {
  94. var cosmosClient = _azureCosmos.GetCosmosClient();
  95. var tableClient = _azureStorage.GetCloudTableClient();
  96. var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
  97. if ($"{site}".Equals(BIConst.Global))
  98. {
  99. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  100. tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
  101. blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
  102. }
  103. var (loginId, loginName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
  104. StringBuilder strMsg = new($"{loginName}【{loginId}】");
  105. string type = "";
  106. if (string.IsNullOrEmpty(openApi.id))
  107. {
  108. openApi.id = Guid.NewGuid().ToString();
  109. openApi.code = "Api";
  110. openApi.pk = "Api";
  111. openApi.type = string.IsNullOrEmpty($"{openApi.type}") ? openApi.type : 1;
  112. openApi.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  113. openApi = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BIOpenApi>(openApi, new PartitionKey("Api"));
  114. strMsg.Append($"新增开放API接口:名称:{openApi.name}/ID:{openApi.id}/接口:{openApi.url}");
  115. type = "openApi-add";
  116. }
  117. else
  118. {
  119. var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(openApi.id, new PartitionKey("Api"));
  120. if (response.Status == 200)
  121. {
  122. openApi.ttl = -1;
  123. openApi.pk = "Api";
  124. openApi.code = "Api";
  125. openApi = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BIOpenApi>(openApi, openApi.id, new PartitionKey("Api"));
  126. strMsg.Append($"修改开放API接口:名称:{openApi.name}/ID:{openApi.id}/接口:{openApi.url}");
  127. type = "openApi-update";
  128. }
  129. else return Ok(new { state = 404, msg = "未找到该id相关的Api信息" });
  130. }
  131. //保存操作记录
  132. await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
  133. return Ok(new { state = 200, openApi });
  134. }
  135. catch (Exception e)
  136. {
  137. await _dingDing.SendBotMsg($"BI,{_option.Location} , /openapi/set-api \n {e.Message}\n{e.StackTrace} \n ", GroupNames.成都开发測試群組);
  138. return BadRequest();
  139. }
  140. }
  141. public record ReadApi
  142. {
  143. public string id { get; set; }
  144. public string code { get; set; }
  145. public string pk { get; set; }
  146. public string name { get; set; }
  147. public string rw { get; set; }
  148. public string method { get; set; }
  149. public string url { get; set; }
  150. public string descr { get; set; }
  151. public int type { get; set; }
  152. public string descrUrl { get; set; }
  153. public long createTime { get; set; }
  154. }
  155. }
  156. }