storage.js
1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import constant from './constant'
// 存储变量名
let storageKey = 'storage_data'
// 存储节点变量名
let storageNodeKeys = [
constant.avatar,
constant.name,
constant.roles,
constant.permissions,
// user profile fields persisted by GetInfo
constant.code,
constant.deptPaths,
constant.id,
constant.username,
constant.roleCodes,
constant.telephone
]
const storage = {
set: function(key, value) {
if (storageNodeKeys.indexOf(key) != -1) {
let tmp = uni.getStorageSync(storageKey)
tmp = tmp ? tmp : {}
tmp[key] = value
uni.setStorageSync(storageKey, tmp)
}
},
get: function(key) {
let storageData = uni.getStorageSync(storageKey) || {}
// Provide sensible defaults for array-type keys
const arrayDefaults = [
constant.roles,
constant.permissions,
constant.roleCodes,
constant.deptPaths
]
if (storageData[key] === undefined || storageData[key] === null) {
return arrayDefaults.includes(key) ? [] : ""
}
return storageData[key]
},
remove: function(key) {
let storageData = uni.getStorageSync(storageKey) || {}
delete storageData[key]
uni.setStorageSync(storageKey, storageData)
},
clean: function() {
uni.removeStorageSync(storageKey)
}
}
export default storage