import Vue from 'vue'; import VueRouter from 'vue-router'; import store from '@/store/index'; const jwt = require('jsonwebtoken'); Vue.use(VueRouter); const routes = [ { path: '/', meta: { title: 'index', isleftarrow: false }, component: () => import('../views/index.vue'), }, // 文章辟谣 { path: '/refute/index', meta: { title: 'refute_index', isleftarrow: false }, component: () => import('../views/refute/index.vue'), }, // 社区话题 { path: '/community/index', meta: { title: 'community_index', isleftarrow: false }, component: () => import('../views/community/index.vue'), }, // 个人中心 { path: '/center/index', meta: { title: 'center_index', isleftarrow: false }, component: () => import('../views/center/index.vue'), }, ]; const router = new VueRouter({ mode: 'history', base: process.env.NODE_ENV === 'development' ? '' : process.env.VUE_APP_ROUTER, routes, }); router.beforeEach(async (to, form, next) => { if (to.name == 'account_user') { let res = await store.dispatch('login/toGetUser'); if (res && res.uid) { next(); } else { let key = sessionStorage.getItem('token'); let user = jwt.decode(key); if (user && user.uid) { store.commit('setUser', user, { root: true }); next(); } else { next({ name: 'login' }); } } } else { let res = await store.dispatch('login/toGetUser'); next(); } }); const originalPush = VueRouter.prototype.push; VueRouter.prototype.push = function push(location, onResolve, onReject) { if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject); return originalPush.call(this, location).catch(err => err); }; export default router;