mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 18:30:16 +08:00
112 lines
3.1 KiB
PHP
112 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Aspect;
|
|
|
|
use App\Common\Trait\AdminUserTrait;
|
|
use App\Common\Trait\ClientIpTrait;
|
|
use App\Lib\Log\Logger;
|
|
use App\Lib\Return\AdminReturn;
|
|
use App\Model\AdminUser;
|
|
use App\Model\AdminUserOperationLog;
|
|
use Hyperf\Context\Context;
|
|
use Hyperf\Coroutine\Coroutine;
|
|
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 AdminReturnLogAspect extends AbstractAspect
|
|
{
|
|
use ClientIpTrait;
|
|
use AdminUserTrait;
|
|
|
|
/**
|
|
* 切入类
|
|
* @var array|\class-string[]
|
|
*/
|
|
public array $classes = [
|
|
AdminReturn::class,
|
|
];
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
protected int $adminId = 0;
|
|
|
|
/**
|
|
* @var ?AdminUser
|
|
*/
|
|
protected ?AdminUser $adminUserInfo = null;
|
|
|
|
/**
|
|
* @param Logger $logger
|
|
* @param RequestInterface $request
|
|
* @param AdminUserOperationLog $adminUserOperationLogModel
|
|
*/
|
|
public function __construct(
|
|
protected readonly Logger $logger,
|
|
protected RequestInterface $request,
|
|
protected readonly AdminUserOperationLog $adminUserOperationLogModel,
|
|
) {
|
|
$this->adminId = Context::get('current_admin_id',0);
|
|
if ($this->adminId > 0) $this->adminUserInfo = $this->getAdminUserInfo($this->adminId);
|
|
}
|
|
|
|
/**
|
|
* @param ProceedingJoinPoint $proceedingJoinPoint
|
|
* @return mixed
|
|
* @throws Exception
|
|
*/
|
|
public function process(ProceedingJoinPoint $proceedingJoinPoint)
|
|
{
|
|
// 直接从方法参数获取请求数据
|
|
$requestData = $proceedingJoinPoint->getArguments()[0] ?? [];
|
|
|
|
// 执行原方法并获取返回值
|
|
$responseData = $proceedingJoinPoint->process();
|
|
|
|
// 没登录不记录日志
|
|
if ($this->adminId <= 0) return $responseData;
|
|
|
|
// 写日志
|
|
$this->writeOperationLog($requestData, $responseData);
|
|
|
|
// 返回
|
|
return $responseData;
|
|
}
|
|
|
|
/**
|
|
* @param array $requestData
|
|
* @param array $responseData
|
|
* @return void
|
|
*/
|
|
private function writeOperationLog(array $requestData = [], array $responseData = []): void
|
|
{
|
|
$context = [
|
|
'user_id' => $this->adminId,
|
|
'method' => $this->request->getMethod(),
|
|
'router' => $this->request->getUri(),
|
|
'ip' => $this->getClientIp(),
|
|
'request_data' => $requestData,
|
|
'response_data' => $responseData,
|
|
];
|
|
|
|
Coroutine::create(function () use ($requestData, $responseData, $context) {
|
|
$this->logger->request()->info('admin_request_log', $context);
|
|
|
|
$this->adminUserOperationLogModel->create([
|
|
'admin_user_id' => $this->adminId,
|
|
'username' => $this->adminUserInfo?->username ?? '',
|
|
'method' => $context['method'],
|
|
'router' => $context['router'],
|
|
'service_name' => $context['service_name'] ?? '',
|
|
'ip' => current($context['ip']) ?: '0.0.0.0',
|
|
]);
|
|
});
|
|
}
|
|
}
|