This commit is contained in:
77
app/Aspect/CommonReturnAspect.php
Normal file
77
app/Aspect/CommonReturnAspect.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Aspect;
|
||||
|
||||
use App\Lib\Return\AdminReturn;
|
||||
use App\Lib\Return\CommonReturn;
|
||||
use App\Lib\Return\TestReturn;
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Di\Annotation\Aspect;
|
||||
use Hyperf\Di\Aop\AbstractAspect;
|
||||
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
||||
use Hyperf\Di\Exception\Exception;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
|
||||
#[Aspect]
|
||||
class CommonReturnAspect extends AbstractAspect
|
||||
{
|
||||
/**
|
||||
* 需要切入的类
|
||||
* @var array|string[]
|
||||
*/
|
||||
public array $classes = [
|
||||
AdminReturn::class,
|
||||
TestReturn::class,
|
||||
// CommonReturn::class,
|
||||
];
|
||||
|
||||
/**
|
||||
* 需要切入的注解
|
||||
* @var array
|
||||
*/
|
||||
public array $annotations = [];
|
||||
|
||||
/**
|
||||
* 依赖注入容器
|
||||
* @param RequestInterface $request
|
||||
* @param int $userId
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly RequestInterface $request,
|
||||
private int $userId = 0
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 切面逻辑
|
||||
* @param ProceedingJoinPoint $proceedingJoinPoint
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function process(ProceedingJoinPoint $proceedingJoinPoint): mixed
|
||||
{
|
||||
echo 1;
|
||||
var_dump(1);
|
||||
// 在调用前进行处理
|
||||
$result = $proceedingJoinPoint->process();
|
||||
// 在调用后进行处理
|
||||
|
||||
// 未登录返回 0
|
||||
$this->userId = Context::get('user_id',0);
|
||||
|
||||
$this->writeResponseLog(json_encode($result));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入请求日志
|
||||
* @param string $content
|
||||
* @return void
|
||||
*/
|
||||
private function writeResponseLog(string $content): void
|
||||
{
|
||||
echo json_encode($this->request->all());
|
||||
echo $this->userId;
|
||||
echo json_encode($content);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user