import router from './router' import store from './store' import { Message } from 'element-ui' import NProgress from 'nprogress' import 'nprogress/nprogress.css' import { getToken, getCompanyId } from '@/utils/auth' NProgress.configure({ showSpinner: false }) const whiteList = ['/login', '/auth-redirect', '/bind', '/register'] router.beforeEach((to, from, next) => { NProgress.start() // debugger if (getToken()) { /* has token*/ if (to.path === '/login') { if (!store.getters.userId) { store.dispatch('GetInfo') } next() NProgress.done() } else { if (getCompanyId()) { // 判断是否有公司ID 没有去选择公司 不能进入 if (store.getters.isInit) { next() } else { // 初始化路由 store.dispatch('GenerateRoutes').then(accessRoutes => { store.commit('INIT') router.addRoutes(accessRoutes) // 动态添加可访问路由表 next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 }) } } else { next(`/login?redirect=${to.fullPath}`) } } } else { // 没有token if (store.getters.companyList.length > 0) { store.commit("SET_COMPANYLIST", []) } if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入 next() } else { next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页 NProgress.done() } } }) router.afterEach(() => { NProgress.done() })