修复因kw源歌词接口停用导致该源歌词获取失败的问题

This commit is contained in:
lyswhut 2021-11-20 22:35:34 +08:00
parent bf51a79969
commit f6b5faff11
2 changed files with 46 additions and 5 deletions

View File

@ -2,6 +2,10 @@
- 添加应用初始化出错时的错误捕获输出 - 添加应用初始化出错时的错误捕获输出
### 修复
- 修复因kw源歌词接口停用导致该源歌词获取失败的问题
### 其他 ### 其他
- 更新react-native到v0.66.2 - 更新react-native到v0.66.2

View File

@ -1,19 +1,56 @@
import { httpFetch } from '../../request' import { httpFetch } from '../../request'
import { decodeName } from '../../index'
export default { export default {
formatTime(time) { formatTime(time) {
const m = parseInt(time / 60) let m = parseInt(time / 60)
const s = (time % 60).toFixed(2) let s = (time % 60).toFixed(2)
return (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s) return (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s)
}, },
transformLrc({ songinfo, lrclist }) { sortLrcArr(arr) {
const lrcSet = new Set()
let lrc = []
let lrcT = []
for (const item of arr) {
if (lrcSet.has(item.time)) {
lrc.push(item)
} else {
lrcT.push(item)
lrcSet.add(item.time)
}
}
if (lrc.length) {
lrc.unshift(lrcT.shift())
} else {
lrc = lrcT
lrcT = []
}
return {
lrc,
lrcT,
}
},
transformLrc(songinfo, lrclist) {
return `[ti:${songinfo.songName}]\n[ar:${songinfo.artist}]\n[al:${songinfo.album}]\n[by:]\n[offset:0]\n${lrclist ? lrclist.map(l => `[${this.formatTime(l.time)}]${l.lineLyric}\n`).join('') : '暂无歌词'}` return `[ti:${songinfo.songName}]\n[ar:${songinfo.artist}]\n[al:${songinfo.album}]\n[by:]\n[offset:0]\n${lrclist ? lrclist.map(l => `[${this.formatTime(l.time)}]${l.lineLyric}\n`).join('') : '暂无歌词'}`
}, },
getLyric(songId) { getLyric(songId) {
const requestObj = httpFetch(`http://m.kuwo.cn/newh5/singles/songinfoandlrc?musicId=${songId}`) const requestObj = httpFetch(`http://m.kuwo.cn/newh5/singles/songinfoandlrc?musicId=${songId}`)
requestObj.promise = requestObj.promise.then(({ body }) => { requestObj.promise = requestObj.promise.then(({ body }) => {
if (body.status != 200) return Promise.reject(new Error('请求失败')) // console.log(body)
return { lyric: this.transformLrc(body.data), tlyric: '', lxlyric: null } if (!body.data?.lrclist?.length) return Promise.reject(new Error('Get lyric failed'))
const { lrc, lrcT } = this.sortLrcArr(body.data.lrclist)
// console.log(body.data.lrclist)
// console.log(lrc, lrcT)
// console.log({
// lyric: decodeName(this.transformLrc(body.data.songinfo, lrc)),
// tlyric: decodeName(this.transformLrc(body.data.songinfo, lrcT)),
// })
return {
lyric: decodeName(this.transformLrc(body.data.songinfo, lrc)),
tlyric: lrcT.length ? decodeName(this.transformLrc(body.data.songinfo, lrcT)) : '',
}
}) })
return requestObj return requestObj
}, },