import Vue from 'vue'; import store from '@/store'; import router from './router'; import NProgress from 'nprogress'; import { admin} from './router'; import { getToken, removeToken} from '@/utils/auth'; import { LoginParams } from '@/utils/login'; import { getSessionStorage } from '@/utils/auth'; import localStore from 'storejs'; function hasPermission(roles, permissionRoles) { if (roles.indexOf(admin) >= 0) return true; if (!permissionRoles) return true; return roles.some(role => permissionRoles.indexOf(role) >= 0); } const whiteList = ['/login', '/design/login', '/xty/login', '/designxty/login', '/gzb/login', '/designgzb/login', '/jlmap3d/devicetrain']; // 不重定向白名单 const designPageRegex = [/^\/design/, /^\/scriptDisplay/, /^\/publish/, /^\/orderauthor/, /^\/system/, /^\/display\/record/, /^\/display\/manage/, /^\/apply/, /^\/plan/, /^\/display\/plan/]; function isDesignPage(toRoutePath) { return designPageRegex.some(item => { return item.test(toRoutePath); }); } const loginPage = whiteList[0]; const loginDesignPage = whiteList[1]; const loginXtyPage = whiteList[2]; const loginDesignXtyPage = whiteList[3]; const loginGzbPage = whiteList[4]; const loginDesignGzbPage = whiteList[5]; const loginDesignPageMenu = { design: loginDesignPage, designxty: loginDesignXtyPage, designgzb: loginDesignGzbPage }; const loginPageMenu = { login: loginPage, xty: loginXtyPage, gzb: loginGzbPage }; // 获取路径数据 function getRouteInfo(to) { let loginPath = '/'; let clientId = ''; const toRoutePath = to.redirectedFrom || to.path; const current_session = getSessionStorage('project'); if (/^\/designxty/.test(toRoutePath)) { loginPath = loginDesignXtyPage; clientId = LoginParams.Design.clientId; } else if (/^\/designgzb/.test(toRoutePath)) { loginPath = loginDesignGzbPage; clientId = LoginParams.Design.clientId; } else if (isDesignPage(toRoutePath)) { loginPath = loginDesignPageMenu[current_session] || loginDesignPage; clientId = LoginParams.Design.clientId; if (current_session && !current_session.startsWith('design')) { removeToken(); } } else if ( /^\/xty/.test(toRoutePath)) { loginPath = loginXtyPage; clientId = null; } else if ( /^\/gzb/.test(toRoutePath)) { loginPath = loginGzbPage; clientId = null; } else { loginPath = loginPageMenu[current_session] || loginPath; clientId = null; if (current_session && current_session.startsWith('design')) { removeToken(); } } return { clientId, loginPath }; } function handleRoute(to, from, next, routeInfo) { if (store.getters.roles.length === 0) { // 拉取用户信息 store.dispatch('GetInfo', getToken()).then(res => { // 根据roles权限生成可访问的路由表 const roles = res.roles; store.dispatch('GenerateRoutes', { roles, clientId: routeInfo.clientId }).then(() => { // 动态添加可访问路由表 router.addRoutes(store.getters.addRouters); // router.addRoutes(asyncRouter1); if (to.redirectedFrom) { next({ path: to.redirectedFrom, replace: true }); } else { next({ ...to, replace: true }); } }); }).catch(() => { store.dispatch('FedLogOut').then(() => { Vue.prototype.$messageBox('验证失败,请重新登录!'); next({ path: routeInfo.loginPath }); }); }); } else { // 除没有动态改变权限的需求可直接next() 删下方权限判断 if (hasPermission(store.getters.roles, to.meta.roles)) { if (to.path === '/404' && to.redirectedFrom === '/') { next(localStore.get('trainingPlatformRoute' + store.getters.id) || '/trainingPlatform'); } else { next(); } } else { next({ path: '/401', replace: true, query: { noGoBack: true } }); } } } router.beforeEach((to, from, next) => { NProgress.start(); const routeInfo = getRouteInfo(to); if (getToken()) { // 已登录 if (to.path === routeInfo.loginPath) { // 登录页面不拦截 next(); } else { // 进入系统重新计算路由 handleRoute(to, from, next, routeInfo); } } else { // 未登录情况下 if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入 next(); } else { // 否则全部重定向到登录页 next(routeInfo.loginPath); } } }); NProgress.configure({ easing: 'ease', // 动画方式 speed: 500, // 递增进度条的速度 showSpinner: false, // 是否显示加载ico trickleSpeed: 200, // 自动递增间隔 minimum: 0.3 // 初始化时的最小百分比 }); router.afterEach(() => { // 结束Progress NProgress.done(); });