Commit cc46935a8296dae62ecfc753a956338ba433927e
1 parent
53e79a2d
feat: always refresh userinfo when page reload
每次刷新整个页面时都从接口更新用户信息
Showing
2 changed files
with
12 additions
and
2 deletions
src/router/guard/permissionGuard.ts
@@ -67,10 +67,14 @@ export function createPermissionGuard(router: Router) { | @@ -67,10 +67,14 @@ export function createPermissionGuard(router: Router) { | ||
67 | to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME) | 67 | to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME) |
68 | ) { | 68 | ) { |
69 | next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME); | 69 | next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME); |
70 | - console.log({ from, to }); | ||
71 | return; | 70 | return; |
72 | } | 71 | } |
73 | 72 | ||
73 | + // get userinfo while last fetch time is empty | ||
74 | + if (userStore.getLastUpdateTime === 0) { | ||
75 | + await userStore.getUserInfoAction(); | ||
76 | + } | ||
77 | + | ||
74 | if (permissionStore.getIsDynamicAddedRoute) { | 78 | if (permissionStore.getIsDynamicAddedRoute) { |
75 | next(); | 79 | next(); |
76 | return; | 80 | return; |
@@ -88,7 +92,6 @@ export function createPermissionGuard(router: Router) { | @@ -88,7 +92,6 @@ export function createPermissionGuard(router: Router) { | ||
88 | 92 | ||
89 | if (to.name === PAGE_NOT_FOUND_ROUTE.name) { | 93 | if (to.name === PAGE_NOT_FOUND_ROUTE.name) { |
90 | // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容 | 94 | // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容 |
91 | - // fix: 添加query以免丢失 | ||
92 | next({ path: to.fullPath, replace: true, query: to.query }); | 95 | next({ path: to.fullPath, replace: true, query: to.query }); |
93 | } else { | 96 | } else { |
94 | const redirectPath = (from.query.redirect || to.path) as string; | 97 | const redirectPath = (from.query.redirect || to.path) as string; |
src/store/modules/user.ts
@@ -20,6 +20,7 @@ interface UserState { | @@ -20,6 +20,7 @@ interface UserState { | ||
20 | token?: string; | 20 | token?: string; |
21 | roleList: RoleEnum[]; | 21 | roleList: RoleEnum[]; |
22 | sessionTimeout?: boolean; | 22 | sessionTimeout?: boolean; |
23 | + lastUpdateTime: number; | ||
23 | } | 24 | } |
24 | 25 | ||
25 | export const useUserStore = defineStore({ | 26 | export const useUserStore = defineStore({ |
@@ -33,6 +34,8 @@ export const useUserStore = defineStore({ | @@ -33,6 +34,8 @@ export const useUserStore = defineStore({ | ||
33 | roleList: [], | 34 | roleList: [], |
34 | // Whether the login expired | 35 | // Whether the login expired |
35 | sessionTimeout: false, | 36 | sessionTimeout: false, |
37 | + // Last fetch time | ||
38 | + lastUpdateTime: 0, | ||
36 | }), | 39 | }), |
37 | getters: { | 40 | getters: { |
38 | getUserInfo(): UserInfo { | 41 | getUserInfo(): UserInfo { |
@@ -47,6 +50,9 @@ export const useUserStore = defineStore({ | @@ -47,6 +50,9 @@ export const useUserStore = defineStore({ | ||
47 | getSessionTimeout(): boolean { | 50 | getSessionTimeout(): boolean { |
48 | return !!this.sessionTimeout; | 51 | return !!this.sessionTimeout; |
49 | }, | 52 | }, |
53 | + getLastUpdateTime(): number { | ||
54 | + return this.lastUpdateTime; | ||
55 | + }, | ||
50 | }, | 56 | }, |
51 | actions: { | 57 | actions: { |
52 | setToken(info: string | undefined) { | 58 | setToken(info: string | undefined) { |
@@ -59,6 +65,7 @@ export const useUserStore = defineStore({ | @@ -59,6 +65,7 @@ export const useUserStore = defineStore({ | ||
59 | }, | 65 | }, |
60 | setUserInfo(info: UserInfo) { | 66 | setUserInfo(info: UserInfo) { |
61 | this.userInfo = info; | 67 | this.userInfo = info; |
68 | + this.lastUpdateTime = new Date().getTime(); | ||
62 | setAuthCache(USER_INFO_KEY, info); | 69 | setAuthCache(USER_INFO_KEY, info); |
63 | }, | 70 | }, |
64 | setSessionTimeout(flag: boolean) { | 71 | setSessionTimeout(flag: boolean) { |