storage.js 784 B

123456789101112131415161718192021222324252627282930313233
  1. import constant from './constant'
  2. // 存储变量名
  3. let storageKey = 'storage_data'
  4. // 存储节点变量名
  5. let storageNodeKeys = [constant.avatar, constant.name, constant.roles, constant.permissions]
  6. // 存储的数据
  7. let storageData = uni.getStorageSync(storageKey) || {}
  8. const storage = {
  9. set: function(key, value) {
  10. if (storageNodeKeys.indexOf(key) != -1) {
  11. let tmp = uni.getStorageSync(storageKey)
  12. tmp = tmp ? tmp : {}
  13. tmp[key] = value
  14. uni.setStorageSync(storageKey, tmp)
  15. }
  16. },
  17. get: function(key) {
  18. return storageData[key] || ""
  19. },
  20. remove: function(key) {
  21. delete storageData[key]
  22. uni.setStorageSync(storageKey, storageData)
  23. },
  24. clean: function() {
  25. uni.removeStorageSync(storageKey)
  26. }
  27. }
  28. export default storage