index.js 3.8 KB
Newer Older
yma16's avatar
yma16 已提交
1 2
import { isEmpty } from '@/utils'
import store from '@/store'
yma16's avatar
yma16 已提交
3 4 5 6 7

const Login = () => import('@/components/user/Login')
const Register = () => import('@/components/user/Register')
const Onlinewebsocket = () => import('@/components/websocket/Onlinewebsocket')

8 9 10 11
const Home = () => import('@/components/Home')
const Bilicom = () => import('@/components/Bilicom')
const Mavoneditor = () => import('@/components/Mavoneditor')
const GrilShow = () => import('@/components/GrilShow')
yma16's avatar
yma16 已提交
12 13 14

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

yma16's avatar
yma16 已提交
72 73 74 75 76 77 78 79 80 81 82 83
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
    })
yma16's avatar
yma16 已提交
84

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

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

yma16's avatar
yma16 已提交
94
        console.log('to', to)
yma16's avatar
yma16 已提交
95 96 97 98 99 100
        let hasToken = {
            name: name,
            password: pwd
        }
        console.log('localStorage', hasToken)
        if (hasToken.name && hasToken.password) {
yma16's avatar
yma16 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
            if (!isEmpty(store.state.user.userInfo)) {
                try {
                    // 空的 modules下的user
                    console.log('路由的登录认证')
                    // 用户自主登录
                    await store.dispatch('user/loginUserInfo', hasToken)
                    next()
                } catch (e) {
                    console.error(e, 'e')
                    if (to.name === 'Login' || to.path === '/login' || to.name === 'register' || to.path === '/Register') {
                    // 避免同名路由无限循环
                        console.log('next')
                        next()
                    } else {
                        console.log('login router')
                        return next({ name: 'Login' }) // 去登录
                    }
                }
yma16's avatar
yma16 已提交
119
            } else {
yma16's avatar
yma16 已提交
120
                console.log('next')
yma16's avatar
yma16 已提交
121 122
                next()
            }
yma16's avatar
yma16 已提交
123 124 125
        } else if (to.name === 'Login' || to.path === '/login' || to.name === 'Register' || to.path === '/register') {
            console.log('next login register')
            // 避免同名路由无限循环
yma16's avatar
yma16 已提交
126
            next()
yma16's avatar
yma16 已提交
127 128 129
        } else {
            console.log('login router')
            return next({ name: 'Login' }) // 去登录
yma16's avatar
yma16 已提交
130
        }
yma16's avatar
yma16 已提交
131
        return false
yma16's avatar
yma16 已提交
132
    })
133 134

    Vue.use(VueRouter)
yma16's avatar
yma16 已提交
135
    return router
136
}
yma16's avatar
yma16 已提交
137

yma16's avatar
yma16 已提交
138
export default useRouter