瀏覽代碼

Merge branch '2024鹅状元' of https://gogs.mtavip.com/wangguoyu/uniProject into 2024鹅状元

tanxue 6 月之前
父節點
當前提交
2ba119e0e0
共有 4 個文件被更改,包括 115 次插入45 次删除
  1. 7 4
      pages/study/index.vue
  2. 107 0
      utils/cacheManager.js
  3. 1 9
      utils/constant.js
  4. 0 32
      utils/storage.js

+ 7 - 4
pages/study/index.vue

@@ -60,6 +60,7 @@
 	} from '@/utils/emitEvents.js';
 	import eggDialog from './eggDialog.vue'
 import { toast } from "../../utils/common";
+import  cacheManager  from "../../utils/cacheManager.js";
 	
 	const eggDialogRef = ref(null);
 
@@ -96,7 +97,8 @@ import { toast } from "../../utils/common";
 		
 	//	 appContext.config.globalProperties.$state
 		   // const state = appContext.config.globalProperties.$state;
-		console.log(options)
+		console.log(options)
+		console.log(cacheManager);
 		// 获取路由参数
 		routeParams.value = options;
 		
@@ -143,7 +145,8 @@ import { toast } from "../../utils/common";
 			infoData.zhangId = res.data.zhangId
 			infoData.zhangName = res.data.zhangName
 			gradeTerm.value = translateData(res.data);
-			useTabBarHistory().setTabBarStorage({nianji:res.data.nianji,zhangId:res.data.zhangId,xueqi:res.data.xueqi})
+			useTabBarHistory().setTabBarStorage({nianji:res.data.nianji,zhangId:res.data.zhangId,xueqi:res.data.xueqi})
+			cacheManager.set("zhangJieCacheInfo",res.data)
 		})
 	}
 	
@@ -177,7 +180,8 @@ import { toast } from "../../utils/common";
 			infoData.zhangId = res.data.zhangId
 			infoData.zhangName = res.data.zhangName
 			gradeTerm.value = translateData(res.data);
-			useTabBarHistory().setTabBarStorage({nianji:res.data.nianji,zhangId:res.data.zhangId,xueqi:res.data.xueqi})
+			useTabBarHistory().setTabBarStorage({nianji:res.data.nianji,zhangId:res.data.zhangId,xueqi:res.data.xueqi})
+			cacheManager.set("zhangJieCacheInfo",res.data)
 		})
 	}
 	function getZhangFirst() {
@@ -197,7 +201,6 @@ import { toast } from "../../utils/common";
 			infoData.zhangName = res.data.zhangName
 			gradeTerm.value = translateData(res.data);
 			recordZhangJie()
-			console.log(useTabBarHistory());
 			useTabBarHistory().setTabBarStorage({nianji:res.data.nianji,zhangId:res.data.zhangId,xueqi:res.data.xueqi})
 		})
 	}

+ 107 - 0
utils/cacheManager.js

@@ -0,0 +1,107 @@
+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 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(`Path ${arrayPath} does not point to an array in the object.`);
+		}
+	}
+	//  例子
+	// 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,
+		updateObject,
+		findArrayInObject,
+		clearAll
+	};
+})();
+
+export default cacheManager;

+ 1 - 9
utils/constant.js

@@ -1,10 +1,4 @@
-const constant = {
-   avatar: 'vuex_avatar',
-   name: 'vuex_name',
-   roles: 'vuex_roles',
-   permissions: 'vuex_permissions'
- }
- 
+
  export const nianji_list = [{
 		label: '一年级',
 		id: 1
@@ -41,5 +35,3 @@ export const xueqi_list = [{
 	}
 ]
  
-
- export default constant

+ 0 - 32
utils/storage.js

@@ -1,32 +0,0 @@
-import constant from './constant'
-
-// 存储变量名
-let storageKey = 'storage_data'
-
-// 存储节点变量名
-let storageNodeKeys = [constant.avatar, constant.name, constant.roles, constant.permissions]
-
-const storage = {
-  set: function(key, value) {
-    if (storageNodeKeys.indexOf(key) != -1) {
-      let tmp = uni.getStorageSync(storageKey)
-      tmp = tmp ? tmp : {}
-      tmp[key] = value
-      uni.setStorageSync(storageKey, tmp)
-    }
-  },
-  get: function(key) {
-    let storageData = uni.getStorageSync(storageKey) || {}
-    return storageData[key] || ""
-  },
-  remove: function(key) {
-    let storageData = uni.getStorageSync(storageKey) || {}
-    delete storageData[key]
-    uni.setStorageSync(storageKey, storageData)
-  },
-  clean: function() {
-    uni.removeStorageSync(storageKey)
-  }
-}
-
-export default storage