|
|
@@ -67,24 +67,110 @@ const menuList = [
|
|
|
}
|
|
|
]
|
|
|
|
|
|
-const activeMenu = computed((): string => {
|
|
|
- return (route.meta?.activeMenu as string) || route.path
|
|
|
-})
|
|
|
+const activeMenu = ref()
|
|
|
+activeMenu.value = (route.meta?.activeMenu as string) || route.path
|
|
|
+// console.log('activeMenu', activeMenu.value)
|
|
|
|
|
|
-const changeRouter = (path: string) => {
|
|
|
- router.push({ path })
|
|
|
+// // 未登录白名单
|
|
|
+// const whiteList = ['/login', '/public-tracking', '/public-tracking/detail']
|
|
|
+// // 判断是否允许跳转
|
|
|
+// const isAllowJump = (path: string) => {
|
|
|
+// // 判断是否登录
|
|
|
+// if (!whiteList.includes(path) && !localStorage.getItem('token')) {
|
|
|
+// if (whiteList.includes(route.path)) {
|
|
|
+// ElMessage.warning('Please log in to use this feature.')
|
|
|
+// console.log('跳转')
|
|
|
+// activeMenu.value = (route.meta?.activeMenu as string) || route.path
|
|
|
+// return false
|
|
|
+// } else {
|
|
|
+// ElMessage.warning('Please log in to use this feature.')
|
|
|
+// return '/public-tracking'
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// return path
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+// const changeRouter = (path: string) => {
|
|
|
+// let toPath = path
|
|
|
+// if (path === '/tracking' && !localStorage.getItem('token')) {
|
|
|
+// toPath = '/public-tracking'
|
|
|
+// }
|
|
|
+// if (isAllowJump(toPath)) {
|
|
|
+// router.push(isAllowJump(toPath))
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+const getAllMenuPaths = (menuList) => {
|
|
|
+ let paths = []
|
|
|
+ menuList.forEach((item) => {
|
|
|
+ paths.push(item.path) // 添加主菜单路径
|
|
|
+ if (item.children && item.children.length > 0) {
|
|
|
+ // 递归添加子菜单路径
|
|
|
+ paths = paths.concat(getAllMenuPaths(item.children))
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return paths
|
|
|
}
|
|
|
+// 获取所有菜单项的路径(包括子菜单)
|
|
|
+const menuPaths = getAllMenuPaths(menuList)
|
|
|
+
|
|
|
+// 未登录白名单
|
|
|
+const whiteList = ['/login', '/public-tracking', '/public-tracking/detail']
|
|
|
+
|
|
|
+// 判断是否允许跳转
|
|
|
+const isAllowJump = (path) => {
|
|
|
+ // 判断是否登录
|
|
|
+ if (!whiteList.includes(path) && !localStorage.getItem('token')) {
|
|
|
+ ElMessage.warning('Please log in to use this feature.')
|
|
|
+ activeMenu.value = route.path // 保持选中状态不变
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+// 路由跳转函数
|
|
|
+const changeRouter = (path) => {
|
|
|
+ let toPath = path
|
|
|
+ if (path === '/tracking' && !localStorage.getItem('token')) {
|
|
|
+ toPath = '/public-tracking'
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果允许跳转,执行跳转
|
|
|
+ if (isAllowJump(toPath)) {
|
|
|
+ router.push(toPath)
|
|
|
+ nextTick(() => {
|
|
|
+ activeMenu.value = (route.meta?.activeMenu as string) || route.path // 确保菜单栏选中状态为当前路径
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ // 如果不允许跳转,保持当前 activeMenu 不变
|
|
|
+ nextTick(() => {
|
|
|
+ activeMenu.value = (route.meta?.activeMenu as string) || route.path // 确保菜单栏选中状态为当前路径
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 计算属性,返回当前菜单项
|
|
|
+const computedActiveMenu = computed(() =>
|
|
|
+ menuPaths.includes(activeMenu.value) ? activeMenu.value : ''
|
|
|
+)
|
|
|
|
|
|
const handleCollapseClick = () => {
|
|
|
isCollapse.value = !isCollapse.value
|
|
|
}
|
|
|
+const menuRef = ref()
|
|
|
+const test = () => {
|
|
|
+ console.log('test', menuRef.value)
|
|
|
+}
|
|
|
</script>
|
|
|
|
|
|
<template>
|
|
|
+ <!-- <el-button @click="test">测试</el-button> -->
|
|
|
<el-menu
|
|
|
+ ref="menuRef"
|
|
|
class="layout-menu"
|
|
|
@select="changeRouter"
|
|
|
- :default-active="activeMenu"
|
|
|
+ :default-active="computedActiveMenu"
|
|
|
:collapse="isCollapse"
|
|
|
>
|
|
|
<template v-for="item in menuList" :key="item.index">
|