store.js 973 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. module.exports = {
  2. setStore: setStore,
  3. getStore: getStore,
  4. clearAll: clearAll,
  5. remove: remove,
  6. }
  7. //同步储存缓存数据
  8. function setStore(key, value) {
  9. try {
  10. // console.log("gasdfasfqwerqwerqr===0000==" + JSON.stringify(value));
  11. wx.setStorageSync(key, value);
  12. } catch (e) {
  13. // console.log("gasdfasfqwerqwerqr===111==" + JSON.stringify(e));
  14. }
  15. };
  16. //同步取出缓存数据
  17. function getStore(key) {
  18. try {
  19. // 使用同步方法获取数据
  20. const list = wx.getStorageSync(key);
  21. // console.log("gasdfasfqwerqwerqr===222==" + JSON.stringify(list));
  22. if (list) {
  23. return list;
  24. } else {
  25. return [];
  26. }
  27. } catch (e) {
  28. return [];
  29. }
  30. };
  31. ///不清除的有
  32. // {token,chatRecord,freeTimes}
  33. //清空数据
  34. function clearAll() {
  35. const constant = require('./constant.js');
  36. removeByKey(constant.param.userInfo);
  37. }
  38. //移除指定key
  39. function remove(key) {
  40. try {
  41. wx.removeStorageSync(key)
  42. } catch (e) {}
  43. }