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.
36 lines
911 B
36 lines
911 B
1 year ago
|
import type { Router } from 'vue-router'
|
||
|
import { PageEnum } from '@/enums/pageEnum'
|
||
|
import { useUserStore } from '@/store/moules/userStore/index'
|
||
|
|
||
|
export function setupRouterGuard(router: Router) {
|
||
|
createRouterGuards(router)
|
||
|
}
|
||
|
|
||
|
const WHITE_NAME_LIST: string[] = [
|
||
|
PageEnum.BASE_LOGIN,
|
||
|
]
|
||
|
|
||
|
function createRouterGuards(router: Router) {
|
||
|
const userStore = useUserStore()
|
||
|
// 前置
|
||
|
router.beforeEach(async (to, _from, next) => {
|
||
|
const isErrorPage = router.getRoutes().findIndex(item => item.name === to.name)
|
||
|
if (isErrorPage === -1)
|
||
|
next({ name: PageEnum.ERROR_PAGE_NAME_404 })
|
||
|
|
||
|
if (userStore.getToken) {
|
||
|
next()
|
||
|
}
|
||
|
else {
|
||
|
if (WHITE_NAME_LIST.includes(to.name as string))
|
||
|
next()
|
||
|
else
|
||
|
next({ name: PageEnum.BASE_LOGIN })
|
||
|
}
|
||
|
})
|
||
|
|
||
|
// router.afterEach((to, _) => {
|
||
|
// document.title = (to?.meta?.title as string) || document.title
|
||
|
// })
|
||
|
}
|