Skip to content

Commit

Permalink
fix(projects): fix router when the dynamic routes api was failed [修复当…
Browse files Browse the repository at this point in the history
…动态路由接口失败后路由异常问题]
  • Loading branch information
honghuangdc committed Nov 18, 2022
1 parent 3bd8858 commit f2b580f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
13 changes: 8 additions & 5 deletions src/store/modules/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
12 changes: 9 additions & 3 deletions src/store/modules/route/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface RouteState {
authRouteMode: ImportMetaEnv['VITE_AUTH_ROUTE_MODE'];
/** 是否初始化了权限路由 */
isInitAuthRoute: boolean;
/** 动态路由是否初始化失败 */
failedInitDynamicRoute: boolean;
/** 路由首页name(前端静态路由时生效,后端动态路由该值会被后端返回的值覆盖) */
routeHomeName: AuthRoute.AllRouteKey;
/** 菜单 */
Expand All @@ -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: [],
Expand Down Expand Up @@ -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;
}
},
/** 初始化静态路由 */
Expand All @@ -138,7 +144,7 @@ export const useRouteStore = defineStore('route-store', {

initHomeTab(this.routeHomeName, router);

this.isInitAuthRoute = true;
this.isInitAuthRoute = !this.failedInitDynamicRoute;
}
}
});

1 comment on commit f2b580f

@vercel
Copy link

@vercel vercel bot commented on f2b580f Nov 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.