Skip to content

Commit a97c998

Browse files
authored
fix: user homePath no effect sometimes (#5166)
1 parent b22d900 commit a97c998

File tree

5 files changed

+47
-12
lines changed

5 files changed

+47
-12
lines changed

apps/backend-mock/utils/mock-data.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export interface UserInfo {
44
realName: string;
55
roles: string[];
66
username: string;
7+
homePath?: string;
78
}
89

910
export const MOCK_USERS: UserInfo[] = [
@@ -20,13 +21,15 @@ export const MOCK_USERS: UserInfo[] = [
2021
realName: 'Admin',
2122
roles: ['admin'],
2223
username: 'admin',
24+
homePath: '/workspace',
2325
},
2426
{
2527
id: 2,
2628
password: '123456',
2729
realName: 'Jack',
2830
roles: ['user'],
2931
username: 'jack',
32+
homePath: '/analytics',
3033
},
3134
];
3235

apps/web-antd/src/router/guard.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ function setupAccessGuard(router: Router) {
5454
if (coreRouteNames.includes(to.name as string)) {
5555
if (to.path === LOGIN_PATH && accessStore.accessToken) {
5656
return decodeURIComponent(
57-
(to.query?.redirect as string) || DEFAULT_HOME_PATH,
57+
(to.query?.redirect as string) ||
58+
userStore.userInfo?.homePath ||
59+
DEFAULT_HOME_PATH,
5860
);
5961
}
6062
return true;
@@ -72,7 +74,10 @@ function setupAccessGuard(router: Router) {
7274
return {
7375
path: LOGIN_PATH,
7476
// 如不需要,直接删除 query
75-
query: { redirect: encodeURIComponent(to.fullPath) },
77+
query:
78+
to.fullPath === DEFAULT_HOME_PATH
79+
? {}
80+
: { redirect: encodeURIComponent(to.fullPath) },
7681
// 携带当前跳转的页面,登录后重新跳转该页面
7782
replace: true,
7883
};
@@ -102,7 +107,10 @@ function setupAccessGuard(router: Router) {
102107
accessStore.setAccessMenus(accessibleMenus);
103108
accessStore.setAccessRoutes(accessibleRoutes);
104109
accessStore.setIsAccessChecked(true);
105-
const redirectPath = (from.query.redirect ?? to.fullPath) as string;
110+
const redirectPath = (from.query.redirect ??
111+
(to.path === DEFAULT_HOME_PATH
112+
? userInfo.homePath || DEFAULT_HOME_PATH
113+
: to.fullPath)) as string;
106114

107115
return {
108116
...router.resolve(decodeURIComponent(redirectPath)),

apps/web-ele/src/router/guard.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ function setupAccessGuard(router: Router) {
5454
if (coreRouteNames.includes(to.name as string)) {
5555
if (to.path === LOGIN_PATH && accessStore.accessToken) {
5656
return decodeURIComponent(
57-
(to.query?.redirect as string) || DEFAULT_HOME_PATH,
57+
(to.query?.redirect as string) ||
58+
userStore.userInfo?.homePath ||
59+
DEFAULT_HOME_PATH,
5860
);
5961
}
6062
return true;
@@ -72,7 +74,10 @@ function setupAccessGuard(router: Router) {
7274
return {
7375
path: LOGIN_PATH,
7476
// 如不需要,直接删除 query
75-
query: { redirect: encodeURIComponent(to.fullPath) },
77+
query:
78+
to.fullPath === DEFAULT_HOME_PATH
79+
? {}
80+
: { redirect: encodeURIComponent(to.fullPath) },
7681
// 携带当前跳转的页面,登录后重新跳转该页面
7782
replace: true,
7883
};
@@ -102,7 +107,10 @@ function setupAccessGuard(router: Router) {
102107
accessStore.setAccessMenus(accessibleMenus);
103108
accessStore.setAccessRoutes(accessibleRoutes);
104109
accessStore.setIsAccessChecked(true);
105-
const redirectPath = (from.query.redirect ?? to.fullPath) as string;
110+
const redirectPath = (from.query.redirect ??
111+
(to.path === DEFAULT_HOME_PATH
112+
? userInfo.homePath || DEFAULT_HOME_PATH
113+
: to.fullPath)) as string;
106114

107115
return {
108116
...router.resolve(decodeURIComponent(redirectPath)),

apps/web-naive/src/router/guard.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ function setupAccessGuard(router: Router) {
5454
if (coreRouteNames.includes(to.name as string)) {
5555
if (to.path === LOGIN_PATH && accessStore.accessToken) {
5656
return decodeURIComponent(
57-
(to.query?.redirect as string) || DEFAULT_HOME_PATH,
57+
(to.query?.redirect as string) ||
58+
userStore.userInfo?.homePath ||
59+
DEFAULT_HOME_PATH,
5860
);
5961
}
6062
return true;
@@ -72,7 +74,10 @@ function setupAccessGuard(router: Router) {
7274
return {
7375
path: LOGIN_PATH,
7476
// 如不需要,直接删除 query
75-
query: { redirect: encodeURIComponent(to.fullPath) },
77+
query:
78+
to.fullPath === DEFAULT_HOME_PATH
79+
? {}
80+
: { redirect: encodeURIComponent(to.fullPath) },
7681
// 携带当前跳转的页面,登录后重新跳转该页面
7782
replace: true,
7883
};
@@ -101,7 +106,10 @@ function setupAccessGuard(router: Router) {
101106
accessStore.setAccessMenus(accessibleMenus);
102107
accessStore.setAccessRoutes(accessibleRoutes);
103108
accessStore.setIsAccessChecked(true);
104-
const redirectPath = (from.query.redirect ?? to.fullPath) as string;
109+
const redirectPath = (from.query.redirect ??
110+
(to.path === DEFAULT_HOME_PATH
111+
? userInfo.homePath || DEFAULT_HOME_PATH
112+
: to.fullPath)) as string;
105113

106114
return {
107115
...router.resolve(decodeURIComponent(redirectPath)),

playground/src/router/guard.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ function setupAccessGuard(router: Router) {
5252
if (coreRouteNames.includes(to.name as string)) {
5353
if (to.path === LOGIN_PATH && accessStore.accessToken) {
5454
return decodeURIComponent(
55-
(to.query?.redirect as string) || DEFAULT_HOME_PATH,
55+
(to.query?.redirect as string) ||
56+
userStore.userInfo?.homePath ||
57+
DEFAULT_HOME_PATH,
5658
);
5759
}
5860
return true;
@@ -70,7 +72,10 @@ function setupAccessGuard(router: Router) {
7072
return {
7173
path: LOGIN_PATH,
7274
// 如不需要,直接删除 query
73-
query: { redirect: encodeURIComponent(to.fullPath) },
75+
query:
76+
to.fullPath === DEFAULT_HOME_PATH
77+
? {}
78+
: { redirect: encodeURIComponent(to.fullPath) },
7479
// 携带当前跳转的页面,登录后重新跳转该页面
7580
replace: true,
7681
};
@@ -100,7 +105,10 @@ function setupAccessGuard(router: Router) {
100105
accessStore.setAccessMenus(accessibleMenus);
101106
accessStore.setAccessRoutes(accessibleRoutes);
102107
accessStore.setIsAccessChecked(true);
103-
const redirectPath = (from.query.redirect ?? to.fullPath) as string;
108+
const redirectPath = (from.query.redirect ??
109+
(to.path === DEFAULT_HOME_PATH
110+
? userInfo.homePath || DEFAULT_HOME_PATH
111+
: to.fullPath)) as string;
104112

105113
return {
106114
...router.resolve(decodeURIComponent(redirectPath)),

0 commit comments

Comments
 (0)