diff --git a/OpenAuth.WebApi/Controllers/SystemController.cs b/OpenAuth.WebApi/Controllers/SystemController.cs new file mode 100644 index 00000000..ca3659d2 --- /dev/null +++ b/OpenAuth.WebApi/Controllers/SystemController.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Reflection; +using System.Threading.Tasks; +using Infrastructure; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.Extensions.Configuration; +using Microsoft.OpenApi.Readers; + +[ApiController] +[Route("api/[controller]")] +[ApiExplorerSettings(GroupName = "系统管理_System")] +public class SystemController : ControllerBase +{ + private readonly IHttpClientFactory _httpClientFactory; + private readonly IConfiguration _configuration; + + public SystemController(IHttpClientFactory httpClientFactory + , IConfiguration configuration) + { + _httpClientFactory = httpClientFactory; + _configuration = configuration; + } + + /// + /// 获取所有API接口信息 + /// + /// + [HttpGet] + [AllowAnonymous] + public async Task>> Get() + { + var result = new Response>(); + try + { + var apis = await GetSwaggerEndpoints(); + + result.Result = apis; + } + catch (Exception ex) + { + result.Code = 500; + result.Message = ex.InnerException?.Message ?? ex.Message; + } + + return result; + } + + /// + /// 获取所有API接口信息 + /// + private async Task> GetSwaggerEndpoints() + { + var reader = new OpenApiStringReader(); + var client = _httpClientFactory.CreateClient(); + + var baseUrl = _configuration["AppSetting:HttpHost"]?.Replace("*", "localhost"); + var apis = new List(); + foreach (var controller in GetControllers()) + { + var groupname = GetSwaggerGroupName(controller); + + var swaggerJsonUrl = $"{baseUrl}/swagger/{groupname}/swagger.json"; + var response = await client.GetAsync(swaggerJsonUrl); + var content = await response.Content.ReadAsStringAsync(); + var document = reader.Read(content, out var diagnostic); + //获取所有api + apis.AddRange(document.Paths + .SelectMany(path => path.Value.Operations + .Select(op => new SwaggerEndpointInfo( + path.Key, + op.Key.ToString(), + op.Value.Summary, + op.Value.Description, + op.Value.Tags.FirstOrDefault()?.Name)))); + } + + return apis; + } + + /// + /// 获取控制器对应的swagger分组值 + /// + private string GetSwaggerGroupName(Type controller) + { + var groupname = controller.Name.Replace("Controller", ""); + var apisetting = controller.GetCustomAttribute(typeof(ApiExplorerSettingsAttribute)); + if (apisetting != null) + { + groupname = ((ApiExplorerSettingsAttribute)apisetting).GroupName; + } + + return groupname; + } + + /// + /// 获取所有的控制器 + /// + private List GetControllers() + { + Assembly asm = Assembly.GetExecutingAssembly(); + + var controlleractionlist = asm.GetTypes() + .Where(type => typeof(ControllerBase).IsAssignableFrom(type)) + .OrderBy(x => x.Name).ToList(); + return controlleractionlist; + } + + public record SwaggerEndpointInfo( + string Path, + string HttpMethod, + string Summary, + string Description, + string Tag); +} \ No newline at end of file diff --git a/OpenAuth.WebApi/OpenAuth.WebApi.csproj b/OpenAuth.WebApi/OpenAuth.WebApi.csproj index 438f5b7a..264adb41 100644 --- a/OpenAuth.WebApi/OpenAuth.WebApi.csproj +++ b/OpenAuth.WebApi/OpenAuth.WebApi.csproj @@ -32,6 +32,7 @@ +