storage.js 866 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. constant.cacheToken,constant.isModel]
  7. const storage = {
  8. set: function(key, value) {
  9. if (storageNodeKeys.indexOf(key) != -1) {
  10. let tmp = uni.getStorageSync(storageKey)
  11. tmp = tmp ? tmp : {}
  12. tmp[key] = value
  13. uni.setStorageSync(storageKey, tmp)
  14. }
  15. },
  16. get: function(key) {
  17. let storageData = uni.getStorageSync(storageKey) || {}
  18. return storageData[key] || ""
  19. },
  20. remove: function(key) {
  21. let storageData = uni.getStorageSync(storageKey) || {}
  22. delete storageData[key]
  23. uni.setStorageSync(storageKey, storageData)
  24. },
  25. clean: function() {
  26. uni.removeStorageSync(storageKey)
  27. }
  28. }
  29. export default storage