From 030e5874802f918eb64ba2698c506efbb83183ca Mon Sep 17 00:00:00 2001 From: yuming Date: Tue, 2 Jun 2026 13:35:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A5=E5=8E=86=E8=B7=A8?= =?UTF-8?q?=E5=B9=B4=E4=B8=8D=E6=98=BE=E7=A4=BA=E7=BA=AA=E5=BF=B5=E6=97=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原 buildEventIndex / loadMonthEvents 用 a.solarYear === year 判断, 只匹配纪念日原始录入年份的那一年。生日/纪念日是周年性事件,应 每年这天都出现。 - 去掉 solarYear === year 判断 - 新增 getAnniversaryDateInYear():公历直接用 solarMonth/solarDay - 农历用 lunarToSolar 重算当年公历,因农历对应公历每年浮动 - 当年无闰月时 lunarToSolar 自动忽略 isLeap 回退普通月 Co-Authored-By: Claude Opus 4.7 (1M context) --- pages/calendar/calendar.js | 44 +++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/pages/calendar/calendar.js b/pages/calendar/calendar.js index a3dc04e..a556266 100644 --- a/pages/calendar/calendar.js +++ b/pages/calendar/calendar.js @@ -1,8 +1,20 @@ // calendar.js const storage = require('../../utils/storage') const dateUtils = require('../../utils/date') +const lunar = require('../../utils/lunar') const { TYPE_NAMES, TYPE_ICONS, IMPORTANCE_COLORS } = require('../../utils/constants') +// 算出某条纪念日在指定年份的公历 (month, day) +// 公历纪念日:每年同月同日,直接用录入时的 solarMonth/solarDay +// 农历纪念日:把农历月日转回当年公历,因为农历对应的公历日期每年都不同 +function getAnniversaryDateInYear(a, year) { + if (a.isLunar && a.lunarMonth && a.lunarDay) { + const date = lunar.lunarToSolar(year, a.lunarMonth, a.lunarDay, !!a.isLeapMonth) + return { month: date.getMonth() + 1, day: date.getDate() } + } + return { month: a.solarMonth, day: a.solarDay } +} + Page({ data: { currentYear: new Date().getFullYear(), @@ -27,8 +39,9 @@ Page({ buildEventIndex(anniversaries, year, month) { const index = {} for (const a of anniversaries) { - if (a.solarYear === year && a.solarMonth === month) { - const key = String(a.solarDay) + const { month: m, day: d } = getAnniversaryDateInYear(a, year) + if (m === month) { + const key = String(d) if (!index[key]) index[key] = [] index[key].push(a) } @@ -99,26 +112,27 @@ Page({ const { currentYear, currentMonth } = this.data const persons = storage.getPersons() const anniversaries = storage.getAnniversaries() - - // 筛选本月纪念日 + const monthEvents = anniversaries - .filter(a => a.solarYear === currentYear && a.solarMonth === currentMonth) .map(a => { - const person = persons.find(p => p.id === a.personId) - const date = new Date(currentYear, currentMonth - 1, a.solarDay) - const daysUntil = dateUtils.getDaysUntil(date) - + const { month: m, day: d } = getAnniversaryDateInYear(a, currentYear) + return { ...a, _displayMonth: m, _displayDay: d } + }) + .filter(x => x._displayMonth === currentMonth) + .map(x => { + const person = persons.find(p => p.id === x.personId) + const date = new Date(currentYear, x._displayMonth - 1, x._displayDay) return { - ...a, + ...x, personName: person ? person.name : '未知', - typeIcon: this.getTypeIcon(a.type), - typeName: a.customTypeName || this.getTypeName(a.type), + typeIcon: this.getTypeIcon(x.type), + typeName: x.customTypeName || this.getTypeName(x.type), dateText: dateUtils.formatDate(date, 'MM月DD日'), - daysUntil + daysUntil: dateUtils.getDaysUntil(date) } }) - .sort((a, b) => a.solarDay - b.solarDay) - + .sort((a, b) => a._displayDay - b._displayDay) + this.setData({ monthEvents }) },