import router from '@/router/index'

import { showMessage } from '@/composables/util'
import { showPageLoading, hidePageLoading } from '@/composables/util'
import { getToken } from '@/composables/auth'

// 全局路由前置守卫
router.beforeEach((to, from, next) => {
    console.log('==> 全局路由前置守卫')
    // 展示页面加载 Loading
    showPageLoading()
    const token = getToken()
    if (!token && to.path !== '/login' && to.path !== '/plan/tts') {
        next({ path: '/login' })
        return
    }
    if (token && to.path === '/login') {
        next({ path: '/class' })
        return
    }
    if (to.path === '/plan/tts') {
        next()
        return
    }
	next()
})

// 全局路由后置守卫
router.afterEach((to, from) => {
    // 动态设置页面 Title
    let title = (to.meta.title ? to.meta.title : '') + ' - Enlish'
    document.title = title

    // 隐藏页面加载 Loading
    hidePageLoading()
})
