购物车计算
This commit is contained in:
@@ -5,24 +5,38 @@
|
||||
* @return {String} 缓存值
|
||||
*/
|
||||
function get(key) {
|
||||
try {
|
||||
let res = uni.getStorageSync(key)
|
||||
try {
|
||||
let res = uni.getStorageSync(key)
|
||||
if (!res) {
|
||||
return ''
|
||||
}
|
||||
|
||||
if (!res) {
|
||||
return ''
|
||||
}
|
||||
|
||||
|
||||
// res = JSON.parse(res)
|
||||
|
||||
if (res.__expiretime && res.__expiretime < uni.utils.timestamp()) {
|
||||
if (res.expiretime && res.expiretime < Date.now()) {
|
||||
remove(key)
|
||||
return ''
|
||||
} else {
|
||||
return res
|
||||
}
|
||||
} catch (e) {
|
||||
return ''
|
||||
}
|
||||
return ''
|
||||
} else {
|
||||
// #ifdef APP-PLUS
|
||||
return res.value
|
||||
// #endif
|
||||
// #ifdef H5
|
||||
res = JSON.parse(res)
|
||||
return res.value
|
||||
// #endif
|
||||
// #ifdef MP-WEIXIN
|
||||
return res.value
|
||||
// #endif
|
||||
// #ifdef MP-ALIPAY
|
||||
return res.value
|
||||
// #endif
|
||||
|
||||
|
||||
|
||||
}
|
||||
} catch (e) {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,19 +47,19 @@ function get(key) {
|
||||
* @return {String} 缓存值
|
||||
*/
|
||||
function getStorageData(key) {
|
||||
try {
|
||||
let res = uni.getStorageSync(key);
|
||||
try {
|
||||
let res = uni.getStorageSync(key);
|
||||
|
||||
if (!res) {
|
||||
return ''
|
||||
}
|
||||
if (!res) {
|
||||
return ''
|
||||
}
|
||||
|
||||
res = JSON.parse(res)
|
||||
res = JSON.parse(res)
|
||||
|
||||
return res.data
|
||||
} catch (e) {
|
||||
return ''
|
||||
}
|
||||
return res.data
|
||||
} catch (e) {
|
||||
return ''
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,14 +71,14 @@ function getStorageData(key) {
|
||||
* @return void
|
||||
*/
|
||||
function set(key, value, expire = uni.conf.default_expire) {
|
||||
let cacheItem = {}
|
||||
cacheItem = value
|
||||
let cacheItem = {}
|
||||
cacheItem.value = value
|
||||
// console.log(cacheItem)
|
||||
if (expire > 0) {
|
||||
cacheItem.__expiretime = uni.utils.timestamp() + expire
|
||||
}
|
||||
// uni.setStorageSync(key,JSON.stringify(cacheItem))
|
||||
uni.setStorageSync(key,cacheItem)
|
||||
if (expire > 0) {
|
||||
cacheItem.expiretime = Date.now() + expire * 60 * 1000;
|
||||
}
|
||||
// uni.setStorageSync(key,JSON.stringify(cacheItem))
|
||||
uni.setStorageSync(key, cacheItem)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,14 +90,14 @@ function set(key, value, expire = uni.conf.default_expire) {
|
||||
* @return {Promise} Promise对象
|
||||
*/
|
||||
async function remember(key, callback, expire = uni.conf.default_expire) {
|
||||
let ret = this.get(key)
|
||||
if (ret) {
|
||||
return ret
|
||||
} else {
|
||||
ret = await callback()
|
||||
set(key, ret, expire)
|
||||
return ret
|
||||
}
|
||||
let ret = this.get(key)
|
||||
if (ret) {
|
||||
return ret
|
||||
} else {
|
||||
ret = await callback()
|
||||
set(key, ret, expire)
|
||||
return ret
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -93,7 +107,7 @@ async function remember(key, callback, expire = uni.conf.default_expire) {
|
||||
* @return {void}
|
||||
*/
|
||||
function remove(key) {
|
||||
uni.removeStorageSync(key)
|
||||
uni.removeStorageSync(key)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,22 +117,22 @@ function remove(key) {
|
||||
* @return void
|
||||
*/
|
||||
function removeList(prefix) {
|
||||
let keys = uni.getStorageInfoSync().keys
|
||||
if (keys && keys.length > 0) {
|
||||
keys.forEach(key => {
|
||||
if (key.indexOf(prefix) === 0) {
|
||||
uni.removeStorageSync(key)
|
||||
}
|
||||
})
|
||||
}
|
||||
let keys = uni.getStorageInfoSync().keys
|
||||
if (keys && keys.length > 0) {
|
||||
keys.forEach(key => {
|
||||
if (key.indexOf(prefix) === 0) {
|
||||
uni.removeStorageSync(key)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function _randomRemove() {
|
||||
const info = uni.getStorageInfoSync()
|
||||
if (info.currentSize > 0.7 * info.limitSize
|
||||
|| info.keys.length > uni.conf.autoRemoveCache.count
|
||||
|| info.currentSize > uni.conf.autoRemoveCache.size) {
|
||||
for (let i = 0; i < 100; i++) {
|
||||
if (info.currentSize > 0.7 * info.limitSize ||
|
||||
info.keys.length > uni.conf.autoRemoveCache.count ||
|
||||
info.currentSize > uni.conf.autoRemoveCache.size) {
|
||||
for (let i = 0; i < 100; i++) {
|
||||
if (info.keys.length < 1) {
|
||||
return
|
||||
}
|
||||
@@ -131,24 +145,24 @@ function _randomRemove() {
|
||||
function _removeExpired(key) {
|
||||
let res = uni.getStorageSync(key);
|
||||
if (!res) {
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
res = JSON.parse(res)
|
||||
if (res.__expiretime && res.__expiretime < uni.utils.timestamp()) {
|
||||
if (res.expiretime && res.expiretime < uni.utils.timestamp()) {
|
||||
remove(key)
|
||||
}
|
||||
}
|
||||
|
||||
function _autoRemoveExpired() {
|
||||
const info = uni.getStorageInfoSync()
|
||||
if (info.currentSize > 0.7 * info.limitSize
|
||||
|| info.keys.length > uni.conf.autoRemoveCache.count
|
||||
|| info.currentSize > uni.conf.autoRemoveCache.size) {
|
||||
if (info.currentSize > 0.7 * info.limitSize ||
|
||||
info.keys.length > uni.conf.autoRemoveCache.count ||
|
||||
info.currentSize > uni.conf.autoRemoveCache.size) {
|
||||
if (info.keys && info.keys.length > 0) {
|
||||
info.keys.forEach(key => {
|
||||
_removeExpired(key)
|
||||
})
|
||||
info.keys.forEach(key => {
|
||||
_removeExpired(key)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -158,7 +172,7 @@ function autoRemove(is_once = true) {
|
||||
if (info.currentSize > 0.9 * info.limitSize) {
|
||||
clearMemory()
|
||||
}
|
||||
|
||||
|
||||
if (is_once) {
|
||||
_autoRemoveExpired()
|
||||
} else {
|
||||
@@ -169,13 +183,13 @@ function autoRemove(is_once = true) {
|
||||
|
||||
|
||||
function clearFetch(url) {
|
||||
const prefixCacheKey = 'memory:fetch:' + url
|
||||
removeList(prefixCacheKey)
|
||||
const prefixCacheKey = 'memory:fetch:' + url
|
||||
removeList(prefixCacheKey)
|
||||
}
|
||||
|
||||
function clearMemory() {
|
||||
const prefixCacheKey = 'memory:'
|
||||
removeList(prefixCacheKey)
|
||||
const prefixCacheKey = 'memory:'
|
||||
removeList(prefixCacheKey)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -184,22 +198,22 @@ function clearMemory() {
|
||||
* @return void
|
||||
*/
|
||||
function clear() {
|
||||
uni.clearStorageSync()
|
||||
uni.clearStorageSync()
|
||||
}
|
||||
|
||||
function getInfo() {
|
||||
return uni.getStorageInfoSync()
|
||||
return uni.getStorageInfoSync()
|
||||
}
|
||||
|
||||
uni.cache = {
|
||||
get,
|
||||
getStorageData,
|
||||
set,
|
||||
remove,
|
||||
remember,
|
||||
clearFetch,
|
||||
clearMemory,
|
||||
clear,
|
||||
getInfo,
|
||||
get,
|
||||
getStorageData,
|
||||
set,
|
||||
remove,
|
||||
remember,
|
||||
clearFetch,
|
||||
clearMemory,
|
||||
clear,
|
||||
getInfo,
|
||||
autoRemove,
|
||||
}
|
||||
Reference in New Issue
Block a user