-
Notifications
You must be signed in to change notification settings - Fork 377
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
78 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
interface ExtendedItem { | ||
value: any; | ||
expires?: number; // 可选属性 | ||
} | ||
|
||
const localStorage = { | ||
// 检查是否支持localStorage | ||
isSupported(): boolean { | ||
return typeof window !== 'undefined' && 'localStorage' in window; | ||
}, | ||
|
||
// 设置值 | ||
setItem(key: string, value: any, expires?: number): void { | ||
if (!this.isSupported()) return; | ||
|
||
let item: ExtendedItem= { value }; | ||
|
||
if (expires !== undefined) { | ||
item = { ...item, expires: Date.now() + expires * 1000 }; | ||
} | ||
|
||
let serializedValue = JSON.stringify(item); | ||
|
||
localStorage.setItem(key, serializedValue); | ||
}, | ||
|
||
// 获取值 | ||
getItem<T>(key: string): T | null { | ||
if (!this.isSupported()) return null; | ||
|
||
const serializedValue = localStorage.getItem(key) as string; | ||
if (!serializedValue) return null; | ||
|
||
let item: any; | ||
try { | ||
item = JSON.parse(serializedValue); | ||
} catch (e) { | ||
console.error('Error parsing JSON from localStorage'); | ||
return null; | ||
} | ||
|
||
if (item.expires && item.expires < Date.now()) { | ||
this.removeItem(key); | ||
return null; | ||
} | ||
|
||
return item.value as T; | ||
}, | ||
|
||
// 移除值 | ||
removeItem(key: string): void { | ||
if (!this.isSupported()) return; | ||
|
||
localStorage.removeItem(key); | ||
} | ||
} | ||
|
||
export default localStorage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters