diff --git a/src/store/modules/auth/index.ts b/src/store/modules/auth/index.ts index b1bc89ef..57f02368 100644 --- a/src/store/modules/auth/index.ts +++ b/src/store/modules/auth/index.ts @@ -1,4 +1,4 @@ -import { unref } from 'vue'; +import { unref, nextTick } from 'vue'; import { defineStore } from 'pinia'; import { router } from '@/router'; import { fetchLogin, fetchUserInfo } from '@/service'; @@ -40,12 +40,14 @@ export const useAuthStore = defineStore('auth-store', { clearAuthStorage(); this.$reset(); - resetTabStore(); - resetRouteStore(); - if (route.meta.requiresAuth) { toLogin(); } + + nextTick(() => { + resetTabStore(); + resetRouteStore(); + }); }, /** * 处理登录后成功或失败的逻辑 @@ -58,6 +60,8 @@ export const useAuthStore = defineStore('auth-store', { const loginSuccess = await this.loginByToken(backendToken); if (loginSuccess) { + await route.initAuthRoute(); + // 跳转登录后的地址 toLoginRedirect(); diff --git a/src/store/modules/route/index.ts b/src/store/modules/route/index.ts index 793572e2..c9a757c8 100644 --- a/src/store/modules/route/index.ts +++ b/src/store/modules/route/index.ts @@ -25,8 +25,6 @@ interface RouteState { authRouteMode: ImportMetaEnv['VITE_AUTH_ROUTE_MODE']; /** 是否初始化了权限路由 */ isInitAuthRoute: boolean; - /** 动态路由是否初始化失败 */ - failedInitDynamicRoute: boolean; /** 路由首页name(前端静态路由时生效,后端动态路由该值会被后端返回的值覆盖) */ routeHomeName: AuthRoute.AllRouteKey; /** 菜单 */ @@ -41,7 +39,6 @@ export const useRouteStore = defineStore('route-store', { state: (): RouteState => ({ authRouteMode: import.meta.env.VITE_AUTH_ROUTE_MODE, isInitAuthRoute: false, - failedInitDynamicRoute: false, routeHomeName: transformRoutePathToRouteName(import.meta.env.VITE_ROUTE_HOME_PATH), menus: [], searchMenus: [], @@ -109,6 +106,8 @@ export const useRouteStore = defineStore('route-store', { }, /** 初始化动态路由 */ async initDynamicRoute() { + const { initHomeTab } = useTabStore(); + const { userId } = localStg.get('userInfo') || {}; if (!userId) { @@ -121,8 +120,10 @@ export const useRouteStore = defineStore('route-store', { this.routeHomeName = data.home; this.handleUpdateRootRedirect(data.home); this.handleAuthRoute(data.routes); - } else { - this.failedInitDynamicRoute = true; + + initHomeTab(data.home, router); + + this.isInitAuthRoute = true; } }, /** 初始化静态路由 */ @@ -130,21 +131,15 @@ export const useRouteStore = defineStore('route-store', { const auth = useAuthStore(); const routes = filterAuthRoutesByUserPermission(staticRoutes, auth.userInfo.userRole); this.handleAuthRoute(routes); + this.isInitAuthRoute = true; }, /** 初始化权限路由 */ async initAuthRoute() { - const { initHomeTab } = useTabStore(); - - const isDynamicRoute = this.authRouteMode === 'dynamic'; - if (isDynamicRoute) { + if (this.authRouteMode === 'dynamic') { await this.initDynamicRoute(); } else { await this.initStaticRoute(); } - - initHomeTab(this.routeHomeName, router); - - this.isInitAuthRoute = !this.failedInitDynamicRoute; } } });