From 323fdbed3207fc86abb267e794097ae03e94ee34 Mon Sep 17 00:00:00 2001 From: fanxb Date: Sun, 2 Feb 2020 01:29:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=97=B6=E6=8A=A5=E9=94=99bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/pages/manage/OverView/index.jsx | 21 ++++++++------------- front/src/util/cacheUtil.js | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/front/src/pages/manage/OverView/index.jsx b/front/src/pages/manage/OverView/index.jsx index afcbaf0..85ec664 100644 --- a/front/src/pages/manage/OverView/index.jsx +++ b/front/src/pages/manage/OverView/index.jsx @@ -58,6 +58,10 @@ class OverView extends React.Component { await httpUtil.get("/user/loginStatus"); this.state.timer = setInterval(this.checkCache.bind(this), 5 * 60 * 1000); setTimeout(this.checkCache.bind(this), 5000); + this.props.refresh(); + await cacheBookmarkData(); + this.props.updateTreeData(getBookmarkList("")); + this.props.changeIsInit(true); } async checkCache() { @@ -71,9 +75,10 @@ class OverView extends React.Component { confirm({ title: "缓存过期", content: "书签数据有更新,是否立即刷新?", - onOk() { + async onOk() { _this.state.showDialog = false; - clearCache(); + await clearCache(); + window.location.reload(); }, onCancel() { _this.state.showDialog = false; @@ -82,16 +87,6 @@ class OverView extends React.Component { } } - /** - * 初始化第一级书签 - */ - async componentDidMount() { - this.props.refresh(); - await cacheBookmarkData(); - this.props.updateTreeData(getBookmarkList("")); - this.props.changeIsInit(true); - } - /** * 异步加载 */ @@ -133,7 +128,7 @@ class OverView extends React.Component { async refreshTree() { const { refresh } = this.state; await clearCache(); - refresh(); + window.location.reload(); } render() { diff --git a/front/src/util/cacheUtil.js b/front/src/util/cacheUtil.js index d4764c9..e9d62db 100644 --- a/front/src/util/cacheUtil.js +++ b/front/src/util/cacheUtil.js @@ -13,16 +13,29 @@ export const TREE_LIST_KEY = "treeListData"; * 获取全部书签时间 */ export const TREE_LIST_TIME_KEY = "treeListDataTime"; +/** + * 书签数据所属用户 + */ +export const TREE_LIST_USER_ID = "treeListDataUserId"; /** * 缓存书签数据 */ export async function cacheBookmarkData() { + let currentId = JSON.parse(window.atob(window.token.split(".")[1])).userId; + let cacheId = await localforage.getItem(TREE_LIST_USER_ID); + if (currentId && currentId !== cacheId) { + await clearCache(); + } let res = await localforage.getItem(TREE_LIST_KEY); if (!res) { res = await httpUtil.get("/bookmark/currentUser"); + if (!res[""]) { + res[""] = []; + } await localforage.setItem(TREE_LIST_KEY, res); await localforage.setItem(TREE_LIST_TIME_KEY, Date.now()); + await localforage.setItem(TREE_LIST_USER_ID, currentId); } window[TREE_LIST_KEY] = res; } @@ -32,7 +45,8 @@ export async function cacheBookmarkData() { * @param {*} path path */ export function getBookmarkList(path) { - return window[TREE_LIST_KEY][path]; + let data = window[TREE_LIST_KEY][path]; + return data ? data : []; } /** @@ -51,7 +65,7 @@ export async function checkCacheStatus() { export async function clearCache() { await localforage.removeItem(TREE_LIST_KEY); await localforage.removeItem(TREE_LIST_TIME_KEY); - window.location.reload(); + await localforage.removeItem(TREE_LIST_USER_ID); } /**