initial commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
import { ElMessage } from 'element-plus'
|
||||
|
||||
function notifyStorageError(action: string, key: string, error: unknown): void {
|
||||
const isQuotaExceeded = error instanceof DOMException && (
|
||||
error.name === 'QuotaExceededError' ||
|
||||
error.name === 'NS_ERROR_DOM_QUOTA_REACHED' ||
|
||||
error.code === 22 ||
|
||||
error.code === 1014
|
||||
)
|
||||
|
||||
const message = isQuotaExceeded
|
||||
? '本地存储空间不足,保存失败。建议删除部分历史会话、减少图片附件或导出备份后清理数据。'
|
||||
: `${action}失败:${key}`
|
||||
|
||||
ElMessage.error(message)
|
||||
console.error(`${action}本地存储失败: ${key}`, error)
|
||||
}
|
||||
|
||||
export function loadLocalStorage<T>(key: string, defaultValue: T): T {
|
||||
try {
|
||||
const rawValue = localStorage.getItem(key)
|
||||
return rawValue ? (JSON.parse(rawValue) as T) : defaultValue
|
||||
} catch (error) {
|
||||
notifyStorageError('读取', key, error)
|
||||
return defaultValue
|
||||
}
|
||||
}
|
||||
|
||||
export function saveLocalStorage<T>(key: string, value: T): void {
|
||||
try {
|
||||
localStorage.setItem(key, JSON.stringify(value))
|
||||
} catch (error) {
|
||||
notifyStorageError('保存', key, error)
|
||||
}
|
||||
}
|
||||
|
||||
export function removeLocalStorage(key: string): void {
|
||||
try {
|
||||
localStorage.removeItem(key)
|
||||
} catch (error) {
|
||||
notifyStorageError('删除', key, error)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user