From afd800732ebc1a8340418576117d2c6402ba68a7 Mon Sep 17 00:00:00 2001 From: "qianhao.xu" Date: Tue, 18 Mar 2025 17:50:08 +0800 Subject: [PATCH] 1 --- .../MES/Group/GroupScheduleController.cs | 2 +- DOAN.Model/MES/Group/Dto/GroupPostDto.cs | 4 +- DOAN.Model/MES/Group/Dto/GroupScheduleDto.cs | 22 ++++----- .../Filters/GlobalActionMonitor.cs | 48 +++++++++++-------- 4 files changed, 43 insertions(+), 33 deletions(-) diff --git a/DOAN.Admin.WebApi/Controllers/MES/Group/GroupScheduleController.cs b/DOAN.Admin.WebApi/Controllers/MES/Group/GroupScheduleController.cs index 601e2fa..0fc2de1 100644 --- a/DOAN.Admin.WebApi/Controllers/MES/Group/GroupScheduleController.cs +++ b/DOAN.Admin.WebApi/Controllers/MES/Group/GroupScheduleController.cs @@ -130,7 +130,7 @@ namespace DOAN.Admin.WebApi.Controllers return SUCCESS(null); }; - parm.ScheduleDate = parm.ScheduleDate.Date; + parm.ScheduleDate = parm.ScheduleDate.Value.Date; var modal = parm.Adapt().ToCreate(HttpContext); var response = _GroupScheduleService.AddGroupSchedule(modal); diff --git a/DOAN.Model/MES/Group/Dto/GroupPostDto.cs b/DOAN.Model/MES/Group/Dto/GroupPostDto.cs index 282f8f6..ceca28f 100644 --- a/DOAN.Model/MES/Group/Dto/GroupPostDto.cs +++ b/DOAN.Model/MES/Group/Dto/GroupPostDto.cs @@ -15,10 +15,10 @@ namespace DOAN.Model.MES.group.Dto /// public class GroupPostDto { - [Required(ErrorMessage = "雪花不能为空")] + public string Id { get; set; } - [Required(ErrorMessage = "父id不能为空")] + public string ParentId { get; set; } public string PostName { get; set; } diff --git a/DOAN.Model/MES/Group/Dto/GroupScheduleDto.cs b/DOAN.Model/MES/Group/Dto/GroupScheduleDto.cs index a914f5b..4832599 100644 --- a/DOAN.Model/MES/Group/Dto/GroupScheduleDto.cs +++ b/DOAN.Model/MES/Group/Dto/GroupScheduleDto.cs @@ -26,30 +26,30 @@ namespace DOAN.Model.MES.group.Dto /// public class GroupScheduleDto { - public string Id { get; set; } + public string? Id { get; set; } - public DateTime ScheduleDate { get; set; } + public DateTime? ScheduleDate { get; set; } - public string GroupName { get; set; } + public string? GroupName { get; set; } - public string GroupCode { get; set; } + public string? GroupCode { get; set; } - public int FkShift { get; set; } - public string ShiftName { get; set; } + public int? FkShift { get; set; } + public string? ShiftName { get; set; } - public string Remark { get; set; } + public string? Remark { get; set; } - public string Status { get; set; } + public string? Status { get; set; } - public string CreatedBy { get; set; } + public string? CreatedBy { get; set; } public DateTime? CreatedTime { get; set; } - public string UpdatedBy { get; set; } + public string? UpdatedBy { get; set; } public DateTime? UpdatedTime { get; set; } - public decimal WorkHours { get; set; } + public decimal? WorkHours { get; set; } } } \ No newline at end of file diff --git a/DOAN.ServiceCore/Filters/GlobalActionMonitor.cs b/DOAN.ServiceCore/Filters/GlobalActionMonitor.cs index 9c111ad..589e5bd 100644 --- a/DOAN.ServiceCore/Filters/GlobalActionMonitor.cs +++ b/DOAN.ServiceCore/Filters/GlobalActionMonitor.cs @@ -9,6 +9,7 @@ using NLog; using DOAN.Infrastructure.IPTools; using DOAN.Model.System; using DOAN.ServiceCore.Services; +using System.Text; namespace DOAN.ServiceCore.Middleware { @@ -29,31 +30,40 @@ namespace DOAN.ServiceCore.Middleware /// public override Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { - // 打印请求参数 - string msg = string.Empty; - var values = context.ModelState.Values; - foreach (var item in values) + // 检查模型校验是否失败 + if (!context.ModelState.IsValid) + { + StringBuilder msgBuilder = new StringBuilder("请求参数错误:"); + foreach (var key in context.ModelState.Keys) { - foreach (var err in item.Errors) + var state = context.ModelState[key]; + if (state.Errors.Any()) { - if (!string.IsNullOrEmpty(msg)) - { - msg += " | "; - } + // 获取字段名称 + msgBuilder.Append($"字段: {key}, "); - msg += err.ErrorMessage; + // 获取字段的值 + var attemptedValue = state.AttemptedValue; + msgBuilder.Append($"值: {attemptedValue}, "); + + // 获取错误信息 + var errors = string.Join(" | ", state.Errors.Select(e => e.ErrorMessage)); + msgBuilder.Append($"错误: {errors}; "); } } - if (!string.IsNullOrEmpty(msg)) - { - logger.Info($"请求参数错误,{msg}"); - ApiResult response = new((int)ResultCode.PARAM_ERROR, msg); - context.Result = new JsonResult(response); - } - - - + string msg = msgBuilder.ToString(); + logger.Info(msg); + + // 返回错误响应 + ApiResult response = new((int)ResultCode.PARAM_ERROR, msg); + context.Result = new JsonResult(response); + + // 直接返回,不再执行后续逻辑 + return Task.CompletedTask; + } + + // 如果模型校验通过,继续执行后续逻辑 return base.OnActionExecutionAsync(context, next); }