From 70a68e6ea62dd83c54bd80a357f87c52e3bb139c Mon Sep 17 00:00:00 2001 From: Nice <2017875139@qq.com> Date: Fri, 29 Apr 2022 16:53:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81=E6=9B=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Easy/Realization/IDatabaseExtension.cs | 4 +- Easy.sln | 12 +++--- .../Controllers/WeatherForecastController.cs | 39 +++++++++++++++++++ WebDemo/Program.cs | 26 +++++++++++++ WebDemo/Properties/launchSettings.json | 31 +++++++++++++++ WebDemo/WeatherForecast.cs | 12 ++++++ WebDemo/WebDemo.csproj | 17 ++++++++ WebDemo/appsettings.Development.json | 8 ++++ WebDemo/appsettings.json | 9 +++++ src/Easy.Core/Easy.Core.csproj | 2 +- .../Easy/Exceptions/BusinessException.cs | 0 src/{Easy.Result => Easy.Core}/Easy/When.cs | 0 src/Easy.Core/System/EnumExtension.cs | 2 +- .../Easy.DDD.Application.csproj | 2 +- .../Domain/Entities/ConcurrencyStampConsts.cs | 1 + .../Easy/Aop/FluentValidationRegister.cs | 2 +- src/Easy.Result/Easy.Result.csproj | 2 +- src/Easy.Result/Easy/Aop/ApiResultProxy.cs | 1 + .../Easy/Aop/ParameterVerification.cs | 20 ++++++++++ .../Easy/Extensions/ApiResultExtensions.cs | 4 +- .../ServiceCollectionServiceExtensions.cs | 20 +++++++++- .../Easy/Result/ApiResultOptions.cs | 23 +++++++++++ .../ServiceCollectionServiceExtensions.cs | 1 - 23 files changed, 220 insertions(+), 18 deletions(-) create mode 100644 WebDemo/Controllers/WeatherForecastController.cs create mode 100644 WebDemo/Program.cs create mode 100644 WebDemo/Properties/launchSettings.json create mode 100644 WebDemo/WeatherForecast.cs create mode 100644 WebDemo/WebDemo.csproj create mode 100644 WebDemo/appsettings.Development.json create mode 100644 WebDemo/appsettings.json rename src/{Easy.Result => Easy.Core}/Easy/Exceptions/BusinessException.cs (100%) rename src/{Easy.Result => Easy.Core}/Easy/When.cs (100%) create mode 100644 src/Easy.Result/Easy/Aop/ParameterVerification.cs create mode 100644 src/Easy.Result/Easy/Result/ApiResultOptions.cs diff --git a/Easy.Snowflakes/Easy/Realization/IDatabaseExtension.cs b/Easy.Snowflakes/Easy/Realization/IDatabaseExtension.cs index 39297ac..7b58e03 100644 --- a/Easy.Snowflakes/Easy/Realization/IDatabaseExtension.cs +++ b/Easy.Snowflakes/Easy/Realization/IDatabaseExtension.cs @@ -36,7 +36,7 @@ internal static class IDatabaseExtension internal static async Task SafedUnLockAsync(this IDatabase redisDb, string cacheKey, string lockValue) { var lockKey = GetLockKey(cacheKey); - AutoDelayTimers.Instance.CloseTimer(lockKey); + AutoDelayTimers.CloseTimer(lockKey); var script = @"local invalue = @value local currvalue = redis.call('get',@key) @@ -68,7 +68,7 @@ internal static class IDatabaseExtension var result = redisDb.ScriptEvaluateAsync(prepared, parameters, CommandFlags.None).GetAwaiter().GetResult(); if ((int)result == 0) { - AutoDelayTimers.Instance.CloseTimer(key); + AutoDelayTimers.CloseTimer(key); } return; } diff --git a/Easy.sln b/Easy.sln index 539ff0c..6f82c42 100644 --- a/Easy.sln +++ b/Easy.sln @@ -35,7 +35,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Easy.Snowflakes", "Easy.Sno EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Easy.EventBus", "src\Easy.EventBus\Easy.EventBus.csproj", "{F464D123-49B6-4DC7-AE88-2F78CA0B6A3D}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Guid.Test", "src\Guid.Test\Guid.Test.csproj", "{AB0828ED-6432-4647-8970-B57757ACD945}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebDemo", "WebDemo\WebDemo.csproj", "{A42FC010-4B67-4836-A619-89C7A1C6A802}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -95,10 +95,10 @@ Global {F464D123-49B6-4DC7-AE88-2F78CA0B6A3D}.Debug|Any CPU.Build.0 = Debug|Any CPU {F464D123-49B6-4DC7-AE88-2F78CA0B6A3D}.Release|Any CPU.ActiveCfg = Release|Any CPU {F464D123-49B6-4DC7-AE88-2F78CA0B6A3D}.Release|Any CPU.Build.0 = Release|Any CPU - {AB0828ED-6432-4647-8970-B57757ACD945}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {AB0828ED-6432-4647-8970-B57757ACD945}.Debug|Any CPU.Build.0 = Debug|Any CPU - {AB0828ED-6432-4647-8970-B57757ACD945}.Release|Any CPU.ActiveCfg = Release|Any CPU - {AB0828ED-6432-4647-8970-B57757ACD945}.Release|Any CPU.Build.0 = Release|Any CPU + {A42FC010-4B67-4836-A619-89C7A1C6A802}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A42FC010-4B67-4836-A619-89C7A1C6A802}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A42FC010-4B67-4836-A619-89C7A1C6A802}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A42FC010-4B67-4836-A619-89C7A1C6A802}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -119,7 +119,7 @@ Global {8D281035-BB53-4377-9386-0CB664A5C6B3} = {A263D7E3-0153-48D0-822D-E3E0B51A4890} {9D31086B-1333-4745-AEC2-7966141E503C} = {E2F1B3BA-DD92-4729-BDD0-91BB936520C0} {F464D123-49B6-4DC7-AE88-2F78CA0B6A3D} = {E2F1B3BA-DD92-4729-BDD0-91BB936520C0} - {AB0828ED-6432-4647-8970-B57757ACD945} = {E2F1B3BA-DD92-4729-BDD0-91BB936520C0} + {A42FC010-4B67-4836-A619-89C7A1C6A802} = {A263D7E3-0153-48D0-822D-E3E0B51A4890} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7260112B-0232-4725-AB84-EF4DCCABFD77} diff --git a/WebDemo/Controllers/WeatherForecastController.cs b/WebDemo/Controllers/WeatherForecastController.cs new file mode 100644 index 0000000..f2ac591 --- /dev/null +++ b/WebDemo/Controllers/WeatherForecastController.cs @@ -0,0 +1,39 @@ +using Microsoft.AspNetCore.Mvc; +using System.ComponentModel.DataAnnotations; + +namespace WebDemo.Controllers; +[ApiController] +[Route("[controller]")] +public class WeatherForecastController : ControllerBase +{ + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpPost(Name = "GetWeatherForecast")] + public IEnumerable Post(Test test) + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateTime.Now.AddDays(index), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + + public class Test + { + + public string MyProperty { get; set; } + } +} + diff --git a/WebDemo/Program.cs b/WebDemo/Program.cs new file mode 100644 index 0000000..191a2b5 --- /dev/null +++ b/WebDemo/Program.cs @@ -0,0 +1,26 @@ +using Easy.Extensions; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle +builder.Services.AddEndpointsApiExplorer(); +builder.Services.AddSwaggerGen(); +builder.Services.AddApiResultProxy(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) +{ + app.UseSwagger(); + app.UseSwaggerUI(); +} + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/WebDemo/Properties/launchSettings.json b/WebDemo/Properties/launchSettings.json new file mode 100644 index 0000000..d05cc25 --- /dev/null +++ b/WebDemo/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:28692", + "sslPort": 0 + } + }, + "profiles": { + "WebDemo": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5128", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/WebDemo/WeatherForecast.cs b/WebDemo/WeatherForecast.cs new file mode 100644 index 0000000..3c8b85f --- /dev/null +++ b/WebDemo/WeatherForecast.cs @@ -0,0 +1,12 @@ +namespace WebDemo; + +public class WeatherForecast +{ + public DateTime Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } +} diff --git a/WebDemo/WebDemo.csproj b/WebDemo/WebDemo.csproj new file mode 100644 index 0000000..82e907c --- /dev/null +++ b/WebDemo/WebDemo.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/WebDemo/appsettings.Development.json b/WebDemo/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/WebDemo/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/WebDemo/appsettings.json b/WebDemo/appsettings.json new file mode 100644 index 0000000..10f68b8 --- /dev/null +++ b/WebDemo/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/src/Easy.Core/Easy.Core.csproj b/src/Easy.Core/Easy.Core.csproj index 146a8cc..878829d 100644 --- a/src/Easy.Core/Easy.Core.csproj +++ b/src/Easy.Core/Easy.Core.csproj @@ -5,7 +5,7 @@ enable True - 1.1.5 + 1.1.6 diff --git a/src/Easy.Result/Easy/Exceptions/BusinessException.cs b/src/Easy.Core/Easy/Exceptions/BusinessException.cs similarity index 100% rename from src/Easy.Result/Easy/Exceptions/BusinessException.cs rename to src/Easy.Core/Easy/Exceptions/BusinessException.cs diff --git a/src/Easy.Result/Easy/When.cs b/src/Easy.Core/Easy/When.cs similarity index 100% rename from src/Easy.Result/Easy/When.cs rename to src/Easy.Core/Easy/When.cs diff --git a/src/Easy.Core/System/EnumExtension.cs b/src/Easy.Core/System/EnumExtension.cs index 8f79d2d..a4b2201 100644 --- a/src/Easy.Core/System/EnumExtension.cs +++ b/src/Easy.Core/System/EnumExtension.cs @@ -100,7 +100,7 @@ namespace System throw new Exception("GetEnumDefineAttributes 中的 T 必须为枚举类型!"); } - List list = new List(); + List list = new(); EnumDefineAttribute eda = null; foreach (var en in AsEnumerable()) { diff --git a/src/Easy.DDD.Application/Easy.DDD.Application.csproj b/src/Easy.DDD.Application/Easy.DDD.Application.csproj index b5c70ef..45932e3 100644 --- a/src/Easy.DDD.Application/Easy.DDD.Application.csproj +++ b/src/Easy.DDD.Application/Easy.DDD.Application.csproj @@ -4,7 +4,7 @@ enable True - 1.1.3 + 1.1.4 diff --git a/src/Easy.DDD.Domain/Easy/DDD/Domain/Entities/ConcurrencyStampConsts.cs b/src/Easy.DDD.Domain/Easy/DDD/Domain/Entities/ConcurrencyStampConsts.cs index 1f3df9c..e6f330c 100644 --- a/src/Easy.DDD.Domain/Easy/DDD/Domain/Entities/ConcurrencyStampConsts.cs +++ b/src/Easy.DDD.Domain/Easy/DDD/Domain/Entities/ConcurrencyStampConsts.cs @@ -3,3 +3,4 @@ public static class ConcurrencyStampConsts { public const int MaxLength = 40; } + \ No newline at end of file diff --git a/src/Easy.FluentValidation/Easy/Aop/FluentValidationRegister.cs b/src/Easy.FluentValidation/Easy/Aop/FluentValidationRegister.cs index cb04a75..2b07fc4 100644 --- a/src/Easy.FluentValidation/Easy/Aop/FluentValidationRegister.cs +++ b/src/Easy.FluentValidation/Easy/Aop/FluentValidationRegister.cs @@ -28,7 +28,7 @@ public class FluentValidationRegister : IAsyncActionFilter var validationResult = await validator.ValidateAsync(ValidationContext); - When.Is(!validationResult.IsValid, validationResult.Errors?.First()?.ErrorMessage); + //When.Is(!validationResult.IsValid, validationResult.Errors?.First()?.ErrorMessage); } } } diff --git a/src/Easy.Result/Easy.Result.csproj b/src/Easy.Result/Easy.Result.csproj index 534fc5f..1a35501 100644 --- a/src/Easy.Result/Easy.Result.csproj +++ b/src/Easy.Result/Easy.Result.csproj @@ -4,7 +4,7 @@ net6.0 enable True - 1.1.5 + 1.1.6 diff --git a/src/Easy.Result/Easy/Aop/ApiResultProxy.cs b/src/Easy.Result/Easy/Aop/ApiResultProxy.cs index ed3ad5b..b610684 100644 --- a/src/Easy.Result/Easy/Aop/ApiResultProxy.cs +++ b/src/Easy.Result/Easy/Aop/ApiResultProxy.cs @@ -48,6 +48,7 @@ public class ApiResultProxy : IAsyncExceptionFilter }); } } + } diff --git a/src/Easy.Result/Easy/Aop/ParameterVerification.cs b/src/Easy.Result/Easy/Aop/ParameterVerification.cs new file mode 100644 index 0000000..e5ed656 --- /dev/null +++ b/src/Easy.Result/Easy/Aop/ParameterVerification.cs @@ -0,0 +1,20 @@ +using Easy.Result; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Filters; + +namespace Easy.Aop; +public class ParameterVerification : IAsyncActionFilter +{ + public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) + { + if (!context.ModelState.IsValid) + { + var errorMessage = context.ModelState.Values.SelectMany(v => v.Errors).FirstOrDefault(); + await context.HttpContext.Response.WriteAsJsonAsync(new ApiResult() + { + Message = errorMessage.ErrorMessage, + Status = "RETRY", + }); + } + } +} diff --git a/src/Easy.Result/Easy/Extensions/ApiResultExtensions.cs b/src/Easy.Result/Easy/Extensions/ApiResultExtensions.cs index a6552e5..e22d165 100644 --- a/src/Easy.Result/Easy/Extensions/ApiResultExtensions.cs +++ b/src/Easy.Result/Easy/Extensions/ApiResultExtensions.cs @@ -19,7 +19,7 @@ public static class ApiResultExtensions static public TSource SUCCESS(this TSource source, string message = null) where TSource : ApiResult { - return source.CustomStatusMessage("SUCCESS", message ?? "操作成功"); + return source.CustomStatusMessage(ApiResultConsts.SUCCESS, message ?? "操作成功"); } /// /// 重试 @@ -27,7 +27,7 @@ public static class ApiResultExtensions static public TSource RETRY(this TSource source, string message = null) where TSource : ApiResult { - return source.CustomStatusMessage("RETRY", message ?? "出错了,请稍后再试。"); + return source.CustomStatusMessage(ApiResultConsts.RETRY, message ?? "出错了,请稍后再试。"); } /// /// 自定义状态消息 diff --git a/src/Easy.Result/Easy/Extensions/ServiceCollectionServiceExtensions.cs b/src/Easy.Result/Easy/Extensions/ServiceCollectionServiceExtensions.cs index d6fa131..0757b07 100644 --- a/src/Easy.Result/Easy/Extensions/ServiceCollectionServiceExtensions.cs +++ b/src/Easy.Result/Easy/Extensions/ServiceCollectionServiceExtensions.cs @@ -1,16 +1,32 @@ using Easy.Aop; +using Easy.Result; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; namespace Easy.Extensions; public static class ServiceCollectionServiceExtensions { - public static IServiceCollection AddApiResultProxy(this IServiceCollection services) + public static IServiceCollection AddApiResultProxy(this IServiceCollection services, ApiResultOptions apiResultOptions) { services.Configure(options => { - options.Filters.Add(); + if (apiResultOptions.ApiResultRroxy) + { + options.Filters.Add(); + } + if (apiResultOptions.ParameterVerification) + { + options.Filters.Add(); + } }); + if (apiResultOptions.ParameterVerification) + { + services.Configure(options => + { + // 使用自定义模型验证【Api接口需要添加才能生效】 + options.SuppressModelStateInvalidFilter = true; + }); + } return services; } diff --git a/src/Easy.Result/Easy/Result/ApiResultOptions.cs b/src/Easy.Result/Easy/Result/ApiResultOptions.cs new file mode 100644 index 0000000..83ef404 --- /dev/null +++ b/src/Easy.Result/Easy/Result/ApiResultOptions.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Easy.Result; +public class ApiResultOptions +{ + /// + /// 参数验证 + /// + public bool ParameterVerification { get; set; } + /// + /// 结果代理 + /// + public bool ApiResultRroxy { get; set; } + public ApiResultOptions() + { + ParameterVerification = true; + ApiResultRroxy = true; + } +} diff --git a/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs b/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs index 447c089..3d1422f 100644 --- a/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs +++ b/src/Easy.Uow/Easy/Extensions/ServiceCollectionServiceExtensions.cs @@ -15,7 +15,6 @@ public static class ServiceCollectionServiceExtensions { options.Filters.Add(); }); - //IUnitOfWorkManager return services; }