fix(store): 修复本地存储数据解析异常

- 在 app.js 和 user.js 中添加对本地存储数据的异常处理
- 当解析本地存储数据失败时,移除相关存储项,避免无限错误提示- 优化了数据获取逻辑,提高代码可读性和健壮性
This commit is contained in:
jmal 2025-05-22 16:43:20 +08:00
parent 8d8d273c0c
commit e5b9b594e3
2 changed files with 22 additions and 2 deletions

View File

@ -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,

View File

@ -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 : '',