forked from lvyueyang/web-music
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
311 lines (278 loc) · 7.79 KB
/
client.js
File metadata and controls
311 lines (278 loc) · 7.79 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
(function () {
/** 播放模式 */
const PLAYER_MODE = {
LOOP: {
value: 'loop',
cname: '列表循环',
},
SIGNAL: {
value: 'signal',
cname: '单曲循环',
},
RANDOM: {
value: 'random',
},
};
const CATCH_DATA_KEY = 'MUSIC_DATA';
const coverDom = document.querySelector('#player .cover img');
const audioDom = document.querySelector('#audio');
const listDom = document.querySelector('#list');
const modalDom = document.querySelector('#modal');
const DISABLE_LIST_KEY = 'DISABLE_LIST';
let songList = []; // 歌曲列表
let currentSong = null; // 当前播放的歌曲
let playerMode = PLAYER_MODE.LOOP.value; // 播放模式
const disabledList = JSON.parse(
window.localStorage.getItem(DISABLE_LIST_KEY) || '[]',
); // 忽略播放的歌曲列表
main();
function main() {
getList();
}
/** 创建 歌曲列表 */
function createItemDom(item, index) {
return `<div
class="item"
data-cid="${item.cid}"
data-index=${index}
disabled="${!!item.disabled}"
>
<div class="index">${index + 1}</div>
<div class="name">${item.name}</div>
<div class="more">
<img class="remove" src="./icon/remove.svg" alt="移出播放列表" title="移出播放列表" />
<img class="add" src="./icon/check.svg" alt="加入播放列表" title="加入播放列表" />
</div>
</div>`;
}
/** 设置播放器内容 */
function setPlayer(currentItem = currentSong) {
// 设置封面
coverDom.setAttribute('src', currentItem.songInfo.picUrl);
// 设置播放歌曲
const songUrls = [
currentItem.songInfo.flac,
currentItem.songInfo['320'],
currentItem.songInfo['320'],
].filter((s) => !!s);
audioDom.setAttribute('src', songUrls[0]);
// 设置列表中播放歌曲的选中项
document
.querySelectorAll(`#list .active`)
.forEach((a) => a.classList.remove('active'));
document
.querySelector(`#list .item[data-cid="${currentItem.cid}"]`)
.classList.add('active');
// 设置模态框的内容
modalDom.querySelector('.title').innerHTML = currentSong.songInfo.name;
modalDom.querySelector('.lyric').innerHTML = currentSong.songInfo.lyric
.replace(/\r\n/gi, '</br>')
.replace(/\[(.+?)\]/g, '');
modalDom
.querySelector('.cover')
.setAttribute('src', currentSong.songInfo.bigPicUrl);
}
/** 播放/暂停歌曲 */
function playSong(cid) {
if (disabledList.includes(cid)) return;
if (currentSong.cid === cid && !audioDom.paused) {
// 暂停
audioDom.pause();
} else {
// 播放
currentSong = songList.find((s) => s.cid === cid);
setPlayer();
audioDom.play();
}
}
/** 获取最新的文件地址 */
function getJsonUrl() {
return fetch(
'https://api.github.com/gists/cb11eaafbe69fc7ba63c38f9ff40e0d9',
{
headers: {
Authorization: '',
Accept: 'application/vnd.github+json'
}
}
)
.then((res) => res.json())
.then((res) => {
return res.files['jay-music.json'].raw_url;
});
}
function getDataList() {
return getJsonUrl().then((url) => {
return fetch(url)
.then((res) => res.json())
.then((res) => {
return res.list;
});
});
}
/** 拉取歌曲列表 */
function getList() {
const CATCH_DATA = window.localStorage.getItem(CATCH_DATA_KEY);
const data = isJson(CATCH_DATA);
if (data) {
if ((data.validDate = getToday())) {
renderDataList(data.list);
return;
}
}
getDataList().then((list) => {
window.localStorage.setItem(
CATCH_DATA_KEY,
JSON.stringify({
list,
validDate: getToday(), // 缓存有效日期
}),
);
renderDataList(list);
});
}
function renderDataList(list) {
songList = list.map((item) => {
return {
...item,
disabled: disabledList.includes(item.cid),
songInfo: item.songInfo,
};
});
if (!currentSong) {
currentSong = songList[0];
}
renderList();
setPlayer();
}
function catchDisableList() {
window.localStorage.setItem(DISABLE_LIST_KEY, JSON.stringify(disabledList));
}
// 渲染歌曲列表
function renderList() {
listDom.innerHTML = songList.map(createItemDom).join('');
}
function addDisableList(cid) {
disabledList.push(cid);
songList = songList.map((item) => ({
...item,
disabled: disabledList.includes(item.cid),
}));
catchDisableList();
}
function removeDisableList(cid) {
const disInd = disabledList.indexOf(cid);
disabledList.splice(disInd, 1);
songList = songList.map((item) => ({
...item,
disabled: disabledList.includes(item.cid),
}));
catchDisableList();
}
/** 获取下一首有效歌曲 */
function getNexCurrent() {
let currentIndex;
let l = songList.filter((i, ind) => {
if (i.cid === currentSong.cid) {
currentIndex = ind;
}
return !i.disabled || i.cid === currentSong.cid;
});
const next = l[currentIndex + 1];
if (next) return next;
return l[0];
}
// 移出播放列表
document.querySelector('#list').addEventListener('click', (e) => {
// 点击 item
if (e.target.className.includes('remove')) {
e.stopPropagation();
const item = e.target.parentNode.parentNode;
item.setAttribute('disabled', true);
const cid = item.getAttribute('data-cid');
addDisableList(cid);
}
});
// 添加播放列表
document.querySelector('#list').addEventListener('click', (e) => {
// 点击 item
if (e.target.className.includes('add')) {
e.stopPropagation();
const item = e.target.parentNode.parentNode;
item.setAttribute('disabled', false);
const cid = item.getAttribute('data-cid');
removeDisableList(cid);
}
});
// 点击 item 播放/暂停
document.querySelector('#list').addEventListener('click', (e) => {
let item = null;
// 点击 item
if (e.target.className.includes('item')) {
item = e.target;
}
if (e.target.parentNode.className.includes('item')) {
item = e.target.parentNode;
}
if (item) {
const cid = item.getAttribute('data-cid');
playSong(cid);
}
});
// 自动播放下一首
audioDom.onended = (e) => {
let nextSong;
// 循环
if (playerMode === PLAYER_MODE.LOOP.value) {
nextSong = getNexCurrent().cid;
}
// 随机
if (playerMode === PLAYER_MODE.RANDOM.value) {
}
// 单曲循环
if (playerMode === PLAYER_MODE.SIGNAL.value) {
nextSong = currentSong.cid;
}
playSong(nextSong);
};
let timer;
// 搜索
document.querySelector('#search input').addEventListener('input', (e) => {
clearTimeout(timer);
timer = setTimeout(() => {
document.querySelectorAll('#list .item').forEach((item) => {
const name = item.querySelector('.name').innerText.trim();
if (!name.includes(e.target.value.trim())) {
item.style.display = 'none';
} else {
item.style.display = '';
}
});
}, 500);
});
document.querySelector('#player .cover').addEventListener('click', (e) => {
modalDom.style.display = 'flex';
});
document.querySelector('#modal .footer').addEventListener('click', (e) => {
modalDom.style.display = '';
});
})();
function isJson(value) {
try {
return JSON.parse(value);
} catch (e) {
return false;
}
}
function getDate(date) {
const Y = date.getFullYear();
const M = date.getMonth();
const D = date.getDay();
return `${Y}-${M}-${D}`;
}
function getToday() {
return getDate(new Date());
}
function getTomorrow() {
return getDate(new Date(new Date().getTime() + 1000 * 60 * 60 * 24));
}