Skip to content

Commit

Permalink
fix: multi windows token sharing
Browse files Browse the repository at this point in the history
修复同时打开多个窗口时令牌没能同步共享的问题

fixed: #761
  • Loading branch information
mynetfan committed Jun 29, 2021
1 parent d7b84c7 commit e5f3788
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- **Table** 修复为 table 提供 rowSelection.onChange 时,无法手动变更 table 的选中项的问题
- **Icon** 修复 SvgIcon 缺少部分样式的问题
- **LockScreen** 修复锁屏功能可以通过刷新页面或复制 URL 打开新的浏览器标签来跳过锁定状态的问题
- 修复多个窗口同时打开页面时,`Token` 不会同步的问题

## 2.5.2(2021-06-27)

Expand Down
14 changes: 14 additions & 0 deletions mock/sys/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,18 @@ export default [
return resultSuccess(codeList);
},
},
{
url: '/basic-api/logout',
timeout: 200,
method: 'get',
response: (request: requestParams) => {
const token = getRequestToken(request);
if (!token) return resultError('Invalid token');
const checkUser = createFakeUserList().find((item) => item.token === token);
if (!checkUser) {
return resultError('Invalid token!');
}
return resultSuccess(undefined, { message: 'Token has been destroyed' });
},
},
] as MockMethod[];
5 changes: 5 additions & 0 deletions src/api/sys/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ErrorMessageMode } from '/#/axios';

enum Api {
Login = '/login',
Logout = '/logout',
GetUserInfo = '/getUserInfo',
GetPermCode = '/getPermCode',
}
Expand Down Expand Up @@ -34,3 +35,7 @@ export function getUserInfo() {
export function getPermCode() {
return defHttp.get<string[]>({ url: Api.GetPermCode });
}

export function doLogout() {
return defHttp.get({ url: Api.Logout });
}
11 changes: 9 additions & 2 deletions src/store/modules/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { PageEnum } from '/@/enums/pageEnum';
import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
import { getAuthCache, setAuthCache } from '/@/utils/auth';
import { GetUserInfoModel, LoginParams } from '/@/api/sys/model/userModel';
import { getUserInfo, loginApi } from '/@/api/sys/user';
import { doLogout, getUserInfo, loginApi } from '/@/api/sys/user';
import { useI18n } from '/@/hooks/web/useI18n';
import { useMessage } from '/@/hooks/web/useMessage';
import { router } from '/@/router';
Expand Down Expand Up @@ -105,7 +105,14 @@ export const useUserStore = defineStore({
/**
* @description: logout
*/
logout(goLogin = false) {
async logout(goLogin = false) {
try {
await doLogout();
} catch {
console.log('注销Token失败');
}
this.setToken(undefined);
this.setSessionTimeout(false);
goLogin && router.push(PageEnum.BASE_LOGIN);
},

Expand Down
5 changes: 5 additions & 0 deletions src/utils/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export function setAuthCache(key: BasicKeys, value) {
const fn = isLocal ? Persistent.setLocal : Persistent.setSession;
return fn(key, value, true);
}

export function clearAuthCache(immediate = true) {
const fn = isLocal ? Persistent.clearLocal : Persistent.clearSession;
return fn(immediate);
}
12 changes: 10 additions & 2 deletions src/utils/cache/persistent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '/@/enums/cacheEnum';
import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting';
import { toRaw } from 'vue';
import { pick } from 'lodash-es';

interface BasicStore {
[TOKEN_KEY]: string | number | null | undefined;
Expand Down Expand Up @@ -96,8 +97,15 @@ export class Persistent {
}

window.addEventListener('beforeunload', function () {
ls.set(APP_LOCAL_CACHE_KEY, localMemory.getCache);
ss.set(APP_SESSION_CACHE_KEY, sessionMemory.getCache);
// TOKEN_KEY 在登录或注销时已经写入到storage了,此处为了解决同时打开多个窗口时token不同步的问题
ls.set(APP_LOCAL_CACHE_KEY, {
...localMemory.getCache,
...pick(ls.get(APP_LOCAL_CACHE_KEY), [TOKEN_KEY, USER_INFO_KEY]),
});
ss.set(APP_SESSION_CACHE_KEY, {
...sessionMemory.getCache,
...pick(sessionMemory.getCache, [TOKEN_KEY, USER_INFO_KEY]),
});
});

function storageChange(e: any) {
Expand Down

0 comments on commit e5f3788

Please sign in to comment.