update: 优化路由处理

This commit is contained in:
xieyonghong 2023-02-22 13:36:51 +08:00
parent 58bbb1ac36
commit e313b36471
2 changed files with 35 additions and 45 deletions

View File

@ -14,56 +14,46 @@ const router = createRouter({
}
})
const filterPath = [
'/form',
'/search'
]
router.beforeEach((to, from, next) => {
// TODO 切换路由取消请求
const isFilterPath = filterPath.includes(to.path)
if (isFilterPath) {
next()
} else {
const token = getToken()
if (token) {
if (to.path === LoginPath) {
next({ path: '/' })
} else {
const userInfo = useUserInfo()
const system = useSystem()
const menu = useMenuStore()
if (!menu.siderMenus.length) {
userInfo.getUserInfo().then(() => {
system.getSystemVersion().then((menuData: any[]) => {
menuData.forEach(r => {
router.addRoute('base', r)
})
// router.addRoute('base',{
// path: '/:pathMatch(.*)',
// name: 'error',
// component: () => NotFindPage
// })
next({ ...to, replace: true })
})
}).catch(() => {
cleanToken()
next({ path: LoginPath })
})
} else {
next()
}
}
const token = getToken()
if (token) {
if (to.path === LoginPath) {
next({ path: '/' })
} else {
if (to.path === LoginPath) {
next()
const userInfo = useUserInfo()
const system = useSystem()
const menu = useMenuStore()
if (!menu.siderMenus.length) {
userInfo.getUserInfo().then(() => {
system.getSystemVersion().then((menuData: any[]) => {
menuData.forEach(r => {
router.addRoute('base', r)
})
router.addRoute('base',{
path: '/:pathMatch(.*)',
name: 'error',
component: () => NotFindPage
})
next({ ...to, replace: true })
})
}).catch(() => {
cleanToken()
next({ path: LoginPath })
})
} else {
next({ path: LoginPath })
next()
}
}
} else {
if (to.path === LoginPath) {
next()
} else {
next({ path: LoginPath })
}
}
})

View File

@ -114,7 +114,7 @@ export const useMenuStore = defineStore({
}
})
this.siderMenus = silderMenus
console.log('silderMenus', silderMenus)
console.log('menusData', menusData)
res(menusData)
}
})