AppResult.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\utils;
  3. /**
  4. * Author:Panda
  5. * Email:joeyoung0314@qq.com
  6. * Class AppResult
  7. * @package App\Utils
  8. */
  9. class AppResult
  10. {
  11. /**==============应用返回封装==============**/
  12. /**
  13. * 成功返回200
  14. * @param string $message
  15. * @param $result
  16. * @return string json
  17. */
  18. public static function response200(string $message = 'success', $result = null): string
  19. {
  20. return self::response(200, $message, $result);
  21. }
  22. /**
  23. * 失败返回201
  24. * @param string $message
  25. * @param $result
  26. * @return string json
  27. */
  28. public static function response201(string $message = 'error', $result = null): string
  29. {
  30. return self::response(201, $message, $result);
  31. }
  32. /**
  33. * 参数缺失返回203
  34. * @param string $message
  35. * @param $result
  36. * @return string json
  37. */
  38. public static function response203(string $message = 'error', $result = null): string
  39. {
  40. return self::response(203, $message, $result);
  41. }
  42. /**
  43. * 授权无用户返回204
  44. * @param string $message
  45. * @param $result
  46. * @return string json
  47. */
  48. public static function response204(string $message = '暂无此用户,请先授权手机号,并登录', $result = null): string
  49. {
  50. return self::response(204, $message, $result);
  51. }
  52. /**
  53. * 请求次数过多返回206
  54. * @param string $message
  55. * @param $result
  56. * @return string json
  57. */
  58. public static function response206(string $message = '请求次数过多', $result = null): string
  59. {
  60. return self::response(206, $message, $result);
  61. }
  62. /**
  63. * 身份信息已过期400
  64. * @param string $message
  65. * @param $result
  66. * @return string json
  67. */
  68. public static function response400(string $message = 'Identity is overdue', $result = null): string
  69. {
  70. return self::response(400, $message, $result);
  71. }
  72. /**
  73. * 致命错误500
  74. * @param string $message
  75. * @param $result
  76. * @return string json
  77. */
  78. public static function response500(string $message = 'Fatal error', $result = null): string
  79. {
  80. return self::response(500, $message, $result);
  81. }
  82. /**
  83. * 权限未拥有777
  84. * @param string $message
  85. * @param $result
  86. * @return string json
  87. */
  88. public static function response777(string $message = 'No Access', $result = null): string
  89. {
  90. return self::response(777, $message, $result);
  91. }
  92. /**
  93. * 返回统一格式
  94. * @param int $code
  95. * @param string $message
  96. * @param $result
  97. * @return string
  98. */
  99. public static function response(int $code, string $message = 'success', $result = null): string
  100. {
  101. return json_encode([
  102. 'code' => $code,
  103. 'message' => $message,
  104. 'result' => $result
  105. ], JSON_UNESCAPED_UNICODE);
  106. }
  107. }