create([ 'user_id' => $user_id, 'action' => $action, 'target_type' => $target_type, 'target_id' => $target_id, 'description' => $description, 'detail' => $detail, 'ip' => $ip, ]); } catch (\Throwable $e) { Log::get()->error('OperationLogService: 写入操作日志失败', [ 'error' => $e->getMessage(), 'action' => $action, ]); } }); } /** * 从当前认证上下文获取用户 ID */ public static function getCurrentUserId(): ?int { try { $container = \Hyperf\Context\ApplicationContext::getContainer(); $auth = $container->get(AuthManager::class); $user = $auth->guard('jwt')->user(); if ($user instanceof User) { return $user->id; } return $user?->getId(); } catch (\Throwable $e) { Log::get()->warning('OperationLogService: 获取当前用户 ID 失败', [ 'error' => $e->getMessage(), ]); return null; } } }