From f43b7df8400f4fcffc9f19dca0888d61e2b33d5f Mon Sep 17 00:00:00 2001 From: dengjunjie <dengjunjie@hnkhzn.com> Date: 星期三, 12 三月 2025 18:41:52 +0800 Subject: [PATCH] WMS系统添加PDA权限,PDA程序 --- 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_Core/Extensions/SwaggerSetup.cs | 132 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 132 insertions(+), 0 deletions(-) diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Extensions/SwaggerSetup.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Extensions/SwaggerSetup.cs" new file mode 100644 index 0000000..3037073 --- /dev/null +++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_Core/Extensions/SwaggerSetup.cs" @@ -0,0 +1,132 @@ +锘� +using Microsoft.Extensions.DependencyInjection; +using Microsoft.OpenApi.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; +using WIDESEA_Core.Helper; +using static WIDESEA_Core.Extensions.CustomApiVersion; +using Swashbuckle.AspNetCore.Filters; + +namespace WIDESEA_Core.Extensions +{ + /// <summary> + /// Swagger + /// </summary> + public static class SwaggerSetup + { + /// <summary> + /// Swagger + /// </summary> + /// <param name="services"></param> + /// <exception cref="ArgumentNullException"></exception> + public static void AddSwaggerSetup(this IServiceCollection services) + { + if (services == null) throw new ArgumentNullException(nameof(services)); + + var basePath = AppDomain.CurrentDomain.BaseDirectory; + //var basePath2 = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath; + var ApiName = AppSettings.app(new string[] { "ApiName" }); + + services.AddSwaggerGen(c => + { + //閬嶅巻鍑哄叏閮ㄧ殑鐗堟湰锛屽仛鏂囨。淇℃伅灞曠ず + typeof(ApiVersions).GetEnumNames().ToList().ForEach(version => + { + c.SwaggerDoc(version, new OpenApiInfo + { + Version = version, + Title = $"{ApiName} 鎺ュ彛鏂囨。鈥斺�攞RuntimeInformation.FrameworkDescription}", + Description = $"{ApiName} HTTP API " + version, + //Contact = new OpenApiContact { Name = ApiName, Email = "Blog.Core@xxx.com", Url = new Uri("https://neters.club") }, + //License = new OpenApiLicense { Name = ApiName + " 瀹樻柟鏂囨。", Url = new Uri("http://apk.neters.club/.doc/") } + }); + //c.OrderActionsBy(o => o.RelativePath); + }); + + c.UseInlineDefinitionsForEnums(); + try + { + var xmlPath = Path.Combine(basePath, "WIDESEA_WMSServer.xml"); + //榛樿鐨勭浜屼釜鍙傛暟鏄痜alse锛岃繖涓槸controller鐨勬敞閲婏紝璁板緱淇敼 + c.IncludeXmlComments(xmlPath, true); + + var xmlModelPath = Path.Combine(basePath, "WIDESEA_Model.xml"); + c.IncludeXmlComments(xmlModelPath); + + var xmlDTOPath = Path.Combine(basePath, "WIDESEA_DTO.xml"); + c.IncludeXmlComments(xmlDTOPath); + } + catch (Exception ex) + { + //log.Error("Blog.Core.xml鍜孊log.Core.Model.xml 涓㈠け锛岃妫�鏌ュ苟鎷疯礉銆俓n" + ex.Message); + } + + // 寮�鍚姞鏉冨皬閿� + c.OperationFilter<AddResponseHeadersFilter>(); + c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>(); + + // 鍦╤eader涓坊鍔爐oken锛屼紶閫掑埌鍚庡彴 + //c.OperationFilter<SecurityRequirementsOperationFilter>(); + + c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme + { + Description = "JWT鎺堟潈token鍓嶉潰闇�瑕佸姞涓婂瓧娈礏earer涓庝竴涓┖鏍�,濡侭earer token", + Name = "Authorization", + In = ParameterLocation.Header, + Type = SecuritySchemeType.ApiKey, + BearerFormat = "JWT", + Scheme = "Bearer" + }); + + c.AddSecurityRequirement(new OpenApiSecurityRequirement + { + { + new OpenApiSecurityScheme + { + Reference = new OpenApiReference { + Type = ReferenceType.SecurityScheme, + Id = "Bearer" + } + }, + new string[] { } + } + }); + }).AddControllers() + .ConfigureApiBehaviorOptions(options => + { + options.SuppressConsumesConstraintForFormFileParameters = true; + options.SuppressInferBindingSourcesForParameters = true; + options.SuppressModelStateInvalidFilter = true; + options.SuppressMapClientErrors = true; + options.ClientErrorMapping[404].Link = + "https://*/404"; + }); + //services.AddSwaggerGenNewtonsoftSupport(); + } + } + + /// <summary> + /// 鑷畾涔夌増鏈� + /// </summary> + public class CustomApiVersion + { + /// <summary> + /// Api鎺ュ彛鐗堟湰 鑷畾涔� + /// </summary> + public enum ApiVersions + { + /// <summary> + /// V1 鐗堟湰 + /// </summary> + V1 = 1, + /// <summary> + /// V2 鐗堟湰 + /// </summary> + V2 = 2, + } + } +} -- Gitblit v1.9.3