Files
AlgerMusicPlayer/src/store/index.ts
T

127 lines
3.5 KiB
TypeScript
Raw Normal View History

import { createStore } from 'vuex';
import { getMusicUrl, getParsingMusicUrl } from '@/api/music';
import { useMusicHistory } from '@/hooks/MusicHistoryHook';
import homeRouter from '@/router/home';
2024-09-13 14:11:02 +08:00
import type { SongResult } from '@/type/music';
2024-09-18 15:11:20 +08:00
import { getImgUrl, getMusicProxyUrl } from '@/utils';
import { getImageLinearBackground } from '@/utils/linearColor';
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;
}
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: '',
2024-05-22 15:14:26 +08:00
user: localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user') as string) : null,
playList: [],
playListIndex: 0,
2023-12-28 10:45:11 +08:00
setData: null,
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,
};
const windowData = window as any;
2023-12-28 10:45:11 +08:00
const musicHistory = useMusicHistory();
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) {
2024-09-18 15:11:20 +08:00
await getSongDetail(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) {
2023-12-21 16:45:06 +08:00
if (state.playList.length === 0) {
state.play = true;
return;
2023-12-21 16:45:06 +08:00
}
2024-09-18 15:11:20 +08:00
const playListIndex = (state.playListIndex + 1) % state.playList.length;
await getSongDetail(state, state.playList[playListIndex]);
},
async prevPlay(state: State) {
2023-12-21 16:45:06 +08:00
if (state.playList.length === 0) {
state.play = true;
return;
2023-12-21 16:45:06 +08:00
}
2024-09-18 15:11:20 +08:00
const playListIndex = (state.playListIndex - 1 + state.playList.length) % state.playList.length;
await getSongDetail(state, state.playList[playListIndex]);
},
2023-12-28 10:45:11 +08:00
async setSetData(state: State, setData: any) {
state.setData = setData;
windowData.electron.ipcRenderer.setStoreValue('set', JSON.parse(JSON.stringify(setData)));
2023-12-28 10:45:11 +08:00
},
};
2021-07-21 17:45:21 +08:00
const getSongUrl = async (id: number) => {
const { data } = await getMusicUrl(id);
let url = '';
try {
2024-01-01 00:06:52 +08:00
if (data.data[0].freeTrialInfo || !data.data[0].url) {
const res = await getParsingMusicUrl(id);
url = res.data.data.url;
}
} catch (error) {
console.error('error', error);
}
url = url || data.data[0].url;
return getMusicProxyUrl(url);
};
2021-07-21 17:45:21 +08:00
const updatePlayMusic = async (state: State) => {
state.playMusic = state.playList[state.playListIndex];
state.playMusicUrl = await getSongUrl(state.playMusic.id);
state.play = true;
musicHistory.addMusic(state.playMusic);
};
2024-09-18 15:11:20 +08:00
const getSongDetail = async (state: State, playMusic: SongResult) => {
state.playMusic.playLoading = true;
state.playMusicUrl = await getSongUrl(playMusic.id);
const backgroundColor = playMusic.backgroundColor
? playMusic.backgroundColor
: await getImageLinearBackground(getImgUrl(playMusic?.picUrl, '30y30'));
state.playMusic = { ...playMusic, backgroundColor };
// state.playMusic = { ...playMusic };
state.play = true;
musicHistory.addMusic(playMusic);
state.playListIndex = state.playList.findIndex((item) => item.id === playMusic.id);
state.playMusic.playLoading = false;
};
const store = createStore({
state,
mutations,
});
export default store;