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头部状态码,有些特殊返回需要控制的时候,就不行,方案二,稍微复杂一点,但是更完善

posted on 2024-04-18 22:47  zh7314  阅读(30)  评论(0编辑  收藏  举报