Browse Source

修改返回格式

master
fajiao 3 years ago
parent
commit
ab8c20be14
  1. 3
      BaseModule/EC.App/Core/ApiController.cs
  2. 4
      BaseModule/EC.App/Core/BLL.cs
  3. 4
      BaseModule/EC.App/Core/IBLL.cs
  4. 4
      BaseModule/EC.App/Core/IService.cs
  5. 4
      BaseModule/EC.App/Core/Service.cs
  6. 35
      BaseModule/EC.App/Entity/RespParam.cs
  7. 0
      BaseModule/EC.App/Extensions/RESTfulResultProvider.cs
  8. 83
      BaseModule/EC.App/Extensions/RespParamProvider.cs
  9. 41
      BaseModule/EC.App/Factory/RespParamFactory.cs
  10. 4
      OnvifWebServer/Startup.cs

3
BaseModule/EC.App/Core/ApiController.cs

@ -1,4 +1,5 @@
using Furion.DynamicApiController; using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace EC.App.Core namespace EC.App.Core

4
BaseModule/EC.App/Core/BLL.cs

@ -1,4 +1,6 @@
namespace EC.App.Core using Furion.DependencyInjection;
namespace EC.App.Core
{ {
public class BLL : IBLL public class BLL : IBLL
{ {

4
BaseModule/EC.App/Core/IBLL.cs

@ -1,4 +1,6 @@
namespace EC.App.Core using Furion.DependencyInjection;
namespace EC.App.Core
{ {
public interface IBLL public interface IBLL
{ {

4
BaseModule/EC.App/Core/IService.cs

@ -1,4 +1,6 @@
namespace EC.App.Core using Furion.DependencyInjection;
namespace EC.App.Core
{ {
public interface IService public interface IService
{ {

4
BaseModule/EC.App/Core/Service.cs

@ -1,4 +1,6 @@
namespace EC.App.Core using Furion.DependencyInjection;
namespace EC.App.Core
{ {
public class Service : IService public class Service : IService
{ {

35
BaseModule/EC.App/Entity/RespParam.cs

@ -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
OnvifWebServer/Extensions/RESTfulResultProvider.cs → BaseModule/EC.App/Extensions/RESTfulResultProvider.cs

83
BaseModule/EC.App/Extensions/RespParamProvider.cs

@ -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;
}
}
}
}

41
BaseModule/EC.App/Factory/RespParamFactory.cs

@ -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);
}
}
}

4
OnvifWebServer/Startup.cs

@ -1,9 +1,9 @@
using EC.App.Extensions;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using OnvifWebServer.Extensions;
namespace OnvifWebServer namespace OnvifWebServer
{ {
@ -19,7 +19,7 @@ namespace OnvifWebServer
// This method gets called by the runtime. Use this method to add services to the container. // This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddControllers().AddInject().AddUnifyResult<RESTfulResultProvider>(); // Ìí¼Ó AddInject(); services.AddControllers().AddInject().AddUnifyResult<RespParamProvider>(); // Ìí¼Ó AddInject();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Loading…
Cancel
Save