EP-WachatApp/eaterplanet_ecommerce/utils/wcache.js

48 lines
870 B
JavaScript
Raw Normal View History

2023-03-24 11:24:28 +08:00
var dtime = '_deadtime';
function put(k, v, t) {
wx.setStorageSync(k, v)
var seconds = parseInt(t);
if (seconds > 0) {
var timestamp = Date.parse(new Date());
timestamp = timestamp / 1000 + seconds;
wx.setStorageSync(k + dtime, timestamp + "")
} else {
wx.removeStorageSync(k + dtime)
}
}
function get(k, def) {
var deadtime = parseInt(wx.getStorageSync(k + dtime))
if (deadtime) {
if (parseInt(deadtime) < Date.parse(new Date()) / 1000) {
if (def) {
return def;
} else {
return;
}
}
}
var res = wx.getStorageSync(k);
if (res) {
return res;
} else {
return def;
}
}
function remove(k) {
wx.removeStorageSync(k);
wx.removeStorageSync(k + dtime);
}
function clear() {
wx.clearStorageSync();
}
module.exports = {
put: put,
get: get,
remove: remove,
clear: clear,
}