Skip to content

Commit 051f227

Browse files
committed
go_view 共享token
1 parent 41deaf2 commit 051f227

File tree

5 files changed

+56
-37
lines changed

5 files changed

+56
-37
lines changed

src/enums/cacheEnum.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import exp from "constants";
2+
13
export const TOKEN_KEY = 'TOKEN__';
24
export const REFRESHTOKEN_KEY = 'REFRESHTOKEN__';
35
export const LOCALE_KEY = 'LOCALE__';
@@ -11,8 +13,9 @@ export const APP_DARK_MODE_KEY = 'APP_DARK_MODE__';
1113
export const APP_LOCAL_CACHE_KEY = 'COMMON_LOCAL__';
1214
export const APP_SESSION_CACHE_KEY = 'COMMON_SESSION__';
1315

14-
export const THINGSBOARD_SHARE_TOKEN = 'THINGSBOARD_SHARE_TOKEN__';
15-
16+
export const GO_VIEW_CHAHE_KEY = 'GO_SYSTEM__';
17+
export const GO_VIEW_TOKEN_KEY = 'userToken';
18+
export const GO_VIEW_USER_INFO_KEY = 'userInfo';
1619

1720
export enum CacheTypeEnum {
1821
SESSION,

src/store/modules/user.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
USER_INFO_KEY,
1515
SESSION_TIMEOUT_KEY,
1616
} from '/@/enums/cacheEnum';
17-
import { getAuthCache, setAuthCache, setJwtPairToken } from '/@/utils/auth';
17+
import { getAuthCache, setAuthCache, setGoViewStoreToken, setGoViewStoreUserInfo } from '/@/utils/auth';
1818
import { loginApi, logoutApi, userInfoApi, LoginParams } from '/@/api/tb/login';
1919
import { useI18n } from '/@/hooks/web/useI18n';
2020
import { useMessage } from '/@/hooks/web/useMessage';
@@ -102,9 +102,9 @@ export const useUserStore = defineStore({
102102
this.token = jwtPair ? jwtPair.token : ''; // for null or undefined value
103103
this.refreshToken = jwtPair ? jwtPair.refreshToken : '';
104104
this.lastUpdateTime = new Date().getTime();
105-
// setAuthCache(TOKEN_KEY, jwtPair ? jwtPair.token : '');
106-
// setAuthCache(REFRESHTOKEN_KEY, jwtPair ? jwtPair.refreshToken : '');
107-
setJwtPairToken(jwtPair);
105+
setAuthCache(TOKEN_KEY, jwtPair ? jwtPair.token : '');
106+
setAuthCache(REFRESHTOKEN_KEY, jwtPair ? jwtPair.refreshToken : '');
107+
setGoViewStoreToken(jwtPair)
108108
},
109109
setSessionTimeout(flag: boolean) {
110110
this.sessionTimeout = flag;
@@ -125,6 +125,7 @@ export const useUserStore = defineStore({
125125
this.authority = info?.authority || null;
126126
this.lastUpdateTime = new Date().getTime();
127127
setAuthCache(USER_INFO_KEY, info);
128+
setGoViewStoreUserInfo(info)
128129
},
129130
// setRoleList(roleList: RoleEnum[] | string[]) {
130131
// this.roleList = roleList;

src/utils/auth/go_view.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { JwtPair, UserInfo } from "/#/store";
2+
import { GO_VIEW_CHAHE_KEY, GO_VIEW_TOKEN_KEY, GO_VIEW_USER_INFO_KEY } from "/@/enums/cacheEnum";
3+
4+
5+
6+
export function setGoViewStoreToken(token: JwtPair| undefined | null) {
7+
const storeInfo = getGoViewStoreItem();
8+
if(token){
9+
storeInfo[GO_VIEW_TOKEN_KEY] = token
10+
} else {
11+
delete storeInfo[GO_VIEW_TOKEN_KEY]
12+
}
13+
window.localStorage.setItem(GO_VIEW_CHAHE_KEY, JSON.stringify(storeInfo));
14+
}
15+
16+
export function setGoViewStoreUserInfo(userInfo: UserInfo | null){
17+
const storeInfo = getGoViewStoreItem();
18+
if(userInfo){
19+
storeInfo[GO_VIEW_USER_INFO_KEY] = userInfo
20+
} else {
21+
delete storeInfo[GO_VIEW_USER_INFO_KEY]
22+
}
23+
window.localStorage.setItem(GO_VIEW_CHAHE_KEY, JSON.stringify(storeInfo));
24+
}
25+
26+
export function getGoViewStoreItem(){
27+
const item = window.localStorage.getItem(GO_VIEW_CHAHE_KEY)
28+
try {
29+
return item ? JSON.parse(item) : {}
30+
} catch (err) {
31+
return {}
32+
}
33+
}
34+
35+
export function clearGoviewStore(){
36+
window.localStorage.removeItem(GO_VIEW_CHAHE_KEY);
37+
}

src/utils/auth/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Persistent, BasicKeys } from '/@/utils/cache/persistent';
2-
import { CacheTypeEnum, THINGSBOARD_SHARE_TOKEN } from '/@/enums/cacheEnum';
2+
import { CacheTypeEnum } from '/@/enums/cacheEnum';
33
import projectSetting from '/@/settings/projectSetting';
44
import { TOKEN_KEY, REFRESHTOKEN_KEY } from '/@/enums/cacheEnum';
5-
import { JwtPair } from '/#/store';
6-
import { setLocalStorage } from '../cache';
75

86
const { permissionCacheType } = projectSetting;
97
const isLocal = permissionCacheType === CacheTypeEnum.LOCAL;
@@ -16,11 +14,11 @@ export function getRefreshToken() {
1614
return getAuthCache<string>(REFRESHTOKEN_KEY);
1715
}
1816

19-
export function setJwtPairToken(jwtPair: JwtPair | null | undefined) {
20-
setAuthCache(TOKEN_KEY, jwtPair ? jwtPair.token : '');
21-
setAuthCache(REFRESHTOKEN_KEY, jwtPair ? jwtPair.refreshToken : '');
22-
setLocalStorage(THINGSBOARD_SHARE_TOKEN, jwtPair);
23-
}
17+
// export function setJwtPairToken(jwtPair: JwtPair | null | undefined) {
18+
// setAuthCache(TOKEN_KEY, jwtPair ? jwtPair.token : '');
19+
// setAuthCache(REFRESHTOKEN_KEY, jwtPair ? jwtPair.refreshToken : '');
20+
// setLocalStorage(THINGSBOARD_SHARE_TOKEN, jwtPair);
21+
// }
2422

2523
export function getAuthCache<T>(key: BasicKeys) {
2624
const fn = isLocal ? Persistent.getLocal : Persistent.getSession;
@@ -36,3 +34,6 @@ export function clearAuthCache(immediate = true) {
3634
const fn = isLocal ? Persistent.clearLocal : Persistent.clearSession;
3735
return fn(immediate);
3836
}
37+
38+
39+
export * from './go_view';

src/utils/cache/index.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,4 @@ export const createLocalStorage = (options: Options = {}) => {
2929
return createStorage(localStorage, { ...options, timeout: DEFAULT_CACHE_TIME });
3030
};
3131

32-
33-
export const setLocalStorage = <T>(k: string, v: T) => {
34-
try {
35-
window.localStorage.setItem(k, JSON.stringify(v))
36-
} catch (error) {
37-
return false
38-
}
39-
}
40-
41-
export const getLocalStorage: (k: string) => any = (k: string) => {
42-
const item = window.localStorage.getItem(k)
43-
try {
44-
return item ? JSON.parse(item) : item
45-
} catch (err) {
46-
return item
47-
}
48-
}
49-
50-
export const clearLocalStorage = (name: string) => {
51-
window.localStorage.removeItem(name)
52-
}
53-
54-
5532
export default WebStorage;

0 commit comments

Comments
 (0)