From a3be4bd8a96df6b27e4f0d3883d661d9cb64d1fc Mon Sep 17 00:00:00 2001 From: zhengyiming <540361168@qq.com> Date: 星期五, 21 二月 2025 14:47:05 +0800 Subject: [PATCH] fix: 页面 --- apps/h5/src/utils/storage/storage.ts | 51 +++++++++++++++++++++++++++++---------------------- 1 files changed, 29 insertions(+), 22 deletions(-) diff --git a/apps/h5/src/utils/storage/storage.ts b/apps/h5/src/utils/storage/storage.ts index f15fbd6..50f5804 100644 --- a/apps/h5/src/utils/storage/storage.ts +++ b/apps/h5/src/utils/storage/storage.ts @@ -1,39 +1,46 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import Taro from '@tarojs/taro'; +interface ProxyStorage { + getItem(key: string): any; + setItem(Key: string, value: string): void; + removeItem(key: string): void; + clear(): void; +} //sessionStorage operate -class localStorageProxy { - constructor() {} +class sessionStorageProxy implements ProxyStorage { + protected storage: ProxyStorage; + + constructor(storageModel: ProxyStorage) { + this.storage = storageModel; + } // 瀛� public setItem(key: string, value: any): void { - return Taro.setStorageSync(key, JSON.stringify(value)); + this.storage.setItem(key, JSON.stringify(value)); } // 鍙� - public getItem<T = any>(key: string): Nullable<T> { - try { - const value = Taro.getStorageSync(key); - if (value) { - // Do something with return value - return JSON.parse(value); - } - return null; - } catch (e) { - // Do something when catch error - return null; - } + public getItem<T = any>(key: string): null | T { + return JSON.parse(this.storage.getItem(key)); } // 鍒� - public removeItem(key: string) { - return Taro.removeStorageSync(key); + public removeItem(key: string): void { + this.storage.removeItem(key); } // 娓呯┖ - public clear() { - return Taro.clearStorage(); + public clear(): void { + this.storage.clear(); } } -export const storageLocal = new localStorageProxy(); +//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