BaseController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using ERP.Framework.Exceptions;
  2. using Microsoft.AspNetCore.Mvc;
  3. namespace ERP.Framework.WebApi
  4. {
  5. [ApiController]
  6. public class BaseController : ControllerBase
  7. {
  8. /// <summary>
  9. /// 请求成功后返回Json数据
  10. /// </summary>
  11. /// <returns></returns>
  12. protected IActionResult Success()
  13. {
  14. return new JsonResult(new
  15. {
  16. code = ErrorCode.SUCCESS,
  17. message = "Success",
  18. });
  19. }
  20. /// <summary>
  21. /// 请求成功后返回Json数据
  22. /// </summary>
  23. /// <param name="message">返回消息</param>
  24. /// <returns></returns>
  25. protected IActionResult Success(string message)
  26. {
  27. return new JsonResult(new
  28. {
  29. code = ErrorCode.SUCCESS,
  30. message
  31. });
  32. }
  33. /// <summary>
  34. /// 请求成功后返回Json数据
  35. /// </summary>
  36. /// <typeparam name="T">Data 类型(Data不能为string类型)</typeparam>
  37. /// <param name="data">返回数据</param>
  38. /// <returns></returns>
  39. protected IActionResult Success<T>(T data)
  40. {
  41. return new JsonResult(new
  42. {
  43. code = ErrorCode.SUCCESS,
  44. data,
  45. message = "Success"
  46. });
  47. }
  48. /// <summary>
  49. /// 请求成功后返回Json数据
  50. /// </summary>
  51. /// <typeparam name="T">Data 类型</typeparam>
  52. /// <param name="message">返回消息</param>
  53. /// <param name="data">返回数据</param>
  54. /// <returns></returns>
  55. protected IActionResult Success<T>(string message, T data)
  56. {
  57. return new JsonResult(new
  58. {
  59. code = ErrorCode.SUCCESS,
  60. data,
  61. message
  62. });
  63. }
  64. /// <summary>
  65. ///
  66. /// </summary>
  67. /// <returns></returns>
  68. protected IActionResult Error()
  69. {
  70. return new BadRequestObjectResult(new
  71. {
  72. code = ErrorCode.ERROR,
  73. message = "Failed"
  74. });
  75. }
  76. protected IActionResult Error(string message)
  77. {
  78. return new BadRequestObjectResult(new
  79. {
  80. code = ErrorCode.ERROR,
  81. message
  82. });
  83. }
  84. protected IActionResult Error(int code, string message)
  85. {
  86. return new BadRequestObjectResult(new
  87. {
  88. code,
  89. message
  90. });
  91. }
  92. protected IActionResult Error<T>(T data)
  93. {
  94. return new BadRequestObjectResult(new
  95. {
  96. code = ErrorCode.ERROR,
  97. data,
  98. message = "Failed"
  99. });
  100. }
  101. protected IActionResult Error<T>(string message, T data)
  102. {
  103. return new BadRequestObjectResult(new
  104. {
  105. code = ErrorCode.ERROR,
  106. data,
  107. message
  108. });
  109. }
  110. /// <summary>
  111. /// 判断是否成功后返回
  112. /// </summary>
  113. /// <returns></returns>
  114. protected IActionResult Result(bool isSuccess)
  115. {
  116. if (isSuccess)
  117. {
  118. return Success();
  119. }
  120. else
  121. {
  122. return Error();
  123. }
  124. }
  125. protected IActionResult Result<T>(int code, string message, T data)
  126. {
  127. return new BadRequestObjectResult(new
  128. {
  129. code,
  130. data,
  131. message
  132. });
  133. }
  134. protected IActionResult BadResult<T>(int httpErrorCode, int code, T data, string message)
  135. {
  136. var result = new BadRequestObjectResult(new
  137. {
  138. code,
  139. data,
  140. message
  141. });
  142. result.StatusCode = httpErrorCode;
  143. return result;
  144. }
  145. //public override Task ExecuteResultAsync(ActionContext context)
  146. //{
  147. // //if (context.ModelState.IsValid)
  148. // //{
  149. // // return base.ExecuteResultAsync(context);
  150. // //}
  151. // //else
  152. // //{
  153. // // var errors = context.ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage);
  154. // // return context.HttpContext.Response.WriteAsJsonAsync(new { success = false, message = "Validation failed", errors });
  155. // //}
  156. //}
  157. }
  158. }