feat: 优化设置模块,合并默认设置与存储设置,初始化时读取设置

This commit is contained in:
algerkong
2025-04-23 09:52:07 +08:00
parent 64f5fcaee4
commit 0a22c7b5d7
2 changed files with 24 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
import { cloneDeep } from 'lodash';
import { cloneDeep, merge } from 'lodash';
import { defineStore } from 'pinia';
import { ref } from 'vue';
@@ -7,17 +7,6 @@ import { isElectron } from '@/utils';
import { applyTheme, getCurrentTheme, ThemeType } from '@/utils/theme';
export const useSettingsStore = defineStore('settings', () => {
// 初始化时先从存储中读取设置
const getInitialSettings = () => {
if (isElectron) {
const savedSettings = window.electron.ipcRenderer.sendSync('get-store-value', 'set');
return savedSettings || setDataDefault;
}
const savedSettings = localStorage.getItem('appSettings');
return savedSettings ? JSON.parse(savedSettings) : setDataDefault;
};
const setData = ref(getInitialSettings());
const theme = ref<ThemeType>(getCurrentTheme());
const isMobile = ref(false);
const isMiniMode = ref(false);
@@ -28,7 +17,11 @@ export const useSettingsStore = defineStore('settings', () => {
{ label: '系统默认', value: 'system-ui' }
]);
const showDownloadDrawer = ref(false);
// 先声明 setData ref 但不初始化
const setData = ref<any>({});
// 先定义 setSetData 函数
const setSetData = (data: any) => {
// 合并现有设置和新设置
const mergedData = {
@@ -44,6 +37,24 @@ export const useSettingsStore = defineStore('settings', () => {
setData.value = cloneDeep(mergedData);
};
// 初始化时先从存储中读取设置
const getInitialSettings = () => {
// 从存储中获取保存的设置
const savedSettings = isElectron
? window.electron.ipcRenderer.sendSync('get-store-value', 'set')
: JSON.parse(localStorage.getItem('appSettings') || '{}');
// 合并默认设置和保存的设置
const mergedSettings = merge({}, setDataDefault, savedSettings);
// 更新设置并返回
setSetData(mergedSettings);
return mergedSettings;
};
// 初始化 setData
setData.value = getInitialSettings();
const toggleTheme = () => {
theme.value = theme.value === 'dark' ? 'light' : 'dark';
applyTheme(theme.value);

View File

@@ -575,7 +575,7 @@ import config from '../../../../package.json';
// 手动定义Platform类型避免从主进程导入的问题
type Platform = 'qq' | 'migu' | 'kugou' | 'pyncmd' | 'joox' | 'kuwo' | 'bilibili' | 'youtube' | 'gdmusic';
// 所有平台
const ALL_PLATFORMS: Platform[] = ['migu', 'kugou', 'pyncmd', 'kuwo', 'bilibili', 'youtube', 'gdmusic'];
const ALL_PLATFORMS: Platform[] = ['migu', 'kugou', 'pyncmd', 'bilibili', 'youtube'];
const settingsStore = useSettingsStore();
const userStore = useUserStore();