XnRestfulResultProvider.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using Furion;
  2. using Furion.DataValidation;
  3. using Furion.FriendlyException;
  4. using Furion.UnifyResult;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.AspNetCore.Mvc.Filters;
  8. using System;
  9. using System.Threading.Tasks;
  10. namespace YBEE.EQM.Core
  11. {
  12. /// <summary>
  13. /// 规范化RESTful风格返回值
  14. /// </summary>
  15. [UnifyModel(typeof(XnRestfulResult<>))]
  16. public class XnRestfulResultProvider : IUnifyResultProvider
  17. {
  18. /// <summary>
  19. /// 异常返回值
  20. /// </summary>
  21. /// <param name="context"></param>
  22. /// <param name="metadata"></param>
  23. /// <returns></returns>
  24. public IActionResult OnException(ExceptionContext context, ExceptionMetadata metadata)
  25. {
  26. AntShowType showType = AntShowType.ERROR_MESSAGE;
  27. int statusCode = metadata.Errors.ToString() == "unauthorized" ? 401 : metadata.StatusCode;
  28. //int errorCode = (int)(metadata.ErrorCode ?? 400);
  29. int errorCode = 500;
  30. if (metadata.Errors.ToString() == "unauthorized") { errorCode = 401; }
  31. if (metadata.OriginErrorCode is ErrorCode && metadata.ErrorCode.ToString() != "")
  32. {
  33. string ec = metadata.ErrorCode.ToString().ToUpper();
  34. string st = ec[..1];
  35. switch (st)
  36. {
  37. case "S": showType = AntShowType.SILENT; break;
  38. case "W": showType = AntShowType.WARN_MESSAGE; break;
  39. case "E": showType = AntShowType.ERROR_MESSAGE; break;
  40. case "N": showType = AntShowType.NOTIFICATION; break;
  41. case "R": showType = AntShowType.REDIRECT; break;
  42. }
  43. if (int.TryParse(ec.AsSpan(1), out int ecode))
  44. {
  45. errorCode = ecode;
  46. }
  47. }
  48. return new JsonResult(RESTfulResult(statusCode, errors: metadata.Errors, errorCode: errorCode, showType: showType));
  49. }
  50. /// <summary>
  51. /// 成功返回值
  52. /// </summary>
  53. /// <param name="context"></param>
  54. /// <param name="data"></param>
  55. /// <returns></returns>
  56. public IActionResult OnSucceeded(ActionExecutedContext context, object data)
  57. {
  58. return new JsonResult(RESTfulResult(StatusCodes.Status200OK, true, data));
  59. }
  60. /// <summary>
  61. /// 验证失败返回值
  62. /// </summary>
  63. /// <param name="context"></param>
  64. /// <param name="metadata"></param>
  65. /// <returns></returns>
  66. public IActionResult OnValidateFailed(ActionExecutingContext context, ValidationMetadata metadata)
  67. {
  68. return new JsonResult(RESTfulResult(metadata.StatusCode ?? StatusCodes.Status400BadRequest, errors: metadata.ValidationResult, errorCode: 500, showType: AntShowType.NOTIFICATION));
  69. }
  70. /// <summary>
  71. /// 处理输出状态码
  72. /// </summary>
  73. /// <param name="context"></param>
  74. /// <param name="statusCode"></param>
  75. /// <param name="unifyResultSettings"></param>
  76. /// <returns></returns>
  77. public async Task OnResponseStatusCodes(HttpContext context, int statusCode, UnifyResultSettingsOptions unifyResultSettings)
  78. {
  79. // 设置响应状态码
  80. UnifyContext.SetResponseStatusCodes(context, statusCode, unifyResultSettings);
  81. switch (statusCode)
  82. {
  83. // 处理 401 状态码
  84. case StatusCodes.Status401Unauthorized:
  85. await context.Response.WriteAsJsonAsync(RESTfulResult(statusCode, errors: "401 未授权或授权已过期"), App.GetOptions<JsonOptions>()?.JsonSerializerOptions);
  86. break;
  87. // 处理 403 状态码
  88. case StatusCodes.Status403Forbidden:
  89. await context.Response.WriteAsJsonAsync(RESTfulResult(statusCode, errors: "403 禁止访问"), App.GetOptions<JsonOptions>()?.JsonSerializerOptions);
  90. break;
  91. default: break;
  92. }
  93. }
  94. /// <summary>
  95. /// 返回 RESTful 风格结果集
  96. /// </summary>
  97. /// <param name="statusCode"></param>
  98. /// <param name="succeeded"></param>
  99. /// <param name="data"></param>
  100. /// <param name="errors"></param>
  101. /// <param name="errorCode"></param>
  102. /// <param name="showType"></param>
  103. /// <returns></returns>
  104. private static XnRestfulResult<object> RESTfulResult(int statusCode, bool succeeded = default, object data = default, object errors = default, int errorCode = 0, AntShowType showType = AntShowType.ERROR_MESSAGE)
  105. {
  106. return new XnRestfulResult<object>
  107. {
  108. Success = succeeded,
  109. Code = statusCode,
  110. ErrorCode = errorCode,
  111. ErrorMessage = errors,
  112. ShowType = showType,
  113. Data = data,
  114. Extras = UnifyContext.Take(),
  115. Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  116. };
  117. }
  118. }
  119. /// <summary>
  120. /// RESTful风格---Antd返回格式
  121. /// </summary>
  122. /// <typeparam name="T"></typeparam>
  123. public class XnRestfulResult<T>
  124. {
  125. /// <summary>
  126. /// 执行成功
  127. /// </summary>
  128. public bool Success { get; set; }
  129. /// <summary>
  130. /// 状态码
  131. /// </summary>
  132. public int? Code { get; set; }
  133. /// <summary>
  134. /// 错误码
  135. /// </summary>
  136. public int? ErrorCode { get; set; } = 0;
  137. /// <summary>
  138. /// 错误信息
  139. /// </summary>
  140. public object ErrorMessage { get; set; }
  141. /// <summary>
  142. /// 前端错误显示级别
  143. /// </summary>
  144. public AntShowType? ShowType { get; set; }
  145. /// <summary>
  146. /// 数据
  147. /// </summary>
  148. public T Data { get; set; }
  149. /// <summary>
  150. /// 附加数据
  151. /// </summary>
  152. public object Extras { get; set; }
  153. /// <summary>
  154. /// 时间戳
  155. /// </summary>
  156. public long Timestamp { get; set; }
  157. }
  158. }