统一返回值修复

master
黄欣 10 months ago
parent b3cef8da06
commit a26068eea7

@ -36,5 +36,17 @@ namespace DS.Module.Core
/// </summary>
[Description("其他")]
Other = -3,
/// <summary>
/// 其他
/// </summary>
[Description("没权限")]
NoPermission = 401,
/// <summary>
///
/// </summary>
[Description("没数据")]
NoDatavailable = 201
}
}

@ -0,0 +1,55 @@
using DS.Module.Core;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace Ds.WMS.WebCore
{
public static class ActionResultHelper
{
/// <summary>
/// 统一返回数据结果
/// </summary>
/// <param name="context"></param>
public static ActionExecutedContext ActionForResult(ActionExecutedContext context)
{
var response = new DataResult
{
Data = (context.Result as ObjectResult)?.Value,
Code = ResultCode.Success,
Message = "操作成功"
};
if (context.Result is ForbidResult)
{
response = new DataResult
{
Data = (context.Result as ObjectResult)?.Value,
Code = ResultCode.NoPermission,
Message = "没有权限"
};
}
else if (context.Result is NotFoundResult)
{
response = new DataResult
{
Data = (context.Result as ObjectResult)?.Value,
Code = ResultCode.NoDatavailable,
Message = "没有数据"
};
}
else if (context.Result is BadRequestResult)
{
response = new DataResult
{
Data = (context.Result as ObjectResult)?.Value,
Code = ResultCode.Fail,
Message = "操作失败"
};
}
context.Result = new ObjectResult(response);
return context;
}
}
}

@ -2,24 +2,28 @@
namespace Ds.WMS.WebCore
{
public class DsAppActionFilter : IActionFilter
public class DsAppActionFilter : ActionFilterAttribute
{
/// <summary>
/// 执行
/// 执行
/// </summary>
/// <param name="context"></param>
public void OnActionExecuted(ActionExecutedContext context)
public void OnActionExecuting(ActionExecutingContext context)
{
}
/// <summary>
/// 执行
/// 执行
/// </summary>
/// <param name="context"></param>
public void OnActionExecuting(ActionExecutingContext context)
public override void OnActionExecuted(ActionExecutedContext context)
{
//统一返回值内容
//记录返回信息
context = ActionResultHelper.ActionForResult(context);
base.OnActionExecuted(context);
}
}
}
Loading…
Cancel
Save