storage.js 867 B

123456789101112131415161718192021222324252627282930313233343536
  1. class Storage {
  2. constructor(name) {
  3. this.name = name
  4. }
  5. // 获取缓存
  6. getStorage() {
  7. return uni.getStorageSync(this.name) || []
  8. }
  9. // 设置缓存
  10. setStorage(data = []) {
  11. if (data.length) uni.setStorageSync(this.name, data)
  12. return data
  13. }
  14. // 插入数据
  15. insert(...arg) {
  16. let storage = this.getStorage()
  17. storage.push(...arg)
  18. this.setStorage(storage)
  19. return storage
  20. }
  21. // 删除数据
  22. delete(obj = {}) {
  23. const origin = this.getStorage()
  24. const storage = origin.filter(ele => !Object.keys(obj).some(k => obj[k] === ele[k]))
  25. this.setStorage(storage)
  26. return storage
  27. }
  28. // 查询数据
  29. select(obj = {}) {
  30. const origin = this.getStorage()
  31. const storage = origin.filter(ele => Object.keys(obj).every(k => obj[k] === ele[k]))
  32. return storage
  33. }
  34. }
  35. export default new Storage('imgcache')