feat: always refresh userinfo when page reload

每次刷新整个页面时都从接口更新用户信息
This commit is contained in:
无木 2021-08-16 06:04:12 +08:00
parent 53e79a2d94
commit cc46935a82
2 changed files with 12 additions and 2 deletions

View File

@ -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;
@ -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;

View File

@ -20,6 +20,7 @@ interface UserState {
token?: string;
roleList: RoleEnum[];
sessionTimeout?: boolean;
lastUpdateTime: number;
}
export const useUserStore = defineStore({
@ -33,6 +34,8 @@ export const useUserStore = defineStore({
roleList: [],
// Whether the login expired
sessionTimeout: false,
// Last fetch time
lastUpdateTime: 0,
}),
getters: {
getUserInfo(): UserInfo {
@ -47,6 +50,9 @@ export const useUserStore = defineStore({
getSessionTimeout(): boolean {
return !!this.sessionTimeout;
},
getLastUpdateTime(): number {
return this.lastUpdateTime;
},
},
actions: {
setToken(info: string | undefined) {
@ -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) {