!25 update src/utils/tool.js.

Merge pull request !25 from 喜上眉梢/N/A
This commit is contained in:
sakuya 2022-05-23 16:18:41 +00:00 committed by Gitee
commit 8c6ad81219
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 31 additions and 11 deletions

View File

@ -11,18 +11,38 @@ const tool = {}
/* localStorage */
tool.data = {
set(table, settings) {
var _set = JSON.stringify(settings)
return localStorage.setItem(table, _set);
/**
* 设置缓存
* @param {*} key
* @param {*} data
* @param {*} time 过期时间 单位秒 默认0 不过期
* @returns
*/
set(key, data, time = 0) {
let cacheVal = {
val: data,
time: parseInt(time) === 0 ? 0 : parseInt(time) * 1000 + new Date().getTime()
};
return localStorage.setItem(key, JSON.stringify(cacheVal));
},
get(table) {
var data = localStorage.getItem(table);
get(key) {
try {
data = JSON.parse(data)
} catch (err) {
const value = JSON.parse(localStorage.getItem(key))
if (value) {
let now = new Date().getTime();
if (now > value.time && value.time != 0) {//缓存过期
localStorage.removeItem(key)
return null;
}
return value.val
}
return null
} catch (e) {
return null
}
return data;
},
del (key) {
return localStorage.removeItem(key);
},
remove(table) {
return localStorage.removeItem(table);