hyperf统一请求响应

ops/2024/9/20 15:41:49/

2024年4月18日08:48:45

以下是两个方案:

1,使用注解,直接返回

<?phpnamespace 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 那样使用容器吧响应接口返回出来

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


http://www.ppmy.cn/ops/3340.html

相关文章

CentOS 7静默安装Oracle 11g(记一次最小化CentOS 7安装Oracle 11g的经历)

# [pdf在线免费转word文档](https://orcc.online/pdf) https://orcc.online/pdf 1.最小化安装CentOS 7后首先设置一下固定IP 可以先查询一下自己的网卡设备的名称&#xff0c;是ens33&#xff0c;所以网卡配置文件名称就是ifcfg-ens33&#xff08;前面的ifcfg-不用管&#xf…

backtracking Leetcode 回溯算法题

77.组合 第一个位置选择有 n 种&#xff0c;接下来每个位置只能在前面选择数字的后面选&#xff0c;所以有了 beg 参数&#xff0c;才能保持不重复 剪枝&#xff1a;res.size (n - beg 1) < k , 已有答案的长度 剩余所有未选择的个数 都小于最终答案长度了 就没有必要尝…

AlgorithmDay17

day17 110平衡二叉树&#xff08;优先递归&#xff09; 判断是不是所有节点的左右子树的深度相差不超过 1。 其实是判断高度。 所以采用&#xff1a;后序遍历递归 递归三部曲&#xff1a; 1.终止条件 if(rootnullptr)return true;2.返回值和参数 bool isBalanced(TreeNo…

探索MATLAB在计算机视觉与深度学习领域的实战应用

随着人工智能技术的快速发展&#xff0c;计算机视觉与深度学习已成为科技领域中最热门、最具挑战性的研究方向之一。 它们的应用范围从简单的图像处理扩展到了自动驾驶、医疗影像分析、智能监控行业等多个领域。 在这样的背景下&#xff0c;《MATLAB计算机视觉与深度学习实战…

社媒矩阵运营解决方案:海外云手机

在全球化的浪潮下&#xff0c;企业愈发认识到通过海外社交媒体平台扩大影响力、树立品牌形象及抢占国际市场的巨大机遇。因此&#xff0c;运营海外社交媒体账户已逐渐成为企业战略部署的重要组成部分。为了全面捕捉多渠道的流量&#xff0c;众多企业选择同时运营多个平台的多个…

Linux下SPI设备驱动实验:验证SPI节点及ICM20608设备子节点

一. 简介 前一篇文章在设备树文件中创建了SPI的 IO 的 pinctrl节点&#xff0c;SPI节点及ICM20608设备子节点&#xff0c;文章如下&#xff1a; Linux下SPI设备驱动实验&#xff1a;创建SPI节点及SPI设备子节点-CSDN博客 本文对设备树文件进行加载测试&#xff0c;确定SPI节…

步步精科技获得发明型专利,提升Type-C连接器行业竞争力

在电子科技日新月异的时代&#xff0c;连接器作为电子设备中不可或缺的一部分&#xff0c;其安全性、稳定性和性能水平直接关系到设备的使用效果和用户体验。深圳市步步精科技有限公司&#xff08;以下简称“步步精科技”&#xff09;一直致力于连接器领域的技术创新和产品研发…

bugku-web-文件包含2

页面源码 <!-- upload.php --><!doctype html><html><head><meta charset"utf-8"/><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"viewport" content"widthdevice-widt…