From aa45de802c9980656dc6a85dd3417cd0f77f7292 Mon Sep 17 00:00:00 2001 From: zhengyiming <540361168@qq.com> Date: 星期五, 21 三月 2025 15:49:55 +0800 Subject: [PATCH] fix: 三期需求 --- apps/h5/src/utils/storage/storage.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 46 insertions(+), 0 deletions(-) diff --git a/apps/h5/src/utils/storage/storage.ts b/apps/h5/src/utils/storage/storage.ts new file mode 100644 index 0000000..50f5804 --- /dev/null +++ b/apps/h5/src/utils/storage/storage.ts @@ -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); -- Gitblit v1.9.1