2024-09-13 16:34:15 +08:00
|
|
|
import { router } from '@/router'
|
|
|
|
|
import { useRouteStore } from '@/store/router'
|
|
|
|
|
import { useUserStore } from '@/store/user'
|
|
|
|
|
import { getToken } from '@/utils/auth'
|
|
|
|
|
import { pinia } from '@/store'
|
|
|
|
|
import NProgress from 'nprogress' // progress bar
|
|
|
|
|
import 'nprogress/nprogress.css'
|
|
|
|
|
import { useAppStore } from '@/store/app' // progress bar style
|
2024-09-29 14:59:58 +08:00
|
|
|
import { T } from '@/utils/i18n'
|
2024-09-13 16:34:15 +08:00
|
|
|
|
|
|
|
|
NProgress.configure({ showSpinner: false }) // NProgress Configuration
|
|
|
|
|
|
|
|
|
|
const whiteList = ['/login']
|
|
|
|
|
const routeStore = useRouteStore(pinia)
|
|
|
|
|
const appStore = useAppStore(pinia)
|
|
|
|
|
router.beforeEach(async (to, from, next) => {
|
|
|
|
|
|
2024-09-29 14:59:58 +08:00
|
|
|
document.title = T(to.meta?.title) + ' - ' + appStore.setting.title
|
2024-09-13 16:34:15 +08:00
|
|
|
NProgress.start()
|
|
|
|
|
|
|
|
|
|
const token = getToken()
|
|
|
|
|
if (!token) {
|
|
|
|
|
//无token,跳转到登录
|
|
|
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
|
|
|
next()
|
|
|
|
|
} else {
|
|
|
|
|
next(`/login?redirect=${to.path}`)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
//有token
|
|
|
|
|
|
|
|
|
|
const userStore = useUserStore(pinia)
|
|
|
|
|
|
|
|
|
|
if (!userStore.route_names.length) {
|
|
|
|
|
const info = await userStore.info()
|
|
|
|
|
if (!info) {
|
|
|
|
|
userStore.logout()
|
|
|
|
|
next(`/login?redirect=${to.path}`)
|
|
|
|
|
} else {
|
|
|
|
|
next({ ...to, replace: true })
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
next()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
router.afterEach(() => {
|
|
|
|
|
NProgress.done()
|
|
|
|
|
})
|