From 74ef5a4c7b18461b73cc3567932f6686b8a8f01b Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Tue, 14 Mar 2023 16:14:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAAPI=E5=A2=9E=E5=8A=A0=E6=88=90?= =?UTF-8?q?=E5=8A=9F=E5=92=8C=E5=A4=B1=E8=B4=A5=E8=BF=94=E5=9B=9E=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiDefinistions/ApiControllerBase.cs | 81 +++++++++++++++++-- Falcon.SugarApi/ApiDefinistions/ApiResult.cs | 27 +++++++ 2 files changed, 100 insertions(+), 8 deletions(-) create mode 100644 Falcon.SugarApi/ApiDefinistions/ApiResult.cs diff --git a/Falcon.SugarApi/ApiDefinistions/ApiControllerBase.cs b/Falcon.SugarApi/ApiDefinistions/ApiControllerBase.cs index 78e3243..5a49ac6 100644 --- a/Falcon.SugarApi/ApiDefinistions/ApiControllerBase.cs +++ b/Falcon.SugarApi/ApiDefinistions/ApiControllerBase.cs @@ -1,4 +1,5 @@ using Falcon.SugarApi.DatabaseDefinitions; +using Falcon.SugarApi.ModelValidation; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -6,6 +7,7 @@ using System; using System.Text.Encodings.Web; using System.Text.Json; using System.Text.Unicode; +using System.Threading.Tasks; namespace Falcon.SugarApi.ApiDefinistions { @@ -15,7 +17,7 @@ namespace Falcon.SugarApi.ApiDefinistions [Area("api")] [ApiController] [Route("[Area]/[Controller]/[Action]")] - public abstract class ApiControllerBase : ControllerBase + public abstract class ApiControllerBase:ControllerBase { /// /// 日志记录服务 @@ -55,6 +57,8 @@ namespace Falcon.SugarApi.ApiDefinistions this.SugarDb = service.GetService() ?? throw new NullReferenceException("SugarDbContext"); } + #region 记录日志 + /// /// 保存文本日志 /// @@ -69,7 +73,7 @@ namespace Falcon.SugarApi.ApiDefinistions /// 请求数据 /// 响应数据 /// 异常 - protected virtual void SaveLogger(TRequest data, TResponse result, Exception? exception = null) { + protected virtual void SaveLogger(TRequest data,TResponse result,Exception? exception = null) { var requestStr = this.JsonSerialize(data); var responseStr = this.JsonSerialize(result); var exStr = exception == null ? "" : exception.ToString(); @@ -77,6 +81,9 @@ namespace Falcon.SugarApi.ApiDefinistions this.Logger.LogInformation(logmsg); } + #endregion + + /// /// 从对象序列化字符串 /// @@ -84,7 +91,7 @@ namespace Falcon.SugarApi.ApiDefinistions /// 要序列化的对象 /// 字符串 protected string JsonSerialize(T obj) { - return JsonSerializer.Serialize(obj, new JsonSerializerOptions { + return JsonSerializer.Serialize(obj,new JsonSerializerOptions { Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), }); } @@ -98,14 +105,29 @@ namespace Falcon.SugarApi.ApiDefinistions return JsonSerializer.Deserialize(json); } + /// + /// 验证模型。如果失败抛出异常 + /// + /// 模型 + protected virtual void ModelValidation(object? model) { + model.ThrowNullExceptionWhenNull(); + if(!model.TryModelValidation(out var errors)) { + ThrowApiException("传入的数据验证错误",null,e => { + e.Data.Add("ModelValidation",errors); + }); + } + } + + #region 抛出异常 + /// /// 抛出api异常 /// /// 异常消息 /// 内部异常 /// api异常 - protected virtual void ThrowApiException(string msg, Exception innException) - => throw new ApiException(msg, innException); + protected virtual void ThrowApiException(string msg,Exception innException) + => throw new ApiException(msg,innException); /// /// 抛出api异常 /// @@ -119,7 +141,7 @@ namespace Falcon.SugarApi.ApiDefinistions /// 内部异常 /// api异常 protected virtual void ThrowApiException(Exception innException) - => throw new ApiException(innException.Message, innException); + => throw new ApiException(innException.Message,innException); /// /// 抛出api异常 @@ -127,10 +149,53 @@ namespace Falcon.SugarApi.ApiDefinistions /// 异常消息 /// 内部异常 /// 异常处理方法 - protected virtual void ThrowApiException(string msg, Exception innException, Action exAction) { - var ex = new ApiException(msg, innException); + protected virtual void ThrowApiException(string msg,Exception innException,Action exAction) { + var ex = new ApiException(msg,innException); exAction?.Invoke(ex); throw ex; } + + #endregion + + + #region 返回api调用结果 + + /// + /// 同步返回成功的结果 + /// + /// data携带的数据类型 + /// 携带数据 + /// 异步api结果 + protected virtual ApiResult SuccessResult(T result) + => new() { Code = 0,Msg = "",Data = result,}; + + /// + /// 异步返回成功的结果 + /// + /// data携带的数据类型 + /// 携带数据 + /// 异步api结果 + protected virtual async Task> SuccessResultAsync(T result) + => await Task.FromResult(SuccessResult(result)); + + /// + /// 同步返回失败的结果 + /// + /// data携带的数据类型 + /// 失败信息 + /// 异步api结果 + protected virtual ApiResult FailResult(string failMessage) + => new() { Code = 1,Msg = failMessage,}; + + /// + /// 异步返回失败的结果 + /// + /// data携带的数据类型 + /// 失败信息 + /// 异步api结果 + protected virtual async Task> FailResultAsync(string failMessage) + => await Task.FromResult(FailResult(failMessage)); + + #endregion } } diff --git a/Falcon.SugarApi/ApiDefinistions/ApiResult.cs b/Falcon.SugarApi/ApiDefinistions/ApiResult.cs new file mode 100644 index 0000000..18cde45 --- /dev/null +++ b/Falcon.SugarApi/ApiDefinistions/ApiResult.cs @@ -0,0 +1,27 @@ +using System.Text.Json.Serialization; + +namespace Falcon.SugarApi.ApiDefinistions +{ + /// + /// api返回结果 + /// + /// 携带的数据类型 + public class ApiResult + { + /// + /// 结果代码 0成功 1失败 + /// + [JsonPropertyName("code")] + public int Code { get; set; } + /// + /// 失败信息 + /// + [JsonPropertyName("msg")] + public string Msg { get; set; } + /// + /// 携带数据 + /// + [JsonPropertyName("data")] + public T? Data { get; set; } + } +}