123456789101112131415161718192021222324252627282930313233343536373839404142 |
- import {constant} from './constant'
- // 存储变量名
- let storageKey = 'storage_data'
- // 存储节点变量名
- let storageNodeKeys = [constant.avatar, constant.name, constant.roles, constant.permissions]
- const storage = {
- set: function(key:string, value:any):void {
- if (storageNodeKeys.indexOf(key) != -1) {
- let tmp:UTSJSONObject|null = uni.getStorageSync(storageKey) as UTSJSONObject|null
- let tmpJson:UTSJSONObject = (tmp!=null && tmp.length>0 ? JSON.parseObject(JSON.stringify(tmp)) : {}) as UTSJSONObject
- tmpJson.set(key,value)
- uni.setStorageSync(storageKey, tmpJson)
- }
- },
- get: function(key:string):any|null {
- let storageData:UTSJSONObject|null = uni.getStorageSync(storageKey) as UTSJSONObject|null
- if(storageData!=null && storageData.length > 0){
- let storeJson:UTSJSONObject=JSON.parseObject(JSON.stringify(storageData)) as UTSJSONObject
- return storeJson.get(key)
- }else{
- return ""
- }
- },
- remove: function(key:string):void {
- let storageData:UTSJSONObject|null = uni.getStorageSync(storageKey) as UTSJSONObject|null
- if(storageData!=null){
- let storeMap=storageData.toMap()
- storeMap.delete(key)
- storageData=JSON.parseObject(JSON.stringify(storeMap)) as UTSJSONObject
- uni.setStorageSync(storageKey, storageData)
- }
-
- },
- clean: function():void {
- uni.removeStorageSync(storageKey)
- }
- }
- export default storage
|