diff --git a/server/routes/checkin.js b/server/routes/checkin.js index 5a28016..89139d2 100644 --- a/server/routes/checkin.js +++ b/server/routes/checkin.js @@ -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' }] }) diff --git a/src/pages/checkin/MergedView.jsx b/src/pages/checkin/MergedView.jsx index d15fa2e..2a82bd8 100644 --- a/src/pages/checkin/MergedView.jsx +++ b/src/pages/checkin/MergedView.jsx @@ -130,7 +130,14 @@ export default function MergedView({ date, onDateChange }) { } } - const summary = detail?.summary || { total: 0, done: 0 } + // 当日进度 = 当天已打卡项 / 当天打卡项. 用 dayDone 而非 completed: + // completed 是"周期目标已达成" (每周/每月项可能在本周期其他天完成), 放到 + // 历史日期的标题上会把当天之后、同周期内的打卡也算进来。按 items 现算还能 + // 让乐观更新 (打卡/撤销) 立即反映到标题, 不必等 /habits 重新拉取。 + const summary = useMemo( + () => ({ total: items.length, done: items.filter((i) => i.dayDone).length }), + [items], + ) const fireBurst = (id) => setBursts((m) => ({ ...m, [id]: (m[id] || 0) + 1 })) // ---------- 打卡 / 撤销 (乐观更新) ---------- @@ -256,7 +263,7 @@ export default function MergedView({ date, onDateChange }) { setMonth((m) => addMonths(m, -1))}>‹ {fmtMonth(month)} setMonth((m) => addMonths(m, 1))}>› - onDateChange(today)}>回到今天 + { setMonth(today.slice(0, 7)); onDateChange(today) }}>回到今天