|
|
using EntrustSettle.Common.Const;
|
|
|
using EntrustSettle.Common.Extensions;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Microsoft.OpenApi.Any;
|
|
|
using Microsoft.OpenApi.Models;
|
|
|
using NLog;
|
|
|
using Swashbuckle.AspNetCore.Filters;
|
|
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
|
|
|
namespace EntrustSettle.Extensions
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// Swagger 启动服务
|
|
|
/// </summary>
|
|
|
public static class SwaggerSetup
|
|
|
{
|
|
|
public class SwaggerApiInfo
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// URL前缀
|
|
|
/// </summary>
|
|
|
public string UrlPrefix { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// 名称
|
|
|
/// </summary>
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
|
/// <see cref="Microsoft.OpenApi.Models.OpenApiInfo"/>
|
|
|
/// </summary>
|
|
|
public OpenApiInfo OpenApiInfo { get; set; }
|
|
|
}
|
|
|
public static List<SwaggerApiInfo> ApiInfos = new()
|
|
|
{
|
|
|
new SwaggerApiInfo(){
|
|
|
UrlPrefix = ApiGroupNameConst.Business,
|
|
|
Name = "业务Api列表",
|
|
|
OpenApiInfo = new (){
|
|
|
Version = "v1",
|
|
|
Title = "业务Api",
|
|
|
Description = "用于实现业务逻辑的接口列表",
|
|
|
}
|
|
|
},
|
|
|
new SwaggerApiInfo(){
|
|
|
UrlPrefix = ApiGroupNameConst.System,
|
|
|
Name = "系统Api列表",
|
|
|
OpenApiInfo = new (){
|
|
|
Version = "v1",
|
|
|
Title = "系统Api",
|
|
|
Description = "用于支持框架本身的接口列表",
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
public static void AddSwaggerSetup(this IServiceCollection services)
|
|
|
{
|
|
|
if (services == null) throw new ArgumentNullException(nameof(services));
|
|
|
|
|
|
var basePath = AppContext.BaseDirectory;
|
|
|
//var basePath2 = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath;
|
|
|
|
|
|
services.AddSwaggerGen(c =>
|
|
|
{
|
|
|
ApiInfos.ForEach(x =>
|
|
|
{
|
|
|
c.SwaggerDoc(x.UrlPrefix, x.OpenApiInfo);
|
|
|
});
|
|
|
c.OrderActionsBy(o => o.RelativePath);
|
|
|
|
|
|
c.UseInlineDefinitionsForEnums();
|
|
|
try
|
|
|
{
|
|
|
//这个就是刚刚配置的xml文件名
|
|
|
var xmlPath = Path.Combine(basePath, "EntrustSettle.Api.xml");
|
|
|
//默认的第二个参数是false,这个是controller的注释,记得修改
|
|
|
c.IncludeXmlComments(xmlPath, true);
|
|
|
|
|
|
//这个就是Model层的xml文件名
|
|
|
var xmlModelPath = Path.Combine(basePath, "EntrustSettle.Model.xml");
|
|
|
c.IncludeXmlComments(xmlModelPath);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
LogManager.GetCurrentClassLogger().Error(ex, "EntrustSettle.Api.xml和EntrustSettle.Model.xml 丢失,请检查并拷贝。");
|
|
|
}
|
|
|
|
|
|
// 开启加权小锁
|
|
|
c.OperationFilter<AddResponseHeadersFilter>();
|
|
|
c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
|
|
|
|
|
|
// 在header中添加token,传递到后台
|
|
|
c.OperationFilter<SecurityRequirementsOperationFilter>();
|
|
|
|
|
|
c.SchemaFilter<EnumSchemaFilter>();
|
|
|
|
|
|
// ids4和jwt切换
|
|
|
//if (Permissions.IsUseIds4)
|
|
|
//{
|
|
|
// //接入identityserver4
|
|
|
// c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
|
|
|
// {
|
|
|
// Type = SecuritySchemeType.OAuth2,
|
|
|
// Flows = new OpenApiOAuthFlows
|
|
|
// {
|
|
|
// Implicit = new OpenApiOAuthFlow
|
|
|
// {
|
|
|
// AuthorizationUrl = new Uri($"{AppSettings.app(new string[] { "Startup", "IdentityServer4", "AuthorizationUrl" })}/connect/authorize"),
|
|
|
// Scopes = new Dictionary<string, string>
|
|
|
// {
|
|
|
// {
|
|
|
// "entrust.settle.api", "ApiResource id"
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
// });
|
|
|
//}
|
|
|
//else
|
|
|
{
|
|
|
// Jwt Bearer 认证,必须是 oauth2
|
|
|
c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
|
|
|
{
|
|
|
Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)\"",
|
|
|
Name = "Authorization", //jwt默认的参数名称
|
|
|
In = ParameterLocation.Header, //jwt默认存放Authorization信息的位置(请求头中)
|
|
|
Type = SecuritySchemeType.ApiKey
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
services.AddSwaggerGenNewtonsoftSupport();
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// Swagger文档枚举字段显示枚举属性和枚举值,以及枚举描述
|
|
|
/// </summary>
|
|
|
public class EnumSchemaFilter : ISchemaFilter
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 实现接口
|
|
|
/// </summary>
|
|
|
/// <param name="model"></param>
|
|
|
/// <param name="context"></param>
|
|
|
|
|
|
public void Apply(OpenApiSchema model, SchemaFilterContext context)
|
|
|
{
|
|
|
if (context.Type.IsEnum || Nullable.GetUnderlyingType(context.Type)?.IsEnum == true)
|
|
|
{
|
|
|
model.Enum.Clear();
|
|
|
|
|
|
Type type;
|
|
|
if (Nullable.GetUnderlyingType(context.Type) != null)
|
|
|
{
|
|
|
type = Nullable.GetUnderlyingType(context.Type);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
type = context.Type;
|
|
|
}
|
|
|
List<string> names = Enum.GetNames(type).ToList();
|
|
|
|
|
|
names.ForEach(name =>
|
|
|
{
|
|
|
Enum e = (Enum)Enum.Parse(type, name);
|
|
|
string value = $"{name}({e.EnumDescription()})={Convert.ToInt64(Enum.Parse(type, name))}";
|
|
|
OpenApiString api = new OpenApiString(value);
|
|
|
model.Enum.Add(api);
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
} |