<template>
<div/>
</template>
<script>
import annyang from 'annyang'
import cnchar from 'cnchar'
import { menuRoutes } from '@/router/index'
export default {
name: 'SpeechRecognition',
data () {
return {
routerMap: [],
topMenuMap: []
}
},
created () {
this.initializeRouterData()
},
mounted () {
this.initializeSpeechRecognition()
},
beforeDestroy () {
if (annyang) {
annyang.removeCallback('result', this.handleSpeechResult)
annyang.abort()
}
},
methods: {
initializeRouterData () {
this.routerMap = Object.freeze(this.getAllRoutesMeta(menuRoutes))
this.topMenuMap = Object.freeze(this.findTopLevelPath(menuRoutes))
},
initializeSpeechRecognition () {
if (annyang) {
annyang.setLanguage('zh-CN')
annyang.addCallback('result', this.handleSpeechResult)
annyang.start()
this.$message.success('语音助手已运行!')
} else {
this.$message.error('抱歉,您的浏览器不支持语音识别。')
}
},
handleSpeechResult (phrases) {
console.log('您说了:', phrases)
const [word] = phrases
if (word) {
this.gotoPage(word)
}
},
gotoPage (word) {
const hasKeyWord = word.includes('打开')
const fullStr = cnchar.spell(word, 'first', 'low')
const noActionWord = word.replace('打开', '')
const accurateWord = this.routerMap.find((t) => t.title === noActionWord)
if (accurateWord) {
this.navigateToRoute(accurateWord)
return
}
for (const route of this.routerMap) {
const { title } = route
const pinyin = cnchar.spell(title, 'first', 'low')
if (hasKeyWord && (word.includes(title) || fullStr.includes(pinyin))) {
this.navigateToRoute(route)
return
}
}
if (hasKeyWord) {
this.$message.error('抱歉,未能识别清楚,请重新尝试。')
return
}
console.log('未匹配到关键词', word)
},
navigateToRoute (route) {
const { name: routeName, path, child } = route
const isTop = this.topMenuMap.some((t) => t.path === path)
const jumpPath = isTop ? { path, child } : this.topMenuMap.find((t) => t.children.includes(path))
this.$store.dispatch('permission/setActiveMenuPath', jumpPath).then(() => {
this.$router.push({ name: routeName })
})
},
getAllRoutesMeta (routes) {
const result = []
routes.forEach(route => {
if (route.meta && route.meta.title && route.name) {
result.push({
title: route.meta.title,
name: route.name,
path: route.path,
child: route.children || []
})
}
if (route.children && route.children.length > 0) {
const childMeta = this.getAllRoutesMeta(route.children)
result.push(...childMeta)
}
})
return result
},
findTopLevelPath (routes) {
function getChildren (route) {
if (!route.children || route.children.length === 0) {
return []
} else {
return route.children.flatMap(child => {
const childPaths = [child.path]
if (child.children && child.children.length > 0) {
childPaths.push(...getChildren(child))
}
return childPaths
})
}
}
return routes.map(route => ({
path: route.path,
child: route.children,
children: getChildren(route)
}))
}
}
}
</script>
<style scoped>
</style>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!