原 buildEventIndex / loadMonthEvents 用 a.solarYear === year 判断, 只匹配纪念日原始录入年份的那一年。生日/纪念日是周年性事件,应 每年这天都出现。 - 去掉 solarYear === year 判断 - 新增 getAnniversaryDateInYear():公历直接用 solarMonth/solarDay - 农历用 lunarToSolar 重算当年公历,因农历对应公历每年浮动 - 当年无闰月时 lunarToSolar 自动忽略 isLeap 回退普通月 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+27
-13
@@ -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)
|
||||
}
|
||||
@@ -100,24 +113,25 @@ Page({
|
||||
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 })
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user