BIOpenApiController.cs 7.8 KB

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