import { encrypt, decrypt } from '@/utils/crypto' const encryptFlag = false // 将数据添加到缓存中(期限) export function setStorage(key, value, options = {}) { if (!options.expiredTime) { // 默认有效期为45天 options.expiredTime = 45 * 24 * 60 * 60 * 1000 } const nowTime = Date.now() // 加密 if (encryptFlag) { value = encrypt(value) } const payload = { expiredTime: nowTime + options.expiredTime, data: value, } key = 'zp_' + key localStorage.setItem(key, JSON.stringify(payload)) } // 从缓存中读取数据(期限) export function getStorage(key) { key = 'zp_' + key const nowTime = Date.now() let payload = localStorage.getItem(key) if (!payload) { return null } payload = JSON.parse(payload) if (nowTime > payload.expiredTime) { localStorage.removeItem(key) return null } // 解密 if (encryptFlag) { payload.data = JSON.parse(decrypt(payload.data)) } return payload.data }