128 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			128 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C#
		
	
	
using Microsoft.AspNetCore.Builder;
 | 
						||
using Microsoft.AspNetCore.Hosting;
 | 
						||
using Microsoft.AspNetCore.Mvc;
 | 
						||
using Microsoft.Extensions.DependencyInjection;
 | 
						||
using Microsoft.OpenApi.Models;
 | 
						||
using Swashbuckle.AspNetCore.Filters;
 | 
						||
using Swashbuckle.AspNetCore.SwaggerGen;
 | 
						||
using Swashbuckle.AspNetCore.SwaggerUI;
 | 
						||
using System;
 | 
						||
using System.ComponentModel;
 | 
						||
using System.IO;
 | 
						||
using System.Linq;
 | 
						||
using System.Reflection;
 | 
						||
 | 
						||
namespace IRaCIS.Core.API;
 | 
						||
 | 
						||
public enum SwaggerVersion
 | 
						||
{
 | 
						||
    [Description("医生模块")]
 | 
						||
    Reviewer = 1,
 | 
						||
    [Description("项目模块")]
 | 
						||
    Trial = 2,
 | 
						||
    [Description("入组模块")]
 | 
						||
    Enroll = 3,
 | 
						||
    [Description("工作量模块")]
 | 
						||
    Workload = 4,
 | 
						||
    [Description("通用信息获取")]
 | 
						||
    Common = 5,
 | 
						||
    [Description("机构信息模块")]
 | 
						||
    Institution = 6,
 | 
						||
    [Description("统计模块")]
 | 
						||
    DashboardStatistics = 7,
 | 
						||
    [Description("财务模块")]
 | 
						||
    Financial = 8,
 | 
						||
    [Description("管理模块")]
 | 
						||
    Management =9,
 | 
						||
    [Description("影像模块")]
 | 
						||
    Image =10,
 | 
						||
    [Description("读片模块")]
 | 
						||
    Reading =11
 | 
						||
};
 | 
						||
 | 
						||
 | 
						||
public static class SwaggerSetup
 | 
						||
{
 | 
						||
    public static void AddSwaggerSetup(this IServiceCollection services)
 | 
						||
    {
 | 
						||
        services.AddEndpointsApiExplorer();
 | 
						||
        services.AddSwaggerGen(options =>
 | 
						||
        {
 | 
						||
 | 
						||
            typeof(SwaggerVersion).GetFields(BindingFlags.Public | BindingFlags.Static).ToList()
 | 
						||
             .ForEach(field =>
 | 
						||
             {
 | 
						||
                 var description = field.GetCustomAttribute<DescriptionAttribute>()?.Description ?? field.Name;
 | 
						||
                 options.SwaggerDoc(field.Name, new Microsoft.OpenApi.Models.OpenApiInfo
 | 
						||
                 {
 | 
						||
                     Version = field.Name,
 | 
						||
                     Description = $"{field.Name} API",
 | 
						||
                     Title = description // 使用Description作为Title
 | 
						||
                 });
 | 
						||
             });
 | 
						||
 | 
						||
            // 接口排序
 | 
						||
            options.OrderActionsBy(o => o.GroupName);
 | 
						||
 | 
						||
            //添加注释
 | 
						||
            var basePath = AppContext.BaseDirectory;
 | 
						||
            var xmlPath1 = Path.Combine(basePath, "IRaCIS.Core.Application.xml");
 | 
						||
            var xmlPath2 = Path.Combine(basePath, "IRaCIS.Core.API.xml");
 | 
						||
            options.IncludeXmlComments(xmlPath1, true);
 | 
						||
            options.IncludeXmlComments(xmlPath2, true);
 | 
						||
 | 
						||
            // 在header中添加token,传递到后台
 | 
						||
            options.OperationFilter<SecurityRequirementsOperationFilter>();
 | 
						||
 | 
						||
 | 
						||
            // 添加登录按钮
 | 
						||
            options.AddSecurityDefinition("bearerAuth", new OpenApiSecurityScheme()
 | 
						||
            {
 | 
						||
                Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
 | 
						||
                Name = "Authorization",
 | 
						||
                //In = "header",
 | 
						||
                //Type = "apiKey"
 | 
						||
            });
 | 
						||
 | 
						||
        });
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
 | 
						||
    public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
 | 
						||
    {
 | 
						||
        app.UseSwagger();
 | 
						||
        app.UseSwaggerUI(options =>
 | 
						||
        {
 | 
						||
            typeof(SwaggerVersion).GetFields(BindingFlags.Public | BindingFlags.Static).ToList()
 | 
						||
            .ForEach(field =>
 | 
						||
            {
 | 
						||
                var description = field.GetCustomAttribute<DescriptionAttribute>()?.Description ?? field.Name;
 | 
						||
                options.SwaggerEndpoint($"swagger/{field.Name}/swagger.json", $"{description}");
 | 
						||
            });
 | 
						||
 | 
						||
 | 
						||
            var data = Assembly.GetExecutingAssembly().Location;
 | 
						||
            options.IndexStream = () => Assembly.GetExecutingAssembly()
 | 
						||
            .GetManifestResourceStream("IRaCIS.Core.API.wwwroot.swagger.ui.Index.html");
 | 
						||
 | 
						||
            //路径配置,设置为空,表示直接在根域名(localhost:8001)访问该文件,
 | 
						||
            //注意localhost:8001/swagger是访问不到的,去launchSettings.json把launchUrl去掉,如果你想换一个路径,直接写名字即可,比如直接写c.Route = "doc";
 | 
						||
            options.RoutePrefix = string.Empty;
 | 
						||
 | 
						||
            //DocExpansion设置为none可折叠所有方法
 | 
						||
            options.DocExpansion(DocExpansion.None);
 | 
						||
 | 
						||
            //DefaultModelsExpandDepth设置为 - 1 可不显示models
 | 
						||
            options.DefaultModelsExpandDepth(-1);
 | 
						||
        });
 | 
						||
    }
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
}
 |