From 05efc14c119a0a59135d0192499730034e922584 Mon Sep 17 00:00:00 2001 From: shanji <18996038927@163.com> Date: Fri, 26 May 2023 12:10:09 +0800 Subject: [PATCH] =?UTF-8?q?=E7=89=88=E6=9C=AC=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ClientVersionValidationMiddleWare.cs | 60 +++++++++++++++++++ BBWY.Server.API/Startup.cs | 6 +- BBWY.Server.API/appsettings.json | 38 ++++-------- BBWY.Server.Model/GlobalConfig.cs | 8 --- 4 files changed, 76 insertions(+), 36 deletions(-) create mode 100644 BBWY.Server.API/Middlewares/ClientVersionValidationMiddleWare.cs diff --git a/BBWY.Server.API/Middlewares/ClientVersionValidationMiddleWare.cs b/BBWY.Server.API/Middlewares/ClientVersionValidationMiddleWare.cs new file mode 100644 index 00000000..1d8d4fcd --- /dev/null +++ b/BBWY.Server.API/Middlewares/ClientVersionValidationMiddleWare.cs @@ -0,0 +1,60 @@ +using BBWY.Common.Models; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Options; +using Microsoft.Extensions.Primitives; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace BBWY.Server.API.Middlewares +{ + public class ClientVersionValidationMiddleWare + { + /// + /// 管道请求委托 + /// + private RequestDelegate _next; + + private IDictionary apiVersionDictionary; + + private IOptionsMonitor> _monitor; + + public ClientVersionValidationMiddleWare(RequestDelegate requestDelegate, IOptionsMonitor> monitor) + { + _next = requestDelegate; + _monitor = monitor; + apiVersionDictionary = new Dictionary(); + } + + public async Task Invoke(HttpContext context) + { + try + { + Console.WriteLine(context.Request.Path); + var apiRequirement = _monitor.CurrentValue.FirstOrDefault(x => x.Api.Equals(context.Request.Path, StringComparison.CurrentCultureIgnoreCase)); + if (apiRequirement != null) + { + if (!context.Request.Headers.TryGetValue("ClientVersion", out StringValues clientVersionStr)) + throw new BusinessException("未读取到ClientVersion"); + if (!int.TryParse(clientVersionStr, out int clientVersion)) + throw new BusinessException("非法ClientVersion"); + if (clientVersion < apiRequirement.MinimumVersion) + throw new BusinessException("当前ClientVersion低于接口最低要求,请升级到最新版"); + } + await _next(context); //调用管道执行下一个中间件 + } + catch + { + throw; + } + } + } + + public class ClientVersionValidationModel + { + public string Api { get; set; } + + public int MinimumVersion { get; set; } + } +} diff --git a/BBWY.Server.API/Startup.cs b/BBWY.Server.API/Startup.cs index 97d916ca..84746551 100644 --- a/BBWY.Server.API/Startup.cs +++ b/BBWY.Server.API/Startup.cs @@ -15,6 +15,8 @@ using Microsoft.OpenApi.Models; using Newtonsoft.Json.Serialization; using QuanTan.SDK.Client; using System; +using System.Collections.Generic; +using System.Configuration; using System.IO; using System.Linq; using System.Reflection; @@ -88,9 +90,9 @@ namespace BBWY.Server.API #region עȭ̽SDK services.AddSingleton(); #endregion - //var stores = Configuration.GetSection("Stores").Get>(); services.Configure(Configuration.GetSection("GlobalSetting")); + services.Configure>(Configuration.GetSection("ApiVersionRequirements")); services.AddMapper(new MappingProfiles()); services.AddSwaggerGen(c => @@ -181,6 +183,8 @@ namespace BBWY.Server.API app.UseCors("cors"); + app.UseMiddleware(); + app.UseAuthorization(); app.UseEndpoints(endpoints => diff --git a/BBWY.Server.API/appsettings.json b/BBWY.Server.API/appsettings.json index 7da7ac42..061c13df 100644 --- a/BBWY.Server.API/appsettings.json +++ b/BBWY.Server.API/appsettings.json @@ -20,34 +20,18 @@ "Platform": 1, //云鼎 "APIHost": "https://yunding.qiyue666.com/" } - ], - "Stores": [ - { - "StoreId": "110007637", - "StoreName": "上海商超B母婴玩具仓2号仓库" - }, - { - "StoreId": "800006819", - "StoreName": "泉州惠安齐越云仓1号库" - }, - { - "StoreId": "110023168", - "StoreName": "广州亚一综合B仓6号库" - }, - { - "StoreId": "110008191", - "StoreName": "广州公共平台仓4号库" - }, - { - "StoreId": "110017918", - "StoreName": "北京公共平台仓26号库" - }, - { - "StoreId": "110013947", - "StoreName": "西安亚一3CA仓2号库" - } ] }, "AllowedSwagger": true, - "Secret": "D96BFA5B-F2AF-45BC-9342-5A55C3F9BBB0" + "Secret": "D96BFA5B-F2AF-45BC-9342-5A55C3F9BBB0", + "ApiVersionRequirements": [ + { + "Api": "/api/batchpurchase/previeworder", + "MinimumVersion": 10109 + }, + { + "Api": "/api/batchpurchase/batchcreateorder", + "MinimumVersion": 10109 + } + ] } diff --git a/BBWY.Server.Model/GlobalConfig.cs b/BBWY.Server.Model/GlobalConfig.cs index 1f5dbdbc..f4c6c87c 100644 --- a/BBWY.Server.Model/GlobalConfig.cs +++ b/BBWY.Server.Model/GlobalConfig.cs @@ -10,7 +10,6 @@ namespace BBWY.Server.Model public IList PlatformAPIs { get; set; } - public IList Stores { get; set; } } public class PlatformSetting @@ -19,11 +18,4 @@ namespace BBWY.Server.Model public string APIHost { get; set; } } - - public class StoreSetting - { - public string StoreId { get; set; } - - public string StoreName { get; set; } - } }