修复 P0 五个问题,版本号 v2.1.6 → v2.1.7
1. 删除纪念日不同步云端:storage.deleteAnniversary 只删本地,后端记录仍是
remindEnabled=1,定时任务会继续推已删除的纪念日,换设备恢复时还会被拉回来。
补上 _syncAnniversary('delete')。
2. 农历纪念日日期计算三处口径不一:date.js 新增 getOccurrenceInYear /
getNextOccurrenceOf,首页、详情页、日历页统一复用。
顺带修掉一个隐藏更深的坑——老实现把公历年当农历年传给 lunarToSolar,
农历腊月会整整错一年(农历2026腊月十五 = 公历2027-01-22,老算法给 2028-01-11)。
新实现同时试 year-1 / year 两个农历年,取真正落在该公历年内的那次。
删除已无调用方的旧 getNextOccurrence(month, day),它正是首页 bug 的来源。
3. 详情页倒计时永远显示「已过 N 天」:原先按录入年份算 daysUntil,
改为按下一次发生算;「日期」一行仍展示原始录入日期。
4. 日历页切 tab 后不刷新:渲染从 onLoad 挪到 onShow(tabBar 页面实例常驻),
不重置当前年月,保留用户翻到的月份。
5. 清空数据后重启会复活:只清本地的话 pullFromCloudIfEmpty 会把云端整份拉回来。
新增 storage.clearCloudData()(sync 传空数组),设置页改为先清云端、
成功才清本地;云端失败则中止并提示,本地数据保留。
均为小程序侧改动,server/ 未改动,无需重新部署。
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -4,15 +4,11 @@ const dateUtils = require('../../utils/date')
|
|||||||
const lunar = require('../../utils/lunar')
|
const lunar = require('../../utils/lunar')
|
||||||
const { TYPE_NAMES, TYPE_ICONS, IMPORTANCE_COLORS } = require('../../utils/constants')
|
const { TYPE_NAMES, TYPE_ICONS, IMPORTANCE_COLORS } = require('../../utils/constants')
|
||||||
|
|
||||||
// 算出某条纪念日在指定年份的公历 (month, day)
|
// 算出某条纪念日在指定公历年份的 (month, day)
|
||||||
// 公历纪念日:每年同月同日,直接用录入时的 solarMonth/solarDay
|
// 具体的公历/农历分支逻辑统一在 utils/date.js 的 getOccurrenceInYear 里,这里只做取值
|
||||||
// 农历纪念日:把农历月日转回当年公历,因为农历对应的公历日期每年都不同
|
|
||||||
function getAnniversaryDateInYear(a, year) {
|
function getAnniversaryDateInYear(a, year) {
|
||||||
if (a.isLunar && a.lunarMonth && a.lunarDay) {
|
const date = dateUtils.getOccurrenceInYear(a, year)
|
||||||
const date = lunar.lunarToSolar(year, a.lunarMonth, a.lunarDay, !!a.isLeapMonth)
|
return { month: date.getMonth() + 1, day: date.getDate() }
|
||||||
return { month: date.getMonth() + 1, day: date.getDate() }
|
|
||||||
}
|
|
||||||
return { month: a.solarMonth, day: a.solarDay }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Page({
|
Page({
|
||||||
@@ -29,6 +25,12 @@ Page({
|
|||||||
currentYear: today.getFullYear(),
|
currentYear: today.getFullYear(),
|
||||||
currentMonth: today.getMonth() + 1
|
currentMonth: today.getMonth() + 1
|
||||||
})
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
// 渲染放在 onShow:日历是 tabBar 页面,实例常驻,
|
||||||
|
// 只在 onLoad 渲染会导致新增/删除纪念日后切回来看不到变化。
|
||||||
|
// 这里不重置 currentYear/currentMonth,保留用户当前翻到的月份。
|
||||||
|
onShow() {
|
||||||
this.renderCalendar()
|
this.renderCalendar()
|
||||||
this.loadMonthEvents()
|
this.loadMonthEvents()
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -36,7 +36,8 @@ Page({
|
|||||||
if (personAnniversaries.length > 0) {
|
if (personAnniversaries.length > 0) {
|
||||||
const upcoming = personAnniversaries
|
const upcoming = personAnniversaries
|
||||||
.map(a => {
|
.map(a => {
|
||||||
const { date, daysUntil } = dateUtils.getNextOccurrence(a.solarMonth, a.solarDay)
|
// 农历纪念日的公历日期每年都不同,必须走 getNextOccurrenceOf 而不是直接用 solarMonth/solarDay
|
||||||
|
const { date, daysUntil } = dateUtils.getNextOccurrenceOf(a)
|
||||||
return { ...a, date, daysUntil }
|
return { ...a, date, daysUntil }
|
||||||
})
|
})
|
||||||
.sort((a, b) => a.daysUntil - b.daysUntil)
|
.sort((a, b) => a.daysUntil - b.daysUntil)
|
||||||
|
|||||||
@@ -51,14 +51,16 @@ Page({
|
|||||||
|
|
||||||
// 格式化纪念日数据
|
// 格式化纪念日数据
|
||||||
const formatted = anniversaries.map(a => {
|
const formatted = anniversaries.map(a => {
|
||||||
const date = new Date(a.solarYear, a.solarMonth - 1, a.solarDay)
|
// 「日期」一行展示录入时的原始日期(出生日/结婚日,本身有信息价值)
|
||||||
const daysUntil = dateUtils.getDaysUntil(date)
|
const originDate = new Date(a.solarYear, a.solarMonth - 1, a.solarDay)
|
||||||
const ld = lunar.solarToLunar(date)
|
const ld = lunar.solarToLunar(originDate)
|
||||||
|
// 倒计时必须按「下一次发生」算,原先直接用原始日期导致永远显示「已过 N 天」
|
||||||
|
const { date, daysUntil } = dateUtils.getNextOccurrenceOf(a)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...a,
|
...a,
|
||||||
date,
|
date,
|
||||||
dateText: dateUtils.formatDate(date, 'YYYY年MM月DD日'),
|
dateText: dateUtils.formatDate(originDate, 'YYYY年MM月DD日'),
|
||||||
lunarText: `农历 ${ld.year}年 ${ld.lunarText}`,
|
lunarText: `农历 ${ld.year}年 ${ld.lunarText}`,
|
||||||
daysUntil,
|
daysUntil,
|
||||||
daysUntilAbs: Math.abs(daysUntil),
|
daysUntilAbs: Math.abs(daysUntil),
|
||||||
|
|||||||
+27
-16
@@ -19,7 +19,7 @@ Page({
|
|||||||
personsCount: 0,
|
personsCount: 0,
|
||||||
anniversariesCount: 0,
|
anniversariesCount: 0,
|
||||||
lastBackupText: '从未备份',
|
lastBackupText: '从未备份',
|
||||||
version: 'v2.1.6'
|
version: 'v2.1.7'
|
||||||
},
|
},
|
||||||
|
|
||||||
onLoad() {
|
onLoad() {
|
||||||
@@ -92,23 +92,34 @@ Page({
|
|||||||
onClearData() {
|
onClearData() {
|
||||||
wx.showModal({
|
wx.showModal({
|
||||||
title: '确认清空',
|
title: '确认清空',
|
||||||
content: '确定要清空所有数据吗?此操作不可恢复!',
|
content: '本地和云端备份都会被清空,此操作不可恢复!',
|
||||||
confirmText: '确认清空',
|
confirmText: '确认清空',
|
||||||
confirmColor: '#C8412F',
|
confirmColor: '#C8412F',
|
||||||
success: (res) => {
|
success: async (res) => {
|
||||||
if (res.confirm) {
|
if (!res.confirm) return
|
||||||
const success = storage.clearAllData()
|
|
||||||
if (success) {
|
// 先清云端再清本地:顺序反了的话,本地清空后下次启动会从云端把数据整份拉回来
|
||||||
wx.showToast({
|
wx.showLoading({ title: '正在清空...', mask: true })
|
||||||
title: '已清空',
|
const cloud = await storage.clearCloudData()
|
||||||
icon: 'success'
|
wx.hideLoading()
|
||||||
})
|
|
||||||
setTimeout(() => {
|
if (!cloud.success) {
|
||||||
wx.reLaunch({
|
// 云端没清干净就不动本地,否则会出现「清了又自己回来」的诡异现象
|
||||||
url: '/pages/index/index'
|
wx.showModal({
|
||||||
})
|
title: '清空失败',
|
||||||
}, 1500)
|
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' })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
+36
-9
@@ -2,6 +2,8 @@
|
|||||||
* 日期工具函数
|
* 日期工具函数
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const lunar = require('./lunar')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化日期
|
* 格式化日期
|
||||||
* @param {Date} date - 日期对象
|
* @param {Date} date - 日期对象
|
||||||
@@ -104,18 +106,42 @@ function parseDate(dateString) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 计算指定月日的下一次发生日期(今年或明年)
|
* 算出某条纪念日在指定年份的公历日期
|
||||||
* @param {Number} month - 月份 (1-12)
|
* 公历纪念日:每年同月同日,直接用录入时的 solarMonth/solarDay
|
||||||
* @param {Number} day - 日 (1-31)
|
* 农历纪念日:把农历月日转回当年公历,因为农历对应的公历日期每年都不同
|
||||||
|
* @param {Object} anniv - 纪念日记录
|
||||||
|
* @param {Number} year - 目标公历年份
|
||||||
|
* @returns {Date}
|
||||||
|
*/
|
||||||
|
function getOccurrenceInYear(anniv, year) {
|
||||||
|
if (anniv.isLunar && anniv.lunarMonth && anniv.lunarDay) {
|
||||||
|
const isLeap = !!anniv.isLeapMonth
|
||||||
|
// 农历年和公历年不对齐:农历腊月通常落到下一个公历年(如农历2026年腊月十五 = 公历2027年1月)。
|
||||||
|
// 所以要同时试 year-1 / year 两个农历年,取真正落在公历 year 年内的那个;
|
||||||
|
// 先试 year-1 保证返回的是该公历年内最早的一次。
|
||||||
|
for (const lunarYear of [year - 1, year]) {
|
||||||
|
const d = lunar.lunarToSolar(lunarYear, anniv.lunarMonth, anniv.lunarDay, isLeap)
|
||||||
|
if (d.getFullYear() === year) return d
|
||||||
|
}
|
||||||
|
// 兜底(理论上不会走到,农历年跨度必然覆盖整个公历年)
|
||||||
|
return lunar.lunarToSolar(year, anniv.lunarMonth, anniv.lunarDay, isLeap)
|
||||||
|
}
|
||||||
|
return new Date(year, anniv.solarMonth - 1, anniv.solarDay)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 算出某条纪念日的下一次发生(今年或明年),农历/公历都适用
|
||||||
|
* Why:首页、详情页、日历页原先各算一套,农历纪念日只有日历页算对了。
|
||||||
|
* 统一走这里,避免三处口径不一致。
|
||||||
|
* @param {Object} anniv - 纪念日记录
|
||||||
* @returns {{ date: Date, daysUntil: Number }}
|
* @returns {{ date: Date, daysUntil: Number }}
|
||||||
*/
|
*/
|
||||||
function getNextOccurrence(month, day) {
|
function getNextOccurrenceOf(anniv) {
|
||||||
const today = new Date()
|
const currentYear = new Date().getFullYear()
|
||||||
const currentYear = today.getFullYear()
|
let date = getOccurrenceInYear(anniv, currentYear)
|
||||||
let date = new Date(currentYear, month - 1, day)
|
|
||||||
let daysUntil = getDaysUntil(date)
|
let daysUntil = getDaysUntil(date)
|
||||||
if (daysUntil < 0) {
|
if (daysUntil < 0) {
|
||||||
date = new Date(currentYear + 1, month - 1, day)
|
date = getOccurrenceInYear(anniv, currentYear + 1)
|
||||||
daysUntil = getDaysUntil(date)
|
daysUntil = getDaysUntil(date)
|
||||||
}
|
}
|
||||||
return { date, daysUntil }
|
return { date, daysUntil }
|
||||||
@@ -124,7 +150,8 @@ function getNextOccurrence(month, day) {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
formatDate,
|
formatDate,
|
||||||
getDaysUntil,
|
getDaysUntil,
|
||||||
getNextOccurrence,
|
getOccurrenceInYear,
|
||||||
|
getNextOccurrenceOf,
|
||||||
isToday,
|
isToday,
|
||||||
isPast,
|
isPast,
|
||||||
isUpcoming,
|
isUpcoming,
|
||||||
|
|||||||
+44
-1
@@ -12,6 +12,15 @@ function _syncPerson(action, data) {
|
|||||||
sync.syncOrEnqueue({ kind: 'person', action, data })
|
sync.syncOrEnqueue({ kind: 'person', action, data })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 异步同步纪念日到后端;失败自动入队,启动时 flush
|
||||||
|
// 注:add/update 的同步在 pages/add-anniversary 页面里做(那里才拿得到完整的表单数据),
|
||||||
|
// 这里只负责 delete,避免同一条操作被同步两次。
|
||||||
|
function _syncAnniversary(action, data) {
|
||||||
|
const openid = wx.getStorageSync('openid')
|
||||||
|
if (!openid) return
|
||||||
|
sync.syncOrEnqueue({ kind: 'anniversary', action, data })
|
||||||
|
}
|
||||||
|
|
||||||
// 内存缓存
|
// 内存缓存
|
||||||
const _cache = {
|
const _cache = {
|
||||||
persons: null,
|
persons: null,
|
||||||
@@ -180,7 +189,11 @@ function updateAnniversary(id, updates) {
|
|||||||
*/
|
*/
|
||||||
function deleteAnniversary(id) {
|
function deleteAnniversary(id) {
|
||||||
const anniversaries = getAnniversaries()
|
const anniversaries = getAnniversaries()
|
||||||
return saveAnniversaries(anniversaries.filter(a => a.id !== id)).success
|
const ok = saveAnniversaries(anniversaries.filter(a => a.id !== id)).success
|
||||||
|
// 必须同步删除云端,否则后端那条记录仍是 remindEnabled=1,定时任务会继续推送已删除的纪念日,
|
||||||
|
// 且换设备恢复时它会被重新拉回本地
|
||||||
|
if (ok) _syncAnniversary('delete', { id })
|
||||||
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -295,8 +308,37 @@ async function pullFromCloudIfEmpty() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清空云端数据(用空数组走 sync,后端会先 DELETE 该 openid 的全部记录)
|
||||||
|
* Why:只清本地的话,下次启动 pullFromCloudIfEmpty() 会把云端数据整份拉回来,
|
||||||
|
* 用户以为清空了其实没清。所以清本地之前必须先把云端清掉。
|
||||||
|
* @returns {Promise<{success: boolean, error?: string}>}
|
||||||
|
*/
|
||||||
|
async function clearCloudData() {
|
||||||
|
const openid = wx.getStorageSync('openid')
|
||||||
|
// 从未登录过 → 云端本来就没有这个用户的数据,直接算成功
|
||||||
|
if (!openid) return { success: true }
|
||||||
|
try {
|
||||||
|
const [personRes, annivRes] = await Promise.all([
|
||||||
|
api.person('sync', []),
|
||||||
|
api.anniversary('sync', [])
|
||||||
|
])
|
||||||
|
if (!personRes || personRes.success === false) {
|
||||||
|
return { success: false, error: (personRes && personRes.error) || '清空云端人员失败' }
|
||||||
|
}
|
||||||
|
if (!annivRes || annivRes.success === false) {
|
||||||
|
return { success: false, error: (annivRes && annivRes.error) || '清空云端纪念日失败' }
|
||||||
|
}
|
||||||
|
return { success: true }
|
||||||
|
} catch (e) {
|
||||||
|
console.error('[clearCloudData] 失败', e)
|
||||||
|
return { success: false, error: e.message || '网络异常' }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清空所有数据
|
* 清空所有数据
|
||||||
|
* 注:会连 openid 和待同步队列一起清掉,下次启动会重新 login 拿 openid
|
||||||
*/
|
*/
|
||||||
function clearAllData() {
|
function clearAllData() {
|
||||||
try {
|
try {
|
||||||
@@ -332,5 +374,6 @@ module.exports = {
|
|||||||
exportData,
|
exportData,
|
||||||
importData,
|
importData,
|
||||||
clearAllData,
|
clearAllData,
|
||||||
|
clearCloudData,
|
||||||
pullFromCloudIfEmpty
|
pullFromCloudIfEmpty
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user