feat 搭建架构骨架
This commit is contained in:
34
src/utils/storage.ts
Normal file
34
src/utils/storage.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
// localStorage/sessionStorage 泛型封装(无 Vue 依赖)
|
||||
|
||||
const PREFIX = 'cz:'
|
||||
|
||||
function parse<T>(raw: string | null): T | null {
|
||||
if (raw === null) return null
|
||||
try {
|
||||
return JSON.parse(raw) as T
|
||||
}
|
||||
catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export const storage = {
|
||||
get<T>(key: string): T | null {
|
||||
return parse<T>(localStorage.getItem(PREFIX + key))
|
||||
},
|
||||
set<T>(key: string, value: T): void {
|
||||
localStorage.setItem(PREFIX + key, JSON.stringify(value))
|
||||
},
|
||||
remove(key: string): void {
|
||||
localStorage.removeItem(PREFIX + key)
|
||||
},
|
||||
sessionGet<T>(key: string): T | null {
|
||||
return parse<T>(sessionStorage.getItem(PREFIX + key))
|
||||
},
|
||||
sessionSet<T>(key: string, value: T): void {
|
||||
sessionStorage.setItem(PREFIX + key, JSON.stringify(value))
|
||||
},
|
||||
sessionRemove(key: string): void {
|
||||
sessionStorage.removeItem(PREFIX + key)
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user