hyperf统一请求响应
2024年4月18日08:48:45
以下是两个方案:
1,使用注解,直接返回
<?php
namespace App\Utils;
use App\Utils\GlobalCode;
use App\Utils\GlobalMsg;
use Hyperf\Contract\ContainerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Throwable;
use function Hyperf\Support\env;
trait ResponseTrait
{
#[Inject]
protected RequestInterface $rt;
#[Inject]
protected ResponseInterface $re;
public function success(mixed $data = '', string $msg = GlobalMsg::SUCCESS)
{
return $this->re->json([GlobalCode::CODE => GlobalCode::SUCCESS, GlobalCode::MSG => $msg, GlobalCode::DATA => $data]);
}
public function fail(Throwable $e, $status = 200, array $headers = [])
{
if ($this->rt->input('debug') == env('DEBUG', GlobalCode::DEBUG) || env('DEBUG') == GlobalCode::DEBUG) {
return $this->re->json([GlobalCode::CODE => GlobalCode::FAIL, GlobalCode::MSG => $e->getMessage(), GlobalCode::DATA => $e->getTraceAsString()], $status, $headers);
} else {
return $this->re->json([GlobalCode::CODE => GlobalCode::FAIL, GlobalCode::MSG => $e->getMessage(), GlobalCode::DATA => $e->getMessage()], $status, $headers);
}
}
public function grant(Throwable $e)
{
if ($this->rt->input('debug') == env('DEBUG', GlobalCode::DEBUG) || env('DEBUG') == GlobalCode::DEBUG) {
return $this->re->json([GlobalCode::CODE => GlobalCode::GRANT, GlobalCode::MSG => $e->getMessage(), GlobalCode::DATA => $e->getTraceAsString()]);
} else {
return $this->re->json([GlobalCode::CODE => GlobalCode::GRANT, GlobalCode::MSG => $e->getMessage(), GlobalCode::DATA => $e->getMessage()]);
}
}
}
方案二: 像laravel 那样使用容器吧响应接口返回出来
<?php
namespace App\Utils;
use App\Utils\GlobalCode;
use App\Utils\GlobalMsg;
use Hyperf\Contract\ContainerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Throwable;
use Hyperf\Context\ApplicationContext;
use function Hyperf\Support\env;
trait ResponseTrait
{
public function success(mixed $data = '', string $msg = GlobalMsg::SUCCESS, int $status = 200)
{
return self::response([GlobalCode::CODE => GlobalCode::SUCCESS, GlobalCode::MSG => $msg, GlobalCode::DATA => $data], $status);
}
public function fail(Throwable $e, int $status = 200)
{
if (self::request()->input('debug') == env('DEBUG', GlobalCode::DEBUG) || env('DEBUG') == GlobalCode::DEBUG) {
return self::response([GlobalCode::CODE => GlobalCode::FAIL, GlobalCode::MSG => $e->getMessage(), GlobalCode::DATA => $e->getTraceAsString()], $status);
} else {
return self::response([GlobalCode::CODE => GlobalCode::FAIL, GlobalCode::MSG => $e->getMessage(), GlobalCode::DATA => $e->getMessage()], $status);
}
}
public function grant(Throwable $e, int $status = 200)
{
if (self::request()->input('debug') == env('DEBUG', GlobalCode::DEBUG) || env('DEBUG') == GlobalCode::DEBUG) {
return self::response([GlobalCode::CODE => GlobalCode::GRANT, GlobalCode::MSG => $e->getMessage(), GlobalCode::DATA => $e->getTraceAsString()], $status);
} else {
return self::response([GlobalCode::CODE => GlobalCode::GRANT, GlobalCode::MSG => $e->getMessage(), GlobalCode::DATA => $e->getMessage()], $status);
}
}
private static function response(mixed $data = null, int $status = 200, int $options = JSON_UNESCAPED_UNICODE)
{
$response = self::container()->get(ResponseInterface::class);
return $response->withStatus($status)->withAddedHeader('content-type', 'application/json; charset=utf-8')->withBody(new SwooleStream(json_encode($data, $options)));
}
private static function request()
{
return self::container()->get(RequestInterface::class);
}
/**
* 容器实例
* @return \Psr\Container\ContainerInterface
*/
private static function container()
{
return ApplicationContext::getContainer();
}
}
总结:方案一,很简单,但是不能控制header头部状态码,有些特殊返回需要控制的时候,就不行,方案二,稍微复杂一点,但是更完善
QQ一群 247823727
QQ二群 166427999
博客文件如果不能下载请进群下载
如果公司项目有技术瓶颈问题,请联系↓↓
如果需要定制系统开发服务,请联系↓↓
技术服务QQ: 903464207
QQ二群 166427999
博客文件如果不能下载请进群下载
如果公司项目有技术瓶颈问题,请联系↓↓
如果需要定制系统开发服务,请联系↓↓
技术服务QQ: 903464207