import Vue from 'vue' import router from './router' import store from './store' import storage from 'store' import NProgress from 'nprogress' // progress bar import '@/components/NProgress/nprogress.less' // progress bar custom style import { setDocumentTitle, domTitle } from '@/utils/domUtil' import { ACCESS_TOKEN } from '@/store/mutation-types' import { Modal } from 'ant-design-vue' // NProgress Configuration NProgress.configure({ showSpinner: false }) const whiteList = ['login', 'register', 'registerResult', 'setting'] // no redirect whitelist const loginRoutePath = '/user/login' // 无默认首页的情况 const defaultRoutePath = '/welcome' router.beforeEach((to, from, next) => { NProgress.start() // start progress bar to.meta && (typeof to.meta.title !== 'undefined' && setDocumentTitle(`${to.meta.title} - ${domTitle}`)) if (storage.get(ACCESS_TOKEN)) { if (to.path === loginRoutePath) { next({ path: defaultRoutePath }) NProgress.done() } else { if (store.getters.roles.length === 0) { store .dispatch('GetInfo') .then(res => { if (res.menus.length < 1) { Modal.error({ title: '提示:', content: '无菜单权限,请联系管理员', okText: '确定', onOk: () => { store.dispatch('Logout').then(() => { window.location.reload() }) } }) return } const antDesignMenus = res.menus store.dispatch('GenerateRoutes', { antDesignMenus }).then(() => { // 根据菜单权限生成可访问的路由表 // 动态添加可访问路由表 // const routes = store.getters.addRouters // for(let i =0,length =routes.length;i { store.dispatch('Logout').then(() => { next({ path: loginRoutePath, query: { redirect: to.fullPath } }) }) }) } else { next() } } } else { if (whiteList.includes(to.name)) { // 在免登录白名单,直接进入 next() } else { next({ path: loginRoutePath, query: { redirect: to.fullPath } }) NProgress.done() // if current page is login will not trigger afterEach hook, so manually handle it } } }) router.afterEach(() => { NProgress.done() // finish progress bar })