ApiResponse.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace App\Http\Helpers;
  3. use Symfony\Component\HttpFoundation\Response as FoundationResponse;
  4. trait ApiResponse
  5. {
  6. /**
  7. * @var int
  8. */
  9. protected $statusCode = FoundationResponse::HTTP_OK;
  10. protected $token = '';
  11. /**
  12. * @return mixed
  13. */
  14. public function getStatusCode()
  15. {
  16. return $this->statusCode;
  17. }
  18. /**
  19. * @param $statusCode
  20. * @return $this
  21. */
  22. public function setStatusCode($statusCode)
  23. {
  24. $this->statusCode = $statusCode;
  25. return $this;
  26. }
  27. /**
  28. * @param $token
  29. * @return $this
  30. */
  31. public function setToken($token)
  32. {
  33. $this->token = $token;
  34. return $this;
  35. }
  36. /**
  37. * @param $data
  38. * @return \Illuminate\Http\JsonResponse
  39. */
  40. public function respond($data)
  41. {
  42. $response = response()->json($data, $this->getStatusCode());
  43. if ($this->token) {
  44. $response->headers->set('Authorization', 'Bearer ' . $this->token);
  45. }
  46. return $response;
  47. }
  48. /**
  49. * @param $status
  50. * @param array $data
  51. * @param null $code
  52. * @return mixed
  53. */
  54. public function status($status, array $data, $code = null)
  55. {
  56. if ($code) {
  57. $this->setStatusCode($code);
  58. }
  59. $status = [
  60. 'status' => $status,
  61. 'code' => $this->statusCode
  62. ];
  63. $data = array_merge($status, $data);
  64. return $this->respond($data);
  65. }
  66. /**
  67. * @param $message
  68. * @param int $code
  69. * @param string $status
  70. * @return mixed
  71. */
  72. /*
  73. * 格式
  74. * data:
  75. * code:422
  76. * message:xxx
  77. * status:'error'
  78. */
  79. public function failed($message, $code = FoundationResponse::HTTP_BAD_REQUEST, $status = 'error')
  80. {
  81. return $this->setStatusCode($code)->message($message, $status);
  82. }
  83. /**
  84. * @param $message
  85. * @param string $status
  86. * @return mixed
  87. */
  88. public function message($message, $status = "success")
  89. {
  90. if(!is_array($message)) {
  91. $message = [$message];
  92. }
  93. return $this->status($status, [
  94. 'message' => $message
  95. ]);
  96. }
  97. /**
  98. * @param string $message
  99. * @return mixed
  100. */
  101. public function internalError($message = "Internal Error!")
  102. {
  103. return $this->failed($message, FoundationResponse::HTTP_INTERNAL_SERVER_ERROR);
  104. }
  105. /**
  106. * @param string $message
  107. * @return mixed
  108. */
  109. public function created($message = "created")
  110. {
  111. return $this->setStatusCode(FoundationResponse::HTTP_CREATED)
  112. ->message($message);
  113. }
  114. /**
  115. * @param $data
  116. * @param string $status
  117. * @return mixed
  118. */
  119. public function success($data, $status = "success")
  120. {
  121. return $this->status($status, compact('data'));
  122. }
  123. /**
  124. * @param string $message
  125. * @return mixed
  126. */
  127. public function notFond($message = 'Not Fond!')
  128. {
  129. return $this->failed($message, Foundationresponse::HTTP_NOT_FOUND);
  130. }
  131. }