forked from fajiao/OnvifServer-CSharp
fajiao
3 years ago
10 changed files with 175 additions and 7 deletions
@ -0,0 +1,35 @@ |
|||
namespace EC.App.Entity |
|||
{ |
|||
public class RespParam<T> |
|||
{ |
|||
/// <summary>
|
|||
/// 返回码
|
|||
/// </summary>
|
|||
public int Code { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 是否成功
|
|||
/// </summary>
|
|||
public bool IsSuccess { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 返回数据
|
|||
/// </summary>
|
|||
public T Data { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 错误信息
|
|||
/// </summary>
|
|||
public object Errors { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 附加数据
|
|||
/// </summary>
|
|||
public object Extras { get; set; } |
|||
|
|||
/// <summary>
|
|||
/// 时间戳
|
|||
/// </summary>
|
|||
public long Timestamp { get; set; } |
|||
} |
|||
} |
@ -0,0 +1,83 @@ |
|||
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; |
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,41 @@ |
|||
using EC.App.Entity; |
|||
using Furion.UnifyResult; |
|||
using Microsoft.AspNetCore.Mvc; |
|||
using System; |
|||
|
|||
namespace EC.App.Factory |
|||
{ |
|||
public static class RespParamFactory |
|||
{ |
|||
public static RespParam<object> New(int code, bool isSuccess = default, object data = default, object errors = default) |
|||
{ |
|||
return new RespParam<object> |
|||
{ |
|||
Code = code, |
|||
IsSuccess = isSuccess, |
|||
Data = data, |
|||
Errors = errors, |
|||
Extras = UnifyContext.Take(), |
|||
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() |
|||
}; |
|||
} |
|||
|
|||
public static RespParam<object> New(Enum code, bool isSuccess = default, object data = default, object errors = default) |
|||
{ |
|||
return new RespParam<object> |
|||
{ |
|||
Code = code.GetHashCode(), |
|||
IsSuccess = isSuccess, |
|||
Data = data, |
|||
Errors = errors, |
|||
Extras = UnifyContext.Take(), |
|||
Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() |
|||
}; |
|||
} |
|||
|
|||
public static JsonResult ToJsonResult(this RespParam<object> respParam) |
|||
{ |
|||
return new JsonResult(respParam); |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue