You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
19 lines
867 B
19 lines
867 B
import { useAuthStore } from '~/stores/auth'
|
|
import { MENU_GROUPS, menuItemByPath } from '~/utils/menus'
|
|
|
|
// 菜单权限路由守卫:侧边栏只是隐藏入口,直接敲 URL 也要拦。
|
|
// 命中菜单目录里的路径且当前账号无该菜单权限时,跳到第一个有权限的菜单(兜底登录页)。
|
|
// 仅客户端执行:auth 状态存 localStorage,服务端渲染阶段拿不到、判定必然误伤。
|
|
export default defineNuxtRouteMiddleware((to) => {
|
|
if (!import.meta.client) return
|
|
const auth = useAuthStore()
|
|
if (!auth.token) return // 未登录由布局/登录页自身逻辑处理
|
|
const item = menuItemByPath(to.path)
|
|
if (!item || auth.hasAccess(item.id)) return
|
|
for (const g of MENU_GROUPS) {
|
|
for (const m of g.items) {
|
|
if (auth.hasAccess(m.id)) return navigateTo(m.to)
|
|
}
|
|
}
|
|
return navigateTo('/')
|
|
})
|
|
|