cacheManager.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. const cacheManager = (function() {
  2. const STORAGE_PREFIX = 'App_cache_';
  3. function set(key, value) {
  4. const fullKey = STORAGE_PREFIX + key;
  5. if (typeof value === 'object' && value !== null) {
  6. // 如果是对象,则将其转换为字符串存储
  7. uni.setStorageSync(fullKey, JSON.stringify(value));
  8. } else {
  9. uni.setStorageSync(fullKey, value);
  10. }
  11. }
  12. // 获取缓存
  13. function get(key) {
  14. const fullKey = STORAGE_PREFIX + key;
  15. const value = uni.getStorageSync(fullKey);
  16. try {
  17. return JSON.parse(value);
  18. } catch (e) {
  19. return value;
  20. }
  21. }
  22. // 删除缓存
  23. function remove(key) {
  24. const fullKey = STORAGE_PREFIX + key;
  25. uni.removeStorageSync(fullKey);
  26. }
  27. //例子: cacheManager.updateObject('user', { www: undefined }); //删除
  28. function updateObject(key, updates) {
  29. let obj = get(key) || {};
  30. // 合并更新到对象中
  31. //Object.assign(obj, updates);
  32. for (let [keyToUpdate, value] of Object.entries(updates)) {
  33. if (value === null || value === undefined) {
  34. // 如果值为 null 或 undefined,则删除属性
  35. delete obj[keyToUpdate];
  36. } else {
  37. obj[keyToUpdate] = value;
  38. }
  39. }
  40. set(key, obj);
  41. }
  42. // 用于视频已经看完 修改状态
  43. function updateJieStatus(key, index, currentJieId) {
  44. let obj = get(key) || {};
  45. for (let jie of obj.zhangList[index].jieList) {
  46. if (jie.jieId == currentJieId) {
  47. // console.log('jie',jie);
  48. jie.studyFlag = 1
  49. break
  50. }
  51. }
  52. set(key, obj);
  53. }
  54. // 获得当前jie参数
  55. function getCurrentJieData(key, index, currentJieId) {
  56. let obj = get(key) || {};
  57. for (let jie of obj.zhangList[index].jieList) {
  58. if (jie.jieId == currentJieId) {
  59. return jie
  60. break
  61. }
  62. }
  63. }
  64. function clearAll() {
  65. const keys = uni.getStorageInfoSync().keys;
  66. keys.forEach(key => {
  67. if (key.startsWith(STORAGE_PREFIX)) {
  68. uni.removeStorageSync(key);
  69. }
  70. });
  71. }
  72. // 增量修改对象中的数组,支持添加、更新和删除元素
  73. function updateArrayInObject(key, arrayPath, updateFn) {
  74. let obj = get(key) || {};
  75. // 根据arrayPath找到需要修改的数组
  76. const targetArray = findArrayInObject(obj, arrayPath);
  77. if (Array.isArray(targetArray)) {
  78. // 应用更新函数到目标数组上
  79. updateFn(targetArray);
  80. // 重新设置缓存
  81. set(key, obj);
  82. } else {
  83. console.error(`${arrayPath}不指向对象中的数组。`);
  84. }
  85. }
  86. // 例子
  87. // cacheManager.updateArrayInObject('user', 'hobbies', (hobbies) => {
  88. // hobbies.push('reading');
  89. // hobbies = 'coding';
  90. // hobbies.pop();
  91. // });
  92. // 在对象中根据路径找到数组
  93. function findArrayInObject(obj, path) {
  94. return path.split('.').reduce((acc, curr) => {
  95. if (acc && acc[curr] !== undefined) {
  96. return acc[curr];
  97. } else {
  98. return undefined;
  99. }
  100. }, obj);
  101. }
  102. // 用于新数学产品当前单元节视频看完了 更新完成状态和计算当前单元的进度
  103. // currentJieId:当前jieId
  104. function updateStatusAndProess(currentJieId) {
  105. let obj = get('xuexi-shuxue') || {};
  106. // 标记完成
  107. for (let jie of obj.dagangList) {
  108. if (jie.jieId == currentJieId) {
  109. jie.wanchengFlag = 1;
  110. break;
  111. }
  112. }
  113. // 计算新进度
  114. let newProgress = (parseFloat(obj.curProcess) || 0) + (100 / obj.dagangList.length);
  115. if (newProgress > 100) newProgress = 100;
  116. obj.curProcess = newProgress.toFixed(2);
  117. set('xuexi-shuxue', obj);
  118. }
  119. return {
  120. set,
  121. get,
  122. remove,
  123. updateJieStatus,
  124. getCurrentJieData,
  125. updateStatusAndProess,
  126. updateObject,
  127. findArrayInObject,
  128. clearAll
  129. };
  130. })();
  131. export default cacheManager;
  132. // 单元测试 引导大鹅提示 缓存Key
  133. export const SHOW_UNIT_TEST_TISHI = 'SHOW_UNIT_TEST_TISHI';
  134. // 单元测试大鹅提示缓存
  135. export function useUnitTestTishi() {
  136. return {
  137. updateTishi: () => cacheManager.set(SHOW_UNIT_TEST_TISHI, 'has'),
  138. getTishi: () => cacheManager.get(SHOW_UNIT_TEST_TISHI)
  139. }
  140. }