You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Response.php 2.1KB

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