fix:修复切换用户时报错bug

This commit is contained in:
fanxb 2020-02-02 01:29:38 +08:00
parent 20703ed643
commit 323fdbed32
2 changed files with 24 additions and 15 deletions

View File

@ -58,6 +58,10 @@ class OverView extends React.Component {
await httpUtil.get("/user/loginStatus"); await httpUtil.get("/user/loginStatus");
this.state.timer = setInterval(this.checkCache.bind(this), 5 * 60 * 1000); this.state.timer = setInterval(this.checkCache.bind(this), 5 * 60 * 1000);
setTimeout(this.checkCache.bind(this), 5000); setTimeout(this.checkCache.bind(this), 5000);
this.props.refresh();
await cacheBookmarkData();
this.props.updateTreeData(getBookmarkList(""));
this.props.changeIsInit(true);
} }
async checkCache() { async checkCache() {
@ -71,9 +75,10 @@ class OverView extends React.Component {
confirm({ confirm({
title: "缓存过期", title: "缓存过期",
content: "书签数据有更新,是否立即刷新?", content: "书签数据有更新,是否立即刷新?",
onOk() { async onOk() {
_this.state.showDialog = false; _this.state.showDialog = false;
clearCache(); await clearCache();
window.location.reload();
}, },
onCancel() { onCancel() {
_this.state.showDialog = false; _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() { async refreshTree() {
const { refresh } = this.state; const { refresh } = this.state;
await clearCache(); await clearCache();
refresh(); window.location.reload();
} }
render() { render() {

View File

@ -13,16 +13,29 @@ export const TREE_LIST_KEY = "treeListData";
* 获取全部书签时间 * 获取全部书签时间
*/ */
export const TREE_LIST_TIME_KEY = "treeListDataTime"; export const TREE_LIST_TIME_KEY = "treeListDataTime";
/**
* 书签数据所属用户
*/
export const TREE_LIST_USER_ID = "treeListDataUserId";
/** /**
* 缓存书签数据 * 缓存书签数据
*/ */
export async function cacheBookmarkData() { 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); let res = await localforage.getItem(TREE_LIST_KEY);
if (!res) { if (!res) {
res = await httpUtil.get("/bookmark/currentUser"); res = await httpUtil.get("/bookmark/currentUser");
if (!res[""]) {
res[""] = [];
}
await localforage.setItem(TREE_LIST_KEY, res); await localforage.setItem(TREE_LIST_KEY, res);
await localforage.setItem(TREE_LIST_TIME_KEY, Date.now()); await localforage.setItem(TREE_LIST_TIME_KEY, Date.now());
await localforage.setItem(TREE_LIST_USER_ID, currentId);
} }
window[TREE_LIST_KEY] = res; window[TREE_LIST_KEY] = res;
} }
@ -32,7 +45,8 @@ export async function cacheBookmarkData() {
* @param {*} path path * @param {*} path path
*/ */
export function getBookmarkList(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() { export async function clearCache() {
await localforage.removeItem(TREE_LIST_KEY); await localforage.removeItem(TREE_LIST_KEY);
await localforage.removeItem(TREE_LIST_TIME_KEY); await localforage.removeItem(TREE_LIST_TIME_KEY);
window.location.reload(); await localforage.removeItem(TREE_LIST_USER_ID);
} }
/** /**