Skip to content

Commit 315e053

Browse files
committed
🐞 fix: 修复定时关闭失效
1 parent 10adb9a commit 315e053

4 files changed

Lines changed: 29 additions & 7 deletions

File tree

src/components/Modal/AutoClose.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ const handleUpdate = (value: boolean) => {
8181
} else {
8282
statusStore.autoClose.enable = false;
8383
statusStore.autoClose.remainTime = statusStore.autoClose.time * 60;
84+
statusStore.autoClose.endTime = 0;
8485
}
8586
};
8687
</script>

src/core/player/PlayerController.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -904,26 +904,31 @@ class PlayerController {
904904
public startAutoCloseTimer(time: number, remainTime: number) {
905905
const statusStore = useStatusStore();
906906
if (!time || !remainTime) return;
907-
907+
// 清除已有定时器
908908
if (this.autoCloseInterval) {
909909
clearInterval(this.autoCloseInterval);
910910
}
911-
911+
// 计算目标结束时间戳
912+
const endTime = Date.now() + remainTime * 1000;
912913
statusStore.autoClose.enable = true;
913914
statusStore.autoClose.time = time;
915+
statusStore.autoClose.endTime = endTime;
914916
statusStore.autoClose.remainTime = remainTime;
915-
917+
// 定时器仅用于 UI 更新,实际计时基于系统时间
916918
this.autoCloseInterval = setInterval(() => {
917-
if (statusStore.autoClose.remainTime <= 0) {
919+
const now = Date.now();
920+
const remaining = Math.max(0, Math.ceil((statusStore.autoClose.endTime - now) / 1000));
921+
statusStore.autoClose.remainTime = remaining;
922+
// 到达时间
923+
if (remaining <= 0) {
918924
clearInterval(this.autoCloseInterval);
919925
if (!statusStore.autoClose.waitSongEnd) {
920926
this.pause();
921927
statusStore.autoClose.enable = false;
922928
statusStore.autoClose.remainTime = statusStore.autoClose.time * 60;
929+
statusStore.autoClose.endTime = 0;
923930
}
924-
return;
925931
}
926-
statusStore.autoClose.remainTime--;
927932
}, 1000);
928933
}
929934

@@ -938,6 +943,7 @@ class PlayerController {
938943
statusStore.autoClose.enable = false;
939944
// 重置时间
940945
statusStore.autoClose.remainTime = statusStore.autoClose.time * 60;
946+
statusStore.autoClose.endTime = 0;
941947
return true;
942948
}
943949
return false;

src/stores/status.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ interface StatusState {
9898
time: number;
9999
/** 剩余时长(秒) */
100100
remainTime: number;
101+
/** 目标结束时间戳(毫秒) */
102+
endTime: number;
101103
/** 等待歌曲结束 */
102104
waitSongEnd: boolean;
103105
};
@@ -146,6 +148,7 @@ export const useStatusStore = defineStore("status", {
146148
enable: false,
147149
time: 30,
148150
remainTime: 0,
151+
endTime: 0,
149152
waitSongEnd: true,
150153
},
151154
developerMode: false,

src/utils/init.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,19 @@ const init = async () => {
4646
player.playModeSyncIpc();
4747
// 初始化自动关闭定时器
4848
if (statusStore.autoClose.enable) {
49-
player.startAutoCloseTimer(statusStore.autoClose.time, statusStore.autoClose.remainTime);
49+
const { endTime, time } = statusStore.autoClose;
50+
const now = Date.now();
51+
52+
if (endTime > now) {
53+
// 计算真实剩余时间
54+
const realRemainTime = Math.ceil((endTime - now) / 1000);
55+
player.startAutoCloseTimer(time, realRemainTime);
56+
} else {
57+
// 定时器已过期,重置状态
58+
statusStore.autoClose.enable = false;
59+
statusStore.autoClose.remainTime = time * 60;
60+
statusStore.autoClose.endTime = 0;
61+
}
5062
}
5163

5264
if (isElectron) {

0 commit comments

Comments
 (0)