From 0809fce7c43a94c40f0b355e359221477facc51b Mon Sep 17 00:00:00 2001 From: chensiAb <chenchenco03@163.com> Date: Thu, 16 May 2024 11:01:32 +0800 Subject: [PATCH] fix:'格式化大写PX变小写' --- common/LocalStorage.js | 36 +++++++++++++++++++++++++++++++----- 1 files changed, 31 insertions(+), 5 deletions(-) diff --git a/common/LocalStorage.js b/common/LocalStorage.js index b1eb5a0..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 */ @@ -9,7 +11,7 @@ export class LocalStorage { constructor() { this._data = { - prefixType: '', + prefixType: process.env.TARO_ENV === 'h5' ? 'h5' : 'wx', }; } @@ -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