使用cooKie
class COOKIE {
setCookie (name, value, time) {
if (!name) return
if (time) {
const exp = new Date()
exp.setTime(exp.getTime() + time * 60 * 1000)
document.cookie = name + '=' + escape(value) + 'expires=' + exp.toGMTString()
} else {
document.cookie = name + '=' + escape(value)
}
}
getCookie (name) {
let arr, reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
if (arr = document.cookie.match(reg)) { return (arr[2]) } else { return null }
}
delCookie (name) {
let exp = new Date()
exp.setTime(exp.getTime() - 1)
let cval = this.getCookie(name)
if (cval != null) { document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString() }
}
}
const useCookie = new COOKIE()
export default useCookie
带过期时间
的本地存储
import store from 'store'
class HmmStore {
constructor () {
this.expires = 10 * 60 * 1000
}
setStore (name, data, during = this.expires) {
let tempData = {
time: this.getNowTime(),
during,
data
}
store.set(`${name}`, tempData)
}
getStore (name) {
if (store.get(name)) {
let tempData = store.get(name)
if (tempData.time && tempData.during) {
if (this.getNowTime() < (tempData.time + tempData.during)) {
return tempData.data
} else {
return ''
}
} else {
return ''
}
} else {
return ''
}
}
deleteStore (name) {
store.remove(name)
}
getNowTime () {
return Date.parse(new Date())
}
}
export default new HmmStore()