import * as vueRouter from 'vue-router'
import { getToken } from '~src/api/util/auth'
import baseData from './routers'

//设置路由数据
const router = vueRouter.createRouter({
    history: vueRouter.createWebHashHistory(),
    routes: baseData,
})

//路由拦截
router.beforeResolve(async (to) => {
    const token = getToken()
    if (to.path === '/login') {
        return true
    } else if (!token) {
        return '/login'
    } else {
        return true
    }
})

router.afterEach((to) => {
    if (to.path !== '/') {
        window.document.title = to.meta['title']
    } else {
        window.document.title = ''
    }
    window.scrollTo(0, 0)
})

export function setupRouter(app) {
    app.use(router)
    return router
}

export default router
