2022-04-08 02:42:44 +00:00
|
|
|
/**
|
|
|
|
|
* 权限控制
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import NProgress from 'nprogress'
|
|
|
|
|
import store from './store'
|
2022-06-23 03:41:08 +00:00
|
|
|
import router, { indexName } from './router'
|
2022-04-08 02:42:44 +00:00
|
|
|
import 'nprogress/nprogress.css'
|
|
|
|
|
|
|
|
|
|
// NProgress配置
|
|
|
|
|
NProgress.configure({ showSpinner: false })
|
|
|
|
|
|
|
|
|
|
const loginPath = '/login'
|
|
|
|
|
const defaultPath = '/'
|
|
|
|
|
// 免登录白名单
|
|
|
|
|
const whiteList = ['/login']
|
|
|
|
|
|
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
|
|
NProgress.start()
|
|
|
|
|
// 开始 Progress Bar
|
|
|
|
|
to.meta?.title && (document.title = to.meta.title as string)
|
|
|
|
|
const token = store.getters.token
|
|
|
|
|
if (token) {
|
|
|
|
|
// 获取用户信息
|
2022-04-20 04:10:22 +00:00
|
|
|
if (store.getters.permission.length === 0) {
|
|
|
|
|
try {
|
|
|
|
|
await store.dispatch('user/getUser')
|
|
|
|
|
const routes = await store.dispatch('permission/generateRoutes')
|
|
|
|
|
routes.forEach((route: any) => {
|
2022-06-23 03:41:08 +00:00
|
|
|
if(!route.children) {
|
|
|
|
|
router.addRoute(indexName, route)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
router.addRoute(route) // 动态添加可访问路由表
|
2022-04-20 04:10:22 +00:00
|
|
|
})
|
|
|
|
|
console.log(router.getRoutes())
|
|
|
|
|
if (to.path === '/login') {
|
|
|
|
|
next({ path: '/' })
|
|
|
|
|
} else {
|
|
|
|
|
next({ ...to, replace: true }) //确保addRoutes已完成
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
await store.dispatch('user/logout')
|
|
|
|
|
next({ path: loginPath, query: { redirect: to.fullPath } })
|
|
|
|
|
NProgress.done()
|
|
|
|
|
}
|
2022-04-08 02:42:44 +00:00
|
|
|
} else {
|
|
|
|
|
next()
|
|
|
|
|
}
|
2022-04-20 04:10:22 +00:00
|
|
|
|
2022-04-08 02:42:44 +00:00
|
|
|
} else if (whiteList.includes(to.path as string)) {
|
|
|
|
|
// 在免登录白名单,直接进入
|
|
|
|
|
next()
|
|
|
|
|
} else {
|
|
|
|
|
next({ path: loginPath, query: { redirect: to.fullPath } })
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.afterEach(async (to, from) => {
|
|
|
|
|
NProgress.done()
|
|
|
|
|
})
|