ApiResponseService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author xaboy
  6. * @day 2020-03-24
  7. *
  8. *
  9. */
  10. namespace crmeb\services;
  11. use phpseclib3\Math\PrimeField\Integer;
  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. * @param string|Integer|null $data
  75. * @return Json
  76. */
  77. public function fail($message = self::DEFAULT_FAIL_MESSAGE, $data = null, $code = null)
  78. {
  79. $message = $this->parseData($message);
  80. if (is_array($message)) {
  81. $data = $message;
  82. $message = self::DEFAULT_FAIL_MESSAGE;
  83. } else {
  84. $data = $this->parseData($data);
  85. }
  86. return $this->make($code ?: self::DEFAULT_FAIL_CODE, $message, $data);
  87. }
  88. /**
  89. * @param $status
  90. * @param string|array|Arrayable $message
  91. * @param array|Arrayable $result
  92. * @return Json
  93. */
  94. public function status($status, $message, $result = [])
  95. {
  96. $message = $this->parseData($message);
  97. if (is_array($message)) {
  98. $result = $message;
  99. $message = self::DEFAULT_SUCCESS_MESSAGE;
  100. } else {
  101. $result = $this->parseData($result);
  102. }
  103. return $this->make(self::DEFAULT_SUCCESS_CODE, $message, compact('status', 'result'));
  104. }
  105. /**
  106. * @param string $type
  107. * @param $data
  108. * @return Json
  109. * @author xaboy
  110. * @day 2020/6/13
  111. */
  112. public function message(string $type, $data)
  113. {
  114. $this->response->data(compact('type', 'data'));
  115. return $this->response;
  116. }
  117. }