Browse Source

1、新增 接口和方法通过特性[Order]从小到大排序
2、新增 appconfig.swagger配置项{
//启用枚举架构过滤器
"enableEnumSchemaFilter": true,
//启用接口和方法排序文档过滤器
"enableOrderTagsDocumentFilter": true,
}
3、admin接口已自定义按功能界面顺序排序

zhontai 2 years ago
parent
commit
c8ddb3c2e2
26 changed files with 144 additions and 18 deletions
  1. 4 0
      src/hosts/ZhonTai.Host/Configs/appconfig.json
  2. 10 0
      src/platform/ZhonTai.Admin/Core/Configs/AppConfig.cs
  3. 1 1
      src/platform/ZhonTai.Admin/Core/Extensions/ServiceCollectionExtensions.cs
  4. 3 0
      src/platform/ZhonTai.Admin/Core/Filters/EnumSchemaFilter.cs
  5. 36 0
      src/platform/ZhonTai.Admin/Core/Filters/OrderTagsDocumentFilter.cs
  6. 19 3
      src/platform/ZhonTai.Admin/Core/HostApp.cs
  7. 4 4
      src/platform/ZhonTai.Admin/Core/RegisterModules/RegisterModule.cs
  8. 1 1
      src/platform/ZhonTai.Admin/Services/Api/ApiService.cs
  9. 1 0
      src/platform/ZhonTai.Admin/Services/Cache/CacheService.cs
  10. 1 0
      src/platform/ZhonTai.Admin/Services/Dictionary/DictionaryService.cs
  11. 1 0
      src/platform/ZhonTai.Admin/Services/DictionaryType/DictionaryTypeService.cs
  12. 1 0
      src/platform/ZhonTai.Admin/Services/Document/DocumentService.cs
  13. 1 0
      src/platform/ZhonTai.Admin/Services/File/FileService.cs
  14. 1 0
      src/platform/ZhonTai.Admin/Services/LoginLog/LoginLogService.cs
  15. 1 0
      src/platform/ZhonTai.Admin/Services/OprationLog/OprationLogService.cs
  16. 1 0
      src/platform/ZhonTai.Admin/Services/Org/OrgService.cs
  17. 1 0
      src/platform/ZhonTai.Admin/Services/Permission/PermissionService.cs
  18. 1 0
      src/platform/ZhonTai.Admin/Services/Role/RoleService.cs
  19. 1 0
      src/platform/ZhonTai.Admin/Services/TaskScheduler/TaskLogService.cs
  20. 1 0
      src/platform/ZhonTai.Admin/Services/TaskScheduler/TaskService.cs
  21. 1 0
      src/platform/ZhonTai.Admin/Services/Tenant/TenantService.cs
  22. 1 0
      src/platform/ZhonTai.Admin/Services/User/UserService.cs
  23. 1 0
      src/platform/ZhonTai.Admin/Services/View/ViewService.cs
  24. 9 9
      src/platform/ZhonTai.Admin/ZhonTai.Admin.csproj
  25. 20 0
      src/platform/ZhonTai.Admin/ZhonTai.Admin.xml
  26. 22 0
      src/platform/ZhonTai.DynamicApi/Attributes/OrderAttribute.cs

+ 4 - 0
src/hosts/ZhonTai.Host/Configs/appconfig.json

@@ -22,6 +22,10 @@
   "swagger": {
     //启用
     "enable": true,
+    //启用枚举架构过滤器
+    "enableEnumSchemaFilter": true,
+    //启用接口排序文档过滤器
+    "enableOrderTagsDocumentFilter": true,
     //路由前缀,如配置微服务文档地址:doc/module/swagger
     "routePrefix": "swagger",
     //地址

+ 10 - 0
src/platform/ZhonTai.Admin/Core/Configs/AppConfig.cs

@@ -101,6 +101,16 @@ public class SwaggerConfig
     /// </summary>
     public bool Enable { get; set; } = false;
 
+    /// <summary>
+    /// 启用枚举架构过滤器
+    /// </summary>
+    public bool EnableEnumSchemaFilter { get; set; } = true;
+
+    /// <summary>
+    /// 启用接口排序文档过滤器
+    /// </summary>
+    public bool EnableOrderTagsDocumentFilter { get; set; } = true;
+
     private string _RoutePrefix = "swagger";
     /// <summary>
     /// 访问地址

+ 1 - 1
src/platform/ZhonTai.Admin/Core/Extensions/ServiceCollectionExtensions.cs

@@ -59,11 +59,11 @@ public static class ServiceCollectionExtensions
                     {
                         option.Provider = oSSOptions.Provider;
                         option.Endpoint = oSSOptions.Endpoint;
+                        option.Region = oSSOptions.Region;
                         option.AccessKey = oSSOptions.AccessKey;
                         option.SecretKey = oSSOptions.SecretKey;
                         option.IsEnableHttps = oSSOptions.IsEnableHttps;
                         option.IsEnableCache = oSSOptions.IsEnableCache;
-                        option.Region = oSSOptions.Region;
                     });
 
                     var oSSServiceFactory = services.BuildServiceProvider().GetRequiredService<IOSSServiceFactory>();

+ 3 - 0
src/platform/ZhonTai.Admin/Core/Filters/EnumSchemaFilter.cs

@@ -6,6 +6,9 @@ using ZhonTai.Common.Extensions;
 
 namespace ZhonTai.Admin.Core.Filters;
 
+/// <summary>
+/// 枚举架构过滤器
+/// </summary>
 public class EnumSchemaFilter : ISchemaFilter
 {
     public void Apply(OpenApiSchema schema, SchemaFilterContext context)

+ 36 - 0
src/platform/ZhonTai.Admin/Core/Filters/OrderTagsDocumentFilter.cs

@@ -0,0 +1,36 @@
+using Microsoft.AspNetCore.Mvc.Controllers;
+using Microsoft.OpenApi.Models;
+using Swashbuckle.AspNetCore.SwaggerGen;
+using System.Collections.Concurrent;
+using System.Linq;
+using ZhonTai.DynamicApi.Attributes;
+
+namespace ZhonTai.Admin.Core.Filters;
+
+/// <summary>
+/// 接口排序文档过滤器
+/// </summary>
+public class OrderTagsDocumentFilter : IDocumentFilter
+{
+    public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
+    {
+        var orderTagList = new ConcurrentDictionary<string, int>();
+        foreach (var apiDescription in context.ApiDescriptions)
+        {
+            var order = 0;
+            var actionDescriptor = apiDescription.ActionDescriptor as ControllerActionDescriptor;
+            var objOrderAttribute = actionDescriptor.EndpointMetadata.FirstOrDefault(x => x is OrderAttribute);
+            if (objOrderAttribute != null)
+            {
+                var orderAttribute = objOrderAttribute as OrderAttribute;
+                order = orderAttribute.Value;
+            }
+            orderTagList.TryAdd(actionDescriptor.ControllerName, order);
+        }
+
+        swaggerDoc.Tags = swaggerDoc.Tags
+                                    .OrderBy(u => orderTagList.TryGetValue(u.Name, out int order) ? order : 0)
+                                    .ThenBy(u => u.Name)
+                                    .ToArray();
+    }
+}

+ 19 - 3
src/platform/ZhonTai.Admin/Core/HostApp.cs

@@ -51,7 +51,6 @@ using ZhonTai.Admin.Services.User;
 using ZhonTai.Admin.Core.Middlewares;
 using ZhonTai.Admin.Core.Dto;
 using ZhonTai.DynamicApi.Attributes;
-using Microsoft.Extensions.FileSystemGlobbing.Internal;
 using System.Text.RegularExpressions;
 
 namespace ZhonTai.Admin.Core;
@@ -322,7 +321,6 @@ public class HostApp
                         Version = project.Version,
                         Description = project.Description
                     });
-                    //c.OrderActionsBy(o => o.RelativePath);
                 });
 
                 options.CustomOperationIds(apiDesc =>
@@ -388,7 +386,25 @@ public class HostApp
                 }
                 options.AddServer(server);
 
-                options.SchemaFilter<EnumSchemaFilter>();
+                if(appConfig.Swagger.EnableEnumSchemaFilter)
+                {
+                    options.SchemaFilter<EnumSchemaFilter>();
+                }
+                if(appConfig.Swagger.EnableOrderTagsDocumentFilter)
+                {
+                    options.DocumentFilter<OrderTagsDocumentFilter>();
+                }
+                options.OrderActionsBy(apiDesc =>
+                {
+                    var order = 0;
+                    var objOrderAttribute = apiDesc.ActionDescriptor.EndpointMetadata.FirstOrDefault(x => x is OrderAttribute);
+                    if (objOrderAttribute != null)
+                    {
+                        var orderAttribute = objOrderAttribute as OrderAttribute;
+                        order = orderAttribute.Value;
+                    }
+                    return (int.MaxValue - order).ToString().PadLeft(int.MaxValue.ToString().Length, '0');
+                });
 
                 #region 添加设置Token的按钮
 

+ 4 - 4
src/platform/ZhonTai.Admin/Core/RegisterModules/RegisterModule.cs

@@ -39,7 +39,7 @@ public class RegisterModule : Module
 
         if(_appConfig.AssemblyNames?.Length > 0)
         {
-            //服务
+            //程序集
             Assembly[] assemblies = DependencyContext.Default.RuntimeLibraries
                 .Where(a => _appConfig.AssemblyNames.Contains(a.Name))
                 .Select(o => Assembly.Load(new AssemblyName(o.Name))).ToArray();
@@ -51,7 +51,7 @@ public class RegisterModule : Module
                 && (a.Name.EndsWith("Service") || a.Name.EndsWith("Repository") || iRegisterIOCType.IsAssignableFrom(a)) 
                 && !a.IsAbstract && !a.IsInterface && a.IsPublic;
 
-            //服务接口实例
+            //接口实例
             builder.RegisterAssemblyTypes(assemblies)
             .Where(Predicate)
             .AsImplementedInterfaces()
@@ -60,7 +60,7 @@ public class RegisterModule : Module
             .InterceptedBy(interceptorServiceTypes.ToArray())
             .EnableInterfaceInterceptors();
 
-            //服务实例
+            //无接口实例
             builder.RegisterAssemblyTypes(assemblies)
             .Where(Predicate)
             .InstancePerLifetimeScope()
@@ -68,7 +68,7 @@ public class RegisterModule : Module
             .InterceptedBy(interceptorServiceTypes.ToArray())
             .EnableClassInterceptors();
 
-            //泛型注入
+            //仓储泛型注入
             builder.RegisterGeneric(typeof(RepositoryBase<>)).As(typeof(IRepositoryBase<>)).InstancePerLifetimeScope().PropertiesAutowired();
             builder.RegisterGeneric(typeof(RepositoryBase<,>)).As(typeof(IRepositoryBase<,>)).InstancePerLifetimeScope().PropertiesAutowired();
         }

+ 1 - 1
src/platform/ZhonTai.Admin/Services/Api/ApiService.cs

@@ -10,13 +10,13 @@ using ZhonTai.Admin.Domain.Api.Dto;
 using ZhonTai.DynamicApi;
 using ZhonTai.DynamicApi.Attributes;
 using ZhonTai.Admin.Core.Consts;
-using SixLabors.ImageSharp.Drawing;
 
 namespace ZhonTai.Admin.Services.Api;
 
 /// <summary>
 /// 接口服务
 /// </summary>
+[Order(90)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class ApiService : BaseService, IApiService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/Cache/CacheService.cs

@@ -17,6 +17,7 @@ namespace ZhonTai.Admin.Services.Cache;
 /// <summary>
 /// 缓存服务
 /// </summary>
+[Order(80)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class CacheService : BaseService, ICacheService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/Dictionary/DictionaryService.cs

@@ -14,6 +14,7 @@ namespace ZhonTai.Admin.Services.Dictionary;
 /// <summary>
 /// 数据字典服务
 /// </summary>
+[Order(60)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class DictionaryService : BaseService, IDictionaryService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/DictionaryType/DictionaryTypeService.cs

@@ -16,6 +16,7 @@ namespace ZhonTai.Admin.Services.DictionaryType;
 /// <summary>
 /// 数据字典类型服务
 /// </summary>
+[Order(61)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class DictionaryTypeService : BaseService, IDictionaryTypeService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/Document/DocumentService.cs

@@ -18,6 +18,7 @@ namespace ZhonTai.Admin.Services.Document;
 /// <summary>
 /// 文档服务
 /// </summary>
+[Order(180)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class DocumentService : BaseService, IDocumentService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/File/FileService.cs

@@ -26,6 +26,7 @@ namespace ZhonTai.Admin.Services.File;
 /// <summary>
 /// 文件服务
 /// </summary>
+[Order(110)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class FileService : BaseService, IFileService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/LoginLog/LoginLogService.cs

@@ -15,6 +15,7 @@ namespace ZhonTai.Admin.Services.LoginLog;
 /// <summary>
 /// 登录日志服务
 /// </summary>
+[Order(190)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class LoginLogService : BaseService, ILoginLogService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/OprationLog/OprationLogService.cs

@@ -15,6 +15,7 @@ namespace ZhonTai.Admin.Services.OprationLog;
 /// <summary>
 /// 操作日志服务
 /// </summary>
+[Order(200)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class OprationLogService : BaseService, IOprationLogService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/Org/OrgService.cs

@@ -16,6 +16,7 @@ namespace ZhonTai.Admin.Services.Org;
 /// <summary>
 /// 部门服务
 /// </summary>
+[Order(30)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class OrgService : BaseService, IOrgService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/Permission/PermissionService.cs

@@ -25,6 +25,7 @@ namespace ZhonTai.Admin.Services.Permission;
 /// <summary>
 /// 权限服务
 /// </summary>
+[Order(40)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class PermissionService : BaseService, IPermissionService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/Role/RoleService.cs

@@ -22,6 +22,7 @@ namespace ZhonTai.Admin.Services.Role;
 /// <summary>
 /// 角色服务
 /// </summary>
+[Order(20)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class RoleService : BaseService, IRoleService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/TaskScheduler/TaskLogService.cs

@@ -13,6 +13,7 @@ namespace ZhonTai.Admin.Services.TaskScheduler;
 /// <summary>
 /// 任务日志服务
 /// </summary>
+[Order(71)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class TaskLogService : BaseService, ITaskLogService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/TaskScheduler/TaskService.cs

@@ -18,6 +18,7 @@ namespace ZhonTai.Admin.Services.TaskScheduler;
 /// <summary>
 /// 任务服务
 /// </summary>
+[Order(70)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class TaskService : BaseService, ITaskService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/Tenant/TenantService.cs

@@ -24,6 +24,7 @@ namespace ZhonTai.Admin.Services.Tenant;
 /// <summary>
 /// 租户服务
 /// </summary>
+[Order(50)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class TenantService : BaseService, ITenantService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/User/UserService.cs

@@ -35,6 +35,7 @@ namespace ZhonTai.Admin.Services.User;
 /// <summary>
 /// 用户服务
 /// </summary>
+[Order(10)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public partial class UserService : BaseService, IUserService, IDynamicApi
 {

+ 1 - 0
src/platform/ZhonTai.Admin/Services/View/ViewService.cs

@@ -14,6 +14,7 @@ namespace ZhonTai.Admin.Services.View;
 /// <summary>
 /// 视图服务
 /// </summary>
+[Order(100)]
 [DynamicApi(Area = AdminConsts.AreaName)]
 public class ViewService : BaseService, IViewService, IDynamicApi
 {

+ 9 - 9
src/platform/ZhonTai.Admin/ZhonTai.Admin.csproj

@@ -16,9 +16,9 @@
 		<PackageReference Include="CSRedisCore" Version="3.8.670" />
 		<PackageReference Include="Caching.CSRedis" Version="3.8.670" />
 		<PackageReference Include="FluentValidation.AspNetCore" Version="11.2.2" />
-		<PackageReference Include="FreeSql" Version="3.2.685" />
+		<PackageReference Include="FreeSql" Version="3.2.687" />
 		<PackageReference Include="FreeSql.Cloud" Version="1.6.3" />
-		<PackageReference Include="FreeSql.Repository" Version="3.2.685" />
+		<PackageReference Include="FreeSql.Repository" Version="3.2.687" />
 		<PackageReference Include="FreeScheduler" Version="1.0.9" />
 		<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" />
 		<PackageReference Include="Mapster" Version="7.3.0" />
@@ -31,19 +31,19 @@
 		<PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
 		<PackageReference Include="SixLabors.ImageSharp" Version="2.1.3" />
 		<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta14" />
-		<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.25.1" />
+		<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.26.0" />
 		<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
 		<PackageReference Include="UAParser" Version="3.1.47" />
 		<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
 	</ItemGroup>
 
 	<ItemGroup Condition="'$(Configuration)'=='Debug'">
-		<PackageReference Include="FreeSql.Provider.MySql" Version="3.2.685" />
-		<PackageReference Include="FreeSql.Provider.SqlServer" Version="3.2.685" />
-		<PackageReference Include="FreeSql.Provider.PostgreSQL" Version="3.2.685" />
-		<PackageReference Include="FreeSql.Provider.Oracle" Version="3.2.685" />
-		<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.685" />
-		<PackageReference Include="FreeSql.Provider.MySqlConnector" Version="3.2.685" />
+		<PackageReference Include="FreeSql.Provider.MySql" Version="3.2.687" />
+		<PackageReference Include="FreeSql.Provider.SqlServer" Version="3.2.687" />
+		<PackageReference Include="FreeSql.Provider.PostgreSQL" Version="3.2.687" />
+		<PackageReference Include="FreeSql.Provider.Oracle" Version="3.2.687" />
+		<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.687" />
+		<PackageReference Include="FreeSql.Provider.MySqlConnector" Version="3.2.687" />
 	</ItemGroup>
 
 	<ItemGroup>

+ 20 - 0
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -381,6 +381,16 @@
             启用
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.SwaggerConfig.EnableEnumSchemaFilter">
+            <summary>
+            启用枚举架构过滤器
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.SwaggerConfig.EnableOrderTagsDocumentFilter">
+            <summary>
+            启用接口排序文档过滤器
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Core.Configs.SwaggerConfig.RoutePrefix">
             <summary>
             访问地址
@@ -1635,11 +1645,21 @@
             控制器操作日志记录
             </summary>
         </member>
+        <member name="T:ZhonTai.Admin.Core.Filters.EnumSchemaFilter">
+            <summary>
+            枚举架构过滤器
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Core.Filters.FormatResultFilter">
             <summary>
             结果格式化过滤器
             </summary>
         </member>
+        <member name="T:ZhonTai.Admin.Core.Filters.OrderTagsDocumentFilter">
+            <summary>
+            接口排序文档过滤器
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Core.Filters.ValidateInputFilter">
             <summary>
             输入模型验证过滤器

+ 22 - 0
src/platform/ZhonTai.DynamicApi/Attributes/OrderAttribute.cs

@@ -0,0 +1,22 @@
+using System;
+
+namespace ZhonTai.DynamicApi.Attributes;
+
+[Serializable]
+[AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Method)]
+public class OrderAttribute : Attribute
+{
+    /// <summary>
+    /// 排序值
+    /// </summary>
+    public int Value { get; set; } = 0;
+
+    /// <summary>
+    /// 排序设置
+    /// </summary>
+    /// <param name="value">排序值</param>
+    public OrderAttribute(int value)
+    {
+        Value = value;
+    }
+}