You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
4.4 KiB
138 lines
4.4 KiB
namespace Admin.NET.Core;
|
|
|
|
/// <summary>
|
|
/// 全局规范化结果
|
|
/// </summary>
|
|
[UnifyModel(typeof(AdminResult<>))]
|
|
public class AdminResultProvider : IUnifyResultProvider
|
|
{
|
|
/// <summary>
|
|
/// 异常返回值
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="metadata"></param>
|
|
/// <returns></returns>
|
|
public IActionResult OnException(ExceptionContext context, ExceptionMetadata metadata)
|
|
{
|
|
return new JsonResult(RESTfulResult(metadata.StatusCode, data: metadata.Data, errors: metadata.Errors));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 成功返回值
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public IActionResult OnSucceeded(ActionExecutedContext context, object data)
|
|
{
|
|
return new JsonResult(RESTfulResult(StatusCodes.Status200OK, true, data));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证失败返回值
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="metadata"></param>
|
|
/// <returns></returns>
|
|
public IActionResult OnValidateFailed(ActionExecutingContext context, ValidationMetadata metadata)
|
|
{
|
|
return new JsonResult(RESTfulResult(metadata.StatusCode ?? StatusCodes.Status400BadRequest, data: metadata.Data, errors: metadata.ValidationResult));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 特定状态码返回值
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="statusCode"></param>
|
|
/// <param name="unifyResultSettings"></param>
|
|
/// <returns></returns>
|
|
public async Task OnResponseStatusCodes(HttpContext context, int statusCode, UnifyResultSettingsOptions unifyResultSettings)
|
|
{
|
|
// 设置响应状态码
|
|
UnifyContext.SetResponseStatusCodes(context, statusCode, unifyResultSettings);
|
|
|
|
switch (statusCode)
|
|
{
|
|
// 处理 401 状态码
|
|
case StatusCodes.Status401Unauthorized:
|
|
await context.Response.WriteAsJsonAsync(RESTfulResult(statusCode, errors: "401 登录已过期,请重新登录"),
|
|
App.GetOptions<JsonOptions>()?.JsonSerializerOptions);
|
|
break;
|
|
// 处理 403 状态码
|
|
case StatusCodes.Status403Forbidden:
|
|
await context.Response.WriteAsJsonAsync(RESTfulResult(statusCode, errors: "403 禁止访问,没有权限"),
|
|
App.GetOptions<JsonOptions>()?.JsonSerializerOptions);
|
|
break;
|
|
|
|
default: break;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回 RESTful 风格结果集
|
|
/// </summary>
|
|
/// <param name="statusCode"></param>
|
|
/// <param name="succeeded"></param>
|
|
/// <param name="data"></param>
|
|
/// <param name="errors"></param>
|
|
/// <returns></returns>
|
|
private static AdminResult<object> RESTfulResult(int statusCode, bool succeeded = default, object data = default, object errors = default)
|
|
{
|
|
//// 统一返回值脱敏处理
|
|
//if (data?.GetType() == typeof(String))
|
|
//{
|
|
// data = App.GetRequiredService<ISensitiveDetectionProvider>().ReplaceAsync(data.ToString(), '*').GetAwaiter().GetResult();
|
|
//}
|
|
//else if (data?.GetType() == typeof(JsonResult))
|
|
//{
|
|
// data = App.GetRequiredService<ISensitiveDetectionProvider>().ReplaceAsync(JSON.Serialize(data), '*').GetAwaiter().GetResult();
|
|
//}
|
|
|
|
return new AdminResult<object>
|
|
{
|
|
Code = statusCode,
|
|
Message = errors is null or string ? (errors + "") : JSON.Serialize(errors),
|
|
Result = data,
|
|
Type = succeeded ? "success" : "error",
|
|
Extras = UnifyContext.Take(),
|
|
Time = DateTime.Now
|
|
};
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 全局返回结果
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public class AdminResult<T>
|
|
{
|
|
/// <summary>
|
|
/// 状态码
|
|
/// </summary>
|
|
public int Code { get; set; }
|
|
|
|
/// <summary>
|
|
/// 类型success、warning、error
|
|
/// </summary>
|
|
public string Type { get; set; }
|
|
|
|
/// <summary>
|
|
/// 错误信息
|
|
/// </summary>
|
|
public string Message { get; set; }
|
|
|
|
/// <summary>
|
|
/// 数据
|
|
/// </summary>
|
|
public T Result { get; set; }
|
|
|
|
/// <summary>
|
|
/// 附加数据
|
|
/// </summary>
|
|
public object Extras { get; set; }
|
|
|
|
/// <summary>
|
|
/// 时间
|
|
/// </summary>
|
|
public DateTime Time { get; set; }
|
|
}
|