storage.ts 881 B

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