index.js 2.8 KB
Newer Older
yma16's avatar
yma16 已提交
1
import Vue from 'vue'
yma16's avatar
yma16 已提交
2

yma16's avatar
yma16 已提交
3 4 5 6 7
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
// import { getToken } from '@/utils/auth'
import { isEmpty } from '@/utils'
import VueRouter from 'vue-router'
yma16's avatar
yma16 已提交
8
// import Router from 'vue-router'
yma16's avatar
yma16 已提交
9
import store from '@/store'
yma16's avatar
yma16 已提交
10
// store
yma16's avatar
yma16 已提交
11 12 13 14 15 16 17
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'
yma16's avatar
yma16 已提交
18

yma16's avatar
yma16 已提交
19 20
import GrilShow from '@/components/GrilShow'
import Csslearn from '@/components/Csslearn'
yma16's avatar
yma16 已提交
21

yma16's avatar
yma16 已提交
22
Vue.use(VueRouter)
yma16's avatar
yma16 已提交
23
const defaultRoutes = [
yma16's avatar
yma16 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    {
        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
    }
]
yma16's avatar
yma16 已提交
70

yma16's avatar
yma16 已提交
71
let routes = [
yma16's avatar
yma16 已提交
72 73 74 75 76 77 78 79 80
    ...defaultRoutes
    // ...modulesRoutes,
    // ...errorRoutes,
    // ...dashboardRouters
]
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
    return originalPush.call(this, location).catch((err) => err)
}
yma16's avatar
yma16 已提交
81
const router = new VueRouter({
yma16's avatar
yma16 已提交
82 83
    routes
})
yma16's avatar
yma16 已提交
84

yma16's avatar
yma16 已提交
85
// const whiteList = ['/login', '/register']
yma16's avatar
yma16 已提交
86 87

router.beforeEach(async (to, from, next) => {
yma16's avatar
yma16 已提交
88 89 90 91 92 93
    next()
    let yma16siteUserInfo = localStorage.getItem('yma16siteUserInfo')
        ? JSON.parse(localStorage.getItem('yma16siteUserInfo'))
        : ''
    let name = yma16siteUserInfo.username
    let pwd = yma16siteUserInfo.password
yma16's avatar
yma16 已提交
94

yma16's avatar
yma16 已提交
95 96 97
    let hasToken = {
        name: name,
        password: pwd
yma16's avatar
yma16 已提交
98
    }
yma16's avatar
yma16 已提交
99 100 101 102 103 104 105 106 107 108 109 110
    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 {
yma16's avatar
yma16 已提交
111
    // next({ path: "/login" }); //去登录
yma16's avatar
yma16 已提交
112 113 114
        next()
    }
})
yma16's avatar
yma16 已提交
115 116

router.afterEach((to, from) => {
yma16's avatar
yma16 已提交
117 118
    Nprogress.done()
})
yma16's avatar
yma16 已提交
119

yma16's avatar
yma16 已提交
120
export default router