import router from './router' import store from './store' import { Message } from 'element-ui' import { getToken } from '@/utils/auth' import getPageTitle from '@/utils/get-page-title' const whiteList = ['/login', '/auth-redirect'] // no redirect whitelist const toSupplier = ['/supplier/list', '/supplier'] // const onlyPath = ['/auth/list', '/product/list', '/product/add', '/product/edit'] router.beforeEach(async(to, from, next) => { // 设置页面名称 // eslint-disable-next-line indent document.title = getPageTitle(to.meta.title) const hasToken = getToken() if (hasToken) { /** * 满足要求则关闭所有标签,防止代理数据冲突 * 条件:1、sotre中存有代理数据 即 proxyInfo !== null * 2、即将跳转的页面为 /supplier/list 或者 /supplier */ if (toSupplier.includes(to.path) && store.getters.proxyInfo !== null) { store.dispatch('tagsView/delAllProxyView') store.commit('user/SET_PROXY_INFO', null) console.log('关闭其他的标签') } if (to.path === '/login') { // 在登录状态下访问login页面,直接跳转到首页 next('/supplier') } else { // 加载国家列表 store.dispatch('app/setCountry') const hasInitRouter = store.getters.initRouter // 是否需要初始化路由 if (hasInitRouter) { next() } else { try { // 设置路由的初始化状态 store.commit('permission/SET_INITROUTER', true) // 通过 const accessRoutes = await store.dispatch('permission/generateRoutes', store.getters.roles) // dynamically add accessible routes router.addRoutes(accessRoutes) // hack method to ensure that addRoutes is complete // set the replace: true, so the navigation will not leave a history record next({ ...to, replace: true }) } catch (error) { // remove token and go to login page to re-login await store.dispatch('user/resetToken') Message.error(error || 'Has Error') next(`/login?redirect=${to.path}`) } } } } else { /* has no token*/ if (whiteList.indexOf(to.path) !== -1) { // in the free login whitelist, go directly next() } else { // other pages that do not have permission to access are redirected to the login page. next(`/login`) // Message({ message: '登录失效,请重新登录!' }) // router.replace('/login') return } } })