fix: 修复循环提醒逻辑

- 后端创建时自动计算next_reminder_date
- 勾选完成时新提醒使用正确的reminder_times和next_reminder_date

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
ddshi 2026-03-03 20:27:48 +08:00
parent 1e97f0d957
commit b8392e2c20

View File

@ -153,8 +153,27 @@ router.post(
// 直接存储前端发送的原始日期字符串,不做 toISOString 转换(避免时区问题) // 直接存储前端发送的原始日期字符串,不做 toISOString 转换(避免时区问题)
const dateValue = data.date || null; const dateValue = data.date || null;
const repeatInterval = data.repeat_type === 'weekly' ? (data.repeat_interval || 1) : null; const repeatInterval = data.repeat_type === 'weekly' ? (data.repeat_interval || 1) : null;
// 如果前端传了 next_reminder_date 就使用它,否则根据 repeat_type 计算 // 如果前端传了 next_reminder_date 就使用它,否则根据 repeat_type 自动计算
const nextReminderDate = data.next_reminder_date || null; let nextReminderDate = data.next_reminder_date;
if (!nextReminderDate && data.repeat_type !== 'none' && dateValue) {
// 根据 repeat_type 计算下一个提醒日期
const date = new Date(dateValue);
switch (data.repeat_type) {
case 'daily':
date.setDate(date.getDate() + 1);
break;
case 'weekly':
date.setDate(date.getDate() + 7);
break;
case 'monthly':
date.setMonth(date.getMonth() + 1);
break;
case 'yearly':
date.setFullYear(date.getFullYear() + 1);
break;
}
nextReminderDate = date.toISOString();
}
// 存储 reminder_times 为 JSON 字符串 // 存储 reminder_times 为 JSON 字符串
// 如果有循环类型且 reminder_times 为空,自动设置为 [date] // 如果有循环类型且 reminder_times 为空,自动设置为 [date]
let reminderTimes = data.reminder_times; let reminderTimes = data.reminder_times;
@ -278,17 +297,32 @@ router.put(
// 创建新的提醒事件 // 创建新的提醒事件
const newEventId = crypto.randomUUID(); const newEventId = crypto.randomUUID();
const newDateValue = nextDate.toISOString(); const newDateValue = nextDate.toISOString();
// 新的 reminder_times 应该是原日期,用于下一次完成时再次创建
// reminder_times 在数据库中是 JSON 字符串 // 计算新的 next_reminder_date
let reminderTimesValue = currentEvent.reminder_times; const newNextDate = new Date(nextDate);
if (!reminderTimesValue) { switch (currentEvent.repeat_type) {
reminderTimesValue = JSON.stringify([currentEvent.date]); case 'daily':
newNextDate.setDate(newNextDate.getDate() + 1);
break;
case 'weekly':
newNextDate.setDate(newNextDate.getDate() + 7 * (currentEvent.repeat_interval || 1));
break;
case 'monthly':
newNextDate.setMonth(newNextDate.getMonth() + 1);
break;
case 'yearly':
newNextDate.setFullYear(newNextDate.getFullYear() + 1);
break;
} }
const newNextReminderDate = newNextDate.toISOString();
// reminder_times 存储下一个周期的日期,用于下一次完成时再次创建
const reminderTimesValue = JSON.stringify([newDateValue]);
await db.execute({ await db.execute({
sql: `INSERT INTO events (id, user_id, type, title, content, date, is_lunar, repeat_type, repeat_interval, next_reminder_date, is_holiday, is_completed, priority, reminder_times, created_at, updated_at) sql: `INSERT INTO events (id, user_id, type, title, content, date, is_lunar, repeat_type, repeat_interval, next_reminder_date, is_holiday, is_completed, priority, reminder_times, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, datetime('now'), datetime('now'))`, VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 0, ?, ?, datetime('now'), datetime('now'))`,
args: [newEventId, req.user!.userId, currentEvent.type, currentEvent.title, currentEvent.content, newDateValue, currentEvent.is_lunar, currentEvent.repeat_type, currentEvent.repeat_interval, currentEvent.next_reminder_date, currentEvent.is_holiday, currentEvent.priority, reminderTimesValue], args: [newEventId, req.user!.userId, currentEvent.type, currentEvent.title, currentEvent.content, newDateValue, currentEvent.is_lunar, currentEvent.repeat_type, currentEvent.repeat_interval, newNextReminderDate, currentEvent.is_holiday, currentEvent.priority, reminderTimesValue],
}); });
newEventCreated = true; newEventCreated = true;
console.log('[Complete Repeat] Created new reminder:', newEventId, 'date:', newDateValue); console.log('[Complete Repeat] Created new reminder:', newEventId, 'date:', newDateValue);