diff --git a/Infrastructure/Define.cs b/Infrastructure/Define.cs
index fa4066a3..7be61674 100644
--- a/Infrastructure/Define.cs
+++ b/Infrastructure/Define.cs
@@ -37,6 +37,8 @@
//流程实例知会用户
public const string INSTANCE_NOTICE_USER = "INSTANCE_NOTICE_USER";
//流程实例知会角色
- public const string INSTANCE_NOTICE_ROLE = "INSTANCE_NOTICE_ROLE";
+ public const string INSTANCE_NOTICE_ROLE = "INSTANCE_NOTICE_ROLE";
+
+ public const string API = "API_RESOURCE";
}
}
\ No newline at end of file
diff --git a/OpenAuth.App/OpenAuth.App.csproj b/OpenAuth.App/OpenAuth.App.csproj
index 380c1922..6b714811 100644
--- a/OpenAuth.App/OpenAuth.App.csproj
+++ b/OpenAuth.App/OpenAuth.App.csproj
@@ -27,6 +27,7 @@
+
diff --git a/OpenAuth.App/Resources/ResourceApp.cs b/OpenAuth.App/Resources/ResourceApp.cs
index 55809f14..d24852ab 100644
--- a/OpenAuth.App/Resources/ResourceApp.cs
+++ b/OpenAuth.App/Resources/ResourceApp.cs
@@ -17,6 +17,16 @@ namespace OpenAuth.App
public class ResourceApp:SqlSugarBaseApp
{
private RevelanceManagerApp _revelanceApp;
+ private ApiService _apiService;
+
+ private IAuth _auth;
+
+ public ResourceApp(ISqlSugarClient client, IAuth auth, RevelanceManagerApp revelanceApp, ApiService apiService) : base(client, auth)
+ {
+ _revelanceApp = revelanceApp;
+ _apiService = apiService;
+ _auth = auth;
+ }
public void Add(AddOrUpdateResReq resource)
{
@@ -95,9 +105,38 @@ namespace OpenAuth.App
return result;
}
- public ResourceApp(ISqlSugarClient client, IAuth auth, RevelanceManagerApp revelanceApp) : base(client, auth)
+ ///
+ /// 同步站点API到资源列表
+ /// 读取站点API信息,如果资源列表中不存在,则添加
+ ///
+ public async Task Sync()
{
- _revelanceApp = revelanceApp;
+ var apis = await _apiService.GetSwaggerEndpoints();
+ var user = _auth.GetCurrentUser().User;
+ foreach (var api in apis)
+ {
+ //检查资源是否存在
+ var resource = Repository.GetFirst(u => u.Name == api.Path && u.TypeId == Define.API);
+ if (resource != null)
+ {
+ continue;
+ }
+
+ resource = new SysResource
+ {
+ Name = api.Path,
+ Disable = true,
+ SortNo = 0,
+ TypeId = Define.API,
+ TypeName = "API接口",
+ Description = api.Summary??"",
+ CreateTime = DateTime.Now,
+ CreateUserId = user.Id,
+ CreateUserName = user.Name
+ };
+ CaculateCascade(resource);
+ Repository.Insert(resource);
+ }
}
}
diff --git a/OpenAuth.WebApi/Controllers/SystemController.cs b/OpenAuth.App/System/ApiService.cs
similarity index 58%
rename from OpenAuth.WebApi/Controllers/SystemController.cs
rename to OpenAuth.App/System/ApiService.cs
index ca3659d2..13f6214d 100644
--- a/OpenAuth.WebApi/Controllers/SystemController.cs
+++ b/OpenAuth.App/System/ApiService.cs
@@ -4,22 +4,16 @@ 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
+public class ApiService
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IConfiguration _configuration;
- public SystemController(IHttpClientFactory httpClientFactory
- , IConfiguration configuration)
+ public ApiService(IHttpClientFactory httpClientFactory, IConfiguration configuration)
{
_httpClientFactory = httpClientFactory;
_configuration = configuration;
@@ -27,58 +21,43 @@ public class SystemController : ControllerBase
///
/// 获取所有API接口信息
+ /// 这个方法单元测试必须启动WebApi站点
///
- ///
- [HttpGet]
- [AllowAnonymous]
- public async Task>> Get()
+ public async Task> GetSwaggerEndpoints()
{
- 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 controllers = GetControllers();
+
+ foreach (var controller in controllers)
{
var groupname = GetSwaggerGroupName(controller);
-
var swaggerJsonUrl = $"{baseUrl}/swagger/{groupname}/swagger.json";
- var response = await client.GetAsync(swaggerJsonUrl);
- var content = await response.Content.ReadAsStringAsync();
+
+ var response = await client.GetAsync(swaggerJsonUrl).ConfigureAwait(false);
+
+ var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var document = reader.Read(content, out var diagnostic);
//获取所有api
- apis.AddRange(document.Paths
+ var controllerApis = 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))));
+ op.Value.Tags.FirstOrDefault()?.Name)));
+
+ apis.AddRange(controllerApis);
}
return apis;
+
}
///
@@ -101,18 +80,20 @@ public class SystemController : ControllerBase
///
private List GetControllers()
{
- Assembly asm = Assembly.GetExecutingAssembly();
+ var webApiAssembly = AppDomain.CurrentDomain.GetAssemblies()
+ .FirstOrDefault(a => a.GetName().Name.Contains("OpenAuth.WebApi"));
+
+ var controlleractionlist = webApiAssembly.GetTypes()
+ .Where(type => typeof(Microsoft.AspNetCore.Mvc.ControllerBase).IsAssignableFrom(type))
+ .ToList();
- 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
+public record SwaggerEndpointInfo(
+ string Path,
+ string HttpMethod,
+ string Summary,
+ string Description,
+ string Tag);
\ No newline at end of file
diff --git a/OpenAuth.WebApi/Controllers/ResourcesController.cs b/OpenAuth.WebApi/Controllers/ResourcesController.cs
index 15ef6948..dd1ea330 100644
--- a/OpenAuth.WebApi/Controllers/ResourcesController.cs
+++ b/OpenAuth.WebApi/Controllers/ResourcesController.cs
@@ -1,4 +1,4 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@@ -22,7 +22,7 @@ namespace OpenAuth.WebApi.Controllers
{
private readonly ResourceApp _app;
- public ResourcesController(IAuth authUtil, ResourceApp app)
+ public ResourcesController(ResourceApp app)
{
_app = app;
}
@@ -32,6 +32,28 @@ namespace OpenAuth.WebApi.Controllers
return await _app.Load(request);
}
+ ///
+ /// 同步站点API到资源列表
+ /// 读取站点API信息,如果资源列表中不存在,则添加
+ ///
+ [HttpPost]
+ public async Task Sync()
+ {
+ var result = new Response();
+ try
+ {
+ await _app.Sync();
+ return result;
+ }
+ catch (Exception ex)
+ {
+ result.Code = 500;
+ result.Message = ex.InnerException?.Message ?? ex.Message;
+ }
+
+ return result;
+ }
+
[HttpPost]
public Response Delete([FromBody]string[] ids)
{