CEES-manage/src/utils/auth/index.ts

81 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-10-20 14:32:09 +08:00
import { Persistent, BasicKeys } from '/@/utils/cache/persistent';
import { CacheTypeEnum } from '/@/enums/cacheEnum';
import projectSetting from '/@/settings/projectSetting';
2022-06-10 10:44:44 +08:00
import { TOKEN_KEY, LOGIN_INFO_KEY, TENANT_ID } from '/@/enums/cacheEnum';
2021-10-20 14:32:09 +08:00
const { permissionCacheType } = projectSetting;
const isLocal = permissionCacheType === CacheTypeEnum.LOCAL;
2022-03-10 09:47:29 +08:00
/**
* token
*/
2021-10-20 14:32:09 +08:00
export function getToken() {
return getAuthCache<string>(TOKEN_KEY);
2021-10-20 14:32:09 +08:00
}
2022-03-10 09:47:29 +08:00
/**
*
*/
export function getLoginBackInfo() {
return getAuthCache(LOGIN_INFO_KEY);
}
/**
* id
*/
export function getTenantId() {
return getAuthCache<string>(TENANT_ID);
2022-03-10 09:47:29 +08:00
}
2021-10-20 14:32:09 +08:00
export function getAuthCache<T>(key: BasicKeys) {
const fn = isLocal ? Persistent.getLocal : Persistent.getSession;
return fn(key) as T;
}
export function setAuthCache(key: BasicKeys, value) {
const fn = isLocal ? Persistent.setLocal : Persistent.setSession;
return fn(key, value, true);
}
/**
* key
* @param key
* @param value
*/
export function setCacheByDynKey(key, value) {
const fn = isLocal ? Persistent.setLocal : Persistent.setSession;
return fn(key, value, true);
}
/**
* key
* @param key
*/
export function getCacheByDynKey<T>(key) {
const fn = isLocal ? Persistent.getLocal : Persistent.getSession;
return fn(key) as T;
}
/**
* key
* @param key
*/
export function removeCacheByDynKey<T>(key) {
const fn = isLocal ? Persistent.removeLocal : Persistent.removeSession;
return fn(key) as T;
}
2022-03-10 09:47:29 +08:00
/**
*
* @param key
* @update:
* @updateBy:lsq
* @updateDate:2021-09-07
*/
export function removeAuthCache<T>(key: BasicKeys) {
const fn = isLocal ? Persistent.removeLocal : Persistent.removeSession;
return fn(key) as T;
}
2021-10-20 14:32:09 +08:00
export function clearAuthCache(immediate = true) {
const fn = isLocal ? Persistent.clearLocal : Persistent.clearSession;
return fn(immediate);
}