forked from fajiao/OnvifServer-CSharp
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.
83 lines
2.5 KiB
83 lines
2.5 KiB
using EC.App.Entity;
|
|
using EC.App.Factory;
|
|
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 EC.App.Extensions
|
|
{
|
|
/// <summary>
|
|
/// 返回值
|
|
/// </summary>
|
|
[SuppressSniffer, UnifyModel(typeof(RespParam<>))]
|
|
public class RespParamProvider : IUnifyResultProvider
|
|
{
|
|
/// <summary>
|
|
/// 成功返回值
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="data"></param>
|
|
/// <returns></returns>
|
|
public IActionResult OnSucceeded(ActionExecutedContext context, object data)
|
|
{
|
|
return RespParamFactory.New(StatusCodes.Status200OK, true, data).ToJsonResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 验证失败返回值
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="metadata"></param>
|
|
/// <returns></returns>
|
|
public IActionResult OnValidateFailed(ActionExecutingContext context, ValidationMetadata metadata)
|
|
{
|
|
return RespParamFactory.New(StatusCodes.Status400BadRequest, errors: metadata.ValidationResult).ToJsonResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异常返回值
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="metadata"></param>
|
|
/// <returns></returns>
|
|
public IActionResult OnException(ExceptionContext context, ExceptionMetadata metadata)
|
|
{
|
|
return RespParamFactory.New(metadata.StatusCode, errors: metadata.Errors).ToJsonResult();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 特定状态码返回值
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
/// <param name="code"></param>
|
|
/// <param name="unifyResultSettings"></param>
|
|
/// <returns></returns>
|
|
public async Task OnResponseStatusCodes(HttpContext context, int code, UnifyResultSettingsOptions unifyResultSettings = null)
|
|
{
|
|
// 设置响应状态码
|
|
UnifyContext.SetResponseStatusCodes(context, code, unifyResultSettings);
|
|
|
|
switch (code)
|
|
{
|
|
// 处理 401 状态码
|
|
case StatusCodes.Status401Unauthorized:
|
|
await context.Response.WriteAsJsonAsync(RespParamFactory.New(code, errors: "401 Unauthorized")
|
|
, Furion.App.GetOptions<JsonOptions>()?.JsonSerializerOptions);
|
|
break;
|
|
// 处理 403 状态码
|
|
case StatusCodes.Status403Forbidden:
|
|
await context.Response.WriteAsJsonAsync(RespParamFactory.New(code, errors: "403 Forbidden")
|
|
, Furion.App.GetOptions<JsonOptions>()?.JsonSerializerOptions);
|
|
break;
|
|
|
|
default: break;
|
|
}
|
|
}
|
|
}
|
|
}
|