支持打开短链接歌单

pull/166/head
lyswhut 2020-03-14 23:36:38 +08:00
parent 9ca0ec82a8
commit 353e8bd0de
1 changed files with 34 additions and 19 deletions

View File

@ -12,6 +12,7 @@ export default {
_requestObj_hotTags: null, _requestObj_hotTags: null,
_requestObj_list: null, _requestObj_list: null,
_requestObj_listDetail: null, _requestObj_listDetail: null,
_requestObj_listDetailLink: null,
limit_list: 30, limit_list: 30,
limit_song: 100000, limit_song: 100000,
successCode: 200, successCode: 200,
@ -45,11 +46,26 @@ export default {
return arr.join('、') 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 (this._requestObj_listDetail) this._requestObj_listDetail.cancelHttp()
if (tryNum > 2) return Promise.reject(new Error('try max num')) 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', { this._requestObj_listDetail = httpFetch('https://music.163.com/api/linux/forward', {
method: 'post', method: 'post',
@ -64,23 +80,22 @@ export default {
}, },
}), }),
}) })
return this._requestObj_listDetail.promise.then(({ body }) => { const { body } = await this._requestObj_listDetail.promise
if (body.code !== this.successCode) return this.getListDetail(id, page, ++tryNum) if (body.code !== this.successCode) return this.getListDetail(id, page, ++tryNum)
return { return {
list: this.filterListDetail(body), list: this.filterListDetail(body),
page, page,
limit: this.limit_song, limit: this.limit_song,
total: body.playlist.tracks.length, total: body.playlist.tracks.length,
source: 'wy', source: 'wy',
info: { info: {
play_count: this.formatPlayCount(body.playlist.playCount), play_count: this.formatPlayCount(body.playlist.playCount),
name: body.playlist.name, name: body.playlist.name,
img: body.playlist.coverImgUrl, img: body.playlist.coverImgUrl,
desc: body.playlist.description, desc: body.playlist.description,
author: body.playlist.creator.nickname, author: body.playlist.creator.nickname,
}, },
} }
})
}, },
filterListDetail({ playlist: { tracks }, privileges }) { filterListDetail({ playlist: { tracks }, privileges }) {
// console.log(tracks, privileges) // console.log(tracks, privileges)