index.js 3.0 KB
Newer Older
1
// import Nprogress from 'nprogress'
yma16's avatar
yma16 已提交
2 3 4 5 6
import 'nprogress/nprogress.css'
// import { getToken } from '@/utils/auth'
import { isEmpty } from '@/utils'
import VueRouter from 'vue-router'
import store from '@/store'
7 8 9 10 11 12 13 14 15
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')
yma16's avatar
yma16 已提交
16

yma16's avatar
yma16 已提交
17
const defaultRoutes = [
yma16's avatar
yma16 已提交
18 19 20
    {
        path: '/',
        name: 'Article',
21 22
        component: Article,
        hidden: true
yma16's avatar
yma16 已提交
23 24 25 26
    },
    {
        path: '/login',
        name: 'Login',
27 28
        component: Login,
        hidden: true
yma16's avatar
yma16 已提交
29 30 31 32
    },
    {
        path: '/register',
        name: 'Register',
33 34
        component: Register,
        hidden: true
yma16's avatar
yma16 已提交
35 36 37 38
    },
    {
        path: '/home',
        name: 'Home',
39 40
        component: Home,
        hidden: true
yma16's avatar
yma16 已提交
41 42 43 44
    },
    {
        path: '/onlinewebsocket',
        name: 'Onlinewebsocket',
45 46
        component: Onlinewebsocket,
        hidden: true
yma16's avatar
yma16 已提交
47 48 49 50
    },
    {
        path: '/bilicom',
        name: 'Bilicom',
51 52
        component: Bilicom,
        hidden: true
yma16's avatar
yma16 已提交
53 54 55 56
    },
    {
        path: '/mavoneditor',
        name: 'Mavoneditor',
57 58
        component: Mavoneditor,
        hidden: true
yma16's avatar
yma16 已提交
59 60 61 62
    },
    {
        path: '/gril',
        name: 'grilshow',
63 64
        component: GrilShow,
        hidden: true
yma16's avatar
yma16 已提交
65 66 67 68
    },
    {
        path: '/css',
        name: 'css',
69 70
        component: Csslearn,
        hidden: true
yma16's avatar
yma16 已提交
71 72
    }
]
yma16's avatar
yma16 已提交
73

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

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

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

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

117 118 119 120 121 122 123
// router.afterEach((to, from) => {
//     Nprogress.done()
// })

const useRouter = (Vue) => {
    Vue.use(VueRouter)
}
yma16's avatar
yma16 已提交
124

125
export {router, useRouter}