平安校园
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

98 rindas
3.6 KiB

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