From 47ebe2a2e1549f59a0aa8c82a4831dd1936c6d6b Mon Sep 17 00:00:00 2001 From: mtvpls Date: Wed, 13 May 2026 09:32:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=A9=BA=E9=9F=B3=E4=B9=90=E6=92=AD?= =?UTF-8?q?=E6=94=BE=E8=AE=B0=E5=BD=95=E6=97=B6=EF=BC=8C=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E6=92=AD=E6=94=BE=E7=9A=84=E9=9F=B3=E4=B9=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/music/page.tsx | 89 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 12 deletions(-) diff --git a/src/app/music/page.tsx b/src/app/music/page.tsx index 0d39a7b..b0f85db 100644 --- a/src/app/music/page.tsx +++ b/src/app/music/page.tsx @@ -444,6 +444,35 @@ export default function MusicPage() { localStorage.setItem('musicPlayState', JSON.stringify(playState)); }; + // 清空当前播放状态,并在需要时停止正在播放的音频 + const clearCurrentPlaybackState = () => { + const audio = audioRef.current; + + if (audio) { + audio.pause(); + audio.removeAttribute('src'); + audio.load(); + } + + setIsPlaying(false); + setCurrentSong(null); + setCurrentSongIndex(-1); + setCurrentSongUrl(''); + setCurrentTime(0); + setDuration(0); + setLyrics([]); + setCurrentLyricIndex(-1); + setShowPlayer(false); + setShowLyrics(false); + setShowPiPLyrics(false); + setPendingSongToPlay(null); + restoredTimeRef.current = 0; + lastSaveTimeRef.current = 0; + currentTimeRef.current = 0; + + localStorage.removeItem('musicPlayState'); + }; + // 从 localStorage 恢复播放状态(已废弃,现在统一使用数据库) const restorePlayState = async () => { // 此函数已不再使用,所有状态恢复都在 initializePlayState 中完成 @@ -1317,6 +1346,53 @@ export default function MusicPage() { setQuality(qualities[nextIndex]); }; + // 清空播放记录 + const handleClearPlayRecords = () => { + setConfirmModal({ + isOpen: true, + title: '确认清空', + message: '确定要清空全部播放记录吗?', + onConfirm: async () => { + try { + await fetch('/api/music/v2/history', { method: 'DELETE' }); + clearCurrentPlaybackState(); + setPlaylist([]); + setPlayRecords([]); + setPlaylistIndex(-1); + setToast({ + message: '播放记录已清空', + type: 'success', + onClose: () => setToast(null), + }); + } catch (error) { + console.error('清空播放记录失败:', error); + setToast({ + message: '清空播放记录失败', + type: 'error', + onClose: () => setToast(null), + }); + } finally { + setConfirmModal({ + isOpen: false, + title: '', + message: '', + onConfirm: () => {}, + onCancel: () => {}, + }); + } + }, + onCancel: () => { + setConfirmModal({ + isOpen: false, + title: '', + message: '', + onConfirm: () => {}, + onCancel: () => {}, + }); + }, + }); + }; + // 切换播放模式 const toggleMode = () => { const modes: Array<'loop' | 'single' | 'random'> = ['loop', 'single', 'random']; @@ -2668,18 +2744,7 @@ export default function MusicPage() {
{playlist.length > 0 && (