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.
103 lines
3.1 KiB
103 lines
3.1 KiB
using Furion;
|
|
using Furion.DataValidation;
|
|
using Furion.DependencyInjection;
|
|
using Furion.UnifyResult;
|
|
using Furion.UnifyResult.Internal;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Filters;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace OnvifWebServer.Extensions
|
|
{
|
|
/// <summary>
|
|
/// RESTful 风格返回值
|
|
/// </summary>
|
|
[SuppressSniffer, UnifyModel(typeof(RESTfulResult<>))]
|
|
public class RESTfulResultProvider : 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, 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(StatusCodes.Status400BadRequest, 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 Unauthorized")
|
|
, App.GetOptions<JsonOptions>()?.JsonSerializerOptions);
|
|
break;
|
|
// 处理 403 状态码
|
|
case StatusCodes.Status403Forbidden:
|
|
await context.Response.WriteAsJsonAsync(RESTfulResult(statusCode, errors: "403 Forbidden")
|
|
, 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 RESTfulResult<object> RESTfulResult(int statusCode, bool succeeded = default, object data = default, object errors = default)
|
|
{
|
|
return new RESTfulResult<object>
|
|
{
|
|
StatusCode = statusCode,
|
|
Succeeded = succeeded,
|
|
Data = data,
|
|
Errors = errors,
|
|
Extras = UnifyContext.Take(),
|
|
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
|
|
};
|
|
}
|
|
}
|
|
}
|