From f81a7a619725aa57c48353fca2c21a43fc3b7aee Mon Sep 17 00:00:00 2001
From: Tevin <tingquanren@163.com>
Date: Wed, 06 Mar 2024 16:28:54 +0800
Subject: [PATCH] 公共组件库,文档建设开始

---
 common/LocalStorage.js |   28 ++++++++++++++++++++++++++--
 1 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/common/LocalStorage.js b/common/LocalStorage.js
index aacd37e..e299a5e 100644
--- a/common/LocalStorage.js
+++ b/common/LocalStorage.js
@@ -18,9 +18,22 @@
         this._data.prefixType = prefixType;
     }
 
-    load(key) {
+    load(key, defaultType = 'obj') {
         const name = 'AiSim.' + this._data.prefixType + '@' + key;
-        return JSON.parse(Taro.getStorageSync(name) || '{}');
+        const data = Taro.getStorageSync(name);
+        if (data) {
+            return JSON.parse(data);
+        } else {
+            if (defaultType === 'obj') {
+                return {};
+            } else if (defaultType === 'arr') {
+                return [];
+            }
+        }
+    }
+
+    loadArr(key) {
+        return this.load(key, 'arr');
     }
 
     save(key, value) {
@@ -59,6 +72,17 @@
         return matches;
     }
 
+    // 清除本地存储工具保存的所有数据
+    cleanAll() {
+        const info = Taro.getStorageInfoSync();
+        const prefixReg = /^AiSim\.[a-zA-Z]+@/;
+        info.keys.forEach(storageKey => {
+            if (prefixReg.test(storageKey)) {
+                Taro.removeStorageSync(storageKey);
+            }
+        });
+    }
+
 }
 
 export const $localStorage = new LocalStorage();
\ No newline at end of file

--
Gitblit v1.9.1