90 lines
3.2 KiB
JavaScript
90 lines
3.2 KiB
JavaScript
const CURRENT_GENEALOGY_KEY = 'jiapu_current_genealogy_id'
|
|
const CURRENT_GENEALOGY_INVALIDATED_KEY = 'jiapu_current_genealogy_invalidated'
|
|
|
|
const normalizeGenealogyId = (genealogyId) => {
|
|
if (typeof genealogyId !== 'string') {
|
|
throw new TypeError('当前家谱 ID 必须是词法字符串')
|
|
}
|
|
if (!genealogyId || genealogyId.trim() !== genealogyId || /[\u0000-\u001f\u007f]/.test(genealogyId)) {
|
|
throw new TypeError('当前家谱 ID 必须是无边界空白的非空字符串')
|
|
}
|
|
return genealogyId
|
|
}
|
|
|
|
const markCurrentGenealogyInvalidated = () => {
|
|
uni.removeStorageSync(CURRENT_GENEALOGY_KEY)
|
|
uni.setStorageSync(CURRENT_GENEALOGY_INVALIDATED_KEY, '1')
|
|
}
|
|
|
|
const readCurrentGenealogyInvalidated = () => {
|
|
const stored = uni.getStorageSync(CURRENT_GENEALOGY_INVALIDATED_KEY)
|
|
if (stored === '' || stored === null || stored === undefined) return false
|
|
if (stored !== '1') uni.setStorageSync(CURRENT_GENEALOGY_INVALIDATED_KEY, '1')
|
|
return true
|
|
}
|
|
|
|
const readCurrentGenealogyId = () => {
|
|
const stored = uni.getStorageSync(CURRENT_GENEALOGY_KEY)
|
|
if (stored === '' || stored === null || stored === undefined) return ''
|
|
try {
|
|
return normalizeGenealogyId(stored)
|
|
} catch {
|
|
// 旧版本或损坏的存储值不应继续流入路由和权限判断。发现异常时只清理
|
|
// 这一唯一键;下一次取得可用家谱列表后再按确定顺序建立新上下文。
|
|
markCurrentGenealogyInvalidated()
|
|
return ''
|
|
}
|
|
}
|
|
|
|
export const genealogyContext = {
|
|
getCurrentGenealogyId: readCurrentGenealogyId,
|
|
setCurrentGenealogyId: (genealogyId) => {
|
|
const normalizedId = normalizeGenealogyId(genealogyId)
|
|
uni.setStorageSync(CURRENT_GENEALOGY_KEY, normalizedId)
|
|
uni.removeStorageSync(CURRENT_GENEALOGY_INVALIDATED_KEY)
|
|
return normalizedId
|
|
},
|
|
invalidateCurrentGenealogyId: () => {
|
|
markCurrentGenealogyInvalidated()
|
|
return ''
|
|
},
|
|
isCurrentGenealogyInvalidated: readCurrentGenealogyInvalidated,
|
|
clearCurrentGenealogyId: () => {
|
|
uni.removeStorageSync(CURRENT_GENEALOGY_KEY)
|
|
uni.removeStorageSync(CURRENT_GENEALOGY_INVALIDATED_KEY)
|
|
},
|
|
reconcileCurrentGenealogyId: (availableIds, preferredId = '') => {
|
|
if (!Array.isArray(availableIds)) {
|
|
throw new TypeError('可用家谱 ID 必须是数组')
|
|
}
|
|
const normalizedIds = availableIds.map(normalizeGenealogyId)
|
|
if (new Set(normalizedIds).size !== normalizedIds.length) {
|
|
throw new TypeError('可用家谱列表包含重复 ID')
|
|
}
|
|
const normalizedPreferredId = preferredId === ''
|
|
? ''
|
|
: normalizeGenealogyId(preferredId)
|
|
const storedId = readCurrentGenealogyId()
|
|
if (normalizedPreferredId) {
|
|
if (!normalizedIds.includes(normalizedPreferredId)) {
|
|
markCurrentGenealogyInvalidated()
|
|
return ''
|
|
}
|
|
return genealogyContext.setCurrentGenealogyId(normalizedPreferredId)
|
|
}
|
|
if (storedId) {
|
|
if (!normalizedIds.includes(storedId)) {
|
|
markCurrentGenealogyInvalidated()
|
|
return ''
|
|
}
|
|
return genealogyContext.setCurrentGenealogyId(storedId)
|
|
}
|
|
if (readCurrentGenealogyInvalidated()) return ''
|
|
if (!normalizedIds.length) {
|
|
uni.removeStorageSync(CURRENT_GENEALOGY_KEY)
|
|
return ''
|
|
}
|
|
return genealogyContext.setCurrentGenealogyId(normalizedIds[0])
|
|
}
|
|
}
|