Response.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 app\webscoket;
  12. use think\response\Json;
  13. /**
  14. * socket Response
  15. * Class Response
  16. * @package app\webscoket
  17. * @mixin Json
  18. */
  19. class Response
  20. {
  21. /**
  22. *
  23. * @var Json
  24. */
  25. protected $response;
  26. /**
  27. * Response constructor.
  28. */
  29. public function __construct(Json $response)
  30. {
  31. $this->response = $response;
  32. }
  33. /**
  34. * 设置返回参数
  35. * @param string $type
  36. * @param array|null $data
  37. * @param int $status
  38. * @param array $other
  39. * @return Json
  40. */
  41. public function send(string $type, ?array $data = null, int $status = 200, array $other = [])
  42. {
  43. $res = compact('type', 'status');
  44. if (!is_null($data)) {
  45. $res['data'] = $data;
  46. }
  47. $data = array_merge($res, $other);
  48. $this->response->data($data);
  49. return $this->response;
  50. }
  51. /**
  52. * 成功
  53. * @param string $type
  54. * @param array|null $data
  55. * @param int $status
  56. * @return Json
  57. */
  58. public function success($type = 'success', ?array $data = null, int $status = 200)
  59. {
  60. if (is_array($type)) {
  61. $data = $type;
  62. $type = 'success';
  63. }
  64. return $this->send($type, $data, $status);
  65. }
  66. /**
  67. * 失败
  68. * @param string $type
  69. * @param array|null $data
  70. * @param int $status
  71. * @return Json
  72. */
  73. public function fail($type = 'error', ?array $data = null, int $status = 400)
  74. {
  75. if (is_array($type)) {
  76. $data = $type;
  77. $type = 'error';
  78. }
  79. return $this->send($type, $data, $status);
  80. }
  81. /**
  82. * 设置返只有类型没有状态的返回数据
  83. * @param string $type
  84. * @param $data
  85. * @return Json
  86. */
  87. public function message(string $type, $data)
  88. {
  89. $this->response->data(compact('type', 'data'));
  90. return $this->response;
  91. }
  92. /**
  93. * @param $name
  94. * @param $arguments
  95. * @return mixed
  96. */
  97. public function __call($name, $arguments)
  98. {
  99. return call_user_func_array([$this->response, $name], $arguments);
  100. }
  101. }