update at 2026-02-08 18:28:39
This commit is contained in:
60
miniprogram/utils/mp/storage.js
Normal file
60
miniprogram/utils/mp/storage.js
Normal file
@@ -0,0 +1,60 @@
|
||||
const STORAGE_KEYS = {
|
||||
APP_STATE: 'font2svg:app-state',
|
||||
FAVORITES: 'font2svg:favorites',
|
||||
}
|
||||
|
||||
function getStorage(key, fallbackValue) {
|
||||
try {
|
||||
const value = wx.getStorageSync(key)
|
||||
if (value === '' || value === undefined || value === null) {
|
||||
return fallbackValue
|
||||
}
|
||||
return value
|
||||
} catch (error) {
|
||||
console.warn('读取本地存储失败:', key, error)
|
||||
return fallbackValue
|
||||
}
|
||||
}
|
||||
|
||||
function setStorage(key, value) {
|
||||
try {
|
||||
wx.setStorageSync(key, value)
|
||||
} catch (error) {
|
||||
console.warn('写入本地存储失败:', key, error)
|
||||
}
|
||||
}
|
||||
|
||||
function loadAppState() {
|
||||
return getStorage(STORAGE_KEYS.APP_STATE, {})
|
||||
}
|
||||
|
||||
function saveAppState(partialState) {
|
||||
const current = loadAppState()
|
||||
const next = {
|
||||
...current,
|
||||
...partialState,
|
||||
updatedAt: Date.now(),
|
||||
}
|
||||
setStorage(STORAGE_KEYS.APP_STATE, next)
|
||||
return next
|
||||
}
|
||||
|
||||
function loadFavorites() {
|
||||
return getStorage(STORAGE_KEYS.FAVORITES, [])
|
||||
}
|
||||
|
||||
function saveFavorites(favorites) {
|
||||
const unique = Array.from(new Set(favorites))
|
||||
setStorage(STORAGE_KEYS.FAVORITES, unique)
|
||||
return unique
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
STORAGE_KEYS,
|
||||
getStorage,
|
||||
setStorage,
|
||||
loadAppState,
|
||||
saveAppState,
|
||||
loadFavorites,
|
||||
saveFavorites,
|
||||
}
|
||||
Reference in New Issue
Block a user