ApiResponseService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use think\contract\Arrayable;
  13. use think\response\Json;
  14. class ApiResponseService
  15. {
  16. protected $response;
  17. const DEFAULT_SUCCESS_MESSAGE = 'success';
  18. const DEFAULT_FAIL_MESSAGE = 'fail';
  19. const DEFAULT_SUCCESS_CODE = 200;
  20. const DEFAULT_FAIL_CODE = 400;
  21. public function __construct(Json $response)
  22. {
  23. $this->response = $response;
  24. }
  25. public function code(int $code)
  26. {
  27. $this->response->code($code);
  28. return $this;
  29. }
  30. /**
  31. * @param $data
  32. * @return array|string|null
  33. */
  34. private function parseData($data)
  35. {
  36. if ($data instanceof Arrayable)
  37. return $data->toArray();
  38. else
  39. return $data;
  40. }
  41. /**
  42. * @param int $status
  43. * @param string $message
  44. * @param array|Arrayable|null $data
  45. * @return Json
  46. */
  47. public function make(int $status, string $message, $data = null): Json
  48. {
  49. $content = compact('status', 'message');
  50. if (!is_null($data))
  51. $content['data'] = $this->parseData($data);
  52. $this->response->data($content);
  53. return $this->response;
  54. }
  55. /**
  56. * @param string|array|Arrayable $message
  57. * @param array|Arrayable|null $data
  58. * @return Json
  59. */
  60. public function success($message = self::DEFAULT_SUCCESS_MESSAGE, $data = null)
  61. {
  62. $message = $this->parseData($message);
  63. if (is_array($message)) {
  64. $data = $message;
  65. $message = self::DEFAULT_SUCCESS_MESSAGE;
  66. } else {
  67. $data = $this->parseData($data);
  68. }
  69. return $this->make(self::DEFAULT_SUCCESS_CODE, $message, $data);
  70. }
  71. public function encode($message = self::DEFAULT_SUCCESS_MESSAGE, $data = null)
  72. {
  73. $message = $this->parseData($message);
  74. if (is_array($message)) {
  75. $data = $message;
  76. $message = self::DEFAULT_SUCCESS_MESSAGE;
  77. } else {
  78. $data = $this->parseData($data);
  79. }
  80. $status = self::DEFAULT_SUCCESS_CODE;
  81. $encode = true;
  82. $content = compact('status','encode', 'message');
  83. if (is_null($data) || env('APP_DEBUG', false))
  84. return $this->success($message, $data['data'] ?? $data);
  85. if (!is_array($data) || !isset($data['data'])) {
  86. $res['data'] = $data;
  87. } else {
  88. $res = $data;
  89. }
  90. $res['status'] = $status;
  91. $res['message'] = $message;
  92. $content['data'] = base64_encode(gzdeflate(json_encode($res,JSON_UNESCAPED_UNICODE),9));
  93. $this->response->data($content);
  94. return $this->response;
  95. }
  96. /**
  97. * @param string|array|Arrayable $message
  98. * @param array|Arrayable|null $data
  99. * @return Json
  100. */
  101. public function fail($message = self::DEFAULT_FAIL_MESSAGE, $data = null)
  102. {
  103. $message = $this->parseData($message);
  104. if (is_array($message)) {
  105. $data = $message;
  106. $message = self::DEFAULT_FAIL_MESSAGE;
  107. } else {
  108. $data = $this->parseData($data);
  109. }
  110. return $this->make(self::DEFAULT_FAIL_CODE, $message, $data);
  111. }
  112. /**
  113. * @param $status
  114. * @param string|array|Arrayable $message
  115. * @param array|Arrayable $result
  116. * @return Json
  117. */
  118. public function status($status, $message, $result = [])
  119. {
  120. $message = $this->parseData($message);
  121. if (is_array($message)) {
  122. $result = $message;
  123. $message = self::DEFAULT_SUCCESS_MESSAGE;
  124. } else {
  125. $result = $this->parseData($result);
  126. }
  127. return $this->make(self::DEFAULT_SUCCESS_CODE, $message, compact('status', 'result'));
  128. }
  129. /**
  130. * @param string $type
  131. * @param $data
  132. * @return Json
  133. * @author xaboy
  134. * @day 2020/6/13
  135. */
  136. public function message(string $type, $data)
  137. {
  138. $this->response->data(compact('type', 'data'));
  139. return $this->response;
  140. }
  141. }