平安校园
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.

SafeCampusResultProvider.cs 3.6 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 
  2. //
  3. namespace SafeCampus.Core;
  4. /// <summary>
  5. /// 规范化RESTful风格返回值
  6. /// </summary>
  7. [SuppressSniffer]
  8. [UnifyModel(typeof(SafeCampusResult<>))]
  9. public class SafeCampusResultProvider : IUnifyResultProvider
  10. {
  11. public IActionResult OnAuthorizeException(DefaultHttpContext context, ExceptionMetadata metadata)
  12. {
  13. return new JsonResult(ResTfulResult(metadata.StatusCode, data: metadata.Data, errors: metadata.Errors),
  14. UnifyContext.GetSerializerSettings(context));
  15. }
  16. /// <summary>
  17. /// 异常返回
  18. /// </summary>
  19. /// <param name="context"></param>
  20. /// <param name="metadata"></param>
  21. /// <returns></returns>
  22. public IActionResult OnException(ExceptionContext context, ExceptionMetadata metadata)
  23. {
  24. return new JsonResult(ResTfulResult(metadata.StatusCode, data: metadata.Data, errors: metadata.Errors));
  25. }
  26. /// <summary>
  27. /// 成功返回
  28. /// </summary>
  29. /// <param name="context"></param>
  30. /// <param name="data"></param>
  31. /// <returns></returns>
  32. public IActionResult OnSucceeded(ActionExecutedContext context, object data)
  33. {
  34. return new JsonResult(ResTfulResult(StatusCodes.Status200OK, true, data));
  35. }
  36. /// <summary>
  37. /// 验证失败返回
  38. /// </summary>
  39. /// <param name="context"></param>
  40. /// <param name="metadata"></param>
  41. /// <returns></returns>
  42. public IActionResult OnValidateFailed(ActionExecutingContext context, ValidationMetadata metadata)
  43. {
  44. return new JsonResult(ResTfulResult(metadata.StatusCode ?? StatusCodes.Status400BadRequest, data: metadata.Data,
  45. errors: metadata.FirstErrorMessage ?? metadata.Message));
  46. }
  47. /// <summary>
  48. /// 状态码响应拦截
  49. /// </summary>
  50. /// <param name="context"></param>
  51. /// <param name="statusCode"></param>
  52. /// <param name="unifyResultSettings"></param>
  53. /// <returns></returns>
  54. public async Task OnResponseStatusCodes(HttpContext context, int statusCode, UnifyResultSettingsOptions unifyResultSettings = null)
  55. {
  56. // 设置响应状态码
  57. UnifyContext.SetResponseStatusCodes(context, statusCode, unifyResultSettings);
  58. switch (statusCode)
  59. {
  60. // 处理 401 状态码
  61. case StatusCodes.Status401Unauthorized:
  62. await context.Response.WriteAsJsonAsync(ResTfulResult(statusCode, errors: "登录已过期,请重新登录"),
  63. App.GetOptions<JsonOptions>()?.JsonSerializerOptions);
  64. break;
  65. // 处理 403 状态码
  66. case StatusCodes.Status403Forbidden:
  67. await context.Response.WriteAsJsonAsync(ResTfulResult(statusCode, errors: "禁止访问,没有权限", data: context.Request.Path),
  68. App.GetOptions<JsonOptions>()?.JsonSerializerOptions);
  69. break;
  70. }
  71. }
  72. /// <summary>
  73. /// 返回 RESTful 风格结果集
  74. /// </summary>
  75. /// <param name="statusCode">状态码</param>
  76. /// <param name="succeeded">是否成功</param>
  77. /// <param name="data">数据</param>
  78. /// <param name="errors">错误信息</param>
  79. /// <returns></returns>
  80. private static SafeCampusResult<object> ResTfulResult(int statusCode, bool succeeded = default, object data = default,
  81. object errors = default)
  82. {
  83. return new SafeCampusResult<object>
  84. {
  85. Code = statusCode,
  86. Msg = statusCode == StatusCodes.Status200OK ? "请求成功" : errors,
  87. Data = data,
  88. Extras = UnifyContext.Take(),
  89. Time = DateTime.Now
  90. };
  91. }
  92. }