cacheManager.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. // console.log('obj',obj);
  46. // console.log('index',index);
  47. // console.log('currentJieId',currentJieId);
  48. for(let jie of obj.zhangList[index].jieList){
  49. if(jie.jieId ==currentJieId){
  50. // console.log('jie',jie);
  51. jie.studyFlag = 1
  52. break
  53. }
  54. }
  55. set(key, obj);
  56. }
  57. // 获得当前jie参数
  58. function getCurrentJieData(key, index,currentJieId) {
  59. let obj = get(key) || {};
  60. // console.log('obj',obj);
  61. // console.log('index',index);
  62. // console.log('currentJieId',currentJieId);
  63. for(let jie of obj.zhangList[index].jieList){
  64. if(jie.jieId ==currentJieId){
  65. return jie
  66. break
  67. }
  68. }
  69. }
  70. function clearAll() {
  71. const keys = uni.getStorageInfoSync().keys;
  72. keys.forEach(key => {
  73. if (key.startsWith(STORAGE_PREFIX)) {
  74. uni.removeStorageSync(key);
  75. }
  76. });
  77. }
  78. // 增量修改对象中的数组,支持添加、更新和删除元素
  79. function updateArrayInObject(key, arrayPath, updateFn) {
  80. let obj = get(key) || {};
  81. // 根据arrayPath找到需要修改的数组
  82. const targetArray = findArrayInObject(obj, arrayPath);
  83. if (Array.isArray(targetArray)) {
  84. // 应用更新函数到目标数组上
  85. updateFn(targetArray);
  86. // 重新设置缓存
  87. set(key, obj);
  88. } else {
  89. console.error(`${arrayPath}不指向对象中的数组。`);
  90. }
  91. }
  92. // 例子
  93. // cacheManager.updateArrayInObject('user', 'hobbies', (hobbies) => {
  94. // hobbies.push('reading');
  95. // hobbies = 'coding';
  96. // hobbies.pop();
  97. // });
  98. // 在对象中根据路径找到数组
  99. function findArrayInObject(obj, path) {
  100. return path.split('.').reduce((acc, curr) => {
  101. if (acc && acc[curr] !== undefined) {
  102. return acc[curr];
  103. } else {
  104. return undefined;
  105. }
  106. }, obj);
  107. }
  108. return {
  109. set,
  110. get,
  111. remove,
  112. updateJieStatus,
  113. getCurrentJieData,
  114. updateObject,
  115. findArrayInObject,
  116. clearAll
  117. };
  118. })();
  119. export default cacheManager;
  120. // 单元测试 引导大鹅提示 缓存Key
  121. export const SHOW_UNIT_TEST_TISHI = 'SHOW_UNIT_TEST_TISHI';
  122. // 单元测试大鹅提示缓存
  123. export function useUnitTestTishi() {
  124. return {
  125. updateTishi:() => cacheManager.set(SHOW_UNIT_TEST_TISHI,'has'),
  126. getTishi: () => cacheManager.get(SHOW_UNIT_TEST_TISHI)
  127. }
  128. }