Files
admin-govern/src/router/index.ts

48 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-12-21 16:42:39 +08:00
import { createRouter, createWebHashHistory } from 'vue-router'
import staticRoutes from '@/router/static'
2024-01-05 15:14:35 +08:00
import { useAdminInfo } from '@/stores/adminInfo'
2023-12-22 10:22:22 +08:00
import NProgress from 'nprogress'
import { loading } from '@/utils/loading'
2023-12-26 08:45:15 +08:00
import { ElMessage } from 'element-plus'
2023-12-21 16:42:39 +08:00
const router = createRouter({
history: createWebHashHistory(),
routes: staticRoutes
})
2023-12-22 10:22:22 +08:00
router.beforeEach((to, from, next) => {
NProgress.configure({ showSpinner: false })
NProgress.start()
if (!window.existLoading) {
loading.show()
window.existLoading = true
}
2023-12-26 09:51:16 +08:00
if (to.path == '/login' || to.path == '/404') {
// 登录或者注册才可以往下进行
next()
} else {
// 获取 token
2024-01-05 15:14:35 +08:00
const adminInfo = useAdminInfo()
const token = adminInfo.getToken()
2023-12-26 09:51:16 +08:00
// token 不存在
if (token === null || token === '') {
ElMessage.error('您还没有登录,请先登录')
next('/login')
} else {
next()
}
}
2023-12-26 08:45:15 +08:00
2023-12-26 09:51:16 +08:00
// next()
2023-12-22 10:22:22 +08:00
})
2023-12-21 16:42:39 +08:00
2023-12-22 10:22:22 +08:00
// 路由加载后
router.afterEach(() => {
if (window.existLoading) {
loading.hide()
}
NProgress.done()
})
2023-12-21 16:42:39 +08:00
export default router