Skip to content

Commit e4adf0c

Browse files
committed
🐞 fix: 修复心动模式,将心动模式移出切换循环中
1 parent 88aaa7a commit e4adf0c

3 files changed

Lines changed: 31 additions & 32 deletions

File tree

src/core/player/PlayModeManager.ts

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ export class PlayModeManager {
7777

7878
/**
7979
* 计算下一个随机模式
80+
* 注意:心跳模式只能通过菜单开启,不能通过点击随机按钮进入
8081
*/
8182
public calculateNextShuffleMode(currentMode: ShuffleModeType): ShuffleModeType {
8283
if (currentMode === "off") return "on";
83-
if (currentMode === "on") return "heartbeat";
84+
if (currentMode === "on") return "off";
85+
// 如果是心跳模式,点击随机按钮时退出心跳模式
86+
if (currentMode === "heartbeat") return "off";
8487
return "off";
8588
}
8689

@@ -127,31 +130,36 @@ export class PlayModeManager {
127130
}
128131

129132
this.loadingMessage = window.$message.loading("心动模式开启中...", {
130-
duration: 0, // 不自动关闭,必须手动 destroy
133+
duration: 0,
131134
});
132135

133-
const pid =
134-
musicStore.playPlaylistId || (await dataStore.getUserLikePlaylist())?.detail?.id || 0;
135-
const currentSongId = musicStore.playSong?.id || 0;
136-
137-
if (!currentSongId) throw new Error("无播放歌曲");
136+
try {
137+
const pid =
138+
musicStore.playPlaylistId || (await dataStore.getUserLikePlaylist())?.detail?.id || 0;
139+
if (!musicStore.playSong) throw new Error("无播放歌曲");
140+
// 获取当前歌曲ID,如果不是纯数字则生成随机10位数
141+
let currentSongId = musicStore.playSong.id;
142+
if (!Number.isInteger(currentSongId) || currentSongId <= 0) {
143+
currentSongId = Math.floor(Math.random() * 9000000000) + 1000000000;
144+
}
138145

139-
const res = await heartRateList(currentSongId, pid, undefined, signal);
140-
if (res.code !== 200) throw new Error("获取推荐失败");
146+
const res = await heartRateList(currentSongId, pid, undefined, signal);
147+
if (res.code !== 200) throw new Error("获取推荐失败");
141148

142-
const recList = formatSongsList(res.data);
149+
const recList = formatSongsList(res.data);
143150

144-
// 混合列表
145-
const currentList = [...dataStore.playList];
146-
const mixedList = interleaveLists(currentList, recList);
151+
// 混合列表
152+
const currentList = [...dataStore.playList];
153+
const mixedList = interleaveLists(currentList, recList);
147154

148-
await dataStore.setPlayList(mixedList);
155+
await dataStore.setPlayList(mixedList);
149156

150-
const idx = mixedList.findIndex((s) => s.id === currentSongId);
151-
if (idx !== -1) statusStore.playIndex = idx;
152-
153-
this.clearLoadingMessage();
154-
window.$message.success("心动模式已开启");
157+
const idx = mixedList.findIndex((s) => s.id === currentSongId);
158+
if (idx !== -1) statusStore.playIndex = idx;
159+
window.$message.success("心动模式已开启");
160+
} finally {
161+
this.clearLoadingMessage();
162+
}
155163
}
156164

157165
/**

src/core/player/PlayerController.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,8 @@ class PlayerController {
950950

951951
/**
952952
* 切换随机模式
953-
* @param mode 可选,直接设置目标模式。如果不传则按 Off -> On -> Heartbeat -> Off 顺序轮转
953+
* @param mode 可选,直接设置目标模式。如果不传则按 Off -> On -> Off 顺序轮转
954+
* @note 心跳模式只能通过菜单开启(传入 "heartbeat" 参数),点击随机按钮不会进入心跳模式
954955
* @note 当播放列表包含本地歌曲时,跳过心动模式,只在 Off 和 On 之间切换
955956
*/
956957
public async toggleShuffle(mode?: ShuffleModeType) {
@@ -969,15 +970,6 @@ class PlayerController {
969970
nextMode = "off";
970971
}
971972

972-
// 已经是心动模式,再次触发心动模式并播放
973-
if (currentMode === "heartbeat" && nextMode === "heartbeat") {
974-
if (!statusStore.playStatus) {
975-
await this.play();
976-
}
977-
statusStore.showFullPlayer = true;
978-
return;
979-
}
980-
981973
// 如果模式确实改变了,才让 Manager 进行繁重的数据处理
982974
if (currentMode !== nextMode) {
983975
await this.playModeManager.toggleShuffle(nextMode);

src/stores/status.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,12 @@ export const useStatusStore = defineStore("status", {
255255
},
256256
/**
257257
* 切换随机模式
258-
* 顺序: Off -> On -> Heartbeat -> Off
258+
* 顺序: Off -> On -> Off
259+
* @deprecated 心跳模式只能通过菜单开启,不再通过此方法切换
259260
*/
260261
toggleShuffle() {
261262
if (this.shuffleMode === "off") {
262263
this.shuffleMode = "on";
263-
} else if (this.shuffleMode === "on") {
264-
this.shuffleMode = "heartbeat";
265264
} else {
266265
this.shuffleMode = "off";
267266
}

0 commit comments

Comments
 (0)