Skip to content

🗄️ Store 状态管理

GlassMusicPlayer 使用 Pinia 进行状态管理,所有 Store 均支持持久化存储。

🎧 useAudioStore

音频播放器核心状态。

📁 文件位置src/stores/modules/audio.ts

📊 状态

ts
interface AudioStoreState {
  count: number
  audio: {
    audio: HTMLAudioElement | null  // 音频元素
    isPlaying: boolean              // 是否播放中
    isPaused: boolean               // 是否已暂停
    isLoading: boolean              // 是否加载中
    currentSong: Song | null        // 当前歌曲
    currentIndex: number            // 当前索引
    playlist: Song[]                // 播放列表
    originalPlaylist: Song[]        // 原始列表(随机模式用)
    playMode: PlayMode              // 播放模式
    volume: number                  // 音量 (0-1)
    isMuted: boolean                // 是否静音
    previousVolume: number          // 静音前的音量
    currentTime: number             // 当前时间(秒)
    duration: number                // 总时长(秒)
    playHistory: Song[]             // 播放历史
    error: string | null            // 错误信息
  }
}

🔁 播放模式枚举

ts
enum PlayMode {
  LIST = 'list',      // 列表循环
  SINGLE = 'single',  // 单曲循环
  RANDOM = 'random',  // 随机播放
}

📤 Getters

Getter返回类型说明
getCurrentSongSong | null当前歌曲
getIsPlayingboolean是否播放中
getPlaylistSong[]播放列表
getPlayModePlayMode播放模式
getVolumenumber音量
getIsMutedboolean是否静音
getCurrentTimenumber当前时间
getDurationnumber总时长
getProgressnumber进度百分比
hasNextboolean是否有下一首
hasPreviousboolean是否有上一首

⚡ Actions

Action参数说明
initAudio()-初始化音频播放器
setupAudioEvents()-设置音频事件监听
playSong(song?, index?)Song, number播放歌曲
refreshAndReplay()-刷新播放地址并重试(URL过期时自动调用)
pause()-暂停
resume()-继续播放
togglePlay()-切换播放/暂停
nextSong()-下一首
previousSong()-上一首
handleSongEnd()-处理歌曲播放结束
stop()-停止播放
togglePlayMode()-切换播放模式
setPlayMode(mode)PlayMode设置播放模式
shufflePlaylist()-打乱播放列表
setVolume(vol)number设置音量
toggleMute()-切换静音
setCurrentTime(time)number设置播放时间
setProgress(prog)number设置播放进度
addSong(song)Song添加歌曲
addSongs(songs)Song[]批量添加
removeSong(id)string | number移除歌曲
removeSongs(ids)Array批量移除
moveSong(from, to)number, number移动歌曲
queueNext(id)string | number下一首播放
clearPlaylist()-清空列表
setPlaylist(songs, startIndex?)Song[], number设置播放列表
playByIndex(index)number按索引播放
addToHistory(song)Song添加到历史
clearHistory()-清空历史
clearError()-清除错误
destroy()-销毁播放器

💾 持久化字段

  • count
  • audio.currentSong
  • audio.currentIndex
  • audio.playlist
  • audio.originalPlaylist
  • audio.playMode
  • audio.volume
  • audio.isMuted
  • audio.playHistory

🌐 useGlobalStore

全局应用状态。

📁 文件位置src/stores/modules/global.ts

📊 状态

ts
interface GlobalState {
  count: number
  theme?: 'light' | 'dark' | 'system'  // 主题模式
  searchHistory: string[]               // 搜索历史
  lang?: 'zh' | 'en' | 'ja'            // 语言
}

⚡ Actions

Action参数说明
setGlobalState(key, value)keyof GlobalState, any设置全局状态
setTheme(theme)'light' | 'dark' | 'system'设置主题
toggleTheme()-切换主题(浅/深)
setLang(lang)'zh' | 'en' | 'ja'设置语言
addSearchHistory(q)string添加搜索历史
removeSearchHistory(q)string移除搜索历史
clearSearchHistory()-清空搜索历史

⚙️ useSettingsStore

设置状态(背景、歌词显示等)。

📁 文件位置src/stores/modules/settings.ts

📊 状态

ts
interface SettingsState {
  aurora: AuroraSettingsState           // 极光背景设置
  colorBends: ColorBendsSettingsState   // 渐变背景设置
  ultimate: UltimateSettingsState       // 终极背景设置
  shadowBling: ShadowBlingSettingsState // 光影背景设置
  footerLyrics: FooterLyricsSettingsState // 底栏歌词设置
  audioVisualizer: AudioVisualizerSettingsState // 音频可视化设置
  backgroundType: 'aurora' | 'colorbends' | 'ultimate' | 'shadowBling'
  audioQuality: AudioQuality            // 音频品质
}

🌌 Aurora 设置

ts
interface AuroraSettingsState {
  colorStops: string[]        // 颜色数组
  amplitude: number           // 振幅
  blend: number               // 混合度
  speed: number               // 速度
  intensity: number           // 强度
  colorPositions: number[]    // 颜色位置
}

🌈 ColorBends 设置

ts
interface ColorBendsSettingsState {
  colors: string[]            // 颜色数组
  rotation: number            // 旋转角度
  speed: number               // 速度
  scale: number               // 缩放
  frequency: number           // 频率
  warpStrength: number        // 扭曲强度
  mouseInfluence: number      // 鼠标影响
  parallax: number            // 视差
  noise: number               // 噪点
  transparent: boolean        // 透明
  autoRotate: number          // 自动旋转
}

✨ Ultimate 设置

ts
interface UltimateSettingsState {
  bg1: string                 // 背景色1
  bg2: string                 // 背景色2
  color1: string              // 颜色1
  color2: string              // 颜色2
  color3: string              // 颜色3
  color4: string              // 颜色4
  color5: string              // 颜色5
  interactiveColor: string    // 交互颜色
  circleSize: string          // 圆形大小
  blending: string            // 混合模式
}

💫 ShadowBling 设置

ts
interface ShadowBlingSettingsState {
  bgColors: string[]          // 背景颜色数组
  speed: number               // 速度
  intensity: number           // 强度
}

🎵 底栏歌词设置

ts
interface FooterLyricsSettingsState {
  enabled: boolean                           // 是否启用
  modes: Array<'original' | 'trans' | 'roma'> // 显示模式
}

🎨 音频可视化设置

ts
interface AudioVisualizerSettingsState {
  enabledInFooter: boolean    // 底栏是否启用可视化
  enabledInDrawer: boolean    // 抽屉是否启用可视化
  visualizerType: 'bars' | 'wave' | 'circular' // 可视化类型
}

🎚️ 音频品质

ts
type AudioQuality = 'standard' | 'higher' | 'exhigh' | 'lossless' | 'hires' | 'jyeffect' | 'sky' | 'jymaster'
说明
standard标准品质
higher较高品质
exhigh极高品质
lossless无损品质
hiresHi-Res 品质
jyeffect鲸云臻品音效
sky沉浸环绕声
jymaster超清母带

⚡ Actions

Action参数说明
setBackgroundType(type)'aurora' | 'colorbends' | 'ultimate' | 'shadowBling'设置背景类型
setAurora(partial)Partial<AuroraSettingsState>更新极光设置
setColorStops(stops)string[]设置极光颜色
setColorPositions(positions)number[]设置极光颜色位置
resetAurora()-重置极光设置
setColorBends(partial)Partial<ColorBendsSettingsState>更新渐变设置
setBendsColors(colors)string[]设置渐变颜色
resetColorBends()-重置渐变设置
setUltimate(partial)Partial<UltimateSettingsState>更新终极设置
resetUltimate()-重置终极设置
setShadowBling(partial)Partial<ShadowBlingSettingsState>更新光影设置
resetShadowBling()-重置光影设置
setFooterLyricsEnabled(val)boolean设置底栏歌词开关
setFooterLyricsModes(modes)Array设置底栏歌词模式
setAudioQuality(quality)AudioQuality设置音频品质
setAudioVisualizerFooter(enabled)boolean设置底栏可视化开关
setAudioVisualizerDrawer(enabled)boolean设置抽屉可视化开关
setAudioVisualizerType(type)'bars' | 'wave' | 'circular'设置可视化类型

👤 useUserStore

用户登录状态。

📁 文件位置src/stores/modules/user.ts

📊 状态

ts
interface UserState {
  isLoggedIn: boolean           // 是否已登录
  profile: UserProfile | null   // 用户资料
  lastLoginAt: number           // 上次登录时间
}

interface UserProfile {
  userId: number
  nickname: string
  avatarUrl: string
  vipType?: number
}

📤 Getters

Getter返回类型说明
nicknamestring用户昵称
avatarUrlstring头像 URL

⚡ Actions

Action参数说明
setUser(profile)UserProfile设置用户信息
logout()-退出登录

💾 持久化字段

  • isLoggedIn
  • profile
  • lastLoginAt

💾 useLocalMusicStore

本地音乐管理状态。

📁 文件位置src/stores/modules/localMusic.ts

📊 状态

ts
interface LocalMusicState {
  musics: LocalMusic[]     // 本地音乐列表
  isLoading: boolean       // 是否加载中
}

interface LocalMusic {
  id: string               // 唯一标识
  name: string             // 歌曲名称
  artist: string           // 艺术家
  album: string            // 专辑名称
  duration: number         // 时长(秒)
  cover: string            // 封面(Base64)
  file: File               // 音频文件
  createTime: number       // 添加时间戳
}

⚡ Actions

Action参数返回值说明
loadMusics()-Promise<void>从 IndexedDB 加载所有本地音乐
addMusic(file)FilePromise<LocalMusic>添加本地音乐(自动解析元数据)
deleteMusic(id)stringPromise<void>删除本地音乐
convertToSong(music)LocalMusicSong将本地音乐转换为播放器可用的 Song 对象

💡 使用示例

vue
<script setup lang="ts">
import { useLocalMusicStore } from '@/stores/modules/localMusic'

const localMusicStore = useLocalMusicStore()

// 加载本地音乐
onMounted(() => {
  localMusicStore.loadMusics()
})

// 添加本地音乐
const handleFileSelect = async (file: File) => {
  const music = await localMusicStore.addMusic(file)
  console.log('Added:', music.name)
}

// 播放本地音乐
const playLocalMusic = (music: LocalMusic) => {
  const song = localMusicStore.convertToSong(music)
  audioStore.playSong(song)
}
</script>

🎵 支持的音频格式

本地音乐功能支持大多数常见音频格式:

  • MP3、FLAC、WAV、OGG、AAC、M4A 等
  • 使用 music-metadata-browser 库解析元数据
  • 自动提取专辑封面、艺术家、专辑名等信息

基于 PolyForm-Noncommercial-1.0.0 许可发布