import { isEmpty } from '@/utils' import store from '@/store' const Article = () => import('@/components/Article') const Login = () => import('@/components/Login') const Register = () => import('@/components/Register') const Onlinewebsocket = () => import('@/components/Onlinewebsocket') const Home = () => import('@/components/Home') const Bilicom = () => import('@/components/Bilicom') const Mavoneditor = () => import('@/components/Mavoneditor') const GrilShow = () => import('@/components/GrilShow') const Csslearn = () => import('@/components/Csslearn') const defaultRoutes = [ { path: '/', name: 'Article', component: Article, hidden: true }, { path: '/login', name: 'Login', component: Login, hidden: true }, { path: '/register', name: 'Register', component: Register, hidden: true }, { path: '/home', name: 'Home', component: Home, hidden: true }, { path: '/onlinewebsocket', name: 'Onlinewebsocket', component: Onlinewebsocket, hidden: true }, { path: '/bilicom', name: 'Bilicom', component: Bilicom, hidden: true }, { path: '/mavoneditor', name: 'Mavoneditor', component: Mavoneditor, hidden: true }, { path: '/gril', name: 'grilshow', component: GrilShow, hidden: true }, { path: '/css', name: 'css', component: Csslearn, hidden: true } ] const useRouter = (Vue, VueRouter) => { let routes = [ ...defaultRoutes ] 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() } }) Vue.use(VueRouter) return router } export default useRouter