12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import router from './router'
- import { store } from '@/store/index'
- import getPageTitle from '@/utils/page'
- let asyncRouterFlag = 0
- const whiteList = ['login', 'init']
- router.beforeEach(async (to, from, next) => {
- const token = store.getters['user/token']
-
-
- document.title = getPageTitle(to.meta.title)
- if (whiteList.indexOf(to.name) > -1) {
- if (token) {
- next({ name: store.getters["user/userInfo"].authority.defaultRouter })
- } else {
- next()
- }
- } else {
-
- if (token) {
-
- if (!asyncRouterFlag && store.getters['router/asyncRouters'].length == 0) {
- asyncRouterFlag++
- await store.dispatch('router/SetAsyncRouter')
- const asyncRouters = store.getters['router/asyncRouters']
- asyncRouters.forEach(item => {
- router.addRoute(item)
- })
- next({ ...to, replace: true })
- } else {
- if (to.matched.length) {
- next()
- } else {
- next({ path: "/layout/404" })
- }
- }
- }
-
- if (!token) {
- next({
- name: "login",
- query: {
- redirect: document.location.hash
- }
- })
- }
- }
- })
|