cacheManager.js 3.4 KB

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