| | |
| | | import type { Router, RouteRecordRaw } from 'vue-router'; |
| | | |
| | | import { usePermissionStoreWithOut } from '/@/store/modules/permission'; |
| | | import { usePermissionStoreWithOut } from '@/store/modules/permission'; |
| | | |
| | | import { PageEnum } from '/@/enums/pageEnum'; |
| | | import { useUserStoreWithOut } from '/@/store/modules/user'; |
| | | import { PageEnum } from '@/enums/pageEnum'; |
| | | import { useUserStoreWithOut } from '@/store/modules/user'; |
| | | |
| | | import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic'; |
| | | import { PAGE_NOT_FOUND_ROUTE } from '@/router/routes/basic'; |
| | | |
| | | import { RootRoute } from '/@/router/routes'; |
| | | import { isEmpty, isNullOrUnDef } from '/@/utils/is'; |
| | | import { RootRoute } from '@/router/routes'; |
| | | import { isNullOrUnDef } from '/@/utils/is'; |
| | | |
| | | const LOGIN_PATH = PageEnum.BASE_LOGIN; |
| | | |
| | |
| | | try { |
| | | await userStore.afterLoginAction(); |
| | | if (!isSessionTimeout) { |
| | | next((to.query?.redirect as string) || '/'); |
| | | next(decodeURIComponent((to.query?.redirect as string) || '/')); |
| | | return; |
| | | } |
| | | } catch { |
| | |
| | | path: LOGIN_PATH, |
| | | replace: true, |
| | | }; |
| | | if (to.path) { |
| | | if (to.fullPath) { |
| | | redirectData.query = { |
| | | ...redirectData.query, |
| | | redirect: to.path, |
| | | redirect: to.fullPath, |
| | | }; |
| | | } |
| | | next(redirectData); |
| | | return; |
| | | } |
| | | |
| | | // Jump to the 404 page after processing the login |
| | | if ( |
| | | from.path === LOGIN_PATH && |
| | | to.name === PAGE_NOT_FOUND_ROUTE.name && |
| | | to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME) |
| | | ) { |
| | | next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME); |
| | | return; |
| | | } |
| | | |
| | |
| | | if (userStore.getLastUpdateTime === 0) { |
| | | try { |
| | | const userinfo = await userStore.getUserInfoAction(); |
| | | |
| | | if (isNullOrUnDef(userinfo?.userId)) { |
| | | userStore.setToken(undefined); |
| | | userStore.setSessionTimeout(false); |
| | |
| | | } |
| | | } |
| | | |
| | | if (permissionStore.getIsDynamicAddedRoute) { |
| | | next(); |
| | | // 动态路由加载(首次) |
| | | if (!permissionStore.getIsDynamicAddedRoute) { |
| | | const routes = await permissionStore.buildRoutesAction(); |
| | | [...routes, PAGE_NOT_FOUND_ROUTE].forEach((route) => { |
| | | router.addRoute(route as unknown as RouteRecordRaw); |
| | | }); |
| | | // 记录动态路由加载完成 |
| | | permissionStore.setDynamicAddedRoute(true); |
| | | |
| | | // 现在的to动态路由加载之前的,可能为PAGE_NOT_FOUND_ROUTE(例如,登陆后,刷新的时候) |
| | | // 此处应当重定向到fullPath,否则会加载404页面内容 |
| | | next({ path: to.fullPath, replace: true, query: to.query }); |
| | | return; |
| | | } |
| | | |
| | | const routes = await permissionStore.buildRoutesAction(); |
| | | |
| | | routes.forEach((route) => { |
| | | router.addRoute(route as unknown as RouteRecordRaw); |
| | | }); |
| | | |
| | | router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw); |
| | | |
| | | permissionStore.setDynamicAddedRoute(true); |
| | | |
| | | if (to.name === PAGE_NOT_FOUND_ROUTE.name) { |
| | | // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容 |
| | | next({ path: to.fullPath, replace: true, query: to.query }); |
| | | // 遇到不存在页面,后续逻辑不再处理redirect(阻止下面else逻辑) |
| | | from.query.redirect = ''; |
| | | |
| | | if ( |
| | | from.path === LOGIN_PATH && |
| | | to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME) |
| | | ) { |
| | | // 登陆重定向不存在路由,转去“首页” |
| | | next({ path: userStore.getUserInfo.homePath || PageEnum.BASE_HOME, replace: true }); |
| | | } else { |
| | | // 正常前往“404”页面 |
| | | next(); |
| | | } |
| | | } else if (from.query.redirect) { |
| | | // 存在redirect |
| | | const redirect = decodeURIComponent((from.query.redirect as string) || ''); |
| | | |
| | | // 只处理一次 from.query.redirect |
| | | // 也避免某场景(指向路由定义了 redirect)下的死循环 |
| | | from.query.redirect = ''; |
| | | |
| | | if (redirect === to.fullPath) { |
| | | // 已经被redirect |
| | | next(); |
| | | } else { |
| | | // 指向redirect |
| | | next({ path: redirect, replace: true }); |
| | | } |
| | | } else { |
| | | const redirectPath = (from.query.redirect || to.path) as string; |
| | | const redirect = decodeURIComponent(redirectPath); |
| | | const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }; |
| | | next(nextData); |
| | | // 正常访问 |
| | | next(); |
| | | } |
| | | }); |
| | | } |