-
Notifications
You must be signed in to change notification settings - Fork 177
/
index.js
208 lines (199 loc) · 6.13 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import Vue from 'vue'
import Vuex from 'vuex';
import {getCurrentIndex, shuffle, floatNumber } from '../util';
Vue.use(Vuex);
/* ==========================================================
* Vuex 状态管理
* 简介:
* APP灵魂所在,在这里使用Vuex统一管理页面切换动画,歌曲播放状态,
* 歌曲进度等信息。所有对于歌曲的操作都通过Vuex来进行全局管理,然后
* 对相应的变化做出全局改变。
* ========================================================== */
export default new Vuex.Store({
modules: {
// route switch object
transition: {
namespaced: true,
state: {
transitionName: '',
action: []
},
mutations: {
setTransition(state, transition) {
state.transitionName = transition;
}
}
},
// bottom fixed song list
list: {
namespaced: true,
state: {
// if show the list
show: false,
// modify the list style
class: ''
},
mutations: {
toggleShow(state) {
state.show = !state.show;
},
modifyClass(state, style) {
state.class = style;
}
}
},
playing: {
namespaced: true,
state: {
// playing song detail msg
songMsg: {
data: {},
getMedia: media => media ? `http://ws.stream.qqmusic.qq.com/${media}.m4a?fromtag=46` : '',
songblum_prfix: 'http://imgcache.qq.com/music/photo_new/T002R150x150M000'
},
// song list
songlist: [],
songState: {
// Music Play State Provide Two State
// 1: pause, 2: playing + songid
playingState: 'pause',
// current time play percentage
playingProgress: 0,
// current play progress time
current: 0,
// 用于记录歌曲快进或后退后的时间进度
pruneTime: 0,
// playing song index in current songlist
currentIndex: 0,
// curent playing lyric index in currentLyric array
currentLyricIndex: '',
// current lyric playing time
currentLyricDuration: '',
// current lyrics array
currentLyricArr: [],
// Song Duration Time
timing: 0,
// playingOrder provide three state
// 1: cycle, 2: singleCycle, 3: random,
playingOrder: 'cycle'
}
},
mutations: {
// play or pause music
pause(state, status) {
let songid = state.songMsg.data.songid;
state.songState.playingState = status == 'pause' || songid == null ? 'pause' : 'playing' + songid;
},
pruneCurrentTime(state, time) {
state.songState.pruneTime = state.songState.timing*time || 0;
},
// swtich the current lyric index
switchLyricIndex(state, index) {
state.songState.currentLyricIndex = index;
},
switchLyricsArr(state, lyricArr) {
state.songState.currentLyricArr = lyricArr instanceof Array ? lyricArr : [];
},
switchLyricDuration(state, duration) {
state.songState.currentLyricDuration = duration;
},
// stack songlist
stackSonglist(state, stack) {
state.songlist = [...(stack instanceof Array ? stack : [stack || {}])];
state.songState.currentIndex = 0;
},
pushSonglist(state, stack) {
state.songlist = [...state.songlist, ...(stack instanceof Array ? stack : [])];
},
switchPlayOrder(state, order) {
let orderArr = ['cycle', 'singleCycle', 'random'],
current = orderArr.indexOf(state.songState.playingOrder),
next = orderArr.indexOf(order);
state.songState.playingOrder = next > -1 ? orderArr[next] : orderArr[current >= 2 ? 0 : current + 1];
}
},
actions: {
// Reset Play Progress To Some Point
resetProgress({state, commit, dispatch}, payload = {currentTime: 0, duration: 0}) {
let current = parseInt(payload.currentTime) || 0,
timing = parseInt(payload.duration) || 0;
state.songState.playingProgress = current/timing;
state.songState.current = current;
state.songState.timing = timing;
if (current >= timing && timing>0) {
dispatch('playSong', 'next');
commit('pruneCurrentTime', 0);
}
},
// 用于歌曲的播放进度调整
pruneProgress({state, dispatch}, progress) {
let formatProgress;
if (progress <0) {
formatProgress = 0
}else if (progress > 1) {
formatProgress = 1;
}else {
formatProgress = progress;
}
dispatch('resetProgress', {
currentTime: state.songState.timing*formatProgress,
duration: state.songState.timing
});
},
/*
* play a new song
* index == string ? next : prev
* index == number go specify song
* */
playSong({state, commit, dispatch}, index) {
let nextIndex;
// judge if play next or play specify song
if (typeof index == 'string') {
let playingOrder = state.songState.playingOrder,
songlist = state.songlist,
length = songlist.length,
currentIndex = state.songState.currentIndex;
switch(playingOrder) {
case 'cycle':
if (index == 'next') {
nextIndex = currentIndex >= length - 1 ? 0 : ++currentIndex;
}else if (index == 'prev') {
nextIndex = currentIndex == 0 ? length - 1 : --currentIndex;
}
break;
case 'random':
nextIndex = Math.random()*songlist.length >> 0;
break;
case 'singleCycle':
nextIndex = currentIndex;
break;
}
}else {
nextIndex = index >> 0;
}
let songMsg = state.songlist[nextIndex];
state.songState.currentIndex = nextIndex;
state.songMsg = {...state.songMsg, ...songMsg};
state.songState.playingState = 'play' + state.songMsg.data.songid;
dispatch('resetProgress');
},
// clear song in stack list
clearSong({state, dispatch}, index) {
state.songlist.splice(index >> 0, 1);
},
// clear all song msg
clearSongStack({state, commit, dispatch}) {
let defaultSongMsg = {
data: {},
getMedia: media => media ? `http://ws.stream.qqmusic.qq.com/${media}.m4a?fromtag=46` : '',
songblum_prfix: 'http://imgcache.qq.com/music/photo_new/T002R150x150M000'
};
state.songlist = [];
state.songMsg = defaultSongMsg;
// dispatch('resetProgress');
commit('pause', 'pause');
}
}
}
}
});