getPdo(); $stmt = $pdo->prepare( "INSERT INTO system_logs (user_id, username, action, module, target_table, target_id, content, ip) VALUES (?, ?, ?, ?, ?, ?, ?, ?)" ); $contentJson = ($content !== null && $content !== '') ? json_encode($content, JSON_UNESCAPED_UNICODE) : null; return $stmt->execute([$userId, $username, $action, $module, $targetTable, $targetId, $contentJson, $ip]); } catch (Exception $e) { // 日志失败不影响主流程 return false; } } /** * 基于当前 Session 用户写入日志(便捷函数) * @param string $action * @param string $module * @param string|null $targetTable * @param int|null $targetId * @param mixed $content */ function logCurrent($action, $module, $targetTable = null, $targetId = null, $content = null) { return logAction( $_SESSION['user_id'] ?? null, $_SESSION['username'] ?? null, $action, $module, $targetTable, $targetId, $content ); }