From ab34c947ca5e02485f7dbd577ef504d51effa570 Mon Sep 17 00:00:00 2001 From: hang <872297557@qq.com> Date: Wed, 14 Aug 2024 14:43:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8E=E7=AB=AF=E5=9B=BD=E9=99=85=E5=8C=96?= =?UTF-8?q?=E6=94=AF=E6=8C=81=EF=BC=8C=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E7=9A=84=E4=BF=A1=E6=81=AF=E5=92=8C=E8=BF=94=E5=9B=9E=E5=89=8D?= =?UTF-8?q?=E7=AB=AF=E7=9A=84=E5=9B=BD=E9=99=85=E5=8C=96=E4=B8=8D=E4=B8=80?= =?UTF-8?q?=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IRaCIS.Core.Application/BaseService.cs | 8 +- .../BusinessFilter/ProjectExceptionFilter.cs | 16 +-- .../BusinessFilter/UnifiedApiResultFilter.cs | 23 ++-- .../Helper/InternationalizationHelper.cs | 14 ++- .../Allocation/TaskConsistentRuleService.cs | 2 +- .../Common/InternationalizationService.cs | 2 +- .../Service/Visit/SubjectService.cs | 1 + IRaCIS.Core.Domain/IRaCIS.Core.Domain.csproj | 1 + .../IRaCIS.Core.Infrastructure.csproj | 2 + .../BusinessValidationFailedException.cs | 16 ++- .../_IRaCIS/Output/IResponseOutput.cs | 12 +- .../_IRaCIS/Output/ResponseOutput.cs | 104 +++++------------- .../_IRaCIS}/_Config/_StaticData.cs | 2 + 13 files changed, 92 insertions(+), 111 deletions(-) rename {IRaCIS.Core.Domain => IRaCIS.Core.Infrastructure/_IRaCIS}/_Config/_StaticData.cs (98%) diff --git a/IRaCIS.Core.Application/BaseService.cs b/IRaCIS.Core.Application/BaseService.cs index 9101ffa80..00df0c376 100644 --- a/IRaCIS.Core.Application/BaseService.cs +++ b/IRaCIS.Core.Application/BaseService.cs @@ -8,6 +8,7 @@ using Panda.DynamicWebApi; using Panda.DynamicWebApi.Attributes; using System.Diagnostics.CodeAnalysis; using IRaCIS.Core.Domain.Share; +using Microsoft.AspNetCore.Mvc; namespace IRaCIS.Core.Application { @@ -17,7 +18,8 @@ namespace IRaCIS.Core.Application #region 非泛型版本 - [Authorize, DynamicWebApi, UnifiedApiResultFilter] + [TypeFilter(typeof(UnifiedApiResultFilter))] + [Authorize, DynamicWebApi] public class BaseService : IBaseService, IDynamicWebApi { public IMapper _mapper { get; set; } @@ -83,8 +85,8 @@ namespace IRaCIS.Core.Application } - - [Authorize, DynamicWebApi, UnifiedApiResultFilter] + [TypeFilter(typeof(UnifiedApiResultFilter))] + [Authorize, DynamicWebApi] public class BaseServiceTest : IBaseServiceTest, IDynamicWebApi where T : Entity { public IMapper _mapper { get; set; } diff --git a/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs b/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs index 5638e52ea..62bd9b24c 100644 --- a/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs +++ b/IRaCIS.Core.Application/BusinessFilter/ProjectExceptionFilter.cs @@ -1,4 +1,5 @@ -using IRaCIS.Core.Infrastructure; +using IRaCIS.Core.Domain.Share; +using IRaCIS.Core.Infrastructure; using IRaCIS.Core.Infrastructure.Extention; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; @@ -34,7 +35,10 @@ namespace IRaCIS.Core.Application.Filter { var error = context.Exception as BusinessValidationFailedException; - context.Result = new JsonResult(ResponseOutput.NotOk(context.Exception.Message, error!.Code)); + context.Result = new JsonResult(ResponseOutput.NotOk(context.Exception.Message, error!.Code,localizedInfo: $"[{error!.LocalizedKey}]:{StaticData.Log_Locoalize_Dic[error!.LocalizedKey]}")); + + //warning 级别记录 + //_logger.LogWarning($"[{error!.LocalizedKey}]:{StaticData.Log_Locoalize_Dic[error!.LocalizedKey]}"); } else if(context.Exception.GetType() == typeof(QueryBusinessObjectNotExistException)) { @@ -44,12 +48,10 @@ namespace IRaCIS.Core.Application.Filter { context.Result = new JsonResult(ResponseOutput.NotOk(_localizer["Project_ExceptionContactDeveloper"] + (context.Exception.InnerException is null ? (context.Exception.Message /*+ context.Exception.StackTrace*/) : (context.Exception.InnerException?.Message /*+ context.Exception.InnerException?.StackTrace*/)), ApiResponseCodeEnum.ProgramException)); + + _logger.LogError(context.Exception.InnerException is null ? (context.Exception.Message + context.Exception.StackTrace) : (context.Exception.InnerException?.Message + context.Exception.InnerException?.StackTrace)); + } - - - _logger.LogError(context.Exception.InnerException is null ? (context.Exception.Message + context.Exception.StackTrace) : (context.Exception.InnerException?.Message + context.Exception.InnerException?.StackTrace)); - - } else { diff --git a/IRaCIS.Core.Application/BusinessFilter/UnifiedApiResultFilter.cs b/IRaCIS.Core.Application/BusinessFilter/UnifiedApiResultFilter.cs index 9e5fdf591..39756a340 100644 --- a/IRaCIS.Core.Application/BusinessFilter/UnifiedApiResultFilter.cs +++ b/IRaCIS.Core.Application/BusinessFilter/UnifiedApiResultFilter.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc; using IRaCIS.Core.Domain.Share; +using Microsoft.Extensions.Logging; namespace IRaCIS.Application.Services.BusinessFilter { @@ -10,6 +11,12 @@ namespace IRaCIS.Application.Services.BusinessFilter /// public class UnifiedApiResultFilter : Attribute, IAsyncResultFilter { + private readonly ILogger _logger; + + public UnifiedApiResultFilter(ILogger logger) + { + _logger = logger; + } /// /// 异步版本 @@ -31,12 +38,11 @@ namespace IRaCIS.Application.Services.BusinessFilter var type = objectResult.Value?.GetType(); - if ( type!=null&& type.IsGenericType&&(type.GetGenericTypeDefinition()==typeof(ValueTuple<,>)|| type.GetGenericTypeDefinition()==typeof(Tuple<,>))) + if (type != null && type.IsGenericType && (type.GetGenericTypeDefinition() == typeof(ValueTuple<,>) || type.GetGenericTypeDefinition() == typeof(Tuple<,>))) { //报错 //var tuple = (object, object))objectResult.Value; - //var (val1, val2) = ((dynamic, dynamic))objectResult.Value; //var apiResponse = ResponseOutput.Ok(val1, val2); @@ -56,16 +62,19 @@ namespace IRaCIS.Application.Services.BusinessFilter objectResult.DeclaredType = apiResponse.GetType(); } - + } - //如果不是200 是IResponseOutput 不处理 - else if (statusCode != 200 && (objectResult.Value is IResponseOutput)) + //如果是200 是IResponseOutput 记录下日志 + else if (statusCode == 200 && (objectResult.Value is IResponseOutput)) { + var result = objectResult.Value as IResponseOutput; + + _logger.LogWarning($"{result.LocalizedInfo}"); } - else if(statusCode != 200&&!(objectResult.Value is IResponseOutput)) + else if (statusCode != 200 && !(objectResult.Value is IResponseOutput)) { - //---程序错误,请联系开发人员。 + //---程序错误,请联系开发人员。 var apiResponse = ResponseOutput.NotOk(StaticData.International("UnifiedAPI_ProgramError")); objectResult.Value = apiResponse; diff --git a/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs b/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs index eaa357c55..910f35e14 100644 --- a/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs +++ b/IRaCIS.Core.Application/Helper/InternationalizationHelper.cs @@ -53,6 +53,9 @@ namespace IRaCIS.Core.Application.Helper jsonObject[item.Code] = item.Value; StaticData.En_US_Dic[item.Code] = item.Value; + + //日志记录该信息方便自己人看, 返回给客户的是配置的 + StaticData.Log_Locoalize_Dic[item.Code] = item.Description; } } else @@ -71,7 +74,7 @@ namespace IRaCIS.Core.Application.Helper } } - public static async Task AddOrUpdateJsonKeyValueAsync(string key, string value, string valueCN) + public static async Task AddOrUpdateJsonKeyValueAsync(string key, string value, string valueCN,string description) { VerifyFolder(); @@ -94,6 +97,9 @@ namespace IRaCIS.Core.Application.Helper jsonObject[key] = value; StaticData.En_US_Dic[key] = value; + + //日志记录该信息方便自己人看, 返回给客户的是配置的 + StaticData.Log_Locoalize_Dic[key] = description; } else { @@ -117,7 +123,8 @@ namespace IRaCIS.Core.Application.Helper { t.Code, t.Value, - t.ValueCN + t.ValueCN, + t.Description }).ToListAsync(); //组织成json 文件 @@ -131,6 +138,9 @@ namespace IRaCIS.Core.Application.Helper { StaticData.En_US_Dic[tojsonItem.Code] = tojsonItem.Value; StaticData.Zh_CN_Dic[tojsonItem.Code] = tojsonItem.ValueCN; + + //日志记录该信息方便自己人看, 返回给客户的是配置的 + StaticData.Log_Locoalize_Dic[tojsonItem.Code] = tojsonItem.Description; } File.WriteAllText(usJsonPath, JsonConvert.SerializeObject(StaticData.En_US_Dic)); diff --git a/IRaCIS.Core.Application/Service/Allocation/TaskConsistentRuleService.cs b/IRaCIS.Core.Application/Service/Allocation/TaskConsistentRuleService.cs index eeab0011c..44297c8fa 100644 --- a/IRaCIS.Core.Application/Service/Allocation/TaskConsistentRuleService.cs +++ b/IRaCIS.Core.Application/Service/Allocation/TaskConsistentRuleService.cs @@ -711,7 +711,7 @@ namespace IRaCIS.Core.Application.Service IsHaveGeneratedTask = t.SubjectVisitTaskList.Any(c => c.IsSelfAnalysis == false && c.TrialReadingCriterionId == trialReadingCriterionId), - DoctorUserList = t.SubjectDoctorList.Where(t => t.TrialReadingCriterionId == trialReadingCriterionId && t.IsConfirmed && t.ArmEnum<=Arm.DoubleReadingArm2).Select(t => new UserSimpleInfo() + DoctorUserList = t.SubjectDoctorList.Where(t => t.TrialReadingCriterionId == trialReadingCriterionId && t.IsConfirmed && t.ArmEnum <= Arm.DoubleReadingArm2).Select(t => new UserSimpleInfo() { UserId = t.Id, FullName = t.DoctorUser.FullName, diff --git a/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs b/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs index 10289e9a5..47723708e 100644 --- a/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs +++ b/IRaCIS.Core.Application/Service/Common/InternationalizationService.cs @@ -184,7 +184,7 @@ namespace IRaCIS.Core.Application.Service if (addOrEditInternationalization.InternationalizationType == 1) { - await InternationalizationHelper.AddOrUpdateJsonKeyValueAsync(entity.Code, addOrEditInternationalization.Value, addOrEditInternationalization.ValueCN); + await InternationalizationHelper.AddOrUpdateJsonKeyValueAsync(entity.Code, addOrEditInternationalization.Value, addOrEditInternationalization.ValueCN,addOrEditInternationalization.Description); } else { diff --git a/IRaCIS.Core.Application/Service/Visit/SubjectService.cs b/IRaCIS.Core.Application/Service/Visit/SubjectService.cs index d9e1ace36..cb21db1dc 100644 --- a/IRaCIS.Core.Application/Service/Visit/SubjectService.cs +++ b/IRaCIS.Core.Application/Service/Visit/SubjectService.cs @@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using IRaCIS.Core.Application.Auth; using IRaCIS.Core.Domain.Models; +using IRaCIS.Core.Infrastructure; namespace IRaCIS.Application.Services { diff --git a/IRaCIS.Core.Domain/IRaCIS.Core.Domain.csproj b/IRaCIS.Core.Domain/IRaCIS.Core.Domain.csproj index ac7815099..80a3e8702 100644 --- a/IRaCIS.Core.Domain/IRaCIS.Core.Domain.csproj +++ b/IRaCIS.Core.Domain/IRaCIS.Core.Domain.csproj @@ -10,6 +10,7 @@ + diff --git a/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj b/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj index f2b268e49..324ac91a0 100644 --- a/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj +++ b/IRaCIS.Core.Infrastructure/IRaCIS.Core.Infrastructure.csproj @@ -11,6 +11,8 @@ + + diff --git a/IRaCIS.Core.Infrastructure/_IRaCIS/Exception/BusinessValidationFailedException.cs b/IRaCIS.Core.Infrastructure/_IRaCIS/Exception/BusinessValidationFailedException.cs index cbe9ec767..1939d57cd 100644 --- a/IRaCIS.Core.Infrastructure/_IRaCIS/Exception/BusinessValidationFailedException.cs +++ b/IRaCIS.Core.Infrastructure/_IRaCIS/Exception/BusinessValidationFailedException.cs @@ -1,4 +1,5 @@ using IRaCIS.Core.Infrastructure.Extention; +using Microsoft.Extensions.Localization; using System; namespace IRaCIS.Core.Infrastructure @@ -8,15 +9,24 @@ namespace IRaCIS.Core.Infrastructure public ApiResponseCodeEnum Code { get; set; } - public BusinessValidationFailedException() + public string LocalizedKey { get; set; }=string.Empty; + + public BusinessValidationFailedException() { } + public BusinessValidationFailedException(LocalizedString message, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed) : base(message) + { + Code = code; + LocalizedKey=message.Name; + + } + public BusinessValidationFailedException(string message, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed) : base(message) { - Code = code; + Code = code; - } + } } } diff --git a/IRaCIS.Core.Infrastructure/_IRaCIS/Output/IResponseOutput.cs b/IRaCIS.Core.Infrastructure/_IRaCIS/Output/IResponseOutput.cs index 13b15698a..a0edd84d0 100644 --- a/IRaCIS.Core.Infrastructure/_IRaCIS/Output/IResponseOutput.cs +++ b/IRaCIS.Core.Infrastructure/_IRaCIS/Output/IResponseOutput.cs @@ -16,6 +16,9 @@ /// 消息 /// string ErrorMessage { get; } + + + public string LocalizedInfo { get; set; } } /// @@ -30,13 +33,4 @@ T Data { get; set; } } - //public interface IResponseOutput : IResponseOutput - //{ - // /// - // /// 返回数据 - // /// - // T Data { get; } - - // T2 OtherInfo { get; } - //} } diff --git a/IRaCIS.Core.Infrastructure/_IRaCIS/Output/ResponseOutput.cs b/IRaCIS.Core.Infrastructure/_IRaCIS/Output/ResponseOutput.cs index 74325db91..e0b46da46 100644 --- a/IRaCIS.Core.Infrastructure/_IRaCIS/Output/ResponseOutput.cs +++ b/IRaCIS.Core.Infrastructure/_IRaCIS/Output/ResponseOutput.cs @@ -1,4 +1,7 @@ -using Newtonsoft.Json; +using IRaCIS.Core.Domain.Share; +using Microsoft.Extensions.Localization; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; namespace IRaCIS.Core.Infrastructure.Extention { @@ -7,10 +10,10 @@ namespace IRaCIS.Core.Infrastructure.Extention /// public class ResponseOutput : IResponseOutput { - /// - /// 是否成功标记 - /// - public bool IsSuccess { get; private set; } + /// + /// 是否成功标记 + /// + public bool IsSuccess { get; private set; } public ApiResponseCodeEnum Code { get; set; } = ApiResponseCodeEnum.OK; @@ -25,34 +28,20 @@ namespace IRaCIS.Core.Infrastructure.Extention /// 数据 兼顾以前 Json序列化的时候返回属性名为“Result” /// [JsonProperty("Result")] - public T Data { get; set; } + public T Data { get; set; } [JsonProperty("OtherInfo")] public object OtherData { get; set; } + public string LocalizedInfo { get; set; } - /// - /// 成功 - /// - /// 数据 - /// 消息 - //public ResponseOutput Ok(T data, string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.OK) - //{ - // IsSuccess = true; - // Code = code; - // Data = data; - // ErrorMessage = msg; - - // return this; - //} - - public ResponseOutput Ok(T data, object otherData, string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.OK) + public ResponseOutput Ok(T data, object otherData, string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.OK) { IsSuccess = true; Code = code; Data = data; - OtherData=otherData; + OtherData = otherData; ErrorMessage = msg; return this; @@ -64,12 +53,13 @@ namespace IRaCIS.Core.Infrastructure.Extention /// 提示消息 /// 数据 /// - public ResponseOutput NotOk(string msg = "", T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.OK) + public ResponseOutput NotOk(string msg = "", T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.OK, string localizedInfo = "") { IsSuccess = false; - Code = code; + Code = code; ErrorMessage = msg; Data = data; + LocalizedInfo = localizedInfo; return this; } @@ -77,33 +67,6 @@ namespace IRaCIS.Core.Infrastructure.Extention } - //public class ResponseOutput : IResponseOutput - //{ - // [JsonProperty("Result")] - // public T Data { get; private set; } - - // public T2 OtherInfo { get; private set; } - - // public bool IsSuccess { get; private set; } - - // public ApiResponseCodeEnum Code { get; set; } - - - // public string ErrorMessage { get; private set; } - - - // public ResponseOutput Ok(T data, T2 otherInfo, string msg = "") - // { - // IsSuccess = true; - // Data = data; - // OtherInfo = otherInfo; - // ErrorMessage = msg; - - // return this; - // } - //} - - /// /// 响应数据静态输出 为了代码简洁 不用每处都New /// @@ -111,23 +74,6 @@ namespace IRaCIS.Core.Infrastructure.Extention { - //public static IResponseOutput Ok(T data, T2 otherInfo, string msg = "") - //{ - // return new ResponseOutput().Ok(data, otherInfo); - //} - - - /// - /// 成功 -----适合查询 - /// - /// 数据 - /// 消息 - /// - //public static IResponseOutput Ok(T data = default, string msg = "") - //{ - // return new ResponseOutput().Ok(data, msg); - //} - public static IResponseOutput Ok(T data = default, object otherData = default, string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.OK) { return new ResponseOutput().Ok(data, otherData, msg, code); @@ -147,15 +93,11 @@ namespace IRaCIS.Core.Infrastructure.Extention /// 消息 /// 数据 /// - public static IResponseOutput NotOk(string msg = "", T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed) + public static IResponseOutput NotOk(string msg = "", T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed, string localizedInfo = "") { - return new ResponseOutput().NotOk(msg, data, code); + return new ResponseOutput().NotOk(msg, data, code, localizedInfo); } - //public static IResponseOutput NotOk( T data = default, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed) - //{ - // return new ResponseOutput().NotOk("", data, code); - //} /// /// 失败 @@ -164,7 +106,13 @@ namespace IRaCIS.Core.Infrastructure.Extention /// public static IResponseOutput NotOk(string msg = "", ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed) { - return new ResponseOutput().NotOk(msg,code:code); + return new ResponseOutput().NotOk(msg, code: code); + } + + public static IResponseOutput NotOk(LocalizedString msg, ApiResponseCodeEnum code = ApiResponseCodeEnum.BusinessValidationFailed) + { + + return new ResponseOutput().NotOk(msg, code: code, localizedInfo: $"[{msg.Name}]:{StaticData.Log_Locoalize_Dic[msg.Name]}" ); } public static IResponseOutput DBNotExistIfNUll(object businessObject) @@ -172,7 +120,7 @@ namespace IRaCIS.Core.Infrastructure.Extention return new ResponseOutput().NotOk($"The business object{businessObject.GetType().Name} does not exist in the database, or was deleted by someone else, or an incorrect parameter query caused"); } - + /// /// 根据布尔值返回结果 --适合删除 /// @@ -190,7 +138,7 @@ namespace IRaCIS.Core.Infrastructure.Extention /// public static IResponseOutput Result(bool success, T data = default) { - return success ? Ok(data) : NotOk("Saved failed",data); + return success ? Ok(data) : NotOk("Saved failed", data); } ///// diff --git a/IRaCIS.Core.Domain/_Config/_StaticData.cs b/IRaCIS.Core.Infrastructure/_IRaCIS/_Config/_StaticData.cs similarity index 98% rename from IRaCIS.Core.Domain/_Config/_StaticData.cs rename to IRaCIS.Core.Infrastructure/_IRaCIS/_Config/_StaticData.cs index 117e658fa..42ab4a9ca 100644 --- a/IRaCIS.Core.Domain/_Config/_StaticData.cs +++ b/IRaCIS.Core.Infrastructure/_IRaCIS/_Config/_StaticData.cs @@ -12,6 +12,8 @@ public static class StaticData public static Dictionary Zh_CN_Dic = new Dictionary(); + public static Dictionary Log_Locoalize_Dic = new Dictionary(); + public static readonly string En_US_Json = "en-US.json"; public static readonly string Zh_CN_Json = "zh-CN.json";