mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-03-28 23:50:16 +08:00
fix : update log
This commit is contained in:
@@ -28,20 +28,16 @@ class AdminReturnLogAspect extends AbstractAspect
|
|||||||
* 切入类
|
* 切入类
|
||||||
* @var array|\class-string[]
|
* @var array|\class-string[]
|
||||||
*/
|
*/
|
||||||
public array $classes = [
|
public array $classes = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 切入方法 - 拦截所有带 ResponseFormat 注解且 format='admin' 的方法
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
public array $annotations = [
|
||||||
ResponseFormat::class,
|
ResponseFormat::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
|
||||||
* @var int
|
|
||||||
*/
|
|
||||||
protected int $adminId = 0;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var ?AdminUser
|
|
||||||
*/
|
|
||||||
protected ?AdminUser $adminUserInfo = null;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Logger $logger
|
* @param Logger $logger
|
||||||
* @param RequestInterface $request
|
* @param RequestInterface $request
|
||||||
@@ -51,10 +47,7 @@ class AdminReturnLogAspect extends AbstractAspect
|
|||||||
protected readonly Logger $logger,
|
protected readonly Logger $logger,
|
||||||
protected RequestInterface $request,
|
protected RequestInterface $request,
|
||||||
protected readonly AdminUserOperationLog $adminUserOperationLogModel,
|
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
|
* @param ProceedingJoinPoint $proceedingJoinPoint
|
||||||
@@ -63,49 +56,100 @@ class AdminReturnLogAspect extends AbstractAspect
|
|||||||
*/
|
*/
|
||||||
public function process(ProceedingJoinPoint $proceedingJoinPoint)
|
public function process(ProceedingJoinPoint $proceedingJoinPoint)
|
||||||
{
|
{
|
||||||
// 直接从方法参数获取请求数据
|
// 检查是否是 admin 格式
|
||||||
$requestData = $proceedingJoinPoint->getArguments()[0] ?? [];
|
$annotation = $proceedingJoinPoint->getAnnotationMetadata()->class[ResponseFormat::class]
|
||||||
|
?? $proceedingJoinPoint->getAnnotationMetadata()->method[ResponseFormat::class] ?? null;
|
||||||
|
|
||||||
// 执行原方法并获取返回值
|
// 只处理 admin 格式的请求
|
||||||
$responseData = $proceedingJoinPoint->process();
|
if (!$annotation || $annotation->format !== 'admin') {
|
||||||
|
return $proceedingJoinPoint->process();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取当前登录的管理员ID(在 process 方法中获取,确保 Context 已设置)
|
||||||
|
$adminId = Context::get('current_admin_id', 0);
|
||||||
|
|
||||||
// 没登录不记录日志
|
// 没登录不记录日志
|
||||||
if ($this->adminId <= 0) return $responseData;
|
if ($adminId <= 0) {
|
||||||
|
return $proceedingJoinPoint->process();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取请求数据
|
||||||
|
$requestData = $this->request->all();
|
||||||
|
|
||||||
|
// 执行原方法并获取返回值
|
||||||
|
$responseData = $proceedingJoinPoint->process();
|
||||||
|
|
||||||
// 写日志
|
// 写日志
|
||||||
$this->writeOperationLog($requestData, $responseData);
|
$this->writeOperationLog($adminId, $requestData, $responseData);
|
||||||
|
|
||||||
// 返回
|
// 返回
|
||||||
return $responseData;
|
return $responseData;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param int $adminId
|
||||||
* @param array $requestData
|
* @param array $requestData
|
||||||
* @param array $responseData
|
* @param array $responseData
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function writeOperationLog(array $requestData = [], array $responseData = []): void
|
private function writeOperationLog(int $adminId, array $requestData = [], array $responseData = []): void
|
||||||
{
|
{
|
||||||
|
$router = (string) $this->request->getUri();
|
||||||
|
$method = $this->request->getMethod();
|
||||||
|
$ip = $this->getClientIp();
|
||||||
|
$ipStr = current($ip) ?: '0.0.0.0';
|
||||||
|
|
||||||
$context = [
|
$context = [
|
||||||
'user_id' => $this->adminId,
|
'user_id' => $adminId,
|
||||||
'method' => $this->request->getMethod(),
|
'method' => $method,
|
||||||
'router' => $this->request->getUri(),
|
'router' => $router,
|
||||||
'ip' => $this->getClientIp(),
|
'ip' => $ip,
|
||||||
'request_data' => $requestData,
|
'request_data' => $requestData,
|
||||||
'response_data' => $responseData,
|
'response_data' => $responseData,
|
||||||
];
|
];
|
||||||
|
|
||||||
Coroutine::create(function () use ($requestData, $responseData, $context) {
|
// 先记录日志
|
||||||
$this->logger->request()->info('admin_request_log', $context);
|
$this->logger->request()->info('admin_request_log', $context);
|
||||||
|
|
||||||
$this->adminUserOperationLogModel->create([
|
// 获取用户信息
|
||||||
'admin_user_id' => $this->adminId,
|
$adminUserInfo = $this->getAdminUserInfo($adminId);
|
||||||
'username' => $this->adminUserInfo?->username ?? '',
|
$username = $adminUserInfo?->username ?? '';
|
||||||
'method' => $context['method'],
|
|
||||||
'router' => $context['router'],
|
// 异步写入数据库
|
||||||
'service_name' => $context['service_name'] ?? '',
|
try {
|
||||||
'ip' => current($context['ip']) ?: '0.0.0.0',
|
Coroutine::create(function () use ($adminId, $username, $router, $method, $ipStr) {
|
||||||
]);
|
try {
|
||||||
});
|
$this->adminUserOperationLogModel->create([
|
||||||
|
'admin_user_id' => $adminId,
|
||||||
|
'username' => $username,
|
||||||
|
'method' => $method,
|
||||||
|
'router' => $router,
|
||||||
|
'service_name' => '',
|
||||||
|
'ip' => $ipStr,
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$this->logger->error()->error('写入操作日志失败: ' . $e->getMessage(), [
|
||||||
|
'admin_id' => $adminId,
|
||||||
|
'router' => $router,
|
||||||
|
'exception' => $e->getMessage(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
// 协程创建失败,降级为同步写入
|
||||||
|
$this->logger->error()->error('创建协程失败,同步写入日志: ' . $e->getMessage());
|
||||||
|
try {
|
||||||
|
$this->adminUserOperationLogModel->create([
|
||||||
|
'admin_user_id' => $adminId,
|
||||||
|
'username' => $username,
|
||||||
|
'method' => $method,
|
||||||
|
'router' => $router,
|
||||||
|
'service_name' => '',
|
||||||
|
'ip' => $ipStr,
|
||||||
|
]);
|
||||||
|
} catch (\Throwable $e2) {
|
||||||
|
$this->logger->error()->error('同步写入操作日志也失败: ' . $e2->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user