hyperf-gateway/app/Aspect/ResponseMiddlewareAspect.php

34 lines
1.0 KiB
PHP
Raw Normal View History

2022-11-04 15:37:04 +08:00
<?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();
}
}