diff --git a/Server/YD_XinWei.Commons/Dto/ResponseMsg.cs b/Server/YD_XinWei.Commons/Dto/ResponseMsg.cs
new file mode 100644
index 0000000..f3e6536
--- /dev/null
+++ b/Server/YD_XinWei.Commons/Dto/ResponseMsg.cs
@@ -0,0 +1,61 @@
+
+
+using YD_XinWei.Commons.Enum;
+
+namespace YD_XinWei.Commons.Dto
+{
+ public static class ResponseMsg
+ {
+ public static string GetMsg(this ResponseType responseType)
+ {
+ string msg;
+ switch (responseType)
+ {
+ case ResponseType.LoginExpiration:
+ msg = "登陆已过期,请重新登陆"; break;
+ case ResponseType.TokenExpiration:
+ msg = "Token已过期,请重新登陆"; break;
+ case ResponseType.AccountLocked:
+ msg = "帐号已被锁定"; break;
+ case ResponseType.LoginSuccess:
+ msg = "登陆成功"; break;
+ case ResponseType.ParametersLack:
+ msg = "参数不完整"; break;
+ case ResponseType.NoPermissions:
+ msg = "没有权限操作"; break;
+ case ResponseType.NoRolePermissions:
+ msg = "角色没有权限操作"; break;
+ case ResponseType.ServerError:
+ msg = "服务器好像出了点问题....."; break;
+ case ResponseType.LoginError:
+ msg = "用户名或密码错误"; break;
+ case ResponseType.SaveSuccess:
+ msg = "保存成功"; break;
+ case ResponseType.NoKey:
+ msg = "没有主键不能编辑"; break;
+ case ResponseType.NoKeyDel:
+ msg = "没有主键不能删除"; break;
+ case ResponseType.KeyError:
+ msg = "主键不正确或没有传入主键"; break;
+ case ResponseType.EidtSuccess:
+ msg = "编辑成功"; break;
+ case ResponseType.DelSuccess:
+ msg = "删除成功"; break;
+ case ResponseType.RegisterSuccess:
+ msg = "注册成功"; break;
+ case ResponseType.AuditSuccess:
+ msg = "审核成功"; break;
+ case ResponseType.ModifyPwdSuccess:
+ msg = "密码修改成功"; break;
+ case ResponseType.OperSuccess:
+ msg = "操作成功"; break;
+ case ResponseType.PINError:
+ msg = "验证码不正确"; break;
+
+ default: msg = responseType.ToString(); break;
+ }
+ return msg;
+ }
+
+ }
+}
diff --git a/Server/YD_XinWei.Commons/Dto/WebResponseContent.cs b/Server/YD_XinWei.Commons/Dto/WebResponseContent.cs
new file mode 100644
index 0000000..f0cdae4
--- /dev/null
+++ b/Server/YD_XinWei.Commons/Dto/WebResponseContent.cs
@@ -0,0 +1,84 @@
+
+
+using YD_XinWei.Commons.Enum;
+
+namespace YD_XinWei.Commons.Dto
+{
+ public class WebResponseContent
+ {
+ public WebResponseContent()
+ {
+ }
+ public WebResponseContent(bool Success)
+ {
+ this.Success = Success;
+ }
+ public bool Success { get; set; }
+ public string ErrorCode { get; set; }
+ public string ErrorMsg { get; set; }
+ //public string ErrorMsg { get; set; }
+ public object Data { get; set; }
+
+ public WebResponseContent OK()
+ {
+ this.Success = true;
+ return this;
+ }
+
+ public static WebResponseContent Instance
+ {
+ get { return new WebResponseContent(); }
+ }
+ public WebResponseContent OK(string ErrorMsg = null,object data=null)
+ {
+ this.Success = true;
+ this.ErrorMsg = ErrorMsg;
+ this.Data = data;
+ return this;
+ }
+ public WebResponseContent OK(ResponseType responseType)
+ {
+ return Set(responseType, true);
+ }
+ public WebResponseContent Error(string ErrorMsg = null)
+ {
+ this.Success = false;
+ this.ErrorMsg = ErrorMsg;
+ return this;
+ }
+ public WebResponseContent Error(ResponseType responseType)
+ {
+ return Set(responseType, false);
+ }
+ public WebResponseContent Set(ResponseType responseType)
+ {
+ bool? b = null;
+ return this.Set(responseType, b);
+ }
+ public WebResponseContent Set(ResponseType responseType, bool? Success)
+ {
+ return this.Set(responseType, null, Success);
+ }
+ public WebResponseContent Set(ResponseType responseType, string msg)
+ {
+ bool? b = null;
+ return this.Set(responseType, msg, b);
+ }
+ public WebResponseContent Set(ResponseType responseType, string msg, bool? Success)
+ {
+ if (Success != null)
+ {
+ this.Success = (bool)Success;
+ }
+ this.ErrorCode = ((int)responseType).ToString();
+ if (!string.IsNullOrEmpty(msg))
+ {
+ ErrorMsg = msg;
+ return this;
+ }
+ ErrorMsg = responseType.GetMsg();
+ return this;
+ }
+
+ }
+}
diff --git a/Server/YD_XinWei.Commons/Enum/ResponseType.cs b/Server/YD_XinWei.Commons/Enum/ResponseType.cs
new file mode 100644
index 0000000..e7e3bea
--- /dev/null
+++ b/Server/YD_XinWei.Commons/Enum/ResponseType.cs
@@ -0,0 +1,29 @@
+
+
+namespace YD_XinWei.Commons.Enum
+{
+ public enum ResponseType
+ {
+ ServerError = 1,
+ LoginExpiration = 302,
+ ParametersLack = 303,
+ TokenExpiration,
+ PINError,
+ NoPermissions,
+ NoRolePermissions,
+ LoginError,
+ AccountLocked,
+ LoginSuccess,
+ SaveSuccess,
+ AuditSuccess,
+ OperSuccess,
+ RegisterSuccess,
+ ModifyPwdSuccess,
+ EidtSuccess,
+ DelSuccess,
+ NoKey,
+ NoKeyDel,
+ KeyError,
+ Other
+ }
+}
diff --git a/Server/YD_XinWei.Commons/Exceptions/BizException.cs b/Server/YD_XinWei.Commons/Exceptions/BizException.cs
index af7ef4b..e162643 100644
--- a/Server/YD_XinWei.Commons/Exceptions/BizException.cs
+++ b/Server/YD_XinWei.Commons/Exceptions/BizException.cs
@@ -2,46 +2,48 @@
using System.Collections.Generic;
using System.Text;
-namespace YD_XinWei.Commons.Exceptions
+namespace YD_XinWei.Commons.Exceptions
{
///
/// 通过业务异常类
///
public class BizException : Exception
{
- public string ErrorNo { get; } // 业务异常编号
- public string ErrorInfo { get; }// 业务异常信息
+ public string ErrorCode { get; } // 业务异常编号
+ public string ErrorMsg { get; }// 业务异常信息
+
+ public bool Success { get; set; } = false;
public IDictionary Infos { set; get; } // 业务异常详细信息
- public BizException(string errorNo, string errorInfo) : base(errorInfo)
+ public BizException(string ErrorCode, string ErrorMsg) : base(ErrorMsg)
{
- ErrorNo = errorNo;
- ErrorInfo = errorInfo;
+ ErrorCode = ErrorCode;
+ ErrorMsg = ErrorMsg;
}
- public BizException(string errorNo, string errorInfo, Exception e) : base(errorInfo, e)
+ public BizException(string ErrorCode, string ErrorMsg, Exception e) : base(ErrorMsg, e)
{
- ErrorNo = errorNo;
- ErrorInfo = errorInfo;
+ ErrorCode = ErrorCode;
+ ErrorMsg = ErrorMsg;
}
- public BizException(string errorInfo) : base(errorInfo)
+ public BizException(string ErrorMsg) : base(ErrorMsg)
{
- ErrorNo = "-1";
- ErrorInfo = errorInfo;
+ ErrorCode = "-1";
+ ErrorMsg = ErrorMsg;
}
- public BizException(string errorInfo, Exception e) : base(errorInfo, e)
+ public BizException(string ErrorMsg, Exception e) : base(ErrorMsg, e)
{
- ErrorNo = "-1";
- ErrorInfo = errorInfo;
+ ErrorCode = "-1";
+ ErrorMsg = ErrorMsg;
}
public BizException(Exception e)
{
- ErrorNo = "-1";
- ErrorInfo = e.Message;
+ ErrorCode = "-1";
+ ErrorMsg = e.Message;
}
}
}
diff --git a/Server/YD_XinWei.Commons/Exceptions/CommonException.cs b/Server/YD_XinWei.Commons/Exceptions/CommonException.cs
index b688f23..346a2ef 100644
--- a/Server/YD_XinWei.Commons/Exceptions/CommonException.cs
+++ b/Server/YD_XinWei.Commons/Exceptions/CommonException.cs
@@ -2,11 +2,12 @@
using System.Collections.Generic;
using System.Text;
-namespace YD_XinWei.Commons.Exceptions
+namespace YD_XinWei.Commons.Exceptions
{
public class CommonException
{
- public string ErrorNo; // 错误编号 0001 0002 0033 0004
- public string ErrorInfo;// 规范错误信息
+ public string ErrorCode; // 错误编号 0001 0002 0033 0004
+ public string ErrorMsg;// 规范错误信息
+ public bool Success;
}
}
diff --git a/Server/YD_XinWei.Commons/Exceptions/Handlers/BizExceptionHandler.cs b/Server/YD_XinWei.Commons/Exceptions/Handlers/BizExceptionHandler.cs
index 476ffe2..5b55dc5 100644
--- a/Server/YD_XinWei.Commons/Exceptions/Handlers/BizExceptionHandler.cs
+++ b/Server/YD_XinWei.Commons/Exceptions/Handlers/BizExceptionHandler.cs
@@ -4,8 +4,9 @@ using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Text;
+using static Aliyun.OSS.Model.ListPartsResult;
-namespace YD_XinWei.Commons.Exceptions.Handlers
+namespace YD_XinWei.Commons.Exceptions.Handlers
{
///
/// 自定义业务异常处理,转换成为json格式
@@ -19,8 +20,9 @@ namespace YD_XinWei.Commons.Exceptions.Handlers
{
// 1.1 将异常转换成为json结果
dynamic exceptionResult = new ExpandoObject();
- exceptionResult.ErrorNo = bizException.ErrorNo;
- exceptionResult.ErrorInfo = bizException.ErrorInfo;
+ exceptionResult.Success = false;
+ exceptionResult.ErrorCode = bizException.ErrorCode;
+ exceptionResult.ErrorMsg = bizException.ErrorMsg;
if (bizException.Infos != null)
{
exceptionResult.infos = bizException.Infos;
@@ -31,8 +33,8 @@ namespace YD_XinWei.Commons.Exceptions.Handlers
{
// 1.2 处理其他类型异常Exception
dynamic exceptionResult = new ExpandoObject();
- exceptionResult.ErrorNo = -1;
- exceptionResult.ErrorInfo = context.Exception.Message;
+ exceptionResult.ErrorCode = -1;
+ exceptionResult.ErrorMsg = context.Exception.Message;
// 1.3 包装异常信息进行异常返回
context.Result = new JsonResult(exceptionResult);
diff --git a/Server/YD_XinWei.Commons/Filters/CommonResult.cs b/Server/YD_XinWei.Commons/Filters/CommonResult.cs
deleted file mode 100644
index a03ff70..0000000
--- a/Server/YD_XinWei.Commons/Filters/CommonResult.cs
+++ /dev/null
@@ -1,42 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
-
-namespace YD_XinWei.Commons.Filters
-{
- ///
- /// 通用结果
- ///
- public class CommonResult
- {
- public const string SUCCESS = "0";
- public string ErrorNo { set; get; } // 是否成功状态
- public string ErrorInfo { set; get; } // 失败信息
- public IDictionary resultDic { set; get; }// 用于非结果集返回
- public IList> resultList { set; get; }// 用于结果集返回
-
- public dynamic Result { set; get; }// 返回动态结果(通用)
-
- public CommonResult()
- {
- resultDic = new Dictionary();
- resultList = new List>();
- }
-
- public CommonResult(string errorNo, string errorInfo)
- {
- this.ErrorNo = errorNo;
- this.ErrorInfo = errorInfo;
- resultDic = new Dictionary();
- resultList = new List>();
- }
-
- public CommonResult(string errorNo, string erroInfo, IDictionary resultDic, IList> resultList) : this(errorNo, erroInfo)
- {
- this.resultDic = resultDic;
- this.resultList = resultList;
- this.resultDic = resultDic;
- this.resultList = resultList;
- }
- }
-}
diff --git a/Server/YD_XinWei.Commons/Filters/CustomApiResponseFilter.cs b/Server/YD_XinWei.Commons/Filters/CustomApiResponseFilter.cs
new file mode 100644
index 0000000..a51758b
--- /dev/null
+++ b/Server/YD_XinWei.Commons/Filters/CustomApiResponseFilter.cs
@@ -0,0 +1,140 @@
+using Microsoft.AspNetCore.Mvc.Filters;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Http;
+using YD_XinWei.Commons.Dto;
+
+namespace VYD_XinWei.Commons.Filters
+{
+ public class DictionaryException : Exception
+ {
+ public Dictionary ErrorData { get; set; }
+ //public override string Message => ErrorData??.tojson();
+ }
+ public class CustomApiResponseFilter : IResultFilter
+ {
+ public void OnResultExecuted(ResultExecutedContext context)
+ {
+
+ }
+
+ public void OnResultExecuting(ResultExecutingContext context)
+ {
+ // 检查是否为 FileResult,如果是,则不进行任何操作
+ if (context.Result is FileResult)
+ // 如果是文件结果,保持原样
+ return;
+ // 检查结果是否为 ObjectResult
+ if (context.Result is ObjectResult objectResult)
+ {
+ //LoggerType loggerType = LoggerType.Info;
+ WebResponseContent webResponseContent = new WebResponseContent() { Data= objectResult.Value };
+ // 如果没有设置状态码,默认为 200
+ if (objectResult.StatusCode==null|| objectResult.StatusCode.Value == 200)
+ {
+ webResponseContent.ErrorCode = StatusCodes.Status200OK.ToString();
+ webResponseContent.Success = true;
+ }
+ else
+ {
+ webResponseContent.ErrorCode = objectResult.StatusCode.Value.ToString();
+ webResponseContent.Success = false;
+ webResponseContent.ErrorMsg = "服务器内部状态异常";
+ //loggerType=LoggerType.Exception;
+ }
+ JsonResult res = new JsonResult(webResponseContent) { StatusCode = StatusCodes.Status200OK };
+ context.Result = res;
+ //// 获取 HttpContext
+ //var httpContext = context.HttpContext;
+ //var request = HttpRequestResponseFormat.FormatRequest(httpContext.Request).Result;
+ //VOL.Core.Services.Logger.OK(loggerType, request,webResponseContent.Serialize());
+
+ }
+ }
+ }
+ public class ExceptionFilter : IExceptionFilter
+ {
+ private readonly Dictionary _exceptions;
+ public ExceptionFilter()
+ {
+ _exceptions = new Dictionary
+ {
+ {typeof(ArgumentException),"1001"} ,
+ {typeof(DictionaryException),"1002"} ,
+ };
+ }
+ void IExceptionFilter.OnException(ExceptionContext context)
+ {
+ Exception exception = context.Exception;
+ int statusCode = 500;
+ string message = null;
+ string code = "500";
+ object data = null;
+ if(exception is CustomException customException)
+ {
+ message = customException.Message;
+ code = customException.Code;
+ data = customException.GetResult();
+ statusCode = customException.Status;
+ }
+ if (_exceptions.ContainsKey(exception.GetType()))
+ {
+ code = _exceptions[exception.GetType()];
+ message = exception.Message;
+ }
+ else
+ message = exception.Message;
+ WebResponseContent exceptionJsonResult = new WebResponseContent() { ErrorMsg = message, ErrorCode = code, Data = data, Success = false };
+ context.Result = new JsonResult(exceptionJsonResult) {StatusCode = StatusCodes.Status200OK };
+ //// 获取 HttpContext
+ //var httpContext = context.HttpContext;
+ //var request = HttpRequestResponseFormat.FormatRequest(httpContext.Request).Result;
+ //VOL.Core.Services.Logger.OK(LoggerType.Exception, request, exceptionJsonResult.Serialize());
+ }
+ }
+ public abstract class CustomException : Exception
+ {
+ public CustomException(string? message) : base(message)
+ {
+
+ }
+ public CustomException(string? message, Exception? innerException) : base()
+ {
+ }
+ public virtual int Status { get;protected set; } = 500;
+ public abstract string Code { get; set; }
+ public abstract object GetResult();
+
+ }
+ public abstract class CustomException : CustomException
+ {
+ public CustomException(string? message) : base(message)
+ {
+ }
+
+ public CustomException(string? message, Exception? innerException) : base(message, innerException)
+ {
+ }
+
+ public virtual T Result { get; set; }
+ public override object GetResult()
+ {
+ return Result;
+ }
+ }
+ public class CustomDeleteIdsException : CustomException>
+ {
+ public override string Code { get; set; } = "2001";
+ public CustomDeleteIdsException(List