Przeglądaj źródła

异常处理与业务逻辑优化,Result结构调整

优化异常处理中返回码与消息,Result类新增Msg字段,业务异常改为Result返回,移除无用字段,简化部分提示语,提升接口友好性与规范性。
Lyyyi 1 dzień temu
rodzic
commit
3baf6d9df3

+ 2 - 2
OASystem/OASystem.Api/Middlewares/ExceptionHandlingMiddleware.cs

@@ -68,7 +68,7 @@ namespace OASystem.API.Middlewares
                 var response = context.Response;
                 var errorResponse = new JsonView
                 {
-                    Code = StatusCodes.Status400BadRequest,
+                    Code = StatusCodes.Status500InternalServerError,
                     Data = ""
                 };
 
@@ -110,7 +110,7 @@ namespace OASystem.API.Middlewares
                         break;
                     default:
                         response.StatusCode = StatusCodes.Status400BadRequest;
-                        errorResponse.Msg = "服务器内部错误"; // 不直接暴露异常详细信息
+                        errorResponse.Msg = exception.Message; // 不直接暴露异常详细信息
                         break;
                 }
 

+ 18 - 1
OASystem/OASystem.Domain/Result.cs

@@ -31,32 +31,49 @@
         /// </summary>
         public int Code { get; set; }
 
+
+        /// <summary>
+        /// 错误消息
+        /// </summary>
+        public string Msg{ get; set; }
+
         /// <summary>
         /// 错误详情
         /// </summary>
-        public object Data { get; set; }
+        public new object Data { get; set; }
 
         public BusinessException(string message) : base(message)
         {
             Code = 400;
+            Msg = message;
         }
 
         public BusinessException(int code, string message) : base(message)
         {
             Code = code;
+            Msg = message;
         }
 
         public BusinessException(string message, object details) : base(message)
         {
             Code = 400;
+            Msg = message;
             Data = details;
         }
 
         public BusinessException(int code, string message, object details) : base(message)
         {
             Code = code;
+            Msg = message;
             Data = details;
         }
+
+        public BusinessException(string message, Exception innerException)
+        : base(message, innerException)
+        {
+            Code = 400;
+            Msg = message;
+        }
     }
 
 

+ 0 - 1
OASystem/OASystem.Domain/ViewModels/Groups/ProcessOverView.cs

@@ -56,7 +56,6 @@ namespace OASystem.Domain.ViewModels.Groups
         public string Operator { get; set; }
         public string OpeateTime { get; set; }
         public string ActualDone { get; set; }
-
         public bool IsEnaFileUpBtn { get; set; }
         public bool IsFileUp { get; set; }
     }

+ 9 - 10
OASystem/OASystem.Infrastructure/Repositories/Groups/ProcessOverviewRepository.cs

@@ -2171,7 +2171,7 @@ namespace OASystem.Infrastructure.Repositories.Groups
         public async Task<Result> SetNodeInfoAsync(ConfProcessSetActualDoneDto dto)
         {
             //参与人验证
-            if (dto.Participators?.Any() != true) throw new BusinessException("参与人不能为空。");
+            if (dto.Participators?.Any() != true) return new Result { Code = 400, Msg = "参与人不能为空。" };
 
             var isDtNul = DateTime.TryParse(dto.ActualDone, out DateTime actualDone);
 
@@ -2183,18 +2183,17 @@ namespace OASystem.Infrastructure.Repositories.Groups
 
             // 1. 获取并验证节点和流程
             var node = await _sqlSugar.Queryable<Grp_ConfProcessNode>()
-                .FirstAsync(n => n.Id == nodeId && n.IsDel == 0)
-                ?? throw new BusinessException("当前节点不存在或已被删除。");
+                .FirstAsync(n => n.Id == nodeId && n.IsDel == 0);
+
+            if (node == null) return new Result { Code = 400, Msg = "当前节点不存在或已被删除。" };
 
             // 1.2. 用户权限验证
-            if (!HasConfNodeOperationPermission(node, currUserId))
-            {
-                throw new BusinessException("当前用户没有操作此节点的权限。");
-            }
+            if (!HasConfNodeOperationPermission(node, currUserId)) return new Result { Code = 400, Msg = "当前用户没有操作此节点的权限。" };
 
             var process = await _sqlSugar.Queryable<Grp_ConfProcessOverview>()
-                .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0)
-                ?? throw new BusinessException("当前流程不存在或已被删除。");
+                .FirstAsync(p => p.Id == node.ProcessId && p.IsDel == 0);
+
+            if (process == null) return new Result { Code = 400, Msg = "当前流程不存在或已被删除。" };
 
             // 2.1 存储更新前流程及节点信息
             var nodeBefore = CloneConfNode(node);
@@ -2236,7 +2235,7 @@ namespace OASystem.Infrastructure.Repositories.Groups
 
             }
 
-            return new Result { Code = 200, Msg = "实际操作时间设置成功。" };
+            return new Result { Code = 200, Msg = "设置成功。" };
         }
 
         #endregion