storage.js 859 B

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