// 漏洞管理相关功能 function vulnT(key, opts) { if (typeof window.t === 'function') { return window.t(key, opts); } return key; } function vulnDateLocale() { try { const lang = (window.__locale || '').toLowerCase(); if (lang.indexOf('zh') === 0) { return 'zh-CN'; } } catch (e) { /* ignore */ } return 'en-US'; } function vulnSeverityLabel(code) { const m = { critical: 'dashboard.severityCritical', high: 'dashboard.severityHigh', medium: 'dashboard.severityMedium', low: 'dashboard.severityLow', info: 'dashboard.severityInfo' }; return m[code] ? vulnT(m[code]) : code; } function vulnStatusLabel(code) { const m = { open: 'vulnerabilityPage.statusOpen', confirmed: 'vulnerabilityPage.statusConfirmed', fixed: 'vulnerabilityPage.statusFixed', false_positive: 'vulnerabilityPage.statusFalsePositive' }; return m[code] ? vulnT(m[code]) : code; } // 从localStorage读取每页显示数量,默认为20 const getVulnerabilityPageSize = () => { const saved = localStorage.getItem('vulnerabilityPageSize'); return saved ? parseInt(saved, 10) : 20; }; let currentVulnerabilityId = null; let vulnerabilityFilters = { id: '', conversation_id: '', task_id: '', conversation_tag: '', task_tag: '', severity: '', status: '' }; let vulnerabilityPagination = { currentPage: 1, pageSize: getVulnerabilityPageSize(), total: 0, totalPages: 1 }; // 从地址栏 #vulnerabilities?conversation_id= / ?task_id= 同步筛选(对话菜单、任务管理联动) function syncVulnerabilityFiltersFromLocationHash() { const hash = window.location.hash.slice(1); const hashParts = hash.split('?'); if (hashParts[0] !== 'vulnerabilities' || hashParts.length < 2) { return; } const params = new URLSearchParams(hashParts.slice(1).join('?')); const cid = (params.get('conversation_id') || '').trim(); const tid = (params.get('task_id') || '').trim(); if (!cid && !tid) { return; } vulnerabilityFilters.conversation_id = ''; vulnerabilityFilters.task_id = ''; const convEl = document.getElementById('vulnerability-conversation-filter'); const taskEl = document.getElementById('vulnerability-task-filter'); if (convEl) convEl.value = ''; if (taskEl) taskEl.value = ''; if (cid) { vulnerabilityFilters.conversation_id = cid; if (convEl) convEl.value = cid; } if (tid) { vulnerabilityFilters.task_id = tid; if (taskEl) taskEl.value = tid; } vulnerabilityPagination.currentPage = 1; } // 初始化漏洞管理页面 function initVulnerabilityPage() { // 从localStorage加载每页条数设置 vulnerabilityPagination.pageSize = getVulnerabilityPageSize(); syncVulnerabilityFiltersFromLocationHash(); loadVulnerabilityStats(); loadVulnerabilities(); } // 加载漏洞统计 async function loadVulnerabilityStats() { try { // 检查apiFetch是否可用 if (typeof apiFetch === 'undefined') { console.error('apiFetch未定义,请确保auth.js已加载'); throw new Error('apiFetch未定义'); } const params = new URLSearchParams(); if (vulnerabilityFilters.conversation_id) { params.append('conversation_id', vulnerabilityFilters.conversation_id); } if (vulnerabilityFilters.task_id) { params.append('task_id', vulnerabilityFilters.task_id); } const response = await apiFetch(`/api/vulnerabilities/stats?${params.toString()}`); if (!response.ok) { const errorText = await response.text(); console.error('获取统计失败:', response.status, errorText); throw new Error(`获取统计失败: ${response.status}`); } const stats = await response.json(); updateVulnerabilityStats(stats); } catch (error) { console.error('加载漏洞统计失败:', error); // 统计失败不影响列表显示,只重置统计为0 updateVulnerabilityStats(null); } } // 更新漏洞统计显示 function updateVulnerabilityStats(stats) { // 处理空值情况 if (!stats) { stats = { total: 0, by_severity: {}, by_status: {} }; } document.getElementById('stat-total').textContent = stats.total || 0; const bySeverity = stats.by_severity || {}; document.getElementById('stat-critical').textContent = bySeverity.critical || 0; document.getElementById('stat-high').textContent = bySeverity.high || 0; document.getElementById('stat-medium').textContent = bySeverity.medium || 0; document.getElementById('stat-low').textContent = bySeverity.low || 0; document.getElementById('stat-info').textContent = bySeverity.info || 0; } // 加载漏洞列表 async function loadVulnerabilities(page = null) { const listContainer = document.getElementById('vulnerabilities-list'); listContainer.innerHTML = `
${escapeHtml(s)}`
: `${escapeHtml(s)}`;
const copyBtn = ``;
return `