index.js 4.0 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
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({
yma16's avatar
yma16 已提交
82
        mode: 'history',
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

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

yma16's avatar
yma16 已提交
96
        console.log('to', to)
yma16's avatar
yma16 已提交
97 98
        let hasToken = {
            name: name,
yma16's avatar
yma16 已提交
99 100
            password: pwd,
            thirdUserInfo: thirdUserInfo
yma16's avatar
yma16 已提交
101 102 103
        }
        console.log('localStorage', hasToken)
        if (hasToken.name && hasToken.password) {
yma16's avatar
yma16 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
            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 已提交
122
            } else {
yma16's avatar
yma16 已提交
123
                console.log('next')
yma16's avatar
yma16 已提交
124 125
                next()
            }
yma16's avatar
yma16 已提交
126 127 128
        } else if (to.name === 'Login' || to.path === '/login' || to.name === 'Register' || to.path === '/register') {
            console.log('next login register')
            // 避免同名路由无限循环
yma16's avatar
yma16 已提交
129
            next()
yma16's avatar
yma16 已提交
130 131 132
        } else {
            console.log('login router')
            return next({ name: 'Login' }) // 去登录
yma16's avatar
yma16 已提交
133
        }
yma16's avatar
yma16 已提交
134
        return false
yma16's avatar
yma16 已提交
135
    })
136 137

    Vue.use(VueRouter)
yma16's avatar
yma16 已提交
138
    return router
139
}
yma16's avatar
yma16 已提交
140

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