import Vue from 'vue' import VueRouter from 'vue-router' import util from '@.mobile/plugin/axios/util' Vue.use(VueRouter) const routes = [ { path: '/', name: 'index', component: () => import('@.mobile/views/mobile-page/mobile-home') }, { path: '/login', name: 'login', // component: () => import('@.mobile/views/login.vue') component: () => import('@.mobile/views/mobile-page/mobile-login') }, { path: '/logout', name: 'logout', component: () => import('@.mobile/views/logout.vue') }, { path: '/report', name: 'report', component: () => import('@.mobile/views/report.vue'), redirect: { name: 'reportMain' }, children: [ { path: '/reportMain', name: 'reportMain', meta: { keepAlive: true // 添加这个作为标志符,表明该页面需要保留状态 }, component: () => import('@.mobile/views/reportMain.vue') }, { path: '/reportDetail', name: 'reportDetail', component: () => import('@.mobile/views/reportDetail.vue') } ] }, { path: '/mobile-login', name: 'mobileLogin', meta: { title: '登录' }, component: () => import('@.mobile/views/mobile-page/mobile-login') }, { path: '/mobile-changePsw', name: 'mobilechangePsw', meta: { title: '修改密码' }, component: () => import('@.mobile/views/mobile-page/mobile-changePsw') }, { path: '/mobile-home', name: 'mobileHome', meta: { title: '首页' }, component: () => import('@.mobile/views/mobile-page/mobile-home') }, { path: '/mobile-home', name: 'mobileHome', // meta: { // title: '' // }, component: () => import('@.mobile/views/mobile-page/mobile-home') }, { path: '/mobile-myInfor', name: 'mobileMyInfor', meta: { title: '我的信息' }, component: () => import('@.mobile/views/mobile-page/mobile-myInfor') }, { path: '/mobile-main', name: 'mobileMain', meta: { title: '我的' }, component: () => import('@.mobile/views/mobile-page/mobile-main') }, { path: '/mobile-estimatedData', name: 'mobileEstimatedData', meta: { title: '月预估数据' }, component: () => import('@.mobile/views/mobile-page/mobile-estimatedData') } ] const router = new VueRouter({ routes }) // 免校验token白名单 const whiteList = ['/mobile-login', '/login'] router.beforeEach(async (to, from, next) => { const token = util.getToken() if (whiteList.indexOf(to.path) === -1) { // 这里暂时将cookie里是否存有token作为验证是否登录的条件 // 请根据自身业务需要修改 if (token && token !== 'undefined') { next() } else { // 将当前预计打开的页面完整地址临时存储 登录后继续跳转 // 这个 cookie(redirect) 会在登录后自动删除 util.cookies.set('redirect', to.fullPath) // 没有登录的时候跳转到登录界面 next({ name: 'mobileLogin' }) } } else { if (to.name === 'mobileLogin' || to.name === 'login') { // 如果已经登录,则直接进入系统 if (token && token !== undefined) { next(from.path, true) } else { next() } } else { next() } } }) router.afterEach(to => {}) export default router