import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const router = new Router({
    mode: 'history',
    routes: [{
        path: '/',
        component: () => import('@/views/main/index'),
    }, {
        path: '/login',
        component: () => import('@/views/login/index')
    }]
})

// 路由守卫
import { token } from '@/common/token'
router.beforeEach((to, from, next) => {
    if (token.value) {
        if (to.path === '/login') {
            next({
                path: '/'
            })
        } else {
            next()
        }
    } else if (!token.value && to.path !== '/login') {
        next({ path: '/login' })
    }
    next()
})

export default router