mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-07-10 03:37:30 +08:00
405b144e66
老用户升级后清掉 v1 时代独立 key 释放配额,调用方切到 store API。
- player.ts initializePlayState 调用从 migrateFromLocalStorage 切到
cleanupLegacyPlayHistoryStorage:不再做数据迁移(历史是低关键性
衍生数据,老用户重新听几次即可),仅清掉旧 musicHistory/podcastHistory/
playlistHistory/albumHistory/podcastRadioHistory/playHistory-migrated/
playMode 这 7 个旧 key,避免和新 play-history-store key 双倍占用
- SystemTab.vue 清缓存的 case 'history' 改走 usePlayHistoryStore().
clearMusicHistory(),与 v1 时代行为对齐(只清音乐历史,不动 podcast/
playlist/album/podcastRadio);移除 case 'settings' 里的
removeItem('playMode')——已并入 player-core-store
184 lines
6.0 KiB
TypeScript
184 lines
6.0 KiB
TypeScript
/**
|
|
* - usePlayerCoreStore: 核心播放控制(播放/暂停、音量、速度)
|
|
* - usePlaylistStore: 播放列表管理(列表、索引、模式、上下一首)
|
|
* - useFavoriteStore: 收藏管理(收藏列表、不喜欢列表)
|
|
* - useSleepTimerStore: 定时关闭(时间/歌曲数/列表结束)
|
|
* - useIntelligenceModeStore: 心动模式
|
|
*/
|
|
|
|
import { defineStore, storeToRefs } from 'pinia';
|
|
import { computed } from 'vue';
|
|
|
|
// 导入所有拆分的子 stores
|
|
import { useFavoriteStore } from './favorite';
|
|
import { useIntelligenceModeStore } from './intelligenceMode';
|
|
import { usePlayerCoreStore } from './playerCore';
|
|
import { cleanupLegacyPlayHistoryStorage } from './playHistory';
|
|
import { usePlaylistStore } from './playlist';
|
|
import { type SleepTimerInfo, SleepTimerType, useSleepTimerStore } from './sleepTimer';
|
|
|
|
export { type SleepTimerInfo, SleepTimerType };
|
|
export { getSongUrl, loadLrc, useLyrics, useSongDetail, useSongUrl } from '@/hooks/usePlayerHooks';
|
|
export { isBilibiliIdMatch } from '@/utils/playerUtils';
|
|
|
|
/**
|
|
* 聚合 Player Store
|
|
*/
|
|
export const usePlayerStore = defineStore('player', () => {
|
|
// 获取所有子 stores
|
|
const playerCore = usePlayerCoreStore();
|
|
const playlist = usePlaylistStore();
|
|
const favorite = useFavoriteStore();
|
|
const sleepTimer = useSleepTimerStore();
|
|
const intelligenceMode = useIntelligenceModeStore();
|
|
|
|
// 使用 storeToRefs 获取响应式引用
|
|
const {
|
|
play,
|
|
isPlay,
|
|
playMusic,
|
|
playMusicUrl,
|
|
musicFull,
|
|
playbackRate,
|
|
volume,
|
|
isMuted,
|
|
userPlayIntent,
|
|
isFmPlaying
|
|
} = storeToRefs(playerCore);
|
|
|
|
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 恢复)
|
|
*/
|
|
const initializePlayState = async () => {
|
|
// 一次性清理 v1 时代的旧 localStorage key,不做数据迁移(详见 playHistory.ts 注释)
|
|
cleanupLegacyPlayHistoryStorage();
|
|
|
|
const { initializePlayState: initPlayState } = await import('@/services/playbackController');
|
|
await initPlayState();
|
|
await playlist.initializePlaylist();
|
|
};
|
|
|
|
/**
|
|
* 初始化收藏列表(从服务器同步)
|
|
*/
|
|
const initializeFavoriteList = async () => {
|
|
await favorite.initializeFavoriteList();
|
|
};
|
|
|
|
// ==================== 返回所有状态和方法 ====================
|
|
return {
|
|
// ========== 核心播放控制 (PlayerCore) ==========
|
|
play,
|
|
isPlay,
|
|
playMusic,
|
|
playMusicUrl,
|
|
musicFull,
|
|
playbackRate,
|
|
volume,
|
|
isMuted,
|
|
userPlayIntent,
|
|
isFmPlaying,
|
|
|
|
// 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,
|
|
setMuted: playerCore.setMuted,
|
|
toggleMute: playerCore.toggleMute,
|
|
handlePause: playerCore.handlePause,
|
|
|
|
// ========== 播放列表管理 (Playlist) ==========
|
|
playList,
|
|
playListIndex,
|
|
playMode,
|
|
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) ==========
|
|
favoriteList,
|
|
dislikeList,
|
|
|
|
// Favorite - Actions
|
|
addToFavorite: favorite.addToFavorite,
|
|
removeFromFavorite: favorite.removeFromFavorite,
|
|
addToDislikeList: favorite.addToDislikeList,
|
|
removeFromDislikeList: favorite.removeFromDislikeList,
|
|
|
|
// ========== 定时关闭 (SleepTimer) ==========
|
|
sleepTimer: sleepTimerState,
|
|
showSleepTimer,
|
|
|
|
// SleepTimer - Computed
|
|
currentSleepTimer,
|
|
hasSleepTimerActive,
|
|
sleepTimerRemainingTime,
|
|
sleepTimerRemainingSongs,
|
|
|
|
// SleepTimer - Actions
|
|
setSleepTimerByTime: sleepTimer.setSleepTimerByTime,
|
|
setSleepTimerBySongs: sleepTimer.setSleepTimerBySongs,
|
|
setSleepTimerAtPlaylistEnd: sleepTimer.setSleepTimerAtPlaylistEnd,
|
|
clearSleepTimer: sleepTimer.clearSleepTimer,
|
|
|
|
// ========== 心动模式 (IntelligenceMode) ==========
|
|
isIntelligenceMode,
|
|
intelligenceModeInfo,
|
|
|
|
// IntelligenceMode - Actions
|
|
playIntelligenceMode: intelligenceMode.playIntelligenceMode,
|
|
|
|
// ========== 初始化方法 ==========
|
|
initializePlayState,
|
|
initializeFavoriteList
|
|
};
|
|
});
|