- date.js: addMonths 对 "YYYY-MM" 月粒度保持月粒度, 不再生成 "YYYY-MM-01"。 旧逻辑使日历切上月时请求 month=2026-10-01, 服务端判为非法后静默回退本月, 表现为上个月一片空白。 - MergedView: 「回到今天」同时把月份切回当前月; 日标题「已完成 x/y」改为 按 items 的 dayDone 现算, 避免把同周期后续日期的打卡计入历史日期, 并让打卡/撤销的乐观更新立即反映到标题。 - server /calendar: 兼容旧的 month=YYYY-MM-DD 请求并截回月份, 不再静默回退。 - server /habits: 回看历史日期时过滤当天尚未创建的打卡项, 与 /calendar 口径一致; summary.done 改用 dayDone (当天份额) 而非 completed (周期完成度)。 验证: Vite dev + 生产数据 (admin) 2026-10 显示 10 个环 / 完成率 32%, 10-22 的标题由错误的 3/14 修正为 1/14; 「回到今天」回到 2026-11 (14%)。
This commit is contained in:
@@ -122,11 +122,15 @@ router.get('/habits', async (req, res, next) => {
|
||||
const userId = req.user.sub
|
||||
const today = toDateStr()
|
||||
const date = normDate(req.query.date)
|
||||
const habits = await loadActiveHabits(userId)
|
||||
const habits = (await loadActiveHabits(userId))
|
||||
// 回看历史日期时, 当天还不存在的打卡项不应出现 (与 /calendar 口径一致)
|
||||
.filter((h) => toDateStr(h.createdAt) <= date)
|
||||
const ranges = habits.map((h) => periodRange(h.period, date))
|
||||
const grouped = await loadRecords(userId, habits, ranges.length ? ranges : [{ start: date, end: date }])
|
||||
const items = habits.map((h) => buildItem(h, grouped.get(h.id) || new Map(), date, today))
|
||||
const doneCount = items.filter((i) => i.completed).length
|
||||
// summary 是"当天进度": 按当日份额 (countOnDate >= expectedPerDay) 统计,
|
||||
// 不能用 completed (周期完成度), 否则历史日期会把同周期后续日期的打卡算进来。
|
||||
const doneCount = items.filter((i) => i.dayDone).length
|
||||
res.json({ today, date, items, summary: { total: items.length, done: doneCount } })
|
||||
} catch (e) { next(e) }
|
||||
})
|
||||
@@ -291,7 +295,14 @@ router.get('/calendar', async (req, res, next) => {
|
||||
try {
|
||||
const userId = req.user.sub
|
||||
const today = toDateStr()
|
||||
const month = MONTH_RE.test(String(req.query.month || '')) ? String(req.query.month) : today.slice(0, 7)
|
||||
// 兼容旧版客户端: addMonths 曾把 "YYYY-MM" 算成 "YYYY-MM-01", 请求月份带了日号,
|
||||
// 旧逻辑判为非法后静默回退到本月, 导致切到上月时看不到任何记录。这里截回月份。
|
||||
const rawMonth = String(req.query.month || '')
|
||||
const month = MONTH_RE.test(rawMonth)
|
||||
? rawMonth
|
||||
: DATE_RE.test(rawMonth)
|
||||
? rawMonth.slice(0, 7)
|
||||
: today.slice(0, 7)
|
||||
const start = month + '-01'
|
||||
const end = addDays(monthStartNext(month), -1)
|
||||
const habits = await prisma.habit.findMany({ where: { userId }, orderBy: [{ sortOrder: 'asc' }, { id: 'asc' }] })
|
||||
|
||||
Reference in New Issue
Block a user