From bc8f7123657eb2dde8a328928140c94ea0cc3d29 Mon Sep 17 00:00:00 2001
From: Tevin <tingquanren@163.com>
Date: Thu, 16 May 2024 11:52:06 +0800
Subject: [PATCH] Merge branch 'master' of ssh://dev.zhiheiot.com:29418/mob-components

---
 common/LocalStorage.js |   34 ++++++++++++++++++++++++++++++----
 1 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/common/LocalStorage.js b/common/LocalStorage.js
index aacd37e..ff1c977 100644
--- a/common/LocalStorage.js
+++ b/common/LocalStorage.js
@@ -1,6 +1,8 @@
 /**
  * LocalStorage - 混合App模式,本地存储同步工具
- *  用于区分不同开发环境读取不同数据
+ * @tutorial 注意:混合App模式中,不建议使用 Taro.setStorageSync 存储数据
+ *      因为Taro API无法区分不同开发环境,测试环境与正式环境存储的本地数据,读取时会串通
+ *      本方法解决方式在于,给键名增加了开发环境的前缀,例如:AiSim.lc@***、AiSim.wc@***、AiSim.rb@***
  * @author Tevin
  */
 
@@ -18,9 +20,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 +74,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
+export const $localStorage = new LocalStorage();

--
Gitblit v1.9.1