平安校园
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

108 行
3.6 KiB

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