2020-11-04 11:57:16 +08:00
|
|
|
<?php
|
|
|
|
|
2020-11-07 22:57:10 +08:00
|
|
|
namespace App\Support;
|
2020-11-04 11:57:16 +08:00
|
|
|
|
2020-11-04 16:47:17 +08:00
|
|
|
use Hyperf\Di\Annotation\Inject;
|
2020-11-04 11:57:16 +08:00
|
|
|
use Hyperf\HttpServer\Contract\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
|
|
|
|
use App\Constants\ResponseCode;
|
|
|
|
|
|
|
|
class Response
|
|
|
|
{
|
|
|
|
/**
|
2020-11-04 16:47:17 +08:00
|
|
|
* @Inject
|
|
|
|
* @var ResponseInterface|mixed
|
2020-11-04 11:57:16 +08:00
|
|
|
*/
|
2020-11-14 23:05:45 +08:00
|
|
|
private $response;
|
2020-11-04 11:57:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @return PsrResponseInterface
|
|
|
|
*/
|
|
|
|
public function json($data)
|
|
|
|
{
|
|
|
|
return $this->response->json($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 处理成功信息返回
|
|
|
|
*
|
|
|
|
* @param array $data 响应数据
|
|
|
|
* @param string $message 响应提示
|
|
|
|
* @return PsrResponseInterface
|
|
|
|
*/
|
|
|
|
public function success(array $data = [], $message = 'success')
|
|
|
|
{
|
|
|
|
$code = ResponseCode::SUCCESS;
|
|
|
|
return $this->response->json(compact('code', 'message', 'data'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 处理失败信息返回
|
|
|
|
*
|
|
|
|
* @param array $data 响应数据
|
|
|
|
* @param string $message 响应提示
|
|
|
|
* @param int $code 错误码
|
|
|
|
*
|
|
|
|
* @return PsrResponseInterface
|
|
|
|
*/
|
2020-11-29 17:39:24 +08:00
|
|
|
public function fail($message = 'fail', $data = [], $code = ResponseCode::FAIL)
|
2020-11-04 11:57:16 +08:00
|
|
|
{
|
|
|
|
return $this->response->json(compact('code', 'message', 'data'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $message
|
|
|
|
* @param int $code
|
|
|
|
* @return PsrResponseInterface
|
|
|
|
*/
|
|
|
|
public function error($message = '', $code = ResponseCode::SERVER_ERROR)
|
|
|
|
{
|
|
|
|
return $this->response->withStatus(500)->json([
|
|
|
|
'code' => $code,
|
|
|
|
'message' => $message,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|