|
|
import { defineStore } from 'pinia' |
|
|
import { themeManager, THEME_IDS } from '@/styles/themes' |
|
|
|
|
|
export const useSettingsStore = defineStore('settings', { |
|
|
state: () => ({ |
|
|
settings: { |
|
|
|
|
|
defaultQuality: '320', |
|
|
defaultPlayMode: 'list', |
|
|
rememberProgress: true, |
|
|
autoNext: true, |
|
|
fadeEffect: true, |
|
|
|
|
|
|
|
|
defaultSource: 'joox', |
|
|
searchLimit: 20, |
|
|
saveSearchHistory: true, |
|
|
|
|
|
|
|
|
theme: 'light', |
|
|
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) |
|
|
} |
|
|
} |
|
|
} |
|
|
}) |