Skip to content

Commit c8475ad

Browse files
authored
Merge pull request ZToolsCenter#675 from LowProfile666/plugin/easyreminder
Update plugin 定时提醒 v1.3.2
2 parents 769b119 + 6f74105 commit c8475ad

15 files changed

Lines changed: 98 additions & 869 deletions

File tree

‎plugins/easyreminder/CHANGELOG.md‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
# Changelog
22

3+
## 1.3.2 - 2026-10-19
4+
5+
### 修复
6+
7+
- 修复同一计划时刻触发两次、重复弹提醒的问题:日志里同一任务出现 `16:30:00.020(偏差 +14ms)` 与 `16:29:50.813(偏差 -9193ms)` 两条 `[触发]` 记录。根因是系统时钟被回拨(NTP 校时、休眠唤醒后的时间补偿)时,定时器会按「修正后的挂钟时间」提前唤醒 —— 先触发一次,重排时 `Date.now()` 还差 9 秒才到计划时刻,于是把**同一个**计划时刻又算了一遍,9 秒后第二次触发。现在两处补上防护:
8+
- `triggerDue` 先校验是否真的到点:实际时刻比计划时刻早超过 1s 就不触发,记一条 `[自愈] 定时器提前唤醒,重新等待`,并按剩余时间重新排程(与既有的「错过补发」逻辑对称)
9+
- 每条提醒新增 `lastFiredSlot` 字段记录「最近一次已触发的计划时刻」,同一时刻再次进入触发流程只记一条 `[去重]` 日志、不再重复发通知,跨时钟跳变、多个插件视图各跑一份调度器时同样生效
10+
- 新建/编辑提醒时重置 `lastFiredSlot`,改了调度参数不会被旧的触发时刻挡住
11+
- 修复编辑「一次性提醒」时初始日期会错一天:初始值原先用 `toISOString()`(UTC)换算,本地时间 00:00–07:59 的提醒会显示成前一天;改为按本地时区取年-月-日与时分
12+
13+
### 变更
14+
15+
- 系统日志时间戳由 `16:30:00.020` 改为 `2026-10-19 16:30:00.020`,跨天排查(如次日补发前一天错过的触发)不再分不清日期
16+
17+
### 优化
18+
19+
- 清理死代码(均为模板遗留或历史重构残留,不影响行为):
20+
- preload 中从未被调用的 `readFile` / `writeTextFile` / `writeImageFile` / `notify` / `sendNotification` 五个接口,连同随之无用的 `fs` / `path` 依赖与前端类型声明一起删除
21+
- `useReminders` 中未被使用的 `reminders` / `enabledReminders` 导出
22+
- OnceForm / DailyForm / IntervalForm 三个表单子组件里没有监听方的 `defineEmits(update)`(结果本就由父组件调用 `getData()` 获取)
23+
- 提醒页未被使用的 `enterAction` 属性(App.vue 中对应的 ref 与模板绑定一并删除)
24+
- `OnceForm.getData()` 中父组件不消费的 `date` / `time` 字段,以及 `ReminderForm` 中只写不读的 `form.triggerAt`
25+
326
## 1.3.1 - 2026-10-16
427

528
### 修复

‎plugins/easyreminder/README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@
66

77
## 更新日志
88

9+
### v1.3.2 (2026-10-19)
10+
11+
**修复**
12+
- 修复同一计划时刻触发两次、重复弹提醒的问题(时钟被回拨时定时器提前唤醒,先触发一次、重排后又对同一个计划时刻触发一次):触发前先校验是否真的到点,早于计划时刻超过 1s 就重新等待;并按 `lastFiredSlot` 对同一计划时刻去重,跨时钟跳变或多份调度器都只提醒一次
13+
- 修复编辑一次性提醒时初始日期会错一天(原先用 `toISOString()` 按 UTC 换算,本地 00:00–07:59 的提醒显示成前一天)
14+
- 系统日志时间戳改为 `2026-10-19 16:30:00.020`(原来只有时分秒),跨天排查不再分不清日期
15+
16+
**优化**
17+
- 清理死代码:preload 里未使用的 `readFile`/`writeTextFile`/`writeImageFile`/`notify`/`sendNotification` 及 `fs`/`path` 依赖、`useReminders` 未使用的导出、三个表单组件里没有监听方的 `defineEmits`、提醒页未被使用的 `enterAction` 属性、`OnceForm.getData()` 多余字段
18+
919
### v1.3.1 (2026-10-16)
1020

1121
**修复**

‎plugins/easyreminder/public/plugin.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"title": "定时提醒",
55
"description": "支持间隔提醒、每日定点提醒、一次性定时提醒,后台稳定触发",
66
"author": "咖啡八杯",
7-
"version": "1.3.1",
7+
"version": "1.3.2",
88
"categories": ["productivity"],
99
"main": "index.html",
1010
"preload": "preload/services.js",

‎plugins/easyreminder/public/preload/services.js‎

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
const fs = require('node:fs')
2-
const path = require('node:path')
3-
const https = require('node:https')
1+
const https = require('node:https')
42

53
const STORAGE_KEY = 'easyremind_reminders'
64
const LOG_KEY = 'easyremind_logs'
@@ -16,6 +14,7 @@ const INIT_FAST_TRIES = 40 // 快速重试次数(300ms × 40 ≈ 12s),之
1614
const WATCHDOG_INTERVAL_MS = 30 * 1000 // 看门狗巡检间隔
1715
const MISSED_FIRE_GRACE_MS = 5 * 60 * 1000 // 错过的触发在此时间内仍补发,超过则只重排不补发
1816
const MIN_REARM_DELAY_MS = 1000 // 触发后重排的最小延迟:数据异常(如状态写不进去)时不会 0 延迟自我循环
17+
const TRIGGER_MATCH_WINDOW_MS = 1000 // 触发命中窗口:实际时刻与计划时刻相差在此范围内即视为「到点」
1918
const ERROR_BACKOFF_MS = 30 * 1000 // 触发处理抛错后的退避间隔
2019

2120
// ==================== 工具函数 ====================
@@ -27,6 +26,15 @@ function generateId() {
2726
return Date.now().toString(36) + Math.random().toString(36).slice(2, 8)
2827
}
2928

29+
// 日志时间戳:年月日 + 时分秒.毫秒
30+
function formatTimestamp(d) {
31+
return (
32+
d.getFullYear() + '-' + pad2(d.getMonth() + 1) + '-' + pad2(d.getDate()) + ' ' +
33+
pad2(d.getHours()) + ':' + pad2(d.getMinutes()) + ':' + pad2(d.getSeconds()) + '.' +
34+
String(d.getMilliseconds()).padStart(3, '0')
35+
)
36+
}
37+
3038
// ==================== 日志 ====================
3139
let logId = 1
3240

@@ -50,8 +58,7 @@ function writeLogs(logs) {
5058
function addLog(message, detail) {
5159
try {
5260
const logs = readLogs()
53-
const now = new Date()
54-
const time = pad2(now.getHours()) + ':' + pad2(now.getMinutes()) + ':' + pad2(now.getSeconds()) + '.' + String(now.getMilliseconds()).padStart(3, '0')
61+
const time = formatTimestamp(new Date())
5562
logs.unshift({ id: logId++, time, message, detail: detail || {} })
5663
if (logs.length > 100) logs.length = 100
5764
writeLogs(logs)
@@ -429,12 +436,25 @@ function scheduleNext(minDelay) {
429436
function triggerDue() {
430437
if (scheduledTime === null) return
431438
const target = scheduledTime
439+
const now = Date.now()
440+
441+
// 定时器提前唤醒(系统时钟被回拨、休眠唤醒后的时间补偿)不能当作「到点」:
442+
// 直接跳过这一轮,调用方(定时器回调的 finally)会按剩余时间重新等待。
443+
// 否则会在同一计划时刻先触发一次、重排后再触发一次,变成重复提醒
444+
if (now < target - TRIGGER_MATCH_WINDOW_MS) {
445+
addLog('[自愈] 定时器提前唤醒,重新等待 · ' + formatTime(target) + '(早 ' + Math.round((target - now) / 1000) + 's)', {
446+
scheduledTime: formatTime(target),
447+
earlyByMs: target - now
448+
})
449+
return
450+
}
451+
432452
const reminders = readReminders()
433453

434454
for (const r of reminders) {
435455
if (!r.enabled) continue
436456
const t = getNextTriggerTime(r, target)
437-
if (t !== null && Math.abs(t - target) < 1000) {
457+
if (t !== null && Math.abs(t - target) < TRIGGER_MATCH_WINDOW_MS) {
438458
// 单条提醒出错不影响同一时刻的其他提醒
439459
try {
440460
doTrigger(r, target)
@@ -455,6 +475,16 @@ function doTrigger(reminder, scheduled) {
455475
const target = reminders.find(r => r.id === reminder.id)
456476
if (!target) return
457477

478+
// 同一计划时刻只触发一次:时钟回拨、定时器提前唤醒、多个插件视图各跑一份调度器,
479+
// 都可能让同一时刻再次进到这里。命中 lastFiredSlot 就跳过通知
480+
if (target.lastFiredSlot === scheduled) {
481+
addLog('[去重] 「' + target.title + '」已触发过该时刻,跳过重复通知 · ' + formatTime(scheduled), {
482+
id: target.id,
483+
scheduledTime: formatTime(scheduled)
484+
})
485+
return
486+
}
487+
458488
if (target.type === 'once') {
459489
if (firedOnceIds.has(target.id)) {
460490
// 已通知过,不再重复发;但仍要重试把「已触发」状态落库,
@@ -474,6 +504,7 @@ function doTrigger(reminder, scheduled) {
474504
target.enabled = false
475505
}
476506
target.lastTriggered = now
507+
target.lastFiredSlot = scheduled
477508
try {
478509
writeReminders(reminders)
479510
} catch (e) {
@@ -622,7 +653,7 @@ function watchdogTick() {
622653
return
623654
}
624655
const next = calcNextTick(list)
625-
const mismatch = next === null ? scheduledTime !== null : scheduledTime === null || Math.abs(next - scheduledTime) > 1000
656+
const mismatch = next === null ? scheduledTime !== null : scheduledTime === null || Math.abs(next - scheduledTime) > TRIGGER_MATCH_WINDOW_MS
626657
if (mismatch) scheduleNext()
627658

628659
checkHolidayData(now)
@@ -642,50 +673,6 @@ function checkHolidayData(now) {
642673

643674
// ==================== 暴露服务 ====================
644675
window.services = {
645-
// 读文件
646-
readFile(file) {
647-
try {
648-
return fs.readFileSync(file, { encoding: 'utf-8' })
649-
} catch {
650-
return ''
651-
}
652-
},
653-
// 文本写入到下载目录
654-
writeTextFile(text) {
655-
const filePath = path.join(window.ztools.getPath('downloads'), Date.now().toString() + '.txt')
656-
fs.writeFileSync(filePath, text, { encoding: 'utf-8' })
657-
return filePath
658-
},
659-
// 图片写入到下载目录
660-
writeImageFile(base64Url) {
661-
const matches = /^data:image\/([a-z]{1,20});base64,/i.exec(base64Url)
662-
if (!matches) return
663-
const filePath = path.join(
664-
window.ztools.getPath('downloads'),
665-
Date.now().toString() + '.' + matches[1]
666-
)
667-
fs.writeFileSync(filePath, base64Url.substring(matches[0].length), { encoding: 'base64' })
668-
return filePath
669-
},
670-
// 自定义标题的通知
671-
notify(title, body) {
672-
if (typeof Notification === 'undefined') return
673-
if (Notification.permission === 'granted') {
674-
new Notification(title, { body })
675-
} else if (Notification.permission !== 'denied') {
676-
Notification.requestPermission().then(perm => {
677-
if (perm === 'granted') new Notification(title, { body })
678-
})
679-
}
680-
},
681-
sendNotification(title, body) {
682-
if (window.ztools && window.ztools.showNotification) {
683-
window.ztools.showNotification(title + ': ' + body)
684-
} else {
685-
this.notify(title, body)
686-
}
687-
},
688-
689676
// ===== 提醒数据 CRUD =====
690677
getReminders() {
691678
return readReminders()
@@ -706,6 +693,7 @@ window.services = {
706693
dayFilter: formData.dayFilter || 'all',
707694
enabled: true,
708695
lastTriggered: 0,
696+
lastFiredSlot: 0,
709697
createdAt: Date.now()
710698
}
711699
const reminders = readReminders()
@@ -737,7 +725,9 @@ window.services = {
737725
triggerAt: formData.triggerAt,
738726
weekdays: formData.weekdays,
739727
triggerTime: formData.triggerTime,
740-
dayFilter: formData.dayFilter || 'all'
728+
dayFilter: formData.dayFilter || 'all',
729+
// 改了调度参数,旧的「已触发时刻」不能再挡住重新算出来的同一时刻
730+
lastFiredSlot: 0
741731
}
742732
writeReminders(reminders)
743733
const next = getNextTriggerTime(reminders[index], Date.now())

‎plugins/easyreminder/src/App.vue‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import { onMounted, ref } from 'vue'
33
import Reminder from './Reminder/index.vue'
44
55
const route = ref('')
6-
const enterAction = ref<any>({})
76
87
onMounted(() => {
98
window.ztools.onPluginEnter((action) => {
109
route.value = action.code
11-
enterAction.value = action
1210
// 进入插件时立刻对齐一次调度:睡眠唤醒或长时间后台后不必等看门狗巡检
1311
try {
1412
window.services?.refreshScheduler?.()
@@ -21,5 +19,5 @@ onMounted(() => {
2119
</script>
2220

2321
<template>
24-
<Reminder v-if="route === 'easyreminder'" :enter-action="enterAction" />
22+
<Reminder v-if="route === 'easyreminder'" />
2523
</template>

‎plugins/easyreminder/src/Reminder/components/ReminderForm.vue‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref, onMounted, watch } from 'vue'
2+
import { ref, computed, onMounted, watch } from 'vue'
33
import { ElMessage } from 'element-plus'
44
import type { Reminder, ReminderFormData, ReminderType } from '../../types/reminder'
55
import { REMINDER_TYPE_LABELS, DAY_FILTER_LABELS, createEmptyReminder } from '../../types/reminder'
@@ -31,6 +31,21 @@ const TYPE_DESCRIPTIONS: Record<ReminderType, string> = {
3131
once: '在指定的日期和时刻只提醒一次,触发后自动停用'
3232
}
3333
34+
// once 编辑时的初始日期/时间:按本地时区取(toISOString 是 UTC,会把凌晨的提醒错算到前一天)
35+
const onceInitialDate = computed(() => {
36+
const ts = props.editReminder?.triggerAt
37+
if (!ts) return undefined
38+
const d = new Date(ts)
39+
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
40+
})
41+
42+
const onceInitialTime = computed(() => {
43+
const ts = props.editReminder?.triggerAt
44+
if (!ts) return undefined
45+
const d = new Date(ts)
46+
return `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`
47+
})
48+
3449
function loadFromReminder(r: Reminder) {
3550
form.value = {
3651
title: r.title,
@@ -39,7 +54,6 @@ function loadFromReminder(r: Reminder) {
3954
schedules: (r.schedules || []).map(s => ({ ...s })),
4055
interval: r.interval,
4156
align: r.align === 'start' ? 'start' : 'create',
42-
triggerAt: r.triggerAt,
4357
weekdays: r.weekdays ? [...r.weekdays] : [],
4458
triggerTime: r.triggerTime || '09:00',
4559
dayFilter: r.dayFilter || 'all'
@@ -141,8 +155,8 @@ function handleSubmit() {
141155
<OnceForm
142156
v-if="form.type === 'once'"
143157
ref="onceFormRef"
144-
:initial-date="editReminder?.triggerAt ? new Date(editReminder.triggerAt).toISOString().slice(0, 10) : undefined"
145-
:initial-time="editReminder?.triggerAt ? `${String(new Date(editReminder.triggerAt).getHours()).padStart(2, '0')}:${String(new Date(editReminder.triggerAt).getMinutes()).padStart(2, '0')}` : undefined"
158+
:initial-date="onceInitialDate"
159+
:initial-time="onceInitialTime"
146160
/>
147161

148162
<!-- ===== daily:每日提醒 ===== -->

0 commit comments

Comments
 (0)