Skip to content

Commit

Permalink
feat: always refresh userinfo when page reload
Browse files Browse the repository at this point in the history
每次刷新整个页面时都从接口更新用户信息
  • Loading branch information
mynetfan committed Aug 15, 2021
1 parent 53e79a2 commit cc46935
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/router/guard/permissionGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,14 @@ export function createPermissionGuard(router: Router) {
to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME)
) {
next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
console.log({ from, to });
return;
}

// get userinfo while last fetch time is empty
if (userStore.getLastUpdateTime === 0) {
await userStore.getUserInfoAction();
}

if (permissionStore.getIsDynamicAddedRoute) {
next();
return;
Expand All @@ -88,7 +92,6 @@ export function createPermissionGuard(router: Router) {

if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
// 动态添加路由后,此处应当重定向到fullPath,否则会加载404页面内容
// fix: 添加query以免丢失
next({ path: to.fullPath, replace: true, query: to.query });
} else {
const redirectPath = (from.query.redirect || to.path) as string;
Expand Down
7 changes: 7 additions & 0 deletions src/store/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ interface UserState {
token?: string;
roleList: RoleEnum[];
sessionTimeout?: boolean;
lastUpdateTime: number;
}

export const useUserStore = defineStore({
Expand All @@ -33,6 +34,8 @@ export const useUserStore = defineStore({
roleList: [],
// Whether the login expired
sessionTimeout: false,
// Last fetch time
lastUpdateTime: 0,
}),
getters: {
getUserInfo(): UserInfo {
Expand All @@ -47,6 +50,9 @@ export const useUserStore = defineStore({
getSessionTimeout(): boolean {
return !!this.sessionTimeout;
},
getLastUpdateTime(): number {
return this.lastUpdateTime;
},
},
actions: {
setToken(info: string | undefined) {
Expand All @@ -59,6 +65,7 @@ export const useUserStore = defineStore({
},
setUserInfo(info: UserInfo) {
this.userInfo = info;
this.lastUpdateTime = new Date().getTime();
setAuthCache(USER_INFO_KEY, info);
},
setSessionTimeout(flag: boolean) {
Expand Down

0 comments on commit cc46935

Please sign in to comment.