storage.ts 841 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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. ]
  12. // 存储的数据
  13. let storageData = uni.getStorageSync(storageKey) || {}
  14. const storage = {
  15. set: function(key:string, value:any) {
  16. if (storageNodeKeys.indexOf(key) != -1) {
  17. let tmp = uni.getStorageSync(storageKey)
  18. tmp = tmp ? tmp : {}
  19. tmp[key] = value
  20. uni.setStorageSync(storageKey, tmp)
  21. }
  22. },
  23. get: function(key:string) {
  24. return storageData[key] || ""
  25. },
  26. remove: function(key:string) {
  27. delete storageData[key]
  28. uni.setStorageSync(storageKey, storageData)
  29. },
  30. clean: function() {
  31. uni.removeStorageSync(storageKey)
  32. }
  33. }
  34. export default storage