From f462f4c9ed8b90dd73c9d738436788097cb734b8 Mon Sep 17 00:00:00 2001 From: "Ray.Hao" <1490493387@qq.com> Date: Tue, 20 May 2025 11:11:22 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20:bug:=20=E4=BF=AE=E5=A4=8D=20Storage=20?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E5=A4=84=E7=90=86=E9=9D=9E=20JSON?= =?UTF-8?q?=20=E6=A0=BC=E5=BC=8F=E6=95=B0=E6=8D=AE=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/storage.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/utils/storage.ts b/src/utils/storage.ts index 8c6bef24..25bf6f30 100644 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -12,7 +12,14 @@ export class Storage { static get(key: string, defaultValue?: T): T { const value = localStorage.getItem(key); - return value ? JSON.parse(value) : defaultValue; + if (!value) return defaultValue as T; + + try { + return JSON.parse(value); + } catch { + // 如果解析失败,返回原始字符串 + return value as unknown as T; + } } static remove(key: string): void { @@ -28,7 +35,14 @@ export class Storage { static sessionGet(key: string, defaultValue?: T): T { const value = sessionStorage.getItem(key); - return value ? JSON.parse(value) : defaultValue; + if (!value) return defaultValue as T; + + try { + return JSON.parse(value); + } catch { + // 如果解析失败,返回原始字符串 + return value as unknown as T; + } } static sessionRemove(key: string): void {