|
|
|
|
|
export const config = {
|
|
|
// 图片临时储存保留的时间,单位秒,默认30秒
|
|
|
imageCacheTime: 30000000000000000000,
|
|
|
}
|
|
|
/**
|
|
|
* 缓存方法
|
|
|
* @param {*} key 要设置缓存的key
|
|
|
* @param {*} val 要设置缓存的值
|
|
|
* @param {*} timeout 要设置缓存的过期时间
|
|
|
* @returns
|
|
|
* 使用:1、设置缓存:storage(key,val)
|
|
|
* 2、设置缓存有效期:storage(key,val,timeout),其中timeout为秒数
|
|
|
* 3、获取缓存:storage(key)
|
|
|
* 4、清除缓存:storage(key,null)
|
|
|
*/
|
|
|
export const storage = (key, val, timeout) => {
|
|
|
if (key) {
|
|
|
const date = new Date()
|
|
|
if (val === null) {
|
|
|
// 清除缓存
|
|
|
uni.removeStorageSync(`${key}_timeout`)
|
|
|
return uni.removeStorageSync(key)
|
|
|
} else if (val === undefined && timeout === undefined) {
|
|
|
// 获取缓存
|
|
|
const cache = uni.getStorageSync(key)
|
|
|
if (cache === undefined) {
|
|
|
return undefined
|
|
|
} else {
|
|
|
const expired = uni.getStorageSync(`${key}_timeout`)
|
|
|
if (expired && date > expired) {
|
|
|
uni.removeStorageSync(key)
|
|
|
uni.removeStorageSync(`${key}_timeout`)
|
|
|
return undefined
|
|
|
}
|
|
|
return cache
|
|
|
}
|
|
|
} else if (val !== undefined) {
|
|
|
// 存储缓存
|
|
|
if (timeout) {
|
|
|
let throughSecond = 0
|
|
|
if (typeof timeout === 'number') {
|
|
|
throughSecond = timeout
|
|
|
} else if (typeof timeout === 'string') {
|
|
|
const number = parseInt(timeout)
|
|
|
const unit = timeout.charAt(timeout.length - 1)
|
|
|
if (number && number > 0) {
|
|
|
if (unit === 'd' || unit === 'day' || unit === 'D') {
|
|
|
throughSecond = number * 60 * 60 * 24
|
|
|
} else if (unit === 'h' || unit === 'hour' || unit === 'H') {
|
|
|
throughSecond = number * 60 * 60
|
|
|
} else if (unit === 'm' || unit === 'minute' || unit === 'H') {
|
|
|
throughSecond = number * 60
|
|
|
} else {
|
|
|
throughSecond = number
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (throughSecond) {
|
|
|
const expired = new Date(throughSecond * 1000 + new Date().valueOf())
|
|
|
uni.setStorageSync(`${key}_timeout`, expired)
|
|
|
}
|
|
|
}
|
|
|
return uni.setStorageSync(key, val)
|
|
|
}
|
|
|
}
|
|
|
return undefined
|
|
|
}
|
|
|
/**
|
|
|
* 描述:使用缓存的标识,为了后面能统一精准在storage里面辨别
|
|
|
* 永久储存:不加_temp
|
|
|
* 临时储存:加_temp后缀
|
|
|
*/
|
|
|
export const CHCHEPREFIX = {
|
|
|
// 默认-永久储存
|
|
|
default: 'default',
|
|
|
// 默认-临时储存
|
|
|
default_temp: 'default_temp',
|
|
|
}
|