import Vue from 'vue' import Router from 'vue-router' import { routes } from './routes' import { getToken } from '@/utils/navigation' import 'nprogress/nprogress.css' const appConfig = require('@/app.config') const { homeRouterName, loginRouteName, routeMode } = appConfig const LOGIN_PAGE_ROUTE_NAME = loginRouteName Vue.use(Router) const baseName = process.env.NODE_ENV === 'production' ? `/${appConfig.projectName}/` : '/' const router = new Router({ base: baseName, mode: routeMode || 'hash', routes: routes }) router.beforeEach((to, from, next) => { Vue.prototype.$cancels.forEach((cancel) => { cancel() }) Vue.prototype.$cancels = [] const token = getToken() if (!token && to.name !== LOGIN_PAGE_ROUTE_NAME) { // 未登录且要跳转的页面不是登录页 /* next({ name: LOGIN_PAGE_ROUTE_NAME // 跳转到登录页 }) */ next() // 跳转 } else if (!token && to.name === LOGIN_PAGE_ROUTE_NAME) { // 未登陆且要跳转的页面是登录页 next() // 跳转 } else if (token && to.name === LOGIN_PAGE_ROUTE_NAME) { // 已登录且要跳转的页面是登录页 // next({ // name: homeRouterName // 跳转到homeName页 // }) console.log(homeRouterName) next() } else if (!token) { // next({ // name: LOGIN_PAGE_ROUTE_NAME // 跳转到登录页 // }) console.log(LOGIN_PAGE_ROUTE_NAME) next() } else { next() } // 不需要登录认证的路由 if (Object.hasOwnProperty.call(to.meta, 'noLoginIdentify') && to.meta.noLoginIdentify) { next() } // next() }) router.beforeResolve((to, from, next) => { next() }) router.afterEach((to, from) => { }) export default router