48 lines
1.7 KiB
JavaScript
48 lines
1.7 KiB
JavaScript
const MESSAGE_DETAIL_KEY = 'messageDetailPayload'
|
|
const MESSAGE_LIST_KEY = 'messageListSnapshot'
|
|
|
|
function normalizeLookupValue(value = '') {
|
|
return String(value || '').trim()
|
|
}
|
|
|
|
function getMessageStorageKey(baseKey, language = '') {
|
|
const normalizedLanguage = String(language || '').trim().toLowerCase() === 'en' ? 'en' : 'zh'
|
|
return `${baseKey}_${normalizedLanguage}`
|
|
}
|
|
|
|
export function saveMessageDetailPayload(payload = {}, language = '') {
|
|
uni.setStorageSync(getMessageStorageKey(MESSAGE_DETAIL_KEY, language), payload || {})
|
|
return payload || {}
|
|
}
|
|
|
|
export function getMessageDetailPayload(language = '') {
|
|
return uni.getStorageSync(getMessageStorageKey(MESSAGE_DETAIL_KEY, language)) || {}
|
|
}
|
|
|
|
export function saveMessageListSnapshot(list = [], language = '') {
|
|
const nextList = Array.isArray(list) ? list : []
|
|
uni.setStorageSync(getMessageStorageKey(MESSAGE_LIST_KEY, language), nextList)
|
|
return nextList
|
|
}
|
|
|
|
export function getMessageListSnapshot(language = '') {
|
|
const list = uni.getStorageSync(getMessageStorageKey(MESSAGE_LIST_KEY, language))
|
|
return Array.isArray(list) ? list : []
|
|
}
|
|
|
|
export function findMessageDetailPayload(id = '', code = '', language = '') {
|
|
const targetId = normalizeLookupValue(id)
|
|
const targetCode = normalizeLookupValue(code)
|
|
const list = getMessageListSnapshot(language)
|
|
|
|
return list.find(item => {
|
|
const currentId = normalizeLookupValue(item && item.id)
|
|
const currentCode = normalizeLookupValue(item && item.code)
|
|
return (targetId && currentId === targetId) || (targetCode && currentCode === targetCode)
|
|
}) || {}
|
|
}
|
|
|
|
export function clearMessageDetailPayload(language = '') {
|
|
uni.removeStorageSync(getMessageStorageKey(MESSAGE_DETAIL_KEY, language))
|
|
}
|