/** * @author chuzhixin 1204505056@qq.com * @description 路由守卫,目前两种模式:all模式与intelligence模式 */ import router from '@/router' import store from '@/store' import getPageTitle from '@/utils/pageTitle' // import * as api from '@/api/publicList' import { authentication, loginInterception, recordRoute, routesWhiteList, } from '@/config' router.beforeEach(async(to, from, next) => { let hasToken = store.getters['user/accessToken'] if (!loginInterception) hasToken = true console.log(localStorage.getItem('accessToken')) console.log(localStorage.getItem('accessToken')) if (hasToken) { if (to.path === '/login') { next({ path: '/' }) } else { const hasRoles = store.getters['acl/admin'] || store.getters['acl/role'].length > 0 || store.getters['acl/ability'].length > 0 if (hasRoles) { // console.log('to.matched') // console.log(to.matched) if (to.matched.length === 0) { // alert('没有匹配') next('/404') // 判断此跳转路由的来源路由是否存在,存在的情况跳转到来源路由,否则跳转到404页面 } next() } else { store.commit('getMiddleConfig') try { if (loginInterception) { await store.dispatch('user/getUserInfo') } else { //loginInterception为false(关闭登录拦截时)时,创建虚拟角色 await store.dispatch('user/setVirtualRoles') } let accessRoutes = [] if (authentication === 'intelligence') { accessRoutes = await store.dispatch('routes/setRoutes') } else if (authentication === 'all') { accessRoutes = await store.dispatch('routes/setAllRoutes') } accessRoutes.forEach((item) => { router.addRoute(item) }) console.log('accessRoutes') console.log(accessRoutes) // alert('判断是否显示中间页的middleStatus值' + store.state.middleStatus) // alert(store.state.firstMiddleState) if (store.state.middleStatus == 1 && store.state.firstMiddleState == true) { next('/middle') } else { var aa = (store.state.homePageUrl).slice(7) var bb = aa.slice(0, -6) // 首页请求地址动态配置 if (store.state.homePageUrl) { var pathUrl = bb next(pathUrl) } else { next(accessRoutes[0].children[0].path) } } } catch { await store.dispatch('user/resetAll') if (recordRoute) next({ path: '/login', query: { redirect: to.path }, replace: true, }) else next({ path: '/login', replace: true }) } } } } else { routesWhiteList // next({ path: '/login', replace: true }) if (routesWhiteList.indexOf(to.path) !== -1) { next() } else { if (recordRoute) next({ path: '/login', query: { redirect: to.path }, replace: true }) else next({ path: '/login', replace: true }) } } }) router.afterEach((to) => { document.title = getPageTitle(to.meta.title) })