From e5b9b594e3f0e950da3a934f90be133d9ff29a8f Mon Sep 17 00:00:00 2001 From: jmal Date: Thu, 22 May 2025 16:43:20 +0800 Subject: [PATCH] =?UTF-8?q?fix(store):=20=E4=BF=AE=E5=A4=8D=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E5=AD=98=E5=82=A8=E6=95=B0=E6=8D=AE=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 app.js 和 user.js 中添加对本地存储数据的异常处理 - 当解析本地存储数据失败时,移除相关存储项,避免无限错误提示- 优化了数据获取逻辑,提高代码可读性和健壮性 --- src/store/modules/app.js | 13 ++++++++++++- src/store/modules/user.js | 11 ++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/store/modules/app.js b/src/store/modules/app.js index 71a973f..e25440f 100644 --- a/src/store/modules/app.js +++ b/src/store/modules/app.js @@ -2,7 +2,18 @@ import Cookies from 'js-cookie' const getDefaultState = () => { - const app = localStorage.getItem('app') && JSON.parse(localStorage.getItem('app')) + + const appData = localStorage.getItem('app') + let app = null + if (appData) { + try { + app = JSON.parse(appData) + } catch (e) { + console.error('Failed to parse app state from localStorage, it might be corrupted:', e) + localStorage.removeItem('app') + } + } + return { sidebar: { opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true, diff --git a/src/store/modules/user.js b/src/store/modules/user.js index a4fbe4b..6c91aa5 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -8,7 +8,16 @@ import Layout from '@/layout' import ParentView from '@/components/ParentView' const getDefaultState = () => { - const store = localStorage.getItem('store') && JSON.parse(localStorage.getItem('store')) + const storeData = localStorage.getItem('store') + let store = null + if (storeData) { + try { + store = JSON.parse(storeData) + } catch (e) { + console.error('Failed to parse user state from localStorage, it might be corrupted:', e) + localStorage.removeItem('store') + } + } return { token: store ? store.token : '', shareToken: store ? store.shareToken : '',