From ab8c20be14e5e88dd5581c4f0b45a8243bc74f04 Mon Sep 17 00:00:00 2001 From: fajiao <1519100073@qq.com> Date: Tue, 25 Jan 2022 12:47:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=BF=94=E5=9B=9E=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BaseModule/EC.App/Core/ApiController.cs | 3 +- BaseModule/EC.App/Core/BLL.cs | 4 +- BaseModule/EC.App/Core/IBLL.cs | 4 +- BaseModule/EC.App/Core/IService.cs | 4 +- BaseModule/EC.App/Core/Service.cs | 4 +- BaseModule/EC.App/Entity/RespParam.cs | 35 ++++++++ .../Extensions/RESTfulResultProvider.cs | 0 .../EC.App/Extensions/RespParamProvider.cs | 83 +++++++++++++++++++ BaseModule/EC.App/Factory/RespParamFactory.cs | 41 +++++++++ OnvifWebServer/Startup.cs | 4 +- 10 files changed, 175 insertions(+), 7 deletions(-) create mode 100644 BaseModule/EC.App/Entity/RespParam.cs rename {OnvifWebServer => BaseModule/EC.App}/Extensions/RESTfulResultProvider.cs (100%) create mode 100644 BaseModule/EC.App/Extensions/RespParamProvider.cs create mode 100644 BaseModule/EC.App/Factory/RespParamFactory.cs diff --git a/BaseModule/EC.App/Core/ApiController.cs b/BaseModule/EC.App/Core/ApiController.cs index 255d9a5..d1c3c0b 100644 --- a/BaseModule/EC.App/Core/ApiController.cs +++ b/BaseModule/EC.App/Core/ApiController.cs @@ -1,4 +1,5 @@ -using Furion.DynamicApiController; +using Furion.DependencyInjection; +using Furion.DynamicApiController; using Microsoft.AspNetCore.Mvc; namespace EC.App.Core diff --git a/BaseModule/EC.App/Core/BLL.cs b/BaseModule/EC.App/Core/BLL.cs index 125af19..dc80cfc 100644 --- a/BaseModule/EC.App/Core/BLL.cs +++ b/BaseModule/EC.App/Core/BLL.cs @@ -1,4 +1,6 @@ -namespace EC.App.Core +using Furion.DependencyInjection; + +namespace EC.App.Core { public class BLL : IBLL { diff --git a/BaseModule/EC.App/Core/IBLL.cs b/BaseModule/EC.App/Core/IBLL.cs index ffceb26..d952a98 100644 --- a/BaseModule/EC.App/Core/IBLL.cs +++ b/BaseModule/EC.App/Core/IBLL.cs @@ -1,4 +1,6 @@ -namespace EC.App.Core +using Furion.DependencyInjection; + +namespace EC.App.Core { public interface IBLL { diff --git a/BaseModule/EC.App/Core/IService.cs b/BaseModule/EC.App/Core/IService.cs index 679cbcb..deb2ed7 100644 --- a/BaseModule/EC.App/Core/IService.cs +++ b/BaseModule/EC.App/Core/IService.cs @@ -1,4 +1,6 @@ -namespace EC.App.Core +using Furion.DependencyInjection; + +namespace EC.App.Core { public interface IService { diff --git a/BaseModule/EC.App/Core/Service.cs b/BaseModule/EC.App/Core/Service.cs index adb0ddd..262127c 100644 --- a/BaseModule/EC.App/Core/Service.cs +++ b/BaseModule/EC.App/Core/Service.cs @@ -1,4 +1,6 @@ -namespace EC.App.Core +using Furion.DependencyInjection; + +namespace EC.App.Core { public class Service : IService { diff --git a/BaseModule/EC.App/Entity/RespParam.cs b/BaseModule/EC.App/Entity/RespParam.cs new file mode 100644 index 0000000..a965564 --- /dev/null +++ b/BaseModule/EC.App/Entity/RespParam.cs @@ -0,0 +1,35 @@ +namespace EC.App.Entity +{ + public class RespParam + { + /// + /// 返回码 + /// + public int Code { get; set; } + + /// + /// 是否成功 + /// + public bool IsSuccess { get; set; } + + /// + /// 返回数据 + /// + public T Data { get; set; } + + /// + /// 错误信息 + /// + public object Errors { get; set; } + + /// + /// 附加数据 + /// + public object Extras { get; set; } + + /// + /// 时间戳 + /// + public long Timestamp { get; set; } + } +} \ No newline at end of file diff --git a/OnvifWebServer/Extensions/RESTfulResultProvider.cs b/BaseModule/EC.App/Extensions/RESTfulResultProvider.cs similarity index 100% rename from OnvifWebServer/Extensions/RESTfulResultProvider.cs rename to BaseModule/EC.App/Extensions/RESTfulResultProvider.cs diff --git a/BaseModule/EC.App/Extensions/RespParamProvider.cs b/BaseModule/EC.App/Extensions/RespParamProvider.cs new file mode 100644 index 0000000..53e7ef1 --- /dev/null +++ b/BaseModule/EC.App/Extensions/RespParamProvider.cs @@ -0,0 +1,83 @@ +using EC.App.Entity; +using EC.App.Factory; +using Furion.DataValidation; +using Furion.DependencyInjection; +using Furion.UnifyResult; +using Furion.UnifyResult.Internal; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using System; +using System.Threading.Tasks; + +namespace EC.App.Extensions +{ + /// + /// 返回值 + /// + [SuppressSniffer, UnifyModel(typeof(RespParam<>))] + public class RespParamProvider : IUnifyResultProvider + { + /// + /// 成功返回值 + /// + /// + /// + /// + public IActionResult OnSucceeded(ActionExecutedContext context, object data) + { + return RespParamFactory.New(StatusCodes.Status200OK, true, data).ToJsonResult(); + } + + /// + /// 验证失败返回值 + /// + /// + /// + /// + public IActionResult OnValidateFailed(ActionExecutingContext context, ValidationMetadata metadata) + { + return RespParamFactory.New(StatusCodes.Status400BadRequest, errors: metadata.ValidationResult).ToJsonResult(); + } + + /// + /// 异常返回值 + /// + /// + /// + /// + public IActionResult OnException(ExceptionContext context, ExceptionMetadata metadata) + { + return RespParamFactory.New(metadata.StatusCode, errors: metadata.Errors).ToJsonResult(); + } + + /// + /// 特定状态码返回值 + /// + /// + /// + /// + /// + public async Task OnResponseStatusCodes(HttpContext context, int code, UnifyResultSettingsOptions unifyResultSettings = null) + { + // 设置响应状态码 + UnifyContext.SetResponseStatusCodes(context, code, unifyResultSettings); + + switch (code) + { + // 处理 401 状态码 + case StatusCodes.Status401Unauthorized: + await context.Response.WriteAsJsonAsync(RespParamFactory.New(code, errors: "401 Unauthorized") + , Furion.App.GetOptions()?.JsonSerializerOptions); + break; + // 处理 403 状态码 + case StatusCodes.Status403Forbidden: + await context.Response.WriteAsJsonAsync(RespParamFactory.New(code, errors: "403 Forbidden") + , Furion.App.GetOptions()?.JsonSerializerOptions); + break; + + default: break; + } + } + } +} \ No newline at end of file diff --git a/BaseModule/EC.App/Factory/RespParamFactory.cs b/BaseModule/EC.App/Factory/RespParamFactory.cs new file mode 100644 index 0000000..79e0456 --- /dev/null +++ b/BaseModule/EC.App/Factory/RespParamFactory.cs @@ -0,0 +1,41 @@ +using EC.App.Entity; +using Furion.UnifyResult; +using Microsoft.AspNetCore.Mvc; +using System; + +namespace EC.App.Factory +{ + public static class RespParamFactory + { + public static RespParam New(int code, bool isSuccess = default, object data = default, object errors = default) + { + return new RespParam + { + Code = code, + IsSuccess = isSuccess, + Data = data, + Errors = errors, + Extras = UnifyContext.Take(), + Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + }; + } + + public static RespParam New(Enum code, bool isSuccess = default, object data = default, object errors = default) + { + return new RespParam + { + Code = code.GetHashCode(), + IsSuccess = isSuccess, + Data = data, + Errors = errors, + Extras = UnifyContext.Take(), + Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() + }; + } + + public static JsonResult ToJsonResult(this RespParam respParam) + { + return new JsonResult(respParam); + } + } +} \ No newline at end of file diff --git a/OnvifWebServer/Startup.cs b/OnvifWebServer/Startup.cs index 7e71201..d4b5827 100644 --- a/OnvifWebServer/Startup.cs +++ b/OnvifWebServer/Startup.cs @@ -1,9 +1,9 @@ +using EC.App.Extensions; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using OnvifWebServer.Extensions; namespace OnvifWebServer { @@ -19,7 +19,7 @@ namespace OnvifWebServer // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - services.AddControllers().AddInject().AddUnifyResult(); // AddInject(); + services.AddControllers().AddInject().AddUnifyResult(); // AddInject(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.