原 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:
+29
-15
@@ -1,8 +1,20 @@
|
|||||||
// calendar.js
|
// calendar.js
|
||||||
const storage = require('../../utils/storage')
|
const storage = require('../../utils/storage')
|
||||||
const dateUtils = require('../../utils/date')
|
const dateUtils = require('../../utils/date')
|
||||||
|
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)
|
||||||
|
// 公历纪念日:每年同月同日,直接用录入时的 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({
|
Page({
|
||||||
data: {
|
data: {
|
||||||
currentYear: new Date().getFullYear(),
|
currentYear: new Date().getFullYear(),
|
||||||
@@ -27,8 +39,9 @@ Page({
|
|||||||
buildEventIndex(anniversaries, year, month) {
|
buildEventIndex(anniversaries, year, month) {
|
||||||
const index = {}
|
const index = {}
|
||||||
for (const a of anniversaries) {
|
for (const a of anniversaries) {
|
||||||
if (a.solarYear === year && a.solarMonth === month) {
|
const { month: m, day: d } = getAnniversaryDateInYear(a, year)
|
||||||
const key = String(a.solarDay)
|
if (m === month) {
|
||||||
|
const key = String(d)
|
||||||
if (!index[key]) index[key] = []
|
if (!index[key]) index[key] = []
|
||||||
index[key].push(a)
|
index[key].push(a)
|
||||||
}
|
}
|
||||||
@@ -99,26 +112,27 @@ Page({
|
|||||||
const { currentYear, currentMonth } = this.data
|
const { currentYear, currentMonth } = this.data
|
||||||
const persons = storage.getPersons()
|
const persons = storage.getPersons()
|
||||||
const anniversaries = storage.getAnniversaries()
|
const anniversaries = storage.getAnniversaries()
|
||||||
|
|
||||||
// 筛选本月纪念日
|
|
||||||
const monthEvents = anniversaries
|
const monthEvents = anniversaries
|
||||||
.filter(a => a.solarYear === currentYear && a.solarMonth === currentMonth)
|
|
||||||
.map(a => {
|
.map(a => {
|
||||||
const person = persons.find(p => p.id === a.personId)
|
const { month: m, day: d } = getAnniversaryDateInYear(a, currentYear)
|
||||||
const date = new Date(currentYear, currentMonth - 1, a.solarDay)
|
return { ...a, _displayMonth: m, _displayDay: d }
|
||||||
const daysUntil = dateUtils.getDaysUntil(date)
|
})
|
||||||
|
.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 {
|
return {
|
||||||
...a,
|
...x,
|
||||||
personName: person ? person.name : '未知',
|
personName: person ? person.name : '未知',
|
||||||
typeIcon: this.getTypeIcon(a.type),
|
typeIcon: this.getTypeIcon(x.type),
|
||||||
typeName: a.customTypeName || this.getTypeName(a.type),
|
typeName: x.customTypeName || this.getTypeName(x.type),
|
||||||
dateText: dateUtils.formatDate(date, 'MM月DD日'),
|
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 })
|
this.setData({ monthEvents })
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user