index.js 3.8 KB
Newer Older
yma16's avatar
yma16 已提交
1 2
import { isEmpty } from '@/utils'
import store from '@/store'
3 4 5 6 7 8 9 10 11
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 已提交
12
const defaultRoutes = [
yma16's avatar
yma16 已提交
13 14 15
    {
        path: '/',
        name: 'Article',
16 17
        component: Article,
        hidden: true
yma16's avatar
yma16 已提交
18 19 20 21
    },
    {
        path: '/login',
        name: 'Login',
22
        component: Login,
yma16's avatar
yma16 已提交
23
        hidden: false
yma16's avatar
yma16 已提交
24 25 26 27
    },
    {
        path: '/register',
        name: 'Register',
28
        component: Register,
yma16's avatar
yma16 已提交
29
        hidden: false
yma16's avatar
yma16 已提交
30 31 32 33
    },
    {
        path: '/home',
        name: 'Home',
34 35
        component: Home,
        hidden: true
yma16's avatar
yma16 已提交
36 37 38 39
    },
    {
        path: '/onlinewebsocket',
        name: 'Onlinewebsocket',
40 41
        component: Onlinewebsocket,
        hidden: true
yma16's avatar
yma16 已提交
42 43 44 45
    },
    {
        path: '/bilicom',
        name: 'Bilicom',
46 47
        component: Bilicom,
        hidden: true
yma16's avatar
yma16 已提交
48 49 50 51
    },
    {
        path: '/mavoneditor',
        name: 'Mavoneditor',
52 53
        component: Mavoneditor,
        hidden: true
yma16's avatar
yma16 已提交
54 55 56 57
    },
    {
        path: '/gril',
        name: 'grilshow',
58 59
        component: GrilShow,
        hidden: true
yma16's avatar
yma16 已提交
60 61 62 63
    },
    {
        path: '/css',
        name: 'css',
64 65
        component: Csslearn,
        hidden: true
yma16's avatar
yma16 已提交
66 67
    }
]
yma16's avatar
yma16 已提交
68

yma16's avatar
yma16 已提交
69 70 71 72 73 74 75 76 77 78 79 80
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 已提交
81

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

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

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

    Vue.use(VueRouter)
yma16's avatar
yma16 已提交
132
    return router
133
}
yma16's avatar
yma16 已提交
134

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