music / src /stores /settings.js
ahutchen's picture
feat(player): 优化播放逻辑并添加状态过期处理
25e7862
import { defineStore } from 'pinia'
import { themeManager, THEME_IDS } from '@/styles/themes'
export const useSettingsStore = defineStore('settings', {
state: () => ({
settings: {
// 播放设置 - 根据需求文档
defaultQuality: '320', // 默认音质:128/192/320/740/999 (默认:320K)
defaultPlayMode: 'list', // 默认播放模式:list/random/single (默认:列表循环)
rememberProgress: true, // 记住播放进度:true/false (默认:开启)
autoNext: true, // 自动播放下一首
fadeEffect: true, // 淡入淡出效果
// 搜索设置 - 根据需求文档
defaultSource: 'joox', // 默认音乐源:joox (默认:JOOX音乐)
searchLimit: 20, // 搜索结果数量
saveSearchHistory: true, // 搜索历史:true/false (默认:保存)
// 外观设置 - 新的多主题系统
theme: 'light', // 主题模式:light/dark/orange/pink/purple/blue/green/gold/gray/wechat
showLyrics: true, // 显示歌词
lyricsFontSize: 18, // 歌词字体大小
// 通知设置
desktopNotification: true, // 桌面通知
mediaSession: true // 媒体控制
}
}),
actions: {
// 获取设置项
getSetting(key) {
return this.settings[key]
},
// 更新设置项
updateSetting(key, value) {
if (key in this.settings) {
this.settings[key] = value
// 特殊处理主题切换
if (key === 'theme') {
this.applyTheme(value)
}
this.saveSettings()
}
},
// 应用主题
applyTheme(themeId = null) {
const targetTheme = themeId || this.settings.theme || 'light'
try {
themeManager.applyTheme(targetTheme)
// 更新设置状态
if (themeId && themeId !== this.settings.theme) {
this.settings.theme = targetTheme
this.saveSettings()
}
console.log(`主题已切换到: ${targetTheme}`)
} catch (error) {
console.error('应用主题失败:', error)
// 回退到默认主题
if (targetTheme !== 'light') {
this.applyTheme('light')
}
}
},
// 获取所有可用主题
getAvailableThemes() {
return themeManager.getAllThemes()
},
// 保存设置到本地存储
saveSettings() {
try {
localStorage.setItem('music-settings', JSON.stringify({
settings: this.settings,
version: '1.0',
lastUpdated: Date.now()
}))
} catch (error) {
console.error('保存设置失败:', error)
}
},
// 从本地存储加载设置
loadSettings() {
try {
const saved = localStorage.getItem('music-settings')
if (saved) {
const data = JSON.parse(saved)
if (data.settings) {
this.settings = {
...this.settings,
...data.settings
}
}
}
} catch (error) {
console.error('加载设置失败:', error)
}
}
}
})