Files
AlgerMusicPlayer/src/store/index.ts
T

105 lines
2.5 KiB
TypeScript
Raw Normal View History

import { createStore } from 'vuex'
import { SongResult } from '@/type/music'
import { getMusicUrl, getParsingMusicUrl } from '@/api/music'
import homeRouter from '@/router/home'
2023-12-20 16:19:16 +08:00
import { getMusicProxyUrl } from '@/utils'
interface State {
menus: any[]
play: boolean
isPlay: boolean
playMusic: SongResult
playMusicUrl: string
user: any
playList: SongResult[]
playListIndex: number
2023-12-28 10:45:11 +08:00
setData: any
}
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,
playMusic: {} as SongResult,
playMusicUrl: '',
user: null,
playList: [],
playListIndex: 0,
2023-12-28 10:45:11 +08:00
setData: null,
}
2023-12-28 10:45:11 +08:00
const windowData = window as any
const mutations = {
setMenus(state: State, menus: any[]) {
state.menus = menus
2021-07-20 22:46:18 +08:00
},
async setPlay(state: State, playMusic: SongResult) {
state.playMusic = playMusic
state.playMusicUrl = await getSongUrl(playMusic.id)
state.play = true
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
},
setPlayList(state: State, playList: SongResult[]) {
state.playListIndex = 0
state.playList = playList
},
async nextPlay(state: State) {
2023-12-21 16:45:06 +08:00
if (state.playList.length === 0) {
state.play = true
return
}
state.playListIndex = (state.playListIndex + 1) % state.playList.length
await updatePlayMusic(state)
},
async prevPlay(state: State) {
2023-12-21 16:45:06 +08:00
if (state.playList.length === 0) {
state.play = true
return
}
state.playListIndex =
(state.playListIndex - 1 + state.playList.length) % state.playList.length
await updatePlayMusic(state)
},
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))
)
},
}
2021-07-21 17:45:21 +08:00
const getSongUrl = async (id: number) => {
const { data } = await getMusicUrl(id)
2023-12-20 16:19:16 +08:00
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)
2023-12-20 16:19:16 +08:00
url = res.data.data.url
}
} catch (error) {
console.error('error', error)
}
url = url ? url : data.data[0].url
2023-12-20 16:19:16 +08:00
return getMusicProxyUrl(url)
}
2021-07-21 17:45:21 +08:00
const updatePlayMusic = async (state: State) => {
state.playMusic = state.playList[state.playListIndex]
2023-12-20 16:19:16 +08:00
state.playMusicUrl = await getSongUrl(state.playMusic.id)
state.play = true
}
const store = createStore({
state: state,
mutations: mutations,
})
export default store