Files
AlgerMusicPlayer/src/store/index.ts
T

135 lines
3.6 KiB
TypeScript
Raw Normal View History

import { createStore } from 'vuex';
import { useMusicListHook } from '@/hooks/MusicListHook';
import homeRouter from '@/router/home';
2024-09-13 14:11:02 +08:00
import type { SongResult } from '@/type/music';
// 默认设置
const defaultSettings = {
isProxy: false,
noAnimate: false,
animationSpeed: 1,
author: 'Alger',
authorUrl: 'https://github.com/algerkong',
};
function getLocalStorageItem<T>(key: string, defaultValue: T): T {
const item = localStorage.getItem(key);
return item ? JSON.parse(item) : defaultValue;
}
interface State {
menus: any[];
play: boolean;
isPlay: boolean;
2024-09-13 14:11:02 +08:00
playMusic: SongResult;
playMusicUrl: string;
user: any;
2024-09-13 14:11:02 +08:00
playList: SongResult[];
playListIndex: number;
setData: any;
2024-05-20 19:54:00 +08:00
lyric: any;
2024-05-23 17:12:35 +08:00
isMobile: boolean;
2024-09-12 17:28:51 +08:00
searchValue: string;
searchType: number;
2024-12-09 21:55:08 +08:00
favoriteList: number[];
playMode: number;
}
const state: State = {
2021-09-29 15:26:13 +08:00
menus: homeRouter,
2021-07-21 22:30:55 +08:00
play: false,
2021-07-20 22:46:18 +08:00
isPlay: false,
2024-09-13 14:11:02 +08:00
playMusic: {} as SongResult,
playMusicUrl: '',
user: getLocalStorageItem('user', null),
playList: [],
playListIndex: 0,
setData: defaultSettings,
2024-05-20 19:54:00 +08:00
lyric: {},
2024-05-23 17:12:35 +08:00
isMobile: false,
2024-09-12 17:28:51 +08:00
searchValue: '',
searchType: 1,
favoriteList: getLocalStorageItem('favoriteList', []),
playMode: getLocalStorageItem('playMode', 0),
};
2023-12-28 10:45:11 +08:00
const { handlePlayMusic, nextPlay, prevPlay } = useMusicListHook();
const mutations = {
setMenus(state: State, menus: any[]) {
state.menus = menus;
2021-07-20 22:46:18 +08:00
},
2024-09-13 14:11:02 +08:00
async setPlay(state: State, playMusic: SongResult) {
await handlePlayMusic(state, playMusic);
2021-07-20 22:46:18 +08:00
},
setIsPlay(state: State, isPlay: boolean) {
state.isPlay = isPlay;
2021-07-20 22:46:18 +08:00
},
setPlayMusic(state: State, play: boolean) {
state.play = play;
2021-07-21 22:30:55 +08:00
},
2024-09-13 14:11:02 +08:00
setPlayList(state: State, playList: SongResult[]) {
state.playListIndex = playList.findIndex((item) => item.id === state.playMusic.id);
state.playList = playList;
},
async nextPlay(state: State) {
await nextPlay(state);
},
async prevPlay(state: State) {
await prevPlay(state);
},
setSetData(state: State, setData: any) {
state.setData = setData;
const isElectron = (window as any).electronAPI !== undefined;
if (isElectron) {
(window as any).electron.ipcRenderer.setStoreValue('set', JSON.parse(JSON.stringify(setData)));
} else {
localStorage.setItem('appSettings', JSON.stringify(setData));
}
},
2024-12-09 21:55:08 +08:00
addToFavorite(state: State, songId: number) {
if (!state.favoriteList.includes(songId)) {
state.favoriteList = [songId, ...state.favoriteList];
localStorage.setItem('favoriteList', JSON.stringify(state.favoriteList));
}
},
removeFromFavorite(state: State, songId: number) {
state.favoriteList = state.favoriteList.filter((id) => id !== songId);
localStorage.setItem('favoriteList', JSON.stringify(state.favoriteList));
},
togglePlayMode(state: State) {
state.playMode = state.playMode === 0 ? 1 : 0;
localStorage.setItem('playMode', JSON.stringify(state.playMode));
},
};
const actions = {
initializeSettings({ commit }: { commit: any }) {
const isElectron = (window as any).electronAPI !== undefined;
if (isElectron) {
const setData = (window as any).electron.ipcRenderer.getStoreValue('set');
commit('setSetData', setData || defaultSettings);
} else {
const savedSettings = localStorage.getItem('appSettings');
if (savedSettings) {
commit('setSetData', {
...defaultSettings,
...JSON.parse(savedSettings),
});
} else {
commit('setSetData', defaultSettings);
}
}
2023-12-28 10:45:11 +08:00
},
};
const store = createStore({
state,
mutations,
actions,
});
export default store;