import Vue from 'vue' import Nprogress from 'nprogress' import 'nprogress/nprogress.css' // import { getToken } from '@/utils/auth' import { isEmpty } from '@/utils' import VueRouter from 'vue-router' // import Router from 'vue-router' import store from '@/store' // store import Article from '@/components/Article' import Login from '@/components/Login' import Register from '@/components/Register' import Onlinewebsocket from '@/components/Onlinewebsocket' import Home from '@/components/Home' import Bilicom from '@/components/Bilicom' import Mavoneditor from '@/components/Mavoneditor' import GrilShow from '@/components/GrilShow' import Csslearn from '@/components/Csslearn' Vue.use(VueRouter) const defaultRoutes = [ { path: '/', name: 'Article', component: Article }, { path: '/login', name: 'Login', component: Login }, { path: '/register', name: 'Register', component: Register }, { path: '/home', name: 'Home', component: Home }, { path: '/onlinewebsocket', name: 'Onlinewebsocket', component: Onlinewebsocket }, { path: '/bilicom', name: 'Bilicom', component: Bilicom }, { path: '/mavoneditor', name: 'Mavoneditor', component: Mavoneditor }, { path: '/gril', name: 'grilshow', component: GrilShow }, { path: '/css', name: 'css', component: Csslearn } ] let routes = [ ...defaultRoutes // ...modulesRoutes, // ...errorRoutes, // ...dashboardRouters ] const originalPush = VueRouter.prototype.push VueRouter.prototype.push = function push (location) { return originalPush.call(this, location).catch((err) => err) } const router = new VueRouter({ routes }) // const whiteList = ['/login', '/register'] router.beforeEach(async (to, from, next) => { next() let yma16siteUserInfo = localStorage.getItem('yma16siteUserInfo') ? JSON.parse(localStorage.getItem('yma16siteUserInfo')) : '' let name = yma16siteUserInfo.username let pwd = yma16siteUserInfo.password let hasToken = { name: name, password: pwd } console.log('localStorage', hasToken) if (hasToken.name && hasToken.password) { if (isEmpty(store.state.user.userInfo)) { // 空的 modules下的user console.log('路由的登录认证') // 用户自主登录 await store.dispatch('user/loginUserInfo', hasToken) next() } else { next() } } else { // next({ path: "/login" }); //去登录 next() } }) router.afterEach((to, from) => { Nprogress.done() }) export default router