Response.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 $message
  54. * @param array|null $data
  55. * @return bool|null
  56. */
  57. public function success($type = 'success', ?array $data = null, int $status = 200)
  58. {
  59. if (is_array($type)) {
  60. $data = $type;
  61. $type = 'success';
  62. }
  63. return $this->send($type, $data, $status);
  64. }
  65. /**
  66. * 失败
  67. * @param string $message
  68. * @param array|null $data
  69. * @return bool|null
  70. */
  71. public function fail($type = 'error', ?array $data = null, int $status = 400)
  72. {
  73. if (is_array($type)) {
  74. $data = $type;
  75. $type = 'error';
  76. }
  77. return $this->send($type, $data, $status);
  78. }
  79. /**
  80. * 设置返只有类型没有状态的返回数据
  81. * @param string $type
  82. * @param $data
  83. * @return Json
  84. */
  85. public function message(string $type, $data)
  86. {
  87. $this->response->data(compact('type', 'data'));
  88. return $this->response;
  89. }
  90. /**
  91. * @param $name
  92. * @param $arguments
  93. * @return mixed
  94. */
  95. public function __call($name, $arguments)
  96. {
  97. return call_user_func_array([$this->response, $name], $arguments);
  98. }
  99. }