123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using Azure.Cosmos;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelBI.DI.BIAzureStorage;
- using TEAMModelBI.Filter;
- using TEAMModelBI.Tool.Context;
- using TEAMModelBI.Tool.Extension;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models.Cosmos.BI;
- namespace TEAMModelBI.Controllers.BINormal
- {
- [Route("openapi")]
- [ApiController]
- public class BIOpenApiController : ControllerBase
- {
- public readonly AzureCosmosFactory _azureCosmos;
- public readonly AzureStorageFactory _azureStorage;
- public readonly DingDing _dingDing;
- public readonly Option _option;
- public BIOpenApiController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option)
- {
- _azureCosmos = azureCosmos;
- _azureStorage = azureStorage;
- _dingDing = dingDing;
- _option = option?.Value;
- }
- /// <summary>
- /// 查询所有开放API列表
- /// </summary>
- /// <param name="jsonElement"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist,sales,company")]
- [HttpPost("get-infos")]
- public async Task<IActionResult> GetOpenApi(JsonElement jsonElement)
- {
- jsonElement.TryGetProperty("id", out JsonElement id);
- jsonElement.TryGetProperty("site", out JsonElement site);
- var cosmosClient = _azureCosmos.GetCosmosClient();
- if ($"{site}".Equals(BIConst.GlobalSite))
- cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
- 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' ");
- if (!string.IsNullOrEmpty($"{id}"))
- {
- sqlTxt.Append($" and c.id='{id}'");
- }
- List<ReadApi> openApis = new();
- await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryStreamIterator(queryText:sqlTxt.ToString(),requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Api")}))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt32() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- ReadApi readApi = new()
- {
- id = obj.GetProperty("id").GetString(),
- pk = obj.GetProperty("pk").GetString(),
- code = obj.GetProperty("code").GetString(),
- name = obj.GetProperty("name").GetString(),
- rw = obj.GetProperty("rw").GetString(),
- method= obj.GetProperty("method").GetString(),
- url = obj.GetProperty("url").GetString(),
- descr = obj.GetProperty("descr").GetString(),
- type = obj.GetProperty("type").GetInt32(),
- descrUrl=obj.GetProperty("descrUrl").GetString(),
- createTime = obj.GetProperty("createTime").GetInt64()
- };
- openApis.Add(readApi);
- }
- }
- }
- return Ok(new { state = 200, openApis });
- }
- /// <summary>
- /// 新增和修改Api信息
- /// </summary>
- /// <param name="openApi"></param>
- /// <returns></returns>
- [ProducesDefaultResponseType]
- [AuthToken(Roles = "admin,rdc,assist,sales,company")]
- [HttpPost("set-api")]
- public async Task<IActionResult> SetOpenApi(BIOpenApi openApi, [FromHeader] string site)
- {
- try
- {
- var cosmosClient = _azureCosmos.GetCosmosClient();
- var tableClient = _azureStorage.GetCloudTableClient();
- var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
- if ($"{site}".Equals(BIConst.GlobalSite))
- {
- cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.GlobalSite);
- tableClient = _azureStorage.GetCloudTableClient(BIConst.GlobalSite);
- blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.GlobalSite);
- }
- var (loginId, loginName, pic, did, dname, dpic) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
- StringBuilder strMsg = new($"{loginName}【{loginId}】");
- string type = "";
- if (string.IsNullOrEmpty(openApi.id))
- {
- openApi.id = Guid.NewGuid().ToString();
- openApi.code = "Api";
- openApi.pk = "Api";
- openApi.type = string.IsNullOrEmpty($"{openApi.type}") ? openApi.type : 1;
- openApi.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- openApi = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BIOpenApi>(openApi, new PartitionKey("Api"));
- strMsg.Append($"新增开放API接口:名称:{openApi.name}/ID:{openApi.id}/接口:{openApi.url}");
- type = "openApi-add";
- }
- else
- {
- var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync(openApi.id, new PartitionKey("Api"));
- if (response.Status == 200)
- {
- openApi.ttl = -1;
- openApi.pk = "Api";
- openApi.code = "Api";
- openApi = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BIOpenApi>(openApi, openApi.id, new PartitionKey("Api"));
- strMsg.Append($"修改开放API接口:名称:{openApi.name}/ID:{openApi.id}/接口:{openApi.url}");
- type = "openApi-update";
- }
- else return Ok(new { state = 404, msg = "未找到该id相关的Api信息" });
- }
- //保存操作记录
- //await _azureStorage.SaveBILog(type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
- await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, type, strMsg.ToString(), _dingDing, httpContext: HttpContext);
-
- return Ok(new { state = 200, openApi });
- }
- catch (Exception e)
- {
- await _dingDing.SendBotMsg($"BI,{_option.Location} , /openapi/set-api \n {e.Message}\n{e.StackTrace} \n ", GroupNames.成都开发測試群組);
- return BadRequest();
- }
- }
- public record ReadApi
- {
- public string id { get; set; }
- public string code { get; set; }
- public string pk { get; set; }
- public string name { get; set; }
- public string rw { get; set; }
- public string method { get; set; }
- public string url { get; set; }
- public string descr { get; set; }
- public int type { get; set; }
- public string descrUrl { get; set; }
- public long createTime { get; set; }
- }
- }
- }
|