// scripts/commands.js // 标准指令路由映射表 export const COMMANDS = [ { key: "首页", menu: "首页", route: "/首页" }, { key: "添加设备", menu: "添加设备", route: "/添加设备/添加设备" }, { key: "监控中心", menu: "监控中心", route: "/监控中心/监控中心" }, { key: "摄像头管理", menu: "摄像头管理", route: "/云视频管理/摄像头管理" }, { key: "云视频监控", menu: "云视频监控", route: "/云视频监控/云视频监控" }, { key: "GIS监控", menu: "GIS监控", route: "/GIS管理/GIS监控" }, { key: "我的流量", menu: "我的流量", route: "/资源管理/我的流量" }, { key: "我的资源", menu: "我的资源", route: "/资源管理/我的资源" }, { key: "我的订单", menu: "我的订单", route: "/资源管理/我的订单" }, { key: "仪表积分", menu: "仪表积分", route: "/资源管理/仪表积分" }, { key: "报警设置", menu: "报警设置", route: "/报警管理/报警设置" }, { key: "报警通知", menu: "报警通知", route: "/报警管理/报警通知" }, { key: "报警记录", menu: "报警记录", route: "/报警管理/报警记录" }, { key: "基础报表", menu: "基础报表", route: "/报表管理/基础报表" }, { key: "分析报表", menu: "分析报表", route: "/报表管理/分析报表" }, { key: "区域管理", menu: "区域管理", route: "/系统管理/区域管理" }, { key: "角色管理", menu: "角色管理", route: "/系统管理/角色管理" }, { key: "用户管理", menu: "用户管理", route: "/系统管理/用户管理" } ]; /** * 展开侧边栏父级菜单 */ function expandParentMenu(span) { let parent = span.closest('li'); while (parent) { if (parent.classList.contains('el-submenu')) { const title = parent.querySelector('.el-submenu__title'); if (title && !parent.classList.contains('is-opened')) { title.click(); } } parent = parent.parentElement.closest('li'); } } /** * 自动填充并提交搜索(模拟针对某些页面的操作) */ function autoFillAndSubmit(arg) { if (!arg) return; setTimeout(() => { const input = document.querySelector('input[placeholder*="名称"], input[placeholder*="编号"]'); if (input) { input.value = arg; input.dispatchEvent(new Event('input', { bubbles: true })); const searchBtn = document.querySelector('button.el-button--primary'); if (searchBtn) searchBtn.click(); } }, 1000); } /** * 核心处理器:根据 AI 结果执行动作 * @param {string} aiResult AI 返回的 XML 字符串 * @param {object} uiRefs UI 更新引用的对象 * @param {boolean} voiceEnabled 是否允许播放语音 (由 ai.js 传入) */ export function handleCommand(aiResult, uiRefs, voiceEnabled = false) { if (!aiResult || aiResult === "UNKNOWN") return; // 1. 解析对话内容并反馈 const commMatch = aiResult.match(/([\s\S]*?)<\/communication>/); if (commMatch && commMatch[1]) { const speechText = commMatch[1].trim(); // 始终更新 UI 界面上的文字状态 if (uiRefs && uiRefs.updateStatus) { uiRefs.updateStatus(speechText); } // 仅在语音开关开启时播报语音 if (voiceEnabled) { const utterance = new SpeechSynthesisUtterance(speechText); utterance.lang = "zh-CN"; window.speechSynthesis.speak(utterance); } } // 2. 解析指令逻辑 const cmdMatch = aiResult.match(/([\s\S]*?)<\/cmd>/); const argMatch = aiResult.match(/([\s\S]*?)<\/arg>/); const key = cmdMatch ? cmdMatch[1].trim() : null; const arg = argMatch ? argMatch[1].trim() : null; if (key) { // 模糊匹配指令 const command = COMMANDS.find(c => c.key === key) || COMMANDS.find(c => key.includes(c.key)); if (command) { console.log("🚀 执行指令:", command.key, "参数:", arg); // 尝试点击侧边栏菜单(针对 Element UI 结构) const allSpans = Array.from(document.querySelectorAll("span")); let span = allSpans.find(el => el.innerText.trim() === command.menu); if (span) { expandParentMenu(span); span.click(); } // 路由跳转 window.location.hash = command.route; // 自动填充搜索参数 if (arg) { autoFillAndSubmit(arg); } } else { console.warn("⚠️ 未找到匹配指令:", key); } } }