e9c7330f31
部署到群晖 / deploy (push) Successful in 57s
后端(server/src/index.js)——两处同步幂等: 1. deleteAnniversary 删除不存在的记录时返回 success:true。 原先返回 false,被前端 sync.js:34 当作失败重新入队,那条删除会永远留在 pending_sync_queue 里每次启动重试且永远清不掉。 2. updateAnniversary / updatePerson 在记录不存在时退化为插入(upsert)。 同一类问题:本地才是主真相源,若某条记录当初的 add 没同步成功, 之后所有 update 都会失败并无限重试。 前端 —— 新增 utils/migrate.js,在 app.js onLaunch 执行: 3. 补齐老数据缺失的农历字段。早期 lunar_birthday 类型只记公历日期, 没有 lunarMonth/lunarDay,这类记录会被安全降级成按公历算——不崩,但每年日期是错的。 用它存的公历日期反算回农历补齐。 4. 为孤儿纪念日重建人员。部分纪念日的 personId 指向已不存在的人(历史上删人没级联干净), 首页按 persons 遍历所以隐形,日历页按 anniversaries 遍历会显示成「未知」。 用记录自带的 personName 重建,并沿用原 personId,纪念日无需改动。 迁移放在客户端而非后端跑 SQL:本地 wx.Storage 是主真相源,改服务端会被客户端同步覆盖。 每次启动都跑而非记版本号:函数是纯检测式的,无坏数据时零写入零请求, 还能顺带覆盖「从云端拉回坏数据」的情况。顺便把原本是死代码的 initData() 替换掉。 ⚠️ 本次后端有改动,需要重新部署;部署顺序应为先后端、后小程序。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
128 lines
3.7 KiB
JavaScript
128 lines
3.7 KiB
JavaScript
// settings.js
|
|
const storage = require('../../utils/storage')
|
|
|
|
const LAST_BACKUP_KEY = 'lastBackupAt'
|
|
|
|
function formatRelativeTime(ts) {
|
|
if (!ts) return '从未备份'
|
|
const diff = Date.now() - ts
|
|
if (diff < 60 * 1000) return '刚刚'
|
|
if (diff < 60 * 60 * 1000) return Math.floor(diff / 60000) + ' 分钟前'
|
|
if (diff < 24 * 60 * 60 * 1000) return Math.floor(diff / 3600000) + ' 小时前'
|
|
if (diff < 30 * 24 * 60 * 60 * 1000) return Math.floor(diff / 86400000) + ' 天前'
|
|
const d = new Date(ts)
|
|
return (d.getMonth() + 1) + '月' + d.getDate() + '日'
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
personsCount: 0,
|
|
anniversariesCount: 0,
|
|
lastBackupText: '从未备份',
|
|
version: 'v2.1.8'
|
|
},
|
|
|
|
onLoad() {
|
|
this.loadSummary()
|
|
},
|
|
|
|
onShow() {
|
|
this.loadSummary()
|
|
},
|
|
|
|
loadSummary() {
|
|
const persons = storage.getPersons()
|
|
const anniversaries = storage.getAnniversaries()
|
|
const lastBackupAt = wx.getStorageSync(LAST_BACKUP_KEY) || 0
|
|
|
|
this.setData({
|
|
personsCount: persons.length,
|
|
anniversariesCount: anniversaries.length,
|
|
lastBackupText: formatRelativeTime(lastBackupAt)
|
|
})
|
|
},
|
|
|
|
onExportData() {
|
|
const data = storage.exportData()
|
|
const jsonStr = JSON.stringify(data, null, 2)
|
|
|
|
wx.setClipboardData({
|
|
data: jsonStr,
|
|
success: () => {
|
|
wx.setStorageSync(LAST_BACKUP_KEY, Date.now())
|
|
this.loadSummary()
|
|
wx.showToast({
|
|
title: '数据已复制到剪贴板',
|
|
icon: 'success'
|
|
})
|
|
}
|
|
})
|
|
},
|
|
|
|
onImportData() {
|
|
wx.showModal({
|
|
title: '导入数据',
|
|
content: '将从剪贴板读取数据并导入。注意:导入会覆盖现有数据,请先备份!',
|
|
confirmText: '确认导入',
|
|
cancelText: '取消',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
wx.getClipboardData({
|
|
success: (res) => {
|
|
try {
|
|
const data = JSON.parse(res.data)
|
|
const result = storage.importData(data)
|
|
|
|
if (result.success) {
|
|
wx.showToast({ title: '导入成功', icon: 'success' })
|
|
setTimeout(() => wx.reLaunch({ url: '/pages/index/index' }), 1500)
|
|
} else {
|
|
wx.showToast({ title: result.error || '导入失败,请检查数据格式', icon: 'none' })
|
|
}
|
|
} catch (e) {
|
|
wx.showToast({ title: '数据格式错误', icon: 'none' })
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
onClearData() {
|
|
wx.showModal({
|
|
title: '确认清空',
|
|
content: '本地和云端备份都会被清空,此操作不可恢复!',
|
|
confirmText: '确认清空',
|
|
confirmColor: '#C8412F',
|
|
success: async (res) => {
|
|
if (!res.confirm) return
|
|
|
|
// 先清云端再清本地:顺序反了的话,本地清空后下次启动会从云端把数据整份拉回来
|
|
wx.showLoading({ title: '正在清空...', mask: true })
|
|
const cloud = await storage.clearCloudData()
|
|
wx.hideLoading()
|
|
|
|
if (!cloud.success) {
|
|
// 云端没清干净就不动本地,否则会出现「清了又自己回来」的诡异现象
|
|
wx.showModal({
|
|
title: '清空失败',
|
|
content: `云端数据未能清除(${cloud.error}),本地数据已保留。请检查网络后重试。`,
|
|
showCancel: false
|
|
})
|
|
return
|
|
}
|
|
|
|
if (storage.clearAllData()) {
|
|
wx.showToast({ title: '已清空', icon: 'success' })
|
|
setTimeout(() => {
|
|
wx.reLaunch({ url: '/pages/index/index' })
|
|
}, 1500)
|
|
} else {
|
|
wx.showToast({ title: '本地清空失败,请重试', icon: 'none' })
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|