增加指令配置

This commit is contained in:
张梦南 2026-01-02 20:28:11 +08:00
parent 7923a945cd
commit d431076623

View File

@ -46,7 +46,7 @@ function expandParentMenu(span) {
if (subMenu) { if (subMenu) {
const title = subMenu.querySelector(".el-sub-menu__title"); const title = subMenu.querySelector(".el-sub-menu__title");
if (title && !subMenu.classList.contains("is-opened")) { if (title && !subMenu.classList.contains("is-opened")) {
title.click(); // 展开父菜单 title.click();
console.log("📂 已展开父菜单"); console.log("📂 已展开父菜单");
} }
} }
@ -54,39 +54,40 @@ function expandParentMenu(span) {
// ========= 4. 路由跳转函数 ========= // ========= 4. 路由跳转函数 =========
function goToRoute(route) { function goToRoute(route) {
// 直接修改 hash
window.location.hash = route; window.location.hash = route;
console.log("✅ 已跳转到路由:", route); console.log("✅ 已跳转到路由:", route);
} }
// ========= 5. 指令解析(文字/语音共用) ========= // ========= 5. 指令配置 =========
const COMMANDS = [
{ key: "首页", menu: "首页", route: "/首页" },
{ key: "添加设备", menu: "添加设备", route: "/添加设备/添加设备" },
{ key: "监控中心", menu: "监控中心", route: "/监控中心/监控中心" },
];
// ========= 6. 指令解析(文字 & 语音共用) =========
function handleCommand(text) { function handleCommand(text) {
console.log("📥 收到指令:", text); console.log("📥 收到指令:", text);
// 找菜单 DOM const command = COMMANDS.find(c => text.includes(c.key));
const span = [...document.querySelectorAll("span")]
.find(el => el.innerText.trim() === text);
if (text.includes("添加设备")) {
// 展开父菜单(如果存在)
span && expandParentMenu(span);
// 路由跳转
goToRoute("/添加设备/添加设备");
return;
}
if (text.includes("设备")) {
span && expandParentMenu(span);
goToRoute("/设备管理");
return;
}
if (!command) {
alert("未识别指令:" + text); alert("未识别指令:" + text);
return;
} }
// ========= 6. 文字输入支持 ========= // 找菜单 DOM 并展开父菜单
const input = panel.querySelector("#voiceTextInput"); const span = [...document.querySelectorAll("span")]
.find(el => el.innerText.trim() === command.menu);
span && expandParentMenu(span);
// 路由跳转
goToRoute(command.route);
}
// ========= 7. 文字输入支持 =========
const input = panel.querySelector("#voiceTextInput");
input.addEventListener("keydown", (e) => { input.addEventListener("keydown", (e) => {
if (e.key === "Enter") { if (e.key === "Enter") {
const value = input.value.trim(); const value = input.value.trim();
@ -97,7 +98,7 @@ input.addEventListener("keydown", (e) => {
} }
}); });
// ========= 7. 语音识别支持 ========= // ========= 8. 语音识别支持 =========
let recognition; let recognition;
if (supportSpeech) { if (supportSpeech) {
recognition = new SpeechRecognition(); recognition = new SpeechRecognition();
@ -122,7 +123,7 @@ if (supportSpeech) {
panel.querySelector("#voiceBtn").innerText = "❌"; panel.querySelector("#voiceBtn").innerText = "❌";
} }
// ========= 8. 快捷键 Alt+V 支持语音 ========= // ========= 9. 快捷键 Alt+V 支持语音 =========
document.addEventListener("keydown", (e) => { document.addEventListener("keydown", (e) => {
if (e.altKey && e.key.toLowerCase() === "v") { if (e.altKey && e.key.toLowerCase() === "v") {
if (supportSpeech && recognition) recognition.start(); if (supportSpeech && recognition) recognition.start();