You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hyperf-gateway/app/Aspect/ResponseMiddlewareAspect.php

34 lines
1.0 KiB
PHTML

2 years ago
<?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();
}
}