chore: initial import
This commit is contained in:
47
utils/messages.js
Normal file
47
utils/messages.js
Normal file
@@ -0,0 +1,47 @@
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user