index.js 2.9 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 23
        component: Login,
        hidden: true
yma16's avatar
yma16 已提交
24 25 26 27
    },
    {
        path: '/register',
        name: 'Register',
28 29
        component: Register,
        hidden: true
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 87 88 89 90
    router.beforeEach(async (to, from, next) => {
        next()
        let yma16siteUserInfo = localStorage.getItem('yma16siteUserInfo')
            ? JSON.parse(localStorage.getItem('yma16siteUserInfo'))
            : ''
        let name = yma16siteUserInfo.username
        let pwd = yma16siteUserInfo.password
yma16's avatar
yma16 已提交
91

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

    Vue.use(VueRouter)
yma16's avatar
yma16 已提交
114
    return router
115
}
yma16's avatar
yma16 已提交
116

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