getAnnotationMetadata()->class[ResponseFormat::class] ?? $proceedingJoinPoint->getAnnotationMetadata()->method[ResponseFormat::class] ?? null; // 只处理 admin 格式的请求 if (!$annotation || $annotation->format !== 'admin') { return $proceedingJoinPoint->process(); } // 获取当前登录的管理员ID(在 process 方法中获取,确保 Context 已设置) $adminId = Context::get('current_admin_id', 0); // 没登录不记录日志 if ($adminId <= 0) { return $proceedingJoinPoint->process(); } // 获取请求数据 $requestData = $this->request->all(); // 执行原方法并获取返回值 $responseData = $proceedingJoinPoint->process(); // 写日志 $this->writeOperationLog($adminId, $requestData, $responseData); // 返回 return $responseData; } /** * @param int $adminId * @param array $requestData * @param array $responseData * @return 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 = [ 'user_id' => $adminId, 'method' => $method, 'router' => $router, 'ip' => $ip, 'request_data' => $requestData, 'response_data' => $responseData, ]; // 先记录日志 $this->logger->request()->info('admin_request_log', $context); // 获取用户信息 $adminUserInfo = $this->getAdminUserInfo($adminId); $username = $adminUserInfo?->username ?? ''; // 异步写入数据库 try { 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()); } } } }