mirror of
https://github.com/algerkong/AlgerMusicPlayer.git
synced 2026-04-24 08:07:23 +08:00
✨ feat(Play): 完成播放可以根据列表播放 上一首 下一首
This commit is contained in:
+49
-16
@@ -2,31 +2,58 @@ import { createStore } from "vuex";
|
||||
import { SongResult } from "@/type/music";
|
||||
import { getMusicUrl } from "@/api/music";
|
||||
import homeRouter from "@/router/home";
|
||||
let state = {
|
||||
|
||||
interface State {
|
||||
menus: any[]
|
||||
play: boolean
|
||||
isPlay: boolean
|
||||
playMusic: SongResult
|
||||
playMusicUrl: string
|
||||
user: any
|
||||
playList: SongResult[]
|
||||
playListIndex: number
|
||||
}
|
||||
|
||||
const state: State = {
|
||||
menus: homeRouter,
|
||||
play: false,
|
||||
isPlay: false,
|
||||
playMusic: {} as SongResult,
|
||||
playMusicUrl: "",
|
||||
user: null as any,
|
||||
};
|
||||
playMusicUrl: '',
|
||||
user: null,
|
||||
playList: [],
|
||||
playListIndex: 0,
|
||||
}
|
||||
|
||||
let mutations = {
|
||||
setMenus(state: any, menus: any[]) {
|
||||
state.menus = menus;
|
||||
const mutations = {
|
||||
setMenus(state: State, menus: any[]) {
|
||||
state.menus = menus
|
||||
},
|
||||
async setPlay(state: any, playMusic: SongResult) {
|
||||
state.playMusic = playMusic;
|
||||
state.playMusicUrl = await getSongUrl(playMusic.id);
|
||||
state.play = true;
|
||||
async setPlay(state: State, playMusic: SongResult) {
|
||||
state.playMusic = playMusic
|
||||
state.playMusicUrl = await getSongUrl(playMusic.id)
|
||||
state.play = true
|
||||
},
|
||||
setIsPlay(state: any, isPlay: boolean) {
|
||||
state.isPlay = isPlay;
|
||||
setIsPlay(state: State, isPlay: boolean) {
|
||||
state.isPlay = isPlay
|
||||
},
|
||||
setPlayMusic(state: any, play: boolean) {
|
||||
state.play = play;
|
||||
setPlayMusic(state: State, play: boolean) {
|
||||
state.play = play
|
||||
},
|
||||
};
|
||||
setPlayList(state: State, playList: SongResult[]) {
|
||||
state.playListIndex = 0
|
||||
state.playList = playList
|
||||
},
|
||||
async nextPlay(state: State) {
|
||||
state.playListIndex = (state.playListIndex + 1) % state.playList.length
|
||||
await updatePlayMusic(state)
|
||||
},
|
||||
async prevPlay(state: State) {
|
||||
state.playListIndex =
|
||||
(state.playListIndex - 1 + state.playList.length) % state.playList.length
|
||||
await updatePlayMusic(state)
|
||||
},
|
||||
}
|
||||
|
||||
const getSongUrl = async (id: number) => {
|
||||
const { data } = await getMusicUrl(id);
|
||||
@@ -35,6 +62,12 @@ const getSongUrl = async (id: number) => {
|
||||
return data.data[0].url;
|
||||
};
|
||||
|
||||
const updatePlayMusic = async (state: State) => {
|
||||
state.playMusic = state.playList[state.playListIndex]
|
||||
state.playMusicUrl = await getSongUrl(state.playMusic.id)
|
||||
state.play = true
|
||||
}
|
||||
|
||||
const store = createStore({
|
||||
state: state,
|
||||
mutations: mutations,
|
||||
|
||||
Reference in New Issue
Block a user