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
53const STORAGE_KEY = 'easyremind_reminders'
64const LOG_KEY = 'easyremind_logs'
@@ -16,6 +14,7 @@ const INIT_FAST_TRIES = 40 // 快速重试次数(300ms × 40 ≈ 12s),之
1614const WATCHDOG_INTERVAL_MS = 30 * 1000 // 看门狗巡检间隔
1715const MISSED_FIRE_GRACE_MS = 5 * 60 * 1000 // 错过的触发在此时间内仍补发,超过则只重排不补发
1816const MIN_REARM_DELAY_MS = 1000 // 触发后重排的最小延迟:数据异常(如状态写不进去)时不会 0 延迟自我循环
17+ const TRIGGER_MATCH_WINDOW_MS = 1000 // 触发命中窗口:实际时刻与计划时刻相差在此范围内即视为「到点」
1918const 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// ==================== 日志 ====================
3139let logId = 1
3240
@@ -50,8 +58,7 @@ function writeLogs(logs) {
5058function 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) {
429436function 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// ==================== 暴露服务 ====================
644675window . 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 = / ^ d a t a : i m a g e \/ ( [ a - z ] { 1 , 20 } ) ; b a s e 6 4 , / 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 ( ) )
0 commit comments