| | |
| | | import { useAuthStore } from '#/store'; |
| | | |
| | | import { generateAccess } from './access'; |
| | | import { safeDestination, terminalHome, terminalType } from '#/mobile/terminal'; |
| | | import { allowedMobilePath } from '#/mobile/access'; |
| | | |
| | | /** |
| | | * 通用守卫配置 |
| | |
| | | const authStore = useAuthStore(); |
| | | const dictStore = useDictStore(); |
| | | |
| | | // Mobile core routes require authentication and per-feature authorization. |
| | | if (to.path.startsWith('/mobile')) { |
| | | if (!accessStore.accessToken) return { path: LOGIN_PATH, query: { redirect: encodeURIComponent(to.fullPath) }, replace: true }; |
| | | if (terminalType.value !== 'mobile') return preferences.app.defaultHomePath; |
| | | if (authStore.loadedTerminal !== 'mobile') { |
| | | try { await authStore.fetchUserInfo(); } |
| | | catch { return { path: LOGIN_PATH, query: { loadError: '1' }, replace: true }; } |
| | | } |
| | | if (!allowedMobilePath(to.path)) return { path: '/mobile/home', query: { forbidden: '1' }, replace: true }; |
| | | return true; |
| | | } |
| | | if (accessStore.accessToken && terminalType.value === 'mobile' && !to.path.startsWith('/auth/')) return '/mobile/home'; |
| | | |
| | | // 基本路由,这些路由不需要进入权限拦截 |
| | | if (coreRouteNames.includes(to.name as string)) { |
| | | if (to.path === LOGIN_PATH && accessStore.accessToken) { |
| | | return decodeURIComponent( |
| | | (to.query?.redirect as string) || |
| | | userStore.userInfo?.homePath || |
| | | preferences.app.defaultHomePath, |
| | | ); |
| | | if (to.path === LOGIN_PATH && accessStore.accessToken && !to.query.loadError) { |
| | | return safeDestination(to.query?.redirect, |
| | | terminalHome() || userStore.userInfo?.homePath || preferences.app.defaultHomePath); |
| | | } |
| | | return true; |
| | | } |
| | |
| | | return to; |
| | | } |
| | | |
| | | // A fresh tab or a terminal switch must reload the corresponding menu set. |
| | | if (authStore.loadedTerminal !== 'pc') { |
| | | try { |
| | | await authStore.fetchUserInfo(); |
| | | accessStore.setIsAccessChecked(false); |
| | | } catch { |
| | | return { path: LOGIN_PATH, query: { loadError: '1' }, replace: true }; |
| | | } |
| | | } |
| | | // 是否已经生成过动态路由 |
| | | if (accessStore.isAccessChecked) { |
| | | return true; |