2023-10-13 08:11:54 +00:00
|
|
|
import { hasPermission } from '@/utils/permission/index'
|
2023-09-15 09:40:35 +00:00
|
|
|
import {
|
|
|
|
|
createRouter,
|
|
|
|
|
createWebHistory,
|
|
|
|
|
type NavigationGuardNext,
|
|
|
|
|
type RouteLocationNormalized,
|
2023-10-18 11:06:22 +00:00
|
|
|
type RouteRecordRaw,
|
|
|
|
|
type RouteRecordName
|
2023-09-15 09:40:35 +00:00
|
|
|
} from 'vue-router'
|
|
|
|
|
import { useUserStore } from '@/stores/user'
|
|
|
|
|
import { store } from '@/stores'
|
2023-10-13 08:11:54 +00:00
|
|
|
import { routes } from '@/router/routes'
|
2023-09-15 09:40:35 +00:00
|
|
|
const router = createRouter({
|
|
|
|
|
history: createWebHistory(import.meta.env.BASE_URL),
|
|
|
|
|
routes: routes
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// 解决刷新获取用户信息问题
|
|
|
|
|
let userStore: any = null
|
|
|
|
|
|
|
|
|
|
// 路由前置拦截器
|
|
|
|
|
router.beforeEach(
|
|
|
|
|
async (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
|
|
|
|
|
if (to.name === '404') {
|
|
|
|
|
next()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (userStore === null) {
|
|
|
|
|
userStore = useUserStore(store)
|
|
|
|
|
}
|
|
|
|
|
const notAuthRouteNameList = ['register', 'login', 'forgot_password', 'reset_password']
|
|
|
|
|
|
|
|
|
|
if (!notAuthRouteNameList.includes(to.name ? to.name.toString() : '')) {
|
|
|
|
|
const token = userStore.getToken()
|
|
|
|
|
if (!token) {
|
|
|
|
|
next({
|
|
|
|
|
path: '/login'
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (!userStore.userInfo) {
|
2023-10-09 11:03:41 +00:00
|
|
|
await userStore.profile()
|
2023-09-15 09:40:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-10-09 11:03:41 +00:00
|
|
|
// 判断是否有菜单权限
|
|
|
|
|
if (to.meta.permission ? hasPermission(to.meta.permission as any, 'OR') : true) {
|
|
|
|
|
next()
|
|
|
|
|
} else {
|
|
|
|
|
// 如果没有权限则直接取404页面
|
|
|
|
|
next('404')
|
|
|
|
|
}
|
2023-09-15 09:40:35 +00:00
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
2023-10-18 11:06:22 +00:00
|
|
|
export const getChildRouteListByPathAndName = (path: string, name?: RouteRecordName | null | undefined) => {
|
2023-09-15 09:40:35 +00:00
|
|
|
return getChildRouteList(routes, path, name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getChildRouteList: (
|
|
|
|
|
routeList: Array<RouteRecordRaw>,
|
|
|
|
|
path: string,
|
2023-10-18 11:06:22 +00:00
|
|
|
name?: RouteRecordName | null | undefined
|
2023-09-15 09:40:35 +00:00
|
|
|
) => Array<RouteRecordRaw> = (routeList, path, name) => {
|
|
|
|
|
for (let index = 0; index < routeList.length; index++) {
|
|
|
|
|
const route = routeList[index]
|
|
|
|
|
if (name === route.name && path === route.path) {
|
|
|
|
|
return route.children || []
|
|
|
|
|
}
|
|
|
|
|
if (route.children && route.children.length > 0) {
|
|
|
|
|
const result = getChildRouteList(route.children, path, name)
|
|
|
|
|
if (result && result?.length > 0) {
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default router
|