using Furion.DataValidation;
|
using Furion.DependencyInjection;
|
using Furion.FriendlyException;
|
using Furion.UnifyResult;
|
//using Furion.UnifyResult.Internal;
|
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc.Filters;
|
using System.Net;
|
using Yw.Untity;
|
using Yw.Application;
|
|
namespace IStation.WebApi
|
{
|
/// <summary>
|
/// 规范化RESTful风格返回值
|
/// </summary>
|
[SuppressSniffer, UnifyModel(typeof(XnRestfulResult<>))]
|
public class XnRestfulResultProvider : IUnifyResultProvider
|
{
|
/// <summary>
|
/// 异常
|
/// </summary>
|
public IActionResult OnException(ExceptionContext context, ExceptionMetadata metadata)
|
{
|
// 解析异常信息
|
//var exceptionMetadata = UnifyContext.GetExceptionMetadata(context);
|
|
XnRestfulResult<object> result = null;
|
|
//furion异常
|
if (context.Exception is AppFriendlyException friendException)
|
{
|
result = new XnRestfulResult<object>
|
{
|
Code = (int)eResultCode.Alert,
|
Error = friendException.ErrorCode,
|
Message = friendException.ErrorMessage,
|
Data = null,
|
Extras = UnifyContext.Take(),
|
Timestamp = DateTime.Now
|
};
|
}
|
|
//api异常
|
else if (context.Exception is ApiException apiException)
|
{
|
result = new XnRestfulResult<object>
|
{
|
Code = apiException.ResultCode,
|
Error = apiException.ErrorCode,
|
Message = apiException.ErrorMsg,
|
Data = apiException.LogicData,
|
Extras = UnifyContext.Take(),
|
Timestamp = DateTime.Now
|
};
|
}
|
|
else
|
{
|
result = new XnRestfulResult<object>
|
{
|
Code = (int)eResultCode.Error,
|
Error = "系统异常",
|
Message = context.Exception.StackTrace,
|
Data = null,
|
Extras = UnifyContext.Take(),
|
Timestamp = DateTime.Now
|
};
|
}
|
|
if (result.Code == (int)eResultCode.Error)
|
{
|
LogHelper.Error($"错误码:{result.Error},错误信息:{result.Message}");
|
}
|
|
return new JsonResult(result);
|
}
|
|
/// <summary>
|
/// 成功
|
/// </summary>
|
public IActionResult OnSucceeded(ActionExecutedContext context, object data)
|
{
|
switch (context.Result)
|
{
|
// 处理内容结果
|
case ContentResult contentResult:
|
data = contentResult.Content;
|
break;
|
// 处理对象结果
|
case ObjectResult objectResult:
|
data = objectResult.Value;
|
break;
|
case EmptyResult:
|
data = null;
|
break;
|
default:
|
return null;
|
}
|
|
return new JsonResult(new XnRestfulResult<object>
|
{
|
Code = (int)eResultCode.Success,
|
Error = null,
|
Message = "请求成功",
|
Data = data,
|
Extras = UnifyContext.Take(),
|
Timestamp = DateTime.Now
|
});
|
}
|
|
/// <summary>
|
/// 验证失败
|
/// </summary>
|
public IActionResult OnValidateFailed(ActionExecutingContext context, ValidationMetadata metadata)
|
{
|
return new JsonResult(new XnRestfulResult<object>
|
{
|
Code = (int)eResultCode.Prompt,
|
Error = ErrorCodes.V001.ToString(),
|
Message = metadata.Message,
|
Data = null,
|
Extras = UnifyContext.Take(),
|
Timestamp = DateTime.Now
|
});
|
}
|
|
/// <summary>
|
/// 响应状态码
|
/// </summary>
|
public async Task OnResponseStatusCodes(HttpContext context, int statusCode, UnifyResultSettingsOptions unifyResultSettings = null)
|
{
|
// 设置响应状态码
|
UnifyContext.SetResponseStatusCodes(context, statusCode, unifyResultSettings);
|
|
if (Enum.IsDefined(typeof(HttpStatusCode), (HttpStatusCode)statusCode))
|
{
|
await context.Response.WriteAsJsonAsync(new XnRestfulResult<object>
|
{
|
Code = (int)eResultCode.Confirm,
|
Error = ErrorCodes.A099.ToString(),
|
Message = "权限验证失败",
|
Data = null,
|
Extras = UnifyContext.Take(),
|
Timestamp = DateTime.Now
|
});
|
}
|
}
|
}
|
|
|
}
|