- Phase 1: 添加纪念日合并人物创建流程(方案 B) - Phase 2: 农历提醒按 lunarMonth/Day 计算每年公历 - Phase 3: 人员数据同步到后端(新增 /api/person) - Phase 4: 新设备启动从云端恢复数据 - Phase 5: 工具函数收敛 utils/format.js - Phase 6: 同步失败入队 + 启动重试 - Phase 7: 闰月生日完整支持(含 isLeapMonth + UI 警示) - 修复 lunarInfo 数据表错位(替换为权威源 jjonline/calendar.js) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+68
-9
@@ -1,7 +1,17 @@
|
||||
/**
|
||||
* 本地存储管理工具(含缓存层)
|
||||
* 本地存储管理工具(含缓存层 + 云端 fire-and-forget 同步)
|
||||
*/
|
||||
|
||||
const api = require('./api')
|
||||
const sync = require('./sync')
|
||||
|
||||
// 异步同步人员到后端;失败自动入队,启动时 flush
|
||||
function _syncPerson(action, data) {
|
||||
const openid = wx.getStorageSync('openid')
|
||||
if (!openid) return
|
||||
sync.syncOrEnqueue({ kind: 'person', action, data })
|
||||
}
|
||||
|
||||
// 内存缓存
|
||||
const _cache = {
|
||||
persons: null,
|
||||
@@ -61,7 +71,9 @@ function addPerson(person) {
|
||||
}
|
||||
persons.push(newPerson)
|
||||
const result = savePersons(persons)
|
||||
return result.success ? newPerson : null
|
||||
if (!result.success) return null
|
||||
_syncPerson('add', newPerson)
|
||||
return newPerson
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -70,11 +82,23 @@ function addPerson(person) {
|
||||
function updatePerson(id, updates) {
|
||||
const persons = getPersons()
|
||||
const index = persons.findIndex(p => p.id === id)
|
||||
if (index !== -1) {
|
||||
persons[index] = { ...persons[index], ...updates, updateTime: new Date().getTime() }
|
||||
return savePersons(persons).success
|
||||
}
|
||||
return false
|
||||
if (index === -1) return false
|
||||
persons[index] = { ...persons[index], ...updates, updateTime: new Date().getTime() }
|
||||
const ok = savePersons(persons).success
|
||||
if (ok) _syncPerson('update', persons[index])
|
||||
return ok
|
||||
}
|
||||
|
||||
/**
|
||||
* 按姓名查找已有人员;找不到则创建一个新的 person(仅 name),返回 person 对象
|
||||
* Why:方案 B 流程合并——用户在「添加纪念日」页直接输入姓名,不再强制先建人
|
||||
*/
|
||||
function ensurePerson(name) {
|
||||
const trimmed = (name || '').trim()
|
||||
if (!trimmed) return null
|
||||
const existing = getPersons().find(p => p.name === trimmed)
|
||||
if (existing) return existing
|
||||
return addPerson({ name: trimmed })
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,7 +106,9 @@ function updatePerson(id, updates) {
|
||||
*/
|
||||
function deletePerson(id) {
|
||||
const persons = getPersons()
|
||||
return savePersons(persons.filter(p => p.id !== id)).success
|
||||
const ok = savePersons(persons.filter(p => p.id !== id)).success
|
||||
if (ok) _syncPerson('delete', { id })
|
||||
return ok
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -168,6 +194,8 @@ function deletePersonWithAnniversaries(personId) {
|
||||
wx.setStorageSync('anniversaries', anniversaries)
|
||||
_cache.persons = persons
|
||||
_cache.anniversaries = anniversaries
|
||||
// 后端 delete person 会级联删除该 personId 的所有 anniversaries(在事务里),无需单独再调
|
||||
_syncPerson('delete', { id: personId })
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('删除人员及纪念日失败', e)
|
||||
@@ -238,6 +266,35 @@ function importData(data) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅当本地为空时,从后端拉取所有人员和纪念日落地到本地
|
||||
* Why:换设备/重装时,云端是唯一真相源,需要把数据拉回来恢复
|
||||
*/
|
||||
async function pullFromCloudIfEmpty() {
|
||||
const hasLocal = getPersons().length > 0 || getAnniversaries().length > 0
|
||||
if (hasLocal) return false
|
||||
const openid = wx.getStorageSync('openid')
|
||||
if (!openid) return false
|
||||
try {
|
||||
const [personsRes, annivRes] = await Promise.all([
|
||||
api.person('get'),
|
||||
api.anniversary('get')
|
||||
])
|
||||
if (personsRes && personsRes.success && Array.isArray(personsRes.data) && personsRes.data.length > 0) {
|
||||
wx.setStorageSync('persons', personsRes.data)
|
||||
_cache.persons = personsRes.data
|
||||
}
|
||||
if (annivRes && annivRes.success && Array.isArray(annivRes.data) && annivRes.data.length > 0) {
|
||||
wx.setStorageSync('anniversaries', annivRes.data)
|
||||
_cache.anniversaries = annivRes.data
|
||||
}
|
||||
return true
|
||||
} catch (e) {
|
||||
console.error('[pullFromCloud] 失败', e)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有数据
|
||||
*/
|
||||
@@ -258,6 +315,7 @@ module.exports = {
|
||||
getPersons,
|
||||
getPersonById,
|
||||
addPerson,
|
||||
ensurePerson,
|
||||
updatePerson,
|
||||
deletePerson,
|
||||
deletePersonWithAnniversaries,
|
||||
@@ -273,5 +331,6 @@ module.exports = {
|
||||
// 工具函数
|
||||
exportData,
|
||||
importData,
|
||||
clearAllData
|
||||
clearAllData,
|
||||
pullFromCloudIfEmpty
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user