BIOpenApiController.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Microsoft.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. ////分开部署,就不需要,一站多用时,取消注释
  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").GetItemQueryStreamIteratorSql(queryText:sqlTxt.ToString(),requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Api")}))
  57. {
  58. using var json = await JsonDocument.ParseAsync(item.Content);
  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.StatusCode == System.Net.HttpStatusCode.OK)
  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 AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
  134. return Ok(new { state = 200, openApi });
  135. }
  136. catch (Exception e)
  137. {
  138. await _dingDing.SendBotMsg($"BI,{_option.Location} , /openapi/set-api \n {e.Message}\n{e.StackTrace} \n ", GroupNames.成都开发測試群組);
  139. return BadRequest();
  140. }
  141. }
  142. public record ReadApi
  143. {
  144. public string id { get; set; }
  145. public string code { get; set; }
  146. public string pk { get; set; }
  147. public string name { get; set; }
  148. public string rw { get; set; }
  149. public string method { get; set; }
  150. public string url { get; set; }
  151. public string descr { get; set; }
  152. public int type { get; set; }
  153. public string descrUrl { get; set; }
  154. public long createTime { get; set; }
  155. }
  156. }
  157. }