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 | 67 | to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME) |
68 | 68 | ) { |
69 | 69 | next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME); |
70 | - console.log({ from, to }); | |
71 | 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 | 78 | if (permissionStore.getIsDynamicAddedRoute) { |
75 | 79 | next(); |
76 | 80 | return; |
... | ... | @@ -88,7 +92,6 @@ export function createPermissionGuard(router: Router) { |
88 | 92 | |
89 | 93 | if (to.name === PAGE_NOT_FOUND_ROUTE.name) { |
90 | 94 | // 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容 |
91 | - // fix: 添加query以免丢失 | |
92 | 95 | next({ path: to.fullPath, replace: true, query: to.query }); |
93 | 96 | } else { |
94 | 97 | const redirectPath = (from.query.redirect || to.path) as string; | ... | ... |
src/store/modules/user.ts
... | ... | @@ -20,6 +20,7 @@ interface UserState { |
20 | 20 | token?: string; |
21 | 21 | roleList: RoleEnum[]; |
22 | 22 | sessionTimeout?: boolean; |
23 | + lastUpdateTime: number; | |
23 | 24 | } |
24 | 25 | |
25 | 26 | export const useUserStore = defineStore({ |
... | ... | @@ -33,6 +34,8 @@ export const useUserStore = defineStore({ |
33 | 34 | roleList: [], |
34 | 35 | // Whether the login expired |
35 | 36 | sessionTimeout: false, |
37 | + // Last fetch time | |
38 | + lastUpdateTime: 0, | |
36 | 39 | }), |
37 | 40 | getters: { |
38 | 41 | getUserInfo(): UserInfo { |
... | ... | @@ -47,6 +50,9 @@ export const useUserStore = defineStore({ |
47 | 50 | getSessionTimeout(): boolean { |
48 | 51 | return !!this.sessionTimeout; |
49 | 52 | }, |
53 | + getLastUpdateTime(): number { | |
54 | + return this.lastUpdateTime; | |
55 | + }, | |
50 | 56 | }, |
51 | 57 | actions: { |
52 | 58 | setToken(info: string | undefined) { |
... | ... | @@ -59,6 +65,7 @@ export const useUserStore = defineStore({ |
59 | 65 | }, |
60 | 66 | setUserInfo(info: UserInfo) { |
61 | 67 | this.userInfo = info; |
68 | + this.lastUpdateTime = new Date().getTime(); | |
62 | 69 | setAuthCache(USER_INFO_KEY, info); |
63 | 70 | }, |
64 | 71 | setSessionTimeout(flag: boolean) { | ... | ... |