zhengyiming
3 天以前 e662aa7d894a0b259dc1816e79514c1f0d38da9f
apps/h5/src/utils/storage/storage.ts
New file
@@ -0,0 +1,46 @@
interface ProxyStorage {
  getItem(key: string): any;
  setItem(Key: string, value: string): void;
  removeItem(key: string): void;
  clear(): void;
}
//sessionStorage operate
class sessionStorageProxy implements ProxyStorage {
  protected storage: ProxyStorage;
  constructor(storageModel: ProxyStorage) {
    this.storage = storageModel;
  }
  // 存
  public setItem(key: string, value: any): void {
    this.storage.setItem(key, JSON.stringify(value));
  }
  // 取
  public getItem<T = any>(key: string): null | T {
    return JSON.parse(this.storage.getItem(key));
  }
  // 删
  public removeItem(key: string): void {
    this.storage.removeItem(key);
  }
  // 清空
  public clear(): void {
    this.storage.clear();
  }
}
//localStorage operate
class localStorageProxy extends sessionStorageProxy implements ProxyStorage {
  constructor(localStorage: ProxyStorage) {
    super(localStorage);
  }
}
export const storageSession = new sessionStorageProxy(sessionStorage);
export const storageLocal = new localStorageProxy(localStorage);