cacheManager.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. // key : xuexi-shuxue
  104. // index : 当前单元下的第几个节index
  105. // currentJieId:当前jieId
  106. function updateStatusAndProess(key, index, currentJieId) {
  107. let obj = get(key) || {};
  108. // 更新完成状态
  109. for (let jie of obj.dagangList) {
  110. if (jie.jieId == currentJieId && jie.wanchengFlag == 0) {
  111. jie.wanchengFlag = 1;
  112. break;
  113. }
  114. }
  115. // 统计视频课程数量(排除单元测试)
  116. const videoSections = obj.dagangList.filter(item => {
  117. return item.type == 1;
  118. });
  119. const videoCount = videoSections.length;
  120. // 计算新进度
  121. const currentProgress = parseFloat(obj.curProcess) || 0;
  122. const progressPerVideo = 100 / videoCount;
  123. let newProgress = currentProgress + progressPerVideo;
  124. // 确保进度不超过100%
  125. if (newProgress > 100) {
  126. newProgress = 100;
  127. }
  128. obj.curProcess = newProgress.toFixed(2);
  129. set(key, obj);
  130. }
  131. return {
  132. set,
  133. get,
  134. remove,
  135. updateJieStatus,
  136. getCurrentJieData,
  137. updateStatusAndProess,
  138. updateObject,
  139. findArrayInObject,
  140. clearAll
  141. };
  142. })();
  143. export default cacheManager;
  144. // 单元测试 引导大鹅提示 缓存Key
  145. export const SHOW_UNIT_TEST_TISHI = 'SHOW_UNIT_TEST_TISHI';
  146. // 单元测试大鹅提示缓存
  147. export function useUnitTestTishi() {
  148. return {
  149. updateTishi: () => cacheManager.set(SHOW_UNIT_TEST_TISHI, 'has'),
  150. getTishi: () => cacheManager.get(SHOW_UNIT_TEST_TISHI)
  151. }
  152. }