From 353e8bd0de12cd3c9fbf9b84cfe573e4723dcd9b Mon Sep 17 00:00:00 2001 From: lyswhut Date: Sat, 14 Mar 2020 23:36:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=89=93=E5=BC=80=E7=9F=AD?= =?UTF-8?q?=E9=93=BE=E6=8E=A5=E6=AD=8C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/utils/music/wy/songList.js | 53 ++++++++++++++++--------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/renderer/utils/music/wy/songList.js b/src/renderer/utils/music/wy/songList.js index e395665e..9791eac5 100644 --- a/src/renderer/utils/music/wy/songList.js +++ b/src/renderer/utils/music/wy/songList.js @@ -12,6 +12,7 @@ export default { _requestObj_hotTags: null, _requestObj_list: null, _requestObj_listDetail: null, + _requestObj_listDetailLink: null, limit_list: 30, limit_song: 100000, successCode: 200, @@ -45,11 +46,26 @@ export default { return arr.join('、') }, - getListDetail(id, page, tryNum = 0) { // 获取歌曲列表内的音乐 + async handleParseId(link, retryNum = 0) { + if (this._requestObj_listDetailLink) this._requestObj_listDetailLink.cancelHttp() + if (retryNum > 2) return Promise.reject(new Error('link try max num')) + + this._requestObj_listDetailLink = httpFetch(link) + const { headers: { location }, statusCode } = await this._requestObj_listDetailLink.promise + // console.log(headers) + if (statusCode > 400) return this.handleParseId(link, ++retryNum) + return location == null ? link : location + }, + + async getListDetail(id, page, tryNum = 0) { // 获取歌曲列表内的音乐 if (this._requestObj_listDetail) this._requestObj_listDetail.cancelHttp() if (tryNum > 2) return Promise.reject(new Error('try max num')) - if ((/[?&:/]/.test(id))) id = id.replace(this.regExps.listDetailLink, '$1') + if ((/[?&:/]/.test(id))) { + if (!this.regExps.listDetailLink.test(id)) id = await this.handleParseId(id) + // console.log(id) + id = id.replace(this.regExps.listDetailLink, '$1') + } this._requestObj_listDetail = httpFetch('https://music.163.com/api/linux/forward', { method: 'post', @@ -64,23 +80,22 @@ export default { }, }), }) - return this._requestObj_listDetail.promise.then(({ body }) => { - if (body.code !== this.successCode) return this.getListDetail(id, page, ++tryNum) - return { - list: this.filterListDetail(body), - page, - limit: this.limit_song, - total: body.playlist.tracks.length, - source: 'wy', - info: { - play_count: this.formatPlayCount(body.playlist.playCount), - name: body.playlist.name, - img: body.playlist.coverImgUrl, - desc: body.playlist.description, - author: body.playlist.creator.nickname, - }, - } - }) + const { body } = await this._requestObj_listDetail.promise + if (body.code !== this.successCode) return this.getListDetail(id, page, ++tryNum) + return { + list: this.filterListDetail(body), + page, + limit: this.limit_song, + total: body.playlist.tracks.length, + source: 'wy', + info: { + play_count: this.formatPlayCount(body.playlist.playCount), + name: body.playlist.name, + img: body.playlist.coverImgUrl, + desc: body.playlist.description, + author: body.playlist.creator.nickname, + }, + } }, filterListDetail({ playlist: { tracks }, privileges }) { // console.log(tracks, privileges)