| | |
| | | import router from './router' |
| | | import type { RouteRecordRaw } from 'vue-router' |
| | | import { isRelogin } from '@/config/axios/service' |
| | | import { getAccessToken } from '@/utils/auth' |
| | | import { getAccessToken, removeToken } from '@/utils/auth' |
| | | import { deleteUserCache } from '@/hooks/web/useCache' |
| | | import { useTitle } from '@/hooks/web/useTitle' |
| | | import { useNProgress } from '@/hooks/web/useNProgress' |
| | | import { usePageLoading } from '@/hooks/web/usePageLoading' |
| | |
| | | import { useUserStoreWithOut } from '@/store/modules/user' |
| | | import { usePermissionStoreWithOut } from '@/store/modules/permission' |
| | | import { useRoomStoreWithOut } from "@/store/modules/room"; |
| | | // 需求:30 分钟无操作才要求重新登录(前端空闲检测) |
| | | import { setupIdleLogout } from '@/hooks/web/useIdleLogout' |
| | | |
| | | const { start, done } = useNProgress() |
| | | |
| | |
| | | return |
| | | } |
| | | |
| | | if (to.path === '/login') { |
| | | next({ path: '/' }) |
| | | return |
| | | } |
| | | // ========================================================================== |
| | | // 【修复】以下所有 await 都必须包在 try/catch 内。 |
| | | // |
| | | // 背景:localStorage 里可能残留**已失效的 token**(服务端重启、令牌过期等)。 |
| | | // 此时 getAccessToken() 仍有值 → 守卫不会跳登录页 → 继续用旧 token 请求后端 |
| | | // → 401 → handleAuthorized() 返回 Promise.reject('登录超时,请重新登录!')。 |
| | | // 原先该 reject 无人捕获,会一路冒泡到 setupRouter(),导致: |
| | | // [Vue Router warn] uncaught error during route navigation |
| | | // [Vue Router warn] Unexpected error when starting the router |
| | | // main.ts:76 Uncaught (in promise) 登录超时,请重新登录! |
| | | // **路由安装失败,整个页面起不来**(用户必须手动重新登录才恢复)。 |
| | | // |
| | | // 现在统一兜底:无论鉴权失败还是其他异常,都清掉本地失效凭证并转登录页, |
| | | // 保证路由器一定能完成初始化。 |
| | | // |
| | | // 注:原先此处还有 `if (to.path === '/login') next({path:'/'})`, |
| | | // 因 '/login' 已在 whiteList 中提前 return,该分支永不可达,已移除。 |
| | | // ========================================================================== |
| | | try { |
| | | // 获取所有字典 |
| | | const dictStore = useDictStoreWithOut() |
| | | const userStore = useUserStoreWithOut() |
| | | const roomStore = useRoomStoreWithOut() |
| | | const checkTypeStore = useCheckTypeStoreWithOut() |
| | | const permissionStore = usePermissionStoreWithOut() |
| | | if (!dictStore.getIsSetDict) { |
| | | await dictStore.setDictMap() |
| | | } |
| | | if (!checkTypeStore.getIsSetCheckType) { |
| | | await checkTypeStore.setCheckTypeMap() |
| | | } |
| | | |
| | | // 获取所有字典 |
| | | const dictStore = useDictStoreWithOut() |
| | | const userStore = useUserStoreWithOut() |
| | | const roomStore = useRoomStoreWithOut() |
| | | const checkTypeStore = useCheckTypeStoreWithOut() |
| | | const permissionStore = usePermissionStoreWithOut() |
| | | if (!dictStore.getIsSetDict) { |
| | | await dictStore.setDictMap() |
| | | } |
| | | if (!checkTypeStore.getIsSetCheckType) { |
| | | await checkTypeStore.setCheckTypeMap() |
| | | } |
| | | |
| | | if (userStore.getIsSetUser ) { |
| | | if (userStore.getIsSetUser) { |
| | | |
| | | // <<<【诊室选择】<<< |
| | | if ( to.path !== '/roomselect' && |
| | | if (to.path !== '/roomselect' && |
| | | userStore.getRoles.includes("doctor") && !userStore.getRoles.includes("super_admin") |
| | | && !roomStore.getIsSetRoom ) { |
| | | next({path: `/roomselect?redirect=${to.fullPath}`}) |
| | | return |
| | | && !roomStore.getIsSetRoom) { |
| | | next({ path: `/roomselect?redirect=${to.fullPath}` }) |
| | | return |
| | | } |
| | | |
| | | next() |
| | | return |
| | | } |
| | | |
| | | isRelogin.show = true |
| | | await userStore.setUserInfoAction() |
| | | isRelogin.show = false |
| | | |
| | | // 后端过滤菜单 |
| | | await permissionStore.generateRoutes() |
| | | permissionStore.getAddRouters.forEach((route) => { |
| | | router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表 |
| | | }) |
| | | |
| | | // <<<【诊室选择】<<< |
| | | if (to.path !== '/roomselect' && |
| | | userStore.getRoles.includes("doctor") && !userStore.getRoles.includes("super_admin") |
| | | && !roomStore.getIsSetRoom) { |
| | | next({ path: `/roomselect?redirect=${to.fullPath}` }) |
| | | return |
| | | } |
| | | |
| | | const redirectPath = from.query.redirect || to.path |
| | | // 修复跳转时不带参数的问题 |
| | | const redirect = decodeURIComponent(redirectPath as string) |
| | | const { paramsObject: query } = parseURL(redirect) |
| | | const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect, query } |
| | | next(nextData) |
| | | } catch (e) { |
| | | // 兜底:清理本地失效凭证,放行到登录页,绝不让异常中断路由初始化 |
| | | console.warn('[permission] 路由守卫执行失败,已清理本地凭证并转登录页:', e) |
| | | isRelogin.show = false |
| | | removeToken() |
| | | deleteUserCache() |
| | | next(`/login?redirect=${to.fullPath}`) |
| | | } |
| | | |
| | | isRelogin.show = true |
| | | await userStore.setUserInfoAction() |
| | | isRelogin.show = false |
| | | |
| | | // 后端过滤菜单 |
| | | await permissionStore.generateRoutes() |
| | | permissionStore.getAddRouters.forEach((route) => { |
| | | router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表 |
| | | }) |
| | | |
| | | // <<<【诊室选择】<<< |
| | | if ( to.path !== '/roomselect' && |
| | | userStore.getRoles.includes("doctor") && !userStore.getRoles.includes("super_admin") |
| | | && !roomStore.getIsSetRoom ) { |
| | | next({path: `/roomselect?redirect=${to.fullPath}`}) |
| | | return |
| | | } |
| | | |
| | | const redirectPath = from.query.redirect || to.path |
| | | // 修复跳转时不带参数的问题 |
| | | const redirect = decodeURIComponent(redirectPath as string) |
| | | const {paramsObject: query} = parseURL(redirect) |
| | | const nextData = to.path === redirect ? {...to, replace: true} : {path: redirect, query} |
| | | next(nextData) |
| | | }) |
| | | |
| | | router.afterEach((to) => { |
| | | useTitle(to?.meta?.title as string) |
| | | done() // 结束Progress |
| | | loadDone() |
| | | |
| | | // ========================================================================== |
| | | // 需求:**30 分钟不操作**才要求重新输入密码。 |
| | | // |
| | | // 背景:后端 token 是固定有效期(system_oauth2_client.access_token_validity_seconds, |
| | | // 当前 default 客户端 = 3600 秒),与用户是否有操作无关 —— 挂着不动也会到点被踢。 |
| | | // 这里在前端补上**真正的空闲检测**: |
| | | // · 有鼠标/键盘/触摸/滚动等操作 → 计时重置; |
| | | // · 连续 30 分钟无任何操作 → 主动清凭证并跳登录页。 |
| | | // 仪表盘/叫号屏等白名单页面不会走到这里,不受影响。 |
| | | // |
| | | // 只在**已登录**且非登录页时启动;setupIdleLogout 内部幂等,重复调用无副作用。 |
| | | // ========================================================================== |
| | | if (getAccessToken() && to.path !== '/login') { |
| | | setupIdleLogout() |
| | | } |
| | | }) |