From 2afddc36f1feb5921e9888b2149cb82f14da8a06 Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Thu, 25 May 2023 01:20:08 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E6=8E=A5=E5=8F=A3=E9=99=90?= =?UTF-8?q?=E5=88=B6=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BBWYB.Client/APIServices/BaseApiService.cs | 4 +- BBWYB.Client/GlobalContext.cs | 8 +++ BBWYB.Client/Views/MainWindow.xaml | 2 +- .../ClientVersionValidationMiddleWare.cs | 55 +++++++++++++++++++ BBWYB.Server.API/Program.cs | 2 + BBWYB.Server.API/appsettings.json | 9 ++- 6 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 BBWYB.Server.API/Middlewares/ClientVersionValidationMiddleWare.cs diff --git a/BBWYB.Client/APIServices/BaseApiService.cs b/BBWYB.Client/APIServices/BaseApiService.cs index 747db0f..1ff52e0 100644 --- a/BBWYB.Client/APIServices/BaseApiService.cs +++ b/BBWYB.Client/APIServices/BaseApiService.cs @@ -32,9 +32,9 @@ namespace BBWYB.Client.APIServices if (headers == null) headers = new Dictionary(); if (!headers.ContainsKey("ClientCode")) - headers.Add("ClientCode", "BBWY"); + headers.Add("ClientCode", "BBWYB"); if (!headers.ContainsKey("ClientVersion")) - headers.Add("ClientVersion", "1.0.0.0"); + headers.Add("ClientVersion", globalContext.BBWYBApiVersion); if (!headers.ContainsKey("Authorization") && !string.IsNullOrEmpty(globalContext.UserToken)) headers.Add("Authorization", $"Bearer {globalContext.UserToken}"); if (!headers.ContainsKey("qy")) diff --git a/BBWYB.Client/GlobalContext.cs b/BBWYB.Client/GlobalContext.cs index becd36d..9a6e868 100644 --- a/BBWYB.Client/GlobalContext.cs +++ b/BBWYB.Client/GlobalContext.cs @@ -13,6 +13,7 @@ namespace BBWYB.Client { public GlobalContext() { + BBWYBApiVersion = "10018"; } private User user; @@ -35,10 +36,17 @@ namespace BBWYB.Client public string BBWYCApiHost { get; set; } public string QKApiHost { get; set; } + public string BBWYBApiVersion { get; set; } + public string GetUserString() { return JsonConvert.SerializeObject(User); } + + public string GetBBWYBApiVersion() + { + return BBWYBApiVersion; + } #endregion } } \ No newline at end of file diff --git a/BBWYB.Client/Views/MainWindow.xaml b/BBWYB.Client/Views/MainWindow.xaml index 0e9be0c..a72f884 100644 --- a/BBWYB.Client/Views/MainWindow.xaml +++ b/BBWYB.Client/Views/MainWindow.xaml @@ -24,7 +24,7 @@ - + diff --git a/BBWYB.Server.API/Middlewares/ClientVersionValidationMiddleWare.cs b/BBWYB.Server.API/Middlewares/ClientVersionValidationMiddleWare.cs new file mode 100644 index 0000000..cdd75c1 --- /dev/null +++ b/BBWYB.Server.API/Middlewares/ClientVersionValidationMiddleWare.cs @@ -0,0 +1,55 @@ +using BBWYB.Common.Models; +using Microsoft.Extensions.Options; +using Microsoft.Extensions.Primitives; + +namespace BBWYB.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/BBWYB.Server.API/Program.cs b/BBWYB.Server.API/Program.cs index 1da4090..76cd58e 100644 --- a/BBWYB.Server.API/Program.cs +++ b/BBWYB.Server.API/Program.cs @@ -63,6 +63,7 @@ services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddMapper(new MappingProfiles()); +services.Configure>(configuration.GetSection("ApiVersionRequirements")); services.AddControllers(c => { @@ -164,6 +165,7 @@ if (isAllowedSwagger) } //app.UseHttpsRedirection(); app.UseMiddleware(); +app.UseMiddleware(); app.UseRouting(); app.UseCors("cors"); app.UseAuthorization(); diff --git a/BBWYB.Server.API/appsettings.json b/BBWYB.Server.API/appsettings.json index 503362a..21910ce 100644 --- a/BBWYB.Server.API/appsettings.json +++ b/BBWYB.Server.API/appsettings.json @@ -13,5 +13,12 @@ "MDSDB": "data source=rm-bp1508okrh23710yfao.mysql.rds.aliyuncs.com;port=3306;user id=qyroot;password=kaicn1132+-;initial catalog=mds;charset=utf8;sslmode=none;" }, "AllowedSwagger": true, - "Secret": "D96BFA5B-F2AF-45BC-9342-5A55C3F9BBB0" + "Secret": "D96BFA5B-F2AF-45BC-9342-5A55C3F9BBB0", + //ApiVersionRequirementsҪվ + "ApiVersionRequirements": [ + { + "Api": "/api/purchaseOrder/createpurchaseorder", + "MinimumVersion": 10017 + } + ] }