30 lines
1013 B
TypeScript
30 lines
1013 B
TypeScript
// middleware/auth.ts
|
|
import { authRoutes, verifyBlankRoute } from '@/constants/index'
|
|
import { useRouter } from 'vue-router'
|
|
|
|
// const router = useRouter()
|
|
/**
|
|
* 全局认证中间件
|
|
* 用于处理需要登录才能访问的路由
|
|
* 如果用户未登录,将显示登录框并重定向到模型广场
|
|
*
|
|
* @param to - 目标路由对象
|
|
* @returns void | NavigationResult - 如果需要重定向则返回导航结果
|
|
*/
|
|
export default defineNuxtRouteMiddleware((to, from) => {
|
|
const userStore = useUserStore()
|
|
const modalStore = useModalStore()
|
|
const menuStore = useMenuStore()
|
|
if (verifyBlankRoute.includes(to.path) && !verifyBlankRoute.includes(from.path)) {
|
|
return abortNavigation()
|
|
}
|
|
// 如果是需要登录的路由,且用户未登录
|
|
if (authRoutes.includes(to.path) && !userStore.isLoggedIn) {
|
|
// 显示登录模态框
|
|
modalStore.showLoginModal()
|
|
menuStore.setActiveMenu(from.path)
|
|
// return abortNavigation()
|
|
// return navigateTo('/model-square')
|
|
}
|
|
})
|