123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace app\webscoket;
- use think\response\Json;
- class Response
- {
-
- protected $response;
-
- public function __construct(Json $response)
- {
- $this->response = $response;
- }
-
- public function send(string $type, ?array $data = null, int $status = 200, array $other = [])
- {
- $res = compact('type', 'status');
- if (!is_null($data)) {
- $res['data'] = $data;
- }
- $data = array_merge($res, $other);
- $this->response->data($data);
- return $this->response;
- }
-
- public function success($type = 'success', ?array $data = null, int $status = 200)
- {
- if (is_array($type)) {
- $data = $type;
- $type = 'success';
- }
- return $this->send($type, $data, $status);
- }
-
- public function fail($type = 'error', ?array $data = null, int $status = 400)
- {
- if (is_array($type)) {
- $data = $type;
- $type = 'error';
- }
- return $this->send($type, $data, $status);
- }
-
- public function message(string $type, $data)
- {
- $this->response->data(compact('type', 'data'));
- return $this->response;
- }
-
- public function __call($name, $arguments)
- {
- return call_user_func_array([$this->response, $name], $arguments);
- }
- }
|