|
@ -5,13 +5,26 @@ let asyncRouterFlag = 0 |
|
|
|
|
|
|
|
|
const whiteList = ['Login', 'Init'] |
|
|
const whiteList = ['Login', 'Init'] |
|
|
|
|
|
|
|
|
|
|
|
const getRouter = async() => { |
|
|
|
|
|
await store.dispatch('router/SetAsyncRouter') |
|
|
|
|
|
await store.dispatch('user/GetUserInfo') |
|
|
|
|
|
const asyncRouters = store.getters['router/asyncRouters'] |
|
|
|
|
|
asyncRouters.map(asyncRouter => { |
|
|
|
|
|
router.addRoute(asyncRouter) |
|
|
|
|
|
}) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
router.beforeEach(async(to, from, next) => { |
|
|
router.beforeEach(async(to, from, next) => { |
|
|
const token = store.getters['user/token'] |
|
|
const token = store.getters['user/token'] |
|
|
// 在白名单中的判断情况
|
|
|
// 在白名单中的判断情况
|
|
|
document.title = getPageTitle(to.meta.title) |
|
|
document.title = getPageTitle(to.meta.title) |
|
|
if (whiteList.indexOf(to.name) > -1) { |
|
|
if (whiteList.indexOf(to.name) > -1) { |
|
|
if (token) { |
|
|
if (token) { |
|
|
next({ path: '/layout/dashboard' }) |
|
|
|
|
|
|
|
|
if (!asyncRouterFlag) { |
|
|
|
|
|
asyncRouterFlag++ |
|
|
|
|
|
await getRouter() |
|
|
|
|
|
} |
|
|
|
|
|
next({ name: store.getters['user/userInfo'].authority.defaultRouter }) |
|
|
} else { |
|
|
} else { |
|
|
next() |
|
|
next() |
|
|
} |
|
|
} |
|
@ -21,15 +34,14 @@ router.beforeEach(async(to, from, next) => { |
|
|
// 添加flag防止多次获取动态路由和栈溢出
|
|
|
// 添加flag防止多次获取动态路由和栈溢出
|
|
|
if (!asyncRouterFlag) { |
|
|
if (!asyncRouterFlag) { |
|
|
asyncRouterFlag++ |
|
|
asyncRouterFlag++ |
|
|
await store.dispatch('router/SetAsyncRouter') |
|
|
|
|
|
await store.dispatch('user/GetUserInfo') |
|
|
|
|
|
const asyncRouters = store.getters['router/asyncRouters'] |
|
|
|
|
|
asyncRouters.map(asyncRouter => { |
|
|
|
|
|
router.addRoute(asyncRouter) |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
await getRouter() |
|
|
next({ ...to, replace: true }) |
|
|
next({ ...to, replace: true }) |
|
|
} else { |
|
|
} else { |
|
|
|
|
|
if (to.matched.length) { |
|
|
next() |
|
|
next() |
|
|
|
|
|
} else { |
|
|
|
|
|
next({ path: '/layout/404' }) |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// 不在白名单中并且未登陆的时候
|
|
|
// 不在白名单中并且未登陆的时候
|
|
|