storage.uts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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:string, value:any):void {
  8. if (storageNodeKeys.indexOf(key) != -1) {
  9. let data = ref(uni.getStorageSync(key));
  10. let tmp:UTSJSONObject|null = data.value!=='' && data.value!==null? uni.getStorageSync(storageKey) as UTSJSONObject|null : null
  11. let tmpJson:UTSJSONObject = (tmp != null ? JSON.parseObject(JSON.stringify(tmp)) : {}) as UTSJSONObject
  12. tmpJson.set(key,value)
  13. uni.setStorageSync(storageKey, tmpJson)
  14. }
  15. },
  16. get: function(key:string):any|null {
  17. let data = ref(uni.getStorageSync(key));
  18. let storageData:UTSJSONObject|null = data.value!=='' && data.value!==null? uni.getStorageSync(storageKey) as UTSJSONObject|null : null
  19. if(storageData != null){
  20. let storeJson:UTSJSONObject=JSON.parseObject(JSON.stringify(storageData)) as UTSJSONObject
  21. return storeJson.get(key)
  22. }else{
  23. return null
  24. }
  25. },
  26. remove: function(key:string):void {
  27. let storageData:UTSJSONObject|null = uni.getStorageSync(storageKey) as UTSJSONObject|null
  28. if(storageData!=null){
  29. let storeMap=storageData.toMap()
  30. storeMap.delete(key)
  31. storageData=JSON.parseObject(JSON.stringify(storeMap)) as UTSJSONObject
  32. uni.setStorageSync(storageKey, storageData)
  33. }
  34. },
  35. clean: function():void {
  36. uni.removeStorageSync(storageKey)
  37. }
  38. }
  39. export default storage