BIOpenApiController.cs 6.8 KB

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