分类信息缓存更新时间设定

This commit is contained in:
Chopper
2021-06-18 17:06:25 +08:00
parent b3fda14322
commit c4cc078680

View File

@@ -93,6 +93,11 @@ export default {
getCategory(0).then(res => { getCategory(0).then(res => {
if (res.success) { if (res.success) {
this.cateList = res.result; this.cateList = res.result;
// 过期时间
var expirationTime = new Date().setHours(new Date().getHours() + 1);
// 存放过期时间
localStorage.setItem('category_expiration_time', expirationTime);
// 存放分类信息
localStorage.setItem('category', JSON.stringify(res.result)) localStorage.setItem('category', JSON.stringify(res.result))
} }
}); });
@@ -117,7 +122,12 @@ export default {
} }
}, },
mounted () { mounted () {
if (localStorage.getItem('category')) { if (localStorage.getItem('category') && localStorage.getItem('category_expiration_time')) {
// 如果缓存过期,则获取最新的信息
if (new Date() > localStorage.getItem('category_expiration_time')) {
this.getCate();
return;
}
this.cateList = JSON.parse(localStorage.getItem('category')) this.cateList = JSON.parse(localStorage.getItem('category'))
} else { } else {
this.getCate() this.getCate()