You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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',
}