Files
AlgerMusicPlayer/src/renderer/store/modules/player.ts
T

181 lines
5.9 KiB
TypeScript
Raw Normal View History

2025-10-11 20:23:54 +08:00
/**
2025-11-08 14:26:04 +08:00
* - usePlayerCoreStore: 核心播放控制(播放/暂停、音量、速度)
* - usePlaylistStore: 播放列表管理(列表、索引、模式、上下一首)
* - useFavoriteStore: 收藏管理(收藏列表、不喜欢列表)
* - useSleepTimerStore: 定时关闭(时间/歌曲数/列表结束)
* - useIntelligenceModeStore: 心动模式
2025-10-11 20:23:54 +08:00
*/
2025-11-08 14:26:04 +08:00
import { defineStore, storeToRefs } from 'pinia';
import { computed } from 'vue';
2025-11-08 14:26:04 +08:00
// 导入所有拆分的子 stores
import { useFavoriteStore } from './favorite';
import { useIntelligenceModeStore } from './intelligenceMode';
import { usePlayerCoreStore } from './playerCore';
2026-02-06 20:35:04 +08:00
import { usePlayHistoryStore } from './playHistory';
2025-11-08 14:26:04 +08:00
import { usePlaylistStore } from './playlist';
import { type SleepTimerInfo, SleepTimerType, useSleepTimerStore } from './sleepTimer';
2025-11-08 14:26:04 +08:00
export { type SleepTimerInfo, SleepTimerType };
export { getSongUrl, loadLrc, useLyrics, useSongDetail, useSongUrl } from '@/hooks/usePlayerHooks';
export { isBilibiliIdMatch } from '@/utils/playerUtils';
2025-11-08 14:26:04 +08:00
/**
* 聚合 Player Store
*/
2025-03-19 22:48:28 +08:00
export const usePlayerStore = defineStore('player', () => {
2025-11-08 14:26:04 +08:00
// 获取所有子 stores
const playerCore = usePlayerCoreStore();
const playlist = usePlaylistStore();
const favorite = useFavoriteStore();
const sleepTimer = useSleepTimerStore();
const intelligenceMode = useIntelligenceModeStore();
// 使用 storeToRefs 获取响应式引用
2026-03-22 16:49:00 +08:00
const {
play,
isPlay,
playMusic,
playMusicUrl,
musicFull,
playbackRate,
volume,
userPlayIntent,
isFmPlaying
} = storeToRefs(playerCore);
2025-11-08 14:26:04 +08:00
const { playList, playListIndex, playMode, originalPlayList, playListDrawerVisible } =
storeToRefs(playlist);
const { favoriteList, dislikeList } = storeToRefs(favorite);
const { sleepTimer: sleepTimerState, showSleepTimer } = storeToRefs(sleepTimer);
const { isIntelligenceMode, intelligenceModeInfo } = storeToRefs(intelligenceMode);
// ==================== Computed ====================
const currentSong = computed(() => playerCore.currentSong);
const isPlaying = computed(() => playerCore.isPlaying);
const currentPlayList = computed(() => playlist.currentPlayList);
const currentPlayListIndex = computed(() => playlist.currentPlayListIndex);
// 定时器相关 computed
const currentSleepTimer = computed(() => sleepTimer.currentSleepTimer);
const hasSleepTimerActive = computed(() => sleepTimer.hasSleepTimerActive);
const sleepTimerRemainingTime = computed(() => sleepTimer.sleepTimerRemainingTime);
const sleepTimerRemainingSongs = computed(() => sleepTimer.sleepTimerRemainingSongs);
// ==================== 初始化方法 ====================
/**
* 初始化播放状态(从 localStorage 恢复)
*/
2025-03-19 22:48:28 +08:00
const initializePlayState = async () => {
2026-02-06 20:35:04 +08:00
// 从旧的 localStorage 迁移播放记录到 Pinia store
const playHistoryStore = usePlayHistoryStore();
playHistoryStore.migrateFromLocalStorage();
const { initializePlayState: initPlayState } = await import('@/services/playbackController');
await initPlayState();
2025-11-08 14:26:04 +08:00
await playlist.initializePlaylist();
2025-03-19 22:48:28 +08:00
};
2025-11-08 14:26:04 +08:00
/**
* 初始化收藏列表(从服务器同步)
*/
2025-03-19 22:48:28 +08:00
const initializeFavoriteList = async () => {
2025-11-08 14:26:04 +08:00
await favorite.initializeFavoriteList();
2025-10-22 22:48:52 +08:00
};
2025-11-08 14:26:04 +08:00
// ==================== 返回所有状态和方法 ====================
2025-03-19 22:48:28 +08:00
return {
2025-11-08 14:26:04 +08:00
// ========== 核心播放控制 (PlayerCore) ==========
2025-03-19 22:48:28 +08:00
play,
isPlay,
playMusic,
playMusicUrl,
2025-11-08 14:26:04 +08:00
musicFull,
playbackRate,
volume,
userPlayIntent,
2026-03-22 16:49:00 +08:00
isFmPlaying,
2025-11-08 14:26:04 +08:00
// PlayerCore - Computed
currentSong,
isPlaying,
// PlayerCore - Actions
setIsPlay: playerCore.setIsPlay,
setMusicFull: playerCore.setMusicFull,
setPlayMusic: playerCore.setPlayMusic,
setPlaybackRate: playerCore.setPlaybackRate,
setVolume: playerCore.setVolume,
getVolume: playerCore.getVolume,
increaseVolume: playerCore.increaseVolume,
decreaseVolume: playerCore.decreaseVolume,
handlePause: playerCore.handlePause,
// ========== 播放列表管理 (Playlist) ==========
2025-03-19 22:48:28 +08:00
playList,
playListIndex,
playMode,
2025-11-08 14:26:04 +08:00
originalPlayList,
playListDrawerVisible,
// Playlist - Computed
currentPlayList,
currentPlayListIndex,
// Playlist - Actions
setPlayList: playlist.setPlayList,
addToNextPlay: playlist.addToNextPlay,
removeFromPlayList: playlist.removeFromPlayList,
clearPlayAll: playlist.clearPlayAll,
togglePlayMode: playlist.togglePlayMode,
shufflePlayList: playlist.shufflePlayList,
restoreOriginalOrder: playlist.restoreOriginalOrder,
preloadNextSongs: playlist.preloadNextSongs,
nextPlay: playlist.nextPlay,
prevPlay: playlist.prevPlay,
setPlayListDrawerVisible: playlist.setPlayListDrawerVisible,
setPlay: playlist.setPlay,
// ========== 收藏管理 (Favorite) ==========
2025-03-19 22:48:28 +08:00
favoriteList,
dislikeList,
2025-11-08 14:26:04 +08:00
// Favorite - Actions
addToFavorite: favorite.addToFavorite,
removeFromFavorite: favorite.removeFromFavorite,
addToDislikeList: favorite.addToDislikeList,
removeFromDislikeList: favorite.removeFromDislikeList,
// ========== 定时关闭 (SleepTimer) ==========
sleepTimer: sleepTimerState,
showSleepTimer,
2025-11-08 14:26:04 +08:00
// SleepTimer - Computed
currentSleepTimer,
hasSleepTimerActive,
sleepTimerRemainingTime,
sleepTimerRemainingSongs,
2025-03-19 22:48:28 +08:00
2025-11-08 14:26:04 +08:00
// SleepTimer - Actions
setSleepTimerByTime: sleepTimer.setSleepTimerByTime,
setSleepTimerBySongs: sleepTimer.setSleepTimerBySongs,
setSleepTimerAtPlaylistEnd: sleepTimer.setSleepTimerAtPlaylistEnd,
clearSleepTimer: sleepTimer.clearSleepTimer,
2025-07-29 22:19:34 +08:00
2025-11-08 14:26:04 +08:00
// ========== 心动模式 (IntelligenceMode) ==========
isIntelligenceMode,
intelligenceModeInfo,
2025-11-08 14:26:04 +08:00
// IntelligenceMode - Actions
playIntelligenceMode: intelligenceMode.playIntelligenceMode,
2025-10-22 22:48:52 +08:00
2025-11-08 14:26:04 +08:00
// ========== 初始化方法 ==========
initializePlayState,
initializeFavoriteList
2025-03-19 22:48:28 +08:00
};
});