2025-10-02 17:23:13 +08:00

77 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 缓存工具 - 提供sessionStorage和localStorage的封装
export default {
// sessionStorage
session: {
// 设置sessionStorage
set(key, value) {
if (typeof value === 'object') {
value = JSON.stringify(value)
}
sessionStorage.setItem(key, value)
},
// 获取sessionStorage
get(key) {
const value = sessionStorage.getItem(key)
try {
return JSON.parse(value)
} catch (e) {
return value
}
},
// 获取sessionStorageJSON格式
getJSON(key) {
const value = sessionStorage.getItem(key)
try {
return JSON.parse(value)
} catch (e) {
return null
}
},
// 移除sessionStorage
remove(key) {
sessionStorage.removeItem(key)
},
// 清空sessionStorage
clear() {
sessionStorage.clear()
}
},
// localStorage
local: {
// 设置localStorage
set(key, value) {
if (typeof value === 'object') {
value = JSON.stringify(value)
}
localStorage.setItem(key, value)
},
// 获取localStorage
get(key) {
const value = localStorage.getItem(key)
try {
return JSON.parse(value)
} catch (e) {
return value
}
},
// 获取localStorageJSON格式
getJSON(key) {
const value = localStorage.getItem(key)
try {
return JSON.parse(value)
} catch (e) {
return null
}
},
// 移除localStorage
remove(key) {
localStorage.removeItem(key)
},
// 清空localStorage
clear() {
localStorage.clear()
}
}
}