username@email.com
2024-09-09 626943b5ba84ce44bc19f4c3b8e8e94638bec733
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/***********************************************************************
 *            Project: baifenBinfa.Net                                     *
 *                Web: https://baifenBinfa.com                             *
 *        ProjectName: 百分兵法管理系统                               *
 *             Author:                                        *
 *              Email:                               *
 *           Versions: 1.0                                             *
 *         CreateTime: 2020-02-02 18:41:38
 *           FileName: SwaggerSetup
 *   ClassDescription: 
 ***********************************************************************/
 
 
using System;
using System.IO;
using System.Linq;
using CoreCms.Net.Loging;
using CoreCms.Net.Swagger;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.Filters;
 
namespace CoreCms.Net.Core.Config
{
    public static class SwaggerSetup
    {
 
        public static void AddAdminSwaggerSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            var apiName = "核心商城系统管理端";
 
            services.AddSwaggerGen((s) =>
            {
                //遍历出全部的版本,做文档信息展示
                typeof(CustomApiVersion.ApiVersions).GetEnumNames().ToList().ForEach(version =>
                {
                    s.SwaggerDoc(version, new OpenApiInfo
                    {
                        Version = version,
                        Title = $"{apiName} 接口文档",
                        Description = $"{apiName} HTTP API " + version,
                        Contact = new OpenApiContact { Name = apiName, Email = "34161541@qq.com", Url = new Uri("https://baifenBinfa.com") },
                    });
                    s.OrderActionsBy(o => o.RelativePath);
                });
 
                try
                {
                    //生成API XML文档
                    var basePath = AppContext.BaseDirectory;
                    var xmlPath = Path.Combine(basePath, "CoreCms.Net.Web.Admin.xml");
                    s.IncludeXmlComments(xmlPath);
 
                    var xmlModelPath = Path.Combine(basePath, "CoreCms.Net.Model.xml");
                    s.IncludeXmlComments(xmlModelPath);
 
                }
                catch (Exception ex)
                {
                    NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Swagger, "Swagger", "Swagger生成失败,Doc.xml丢失,请检查并拷贝。", ex);
                }
 
                // 开启加权小锁
                s.OperationFilter<AddResponseHeadersFilter>();
                s.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
 
                // 在header中添加token,传递到后台
                s.OperationFilter<SecurityRequirementsOperationFilter>();
 
                // 必须是 oauth2
                s.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                {
                    Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)\"",
                    Name = "Authorization",//jwt默认的参数名称
                    In = ParameterLocation.Header,//jwt默认存放Authorization信息的位置(请求头中)
                    Type = SecuritySchemeType.ApiKey
                });
 
            });
        }
 
 
        public static void AddClientSwaggerSetup(this IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
            var apiName = "核心商城系统接口端";
 
            services.AddSwaggerGen((s) =>
            {
                //遍历出全部的版本,做文档信息展示
                typeof(CustomApiVersion.ApiVersions).GetEnumNames().ToList().ForEach(version =>
                {
                    s.SwaggerDoc(version, new OpenApiInfo
                    {
                        Version = version,
                        Title = $"{apiName} 接口文档",
                        Description = $"{apiName} HTTP API " + version,
                        Contact = new OpenApiContact { Name = apiName, Email = "34161541@qq.com", Url = new Uri("https://baifenBinfa.com") },
                    });
                    s.OrderActionsBy(o => o.RelativePath);
                });
 
                try
                {
                    //生成API XML文档
                    var basePath = AppContext.BaseDirectory;
                    var xmlPath = Path.Combine(basePath, "CoreCms.Net.Web.WebApi.xml");
                    s.IncludeXmlComments(xmlPath);
 
                    var xmlModelPath = Path.Combine(basePath, "CoreCms.Net.Model.xml");
                    s.IncludeXmlComments(xmlModelPath);
 
                }
                catch (Exception ex)
                {
                    NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Swagger, "Swagger", "Swagger生成失败,Doc.xml丢失,请检查并拷贝。", ex);
                }
 
                // 开启加权小锁
                s.OperationFilter<AddResponseHeadersFilter>();
                s.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
 
                // 在header中添加token,传递到后台
                s.OperationFilter<SecurityRequirementsOperationFilter>();
 
                // 必须是 oauth2
                s.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                {
                    Description = "JWT授权(数据将在请求头中进行传输) 直接在下框中输入Bearer {token}(注意两者之间是一个空格)\"",
                    Name = "Authorization",//jwt默认的参数名称
                    In = ParameterLocation.Header,//jwt默认存放Authorization信息的位置(请求头中)
                    Type = SecuritySchemeType.ApiKey
                });
 
            });
        }
 
    }
}