ApiResponseService.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace ln\services;
  3. use think\contract\Arrayable;
  4. use think\response\Json;
  5. class ApiResponseService
  6. {
  7. protected $response;
  8. const DEFAULT_SUCCESS_MESSAGE = 'success';
  9. const DEFAULT_FAIL_MESSAGE = 'fail';
  10. const DEFAULT_SUCCESS_CODE = 200;
  11. const DEFAULT_FAIL_CODE = 400;
  12. public function __construct(Json $response)
  13. {
  14. $this->response = $response;
  15. }
  16. public function code(int $code)
  17. {
  18. $this->response->code($code);
  19. return $this;
  20. }
  21. /**
  22. * @param $data
  23. * @return array|string|null
  24. */
  25. private function parseData($data)
  26. {
  27. if ($data instanceof Arrayable)
  28. return $data->toArray();
  29. else
  30. return $data;
  31. }
  32. /**
  33. * @param int $status
  34. * @param string $message
  35. * @param array|Arrayable|null $data
  36. * @return Json
  37. */
  38. public function make(int $status, string $message, $data = null): Json
  39. {
  40. $content = compact('status', 'message');
  41. if (!is_null($data))
  42. $content['data'] = $this->parseData($data);
  43. $this->response->data($content);
  44. return $this->response;
  45. }
  46. /**
  47. * @param string|array|Arrayable $message
  48. * @param array|Arrayable|null $data
  49. * @return Json
  50. */
  51. public function success($message = self::DEFAULT_SUCCESS_MESSAGE, $data = null)
  52. {
  53. $message = $this->parseData($message);
  54. if (is_array($message)) {
  55. $data = $message;
  56. $message = self::DEFAULT_SUCCESS_MESSAGE;
  57. } else {
  58. $data = $this->parseData($data);
  59. }
  60. return $this->make(self::DEFAULT_SUCCESS_CODE, $message, $data);
  61. }
  62. /**
  63. * @param string|array|Arrayable $message
  64. * @param array|Arrayable|null $data
  65. * @return Json
  66. */
  67. public function fail($message = self::DEFAULT_FAIL_MESSAGE, $data = null)
  68. {
  69. $message = $this->parseData($message);
  70. if (is_array($message)) {
  71. $data = $message;
  72. $message = self::DEFAULT_FAIL_MESSAGE;
  73. } else {
  74. $data = $this->parseData($data);
  75. }
  76. return $this->make(self::DEFAULT_FAIL_CODE, $message, $data);
  77. }
  78. /**
  79. * @param $status
  80. * @param string|array|Arrayable $message
  81. * @param array|Arrayable $result
  82. * @return Json
  83. */
  84. public function status($status, $message, $result = [])
  85. {
  86. $message = $this->parseData($message);
  87. if (is_array($message)) {
  88. $result = $message;
  89. $message = self::DEFAULT_SUCCESS_MESSAGE;
  90. } else {
  91. $result = $this->parseData($result);
  92. }
  93. return $this->make(self::DEFAULT_SUCCESS_CODE, $message, compact('status', 'result'));
  94. }
  95. /**
  96. * @param string $type
  97. * @param $data
  98. * @return Json
  99. * @author xaboy
  100. * @day 2020/6/13
  101. */
  102. public function message(string $type, $data)
  103. {
  104. $this->response->data(compact('type', 'data'));
  105. return $this->response;
  106. }
  107. }