<?php declare(strict_types=1); namespace App\Aspect; use App\Helper\Result; use Hyperf\Di\Annotation\Aspect; use Hyperf\Di\Aop\AbstractAspect; use Hyperf\Di\Aop\ProceedingJoinPoint; use Hyperf\HttpServer\CoreMiddleware; use Hyperf\Paginator\Paginator; #[Aspect] class ResponseMiddlewareAspect extends AbstractAspect { // 要切入的类或 Trait,可以多个,亦可通过 :: 标识到具体的某个方法,通过 * 可以模糊匹配 public $classes = [ CoreMiddleware::class . '::transferToResponse' ]; public function process(ProceedingJoinPoint $proceedingJoinPoint) { $response = $proceedingJoinPoint->arguments['keys']['response']; if ($response === null || is_array($response)) { $proceedingJoinPoint->arguments['keys']['response'] = Result::result($response); } else if ($response instanceof Paginator) { $proceedingJoinPoint->arguments['keys']['response'] = Result::result($response->toArray()); } return $proceedingJoinPoint->process(); } }