ApiResponseService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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. /**
  72. * @param string|array|Arrayable $message
  73. * @param array|Arrayable|null $data
  74. * @return Json
  75. */
  76. public function fail($message = self::DEFAULT_FAIL_MESSAGE, $data = null)
  77. {
  78. $message = $this->parseData($message);
  79. if (is_array($message)) {
  80. $data = $message;
  81. $message = self::DEFAULT_FAIL_MESSAGE;
  82. } else {
  83. $data = $this->parseData($data);
  84. }
  85. return $this->make(self::DEFAULT_FAIL_CODE, $message, $data);
  86. }
  87. /**
  88. * @param $status
  89. * @param string|array|Arrayable $message
  90. * @param array|Arrayable $result
  91. * @return Json
  92. */
  93. public function status($status, $message, $result = [])
  94. {
  95. $message = $this->parseData($message);
  96. if (is_array($message)) {
  97. $result = $message;
  98. $message = self::DEFAULT_SUCCESS_MESSAGE;
  99. } else {
  100. $result = $this->parseData($result);
  101. }
  102. return $this->make(self::DEFAULT_SUCCESS_CODE, $message, compact('status', 'result'));
  103. }
  104. /**
  105. * @param string $type
  106. * @param $data
  107. * @return Json
  108. * @author xaboy
  109. * @day 2020/6/13
  110. */
  111. public function message(string $type, $data)
  112. {
  113. $this->response->data(compact('type', 'data'));
  114. return $this->response;
  115. }
  116. }