hyperf-chat/app/Support/Response.php

97 lines
2.3 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
2021-08-31 22:33:41 +08:00
declare(strict_types=1);
2020-11-04 11:57:16 +08:00
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
/**
2021-04-22 16:14:34 +08:00
* 处理json数据
2021-04-20 16:30:57 +08:00
*
2021-04-22 16:14:34 +08:00
* @param $data
2020-11-04 11:57:16 +08:00
* @return PsrResponseInterface
*/
2021-08-31 22:33:41 +08:00
public function json($data): PsrResponseInterface
2020-11-04 11:57:16 +08:00
{
return $this->response->json($data);
}
/**
* 处理成功信息返回
*
2021-04-20 16:30:57 +08:00
* @param array $data 响应数据
2020-11-04 11:57:16 +08:00
* @param string $message 响应提示
* @return PsrResponseInterface
*/
2021-09-12 21:15:38 +08:00
public function success(array $data = [], string $message = 'success'): PsrResponseInterface
2020-11-04 11:57:16 +08:00
{
2022-01-16 10:29:16 +08:00
$resp = [
"code" => ResponseCode::SUCCESS,
"message" => $message,
];
2022-01-22 12:48:28 +08:00
if ($data) $resp["data"] = $data;
2022-01-16 10:29:16 +08:00
return $this->response->json($resp);
2020-11-04 11:57:16 +08:00
}
/**
* 处理失败信息返回
*
2021-04-20 16:30:57 +08:00
* @param array $data 响应数据
2020-11-04 11:57:16 +08:00
* @param string $message 响应提示
2021-04-20 16:30:57 +08:00
* @param int $code 错误码
2020-11-04 11:57:16 +08:00
* @return PsrResponseInterface
*/
2022-01-22 12:48:28 +08:00
public function fail(string $message = 'fail', array $data = [], int $code = ResponseCode::FAIL): PsrResponseInterface
2020-11-04 11:57:16 +08:00
{
2022-01-16 10:29:16 +08:00
$resp = [
"code" => $code,
"message" => $message,
];
if ($data) $resp["data"] = $data;
return $this->response->json($resp);
2020-11-04 11:57:16 +08:00
}
2022-01-22 12:48:28 +08:00
/**
* 参数验证错误
* @param string $message 错误信息
* @return PsrResponseInterface
*/
public function invalidParams(string $message = 'fail'): PsrResponseInterface
{
return $this->response->json([
"code" => ResponseCode::VALIDATION_ERROR,
"message" => $message,
]);
}
2020-11-04 11:57:16 +08:00
/**
2021-04-22 16:14:34 +08:00
* 处理错误信息返回
2021-04-20 16:30:57 +08:00
*
2021-04-22 16:14:34 +08:00
* @param string $message 响应提示
* @param int $code 错误码
2020-11-04 11:57:16 +08:00
* @return PsrResponseInterface
*/
2021-09-12 21:15:38 +08:00
public function error(string $message = '', $code = ResponseCode::SERVER_ERROR): PsrResponseInterface
2020-11-04 11:57:16 +08:00
{
return $this->response->withStatus(500)->json([
2021-04-20 16:30:57 +08:00
'code' => $code,
2020-11-04 11:57:16 +08:00
'message' => $message,
]);
}
}