-
Notifications
You must be signed in to change notification settings - Fork 85
/
index.js
34 lines (34 loc) · 857 Bytes
/
index.js
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
const { Base64 } = require('js-base64')
const moment = require('moment')
/**
* 获取localstorage
* 更新localstorage
* 删除localstorage
*/
class Storage {
constructor() {
this.storage = window.localStorage
}
getItem(key) {
const origin = JSON.parse(Base64.decode(this.storage.getItem(key)))
const { time } = origin
if (time > new Date.getItem()) {
return {
...origin,
message: `'过期时间', ${moment(time).format('YYYY-MM-DD HH:mm:ss')}`,
}
} else {
this.removeItem(key)
return ''
}
}
setItem(key, value, time = new Date.getItem() + 3600 * 24) {
if (typeof time === 'object') {
value = { value }
}
this.storage.setItem(key, Base64.encode(JSON.stringify({ ...value, time }))) // 简单加密
}
removeItem(key) {
this.storage.removeItem(key)
}
}