wupengfei
2025-02-14 9486476bd79bb3bc6eeef2b1b69e6222fcd18b00
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/* eslint-disable @typescript-eslint/no-explicit-any */
import Taro from '@tarojs/taro';
 
//sessionStorage operate
class localStorageProxy {
  constructor() {}
 
  // 存
  public setItem(key: string, value: any): void {
    return Taro.setStorageSync(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 removeItem(key: string) {
    return Taro.removeStorageSync(key);
  }
 
  // 清空
  public clear() {
    return Taro.clearStorage();
  }
}
 
export const storageLocal = new localStorageProxy();