storage.uts 1.4 KB

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