refactor(projects): new storage system [新的本地数据存储系统]

This commit is contained in:
Soybean
2022-11-17 01:47:06 +08:00
parent 7a58035514
commit 971915948b
23 changed files with 166 additions and 191 deletions

View File

@@ -25,3 +25,37 @@ export function removeSession(key: string) {
export function clearSession() {
window.sessionStorage.clear();
}
function createSessionStorage<T extends StorageInterface.Session = StorageInterface.Session>() {
function set<K extends keyof T>(key: K, value: T[K]) {
const json = encrypto(value);
sessionStorage.setItem(key as string, json);
}
function get<K extends keyof T>(key: K) {
const json = sessionStorage.getItem(key as string);
let data: T[K] | null = null;
if (json) {
try {
data = decrypto(json);
} catch {
// 防止解析失败
}
}
return data;
}
function remove(key: keyof T) {
window.sessionStorage.removeItem(key as string);
}
function clear() {
window.sessionStorage.clear();
}
return {
set,
get,
remove,
clear
};
}
export const sessionStg = createSessionStorage();