storage.js 687 B

12345678910111213141516171819202122232425
  1. import config from "../model/config"
  2. export default {
  3. getStorage() {
  4. const res = wx.getStorageSync(config.NAMESPACE);
  5. return JSON.parse(res || "{}");
  6. },
  7. setItem(key, val) {
  8. const storage = this.getStorage();
  9. storage[key] = val;
  10. wx.setStorageSync(config.NAMESPACE, JSON.stringify(storage))
  11. },
  12. getItem(key) {
  13. const storage = this.getStorage();
  14. return storage[key];
  15. },
  16. clearItem(key) {
  17. const storage = this.getStorage();
  18. delete storage[key]
  19. wx.setStorageSync(config.NAMESPACE, JSON.stringify(storage))
  20. },
  21. clearAll() {
  22. wx.removeStorageSync(config.NAMESPACE)
  23. }
  24. }