|
@@ -0,0 +1,143 @@
|
|
|
+const cacheManager = (function() {
|
|
|
+ const STORAGE_PREFIX = 'App_cache_';
|
|
|
+
|
|
|
+ function set(key, value) {
|
|
|
+ const fullKey = STORAGE_PREFIX + key;
|
|
|
+ if (typeof value === 'object' && value !== null) {
|
|
|
+ // 如果是对象,则将其转换为字符串存储
|
|
|
+ uni.setStorageSync(fullKey, JSON.stringify(value));
|
|
|
+ } else {
|
|
|
+ uni.setStorageSync(fullKey, value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取缓存
|
|
|
+ function get(key) {
|
|
|
+ const fullKey = STORAGE_PREFIX + key;
|
|
|
+ const value = uni.getStorageSync(fullKey);
|
|
|
+ try {
|
|
|
+ return JSON.parse(value);
|
|
|
+ } catch (e) {
|
|
|
+ return value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 删除缓存
|
|
|
+ function remove(key) {
|
|
|
+ const fullKey = STORAGE_PREFIX + key;
|
|
|
+ uni.removeStorageSync(fullKey);
|
|
|
+ }
|
|
|
+
|
|
|
+ //例子: cacheManager.updateObject('user', { www: undefined }); //删除
|
|
|
+ function updateObject(key, updates) {
|
|
|
+ let obj = get(key) || {};
|
|
|
+ // 合并更新到对象中
|
|
|
+ //Object.assign(obj, updates);
|
|
|
+ for (let [keyToUpdate, value] of Object.entries(updates)) {
|
|
|
+ if (value === null || value === undefined) {
|
|
|
+ // 如果值为 null 或 undefined,则删除属性
|
|
|
+ delete obj[keyToUpdate];
|
|
|
+ } else {
|
|
|
+ obj[keyToUpdate] = value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ set(key, obj);
|
|
|
+ }
|
|
|
+ // 用于视频已经看完 修改状态
|
|
|
+ function updateJieStatus(key, index, currentJieId) {
|
|
|
+ let obj = get(key) || {};
|
|
|
+ for (let jie of obj.zhangList[index].jieList) {
|
|
|
+ if (jie.jieId == currentJieId) {
|
|
|
+ // console.log('jie',jie);
|
|
|
+ jie.studyFlag = 1
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ set(key, obj);
|
|
|
+ }
|
|
|
+ // 更新会员
|
|
|
+ function updateVipStatus(key, cardId) {
|
|
|
+ let obj = get(key) || {};
|
|
|
+ if (!obj.cardList.includes(cardId)) {
|
|
|
+ obj.cardList.push(Number(cardId));
|
|
|
+ }
|
|
|
+ set(key, obj);
|
|
|
+ }
|
|
|
+ // 获得当前jie参数
|
|
|
+ function getCurrentJieData(key, index, currentJieId) {
|
|
|
+ let obj = get(key) || {};
|
|
|
+ for (let jie of obj.zhangList[index].jieList) {
|
|
|
+ if (jie.jieId == currentJieId) {
|
|
|
+ return jie
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function clearAll() {
|
|
|
+ const keys = uni.getStorageInfoSync().keys;
|
|
|
+ keys.forEach(key => {
|
|
|
+ if (key.startsWith(STORAGE_PREFIX)) {
|
|
|
+ uni.removeStorageSync(key);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // 增量修改对象中的数组,支持添加、更新和删除元素
|
|
|
+ function updateArrayInObject(key, arrayPath, updateFn) {
|
|
|
+ let obj = get(key) || {};
|
|
|
+
|
|
|
+ // 根据arrayPath找到需要修改的数组
|
|
|
+ const targetArray = findArrayInObject(obj, arrayPath);
|
|
|
+
|
|
|
+ if (Array.isArray(targetArray)) {
|
|
|
+ // 应用更新函数到目标数组上
|
|
|
+ updateFn(targetArray);
|
|
|
+
|
|
|
+ // 重新设置缓存
|
|
|
+ set(key, obj);
|
|
|
+ } else {
|
|
|
+ console.error(`${arrayPath}不指向对象中的数组。`);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 例子
|
|
|
+ // cacheManager.updateArrayInObject('user', 'hobbies', (hobbies) => {
|
|
|
+ // hobbies.push('reading');
|
|
|
+ // hobbies = 'coding';
|
|
|
+ // hobbies.pop();
|
|
|
+ // });
|
|
|
+ // 在对象中根据路径找到数组
|
|
|
+ function findArrayInObject(obj, path) {
|
|
|
+ return path.split('.').reduce((acc, curr) => {
|
|
|
+ if (acc && acc[curr] !== undefined) {
|
|
|
+ return acc[curr];
|
|
|
+ } else {
|
|
|
+ return undefined;
|
|
|
+ }
|
|
|
+ }, obj);
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ set,
|
|
|
+ get,
|
|
|
+ remove,
|
|
|
+ updateJieStatus,
|
|
|
+ getCurrentJieData,
|
|
|
+ updateObject,
|
|
|
+ updateVipStatus,
|
|
|
+ findArrayInObject,
|
|
|
+ clearAll
|
|
|
+ };
|
|
|
+})();
|
|
|
+
|
|
|
+export default cacheManager;
|
|
|
+
|
|
|
+// 单元测试 引导大鹅提示 缓存Key
|
|
|
+export const SHOW_UNIT_TEST_TISHI = 'SHOW_UNIT_TEST_TISHI';
|
|
|
+
|
|
|
+// 单元测试大鹅提示缓存
|
|
|
+export function useUnitTestTishi() {
|
|
|
+ return {
|
|
|
+ updateTishi: () => cacheManager.set(SHOW_UNIT_TEST_TISHI, 'has'),
|
|
|
+ getTishi: () => cacheManager.get(SHOW_UNIT_TEST_TISHI)
|
|
|
+ }
|
|
|
+}
|