From f2b580fc067e81202238bf8079a13ff014b0b329 Mon Sep 17 00:00:00 2001 From: Soybean Date: Thu, 17 Nov 2022 22:54:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(projects):=20fix=20router=20when=20the=20dy?= =?UTF-8?q?namic=20routes=20api=20was=20failed=20[=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E5=BD=93=E5=8A=A8=E6=80=81=E8=B7=AF=E7=94=B1=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E5=90=8E=E8=B7=AF=E7=94=B1=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E9=97=AE=E9=A2=98]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/modules/auth/index.ts | 13 ++++++++----- src/store/modules/route/index.ts | 12 +++++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/store/modules/auth/index.ts b/src/store/modules/auth/index.ts index 73464f564..b1bc89efd 100644 --- a/src/store/modules/auth/index.ts +++ b/src/store/modules/auth/index.ts @@ -52,6 +52,7 @@ export const useAuthStore = defineStore('auth-store', { * @param backendToken - 返回的token */ async handleActionAfterLogin(backendToken: ApiAuth.Token) { + const route = useRouteStore(); const { toLoginRedirect } = useRouterPush(false); const loginSuccess = await this.loginByToken(backendToken); @@ -61,11 +62,13 @@ export const useAuthStore = defineStore('auth-store', { toLoginRedirect(); // 登录成功弹出欢迎提示 - window.$notification?.success({ - title: '登录成功!', - content: `欢迎回来,${this.userInfo.userName}!`, - duration: 3000 - }); + if (route.isInitAuthRoute) { + window.$notification?.success({ + title: '登录成功!', + content: `欢迎回来,${this.userInfo.userName}!`, + duration: 3000 + }); + } return; } diff --git a/src/store/modules/route/index.ts b/src/store/modules/route/index.ts index 8151164db..793572e26 100644 --- a/src/store/modules/route/index.ts +++ b/src/store/modules/route/index.ts @@ -25,6 +25,8 @@ interface RouteState { authRouteMode: ImportMetaEnv['VITE_AUTH_ROUTE_MODE']; /** 是否初始化了权限路由 */ isInitAuthRoute: boolean; + /** 动态路由是否初始化失败 */ + failedInitDynamicRoute: boolean; /** 路由首页name(前端静态路由时生效,后端动态路由该值会被后端返回的值覆盖) */ routeHomeName: AuthRoute.AllRouteKey; /** 菜单 */ @@ -39,6 +41,7 @@ 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: [], @@ -112,11 +115,14 @@ export const useRouteStore = defineStore('route-store', { throw new Error('userId 不能为空!'); } - const { data } = await fetchUserRoutes(userId); - if (data) { + const { error, data } = await fetchUserRoutes(userId); + + if (!error) { this.routeHomeName = data.home; this.handleUpdateRootRedirect(data.home); this.handleAuthRoute(data.routes); + } else { + this.failedInitDynamicRoute = true; } }, /** 初始化静态路由 */ @@ -138,7 +144,7 @@ export const useRouteStore = defineStore('route-store', { initHomeTab(this.routeHomeName, router); - this.isInitAuthRoute = true; + this.isInitAuthRoute = !this.failedInitDynamicRoute; } } });