205 lines
4.7 KiB
TypeScript
205 lines
4.7 KiB
TypeScript
export interface Cache {
|
|
/**
|
|
* @update 获取缓存
|
|
*/
|
|
get(key: string): Promise<any>;
|
|
/**
|
|
* @update 设置缓存
|
|
*/
|
|
set(key: string, value: any): Promise<any>;
|
|
/**
|
|
* @update 删除缓存
|
|
*/
|
|
del(): Promise<void>;
|
|
/**
|
|
* 初始化
|
|
*/
|
|
init?: () => Promise<any>;
|
|
}
|
|
type User = {
|
|
avatar?: string;
|
|
description?: string;
|
|
id?: string;
|
|
needChangePassword?: boolean;
|
|
orgs?: string[];
|
|
type?: string;
|
|
username?: string;
|
|
};
|
|
|
|
export type CacheLoginUser = {
|
|
user?: User;
|
|
id?: string;
|
|
accessToken?: string;
|
|
refreshToken?: string;
|
|
};
|
|
type CacheLogin = {
|
|
loginUsers: CacheLoginUser[];
|
|
} & CacheLoginUser;
|
|
|
|
export type CacheStore<T = Cache> = {
|
|
name: string;
|
|
/**
|
|
* 缓存数据
|
|
* @important 需要先调用init
|
|
*/
|
|
cacheData: CacheLogin;
|
|
/**
|
|
* 实际操作的cache, 需要先调用init
|
|
*/
|
|
cache: T;
|
|
|
|
/**
|
|
* 设置当前用户
|
|
*/
|
|
setLoginUser(user: CacheLoginUser): Promise<void>;
|
|
/**
|
|
* 获取当前用户
|
|
*/
|
|
getCurrentUser(): Promise<User>;
|
|
/**
|
|
* 获取当前用户列表
|
|
*/
|
|
getCurrentUserList(): Promise<CacheLoginUser[]>;
|
|
/**
|
|
* 获取缓存的refreshToken
|
|
*/
|
|
getRefreshToken(): Promise<string>;
|
|
/**
|
|
* 获取缓存的accessToken
|
|
*/
|
|
getAccessToken(): Promise<string>;
|
|
/**
|
|
* 清除当前用户
|
|
*/
|
|
clearCurrentUser(): Promise<void>;
|
|
/**
|
|
* 清除所有用户
|
|
*/
|
|
clearAll(): Promise<void>;
|
|
|
|
getValue(): Promise<CacheLogin>;
|
|
setValue(value: CacheLogin): Promise<CacheLogin>;
|
|
delValue(): Promise<void>;
|
|
init(): Promise<any>;
|
|
};
|
|
|
|
export type LoginCacheStoreOpts = {
|
|
name: string;
|
|
cache: Cache;
|
|
};
|
|
export class LoginCacheStore implements CacheStore<any> {
|
|
cache: Cache;
|
|
name: string;
|
|
cacheData: CacheLogin;
|
|
constructor(opts: LoginCacheStoreOpts) {
|
|
if (!opts.cache) {
|
|
throw new Error('cache is required');
|
|
}
|
|
// @ts-ignore
|
|
this.cache = opts.cache;
|
|
this.cacheData = {
|
|
loginUsers: [],
|
|
user: undefined,
|
|
id: undefined,
|
|
accessToken: undefined,
|
|
refreshToken: undefined,
|
|
};
|
|
this.name = opts.name;
|
|
}
|
|
/**
|
|
* 设置缓存
|
|
* @param key
|
|
* @param value
|
|
* @returns
|
|
*/
|
|
async setValue(value: CacheLogin) {
|
|
await this.cache.set(this.name, value);
|
|
this.cacheData = value;
|
|
return value;
|
|
}
|
|
/**
|
|
* 删除缓存
|
|
*/
|
|
async delValue() {
|
|
await this.cache.del();
|
|
}
|
|
getValue(): Promise<CacheLogin> {
|
|
return this.cache.get(this.name);
|
|
}
|
|
/**
|
|
* 初始化,设置默认值
|
|
*/
|
|
async init() {
|
|
const defaultData = {
|
|
loginUsers: [],
|
|
user: null,
|
|
id: null,
|
|
accessToken: null,
|
|
refreshToken: null,
|
|
};
|
|
if (this.cache.init) {
|
|
try {
|
|
const cacheData = await this.cache.init();
|
|
this.cacheData = cacheData || defaultData;
|
|
} catch (error) {
|
|
console.log('cacheInit error', error);
|
|
}
|
|
} else {
|
|
this.cacheData = (await this.getValue()) || defaultData;
|
|
}
|
|
}
|
|
/**
|
|
* 设置当前用户
|
|
* @param user
|
|
*/
|
|
async setLoginUser(user: CacheLoginUser) {
|
|
const has = this.cacheData.loginUsers.find((u) => u.id === user.id);
|
|
if (has) {
|
|
this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id);
|
|
}
|
|
this.cacheData.loginUsers.push(user);
|
|
this.cacheData.user = user.user;
|
|
this.cacheData.id = user.id;
|
|
this.cacheData.accessToken = user.accessToken;
|
|
this.cacheData.refreshToken = user.refreshToken;
|
|
await this.setValue(this.cacheData);
|
|
}
|
|
|
|
getCurrentUser(): Promise<CacheLoginUser> {
|
|
const cacheData = this.cacheData;
|
|
return Promise.resolve(cacheData.user!);
|
|
}
|
|
getCurrentUserList(): Promise<CacheLoginUser[]> {
|
|
return Promise.resolve(this.cacheData.loginUsers.filter((u) => u?.id));
|
|
}
|
|
getRefreshToken(): Promise<string> {
|
|
const cacheData = this.cacheData;
|
|
return Promise.resolve(cacheData.refreshToken || '');
|
|
}
|
|
getAccessToken(): Promise<string> {
|
|
const cacheData = this.cacheData;
|
|
return Promise.resolve(cacheData.accessToken || '');
|
|
}
|
|
|
|
async clearCurrentUser() {
|
|
const user = await this.getCurrentUser();
|
|
const has = this.cacheData.loginUsers.find((u) => u.id === user.id);
|
|
if (has) {
|
|
this.cacheData.loginUsers = this.cacheData?.loginUsers?.filter((u) => u?.id && u.id !== user.id);
|
|
}
|
|
this.cacheData.user = undefined;
|
|
this.cacheData.id = undefined;
|
|
this.cacheData.accessToken = undefined;
|
|
this.cacheData.refreshToken = undefined;
|
|
await this.setValue(this.cacheData);
|
|
}
|
|
async clearAll() {
|
|
this.cacheData.loginUsers = [];
|
|
this.cacheData.user = undefined;
|
|
this.cacheData.id = undefined;
|
|
this.cacheData.accessToken = undefined;
|
|
this.cacheData.refreshToken = undefined;
|
|
await this.setValue(this.cacheData);
|
|
}
|
|
}
|