From b907eb652a715b8fe447ec98f8435da446a628b6 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Wed, 8 Jun 2022 17:38:08 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81mg=E6=BA=90=E7=9A=84=E6=AD=8C?= =?UTF-8?q?=E8=AF=8D=E7=BF=BB=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- publish/changeLog.md | 4 +++ src/utils/music/mg/leaderboard.js | 2 ++ src/utils/music/mg/lyric.js | 55 +++++++++++++++++++++++-------- src/utils/music/mg/musicSearch.js | 4 ++- src/utils/music/mg/songList.js | 2 ++ 5 files changed, 52 insertions(+), 15 deletions(-) diff --git a/publish/changeLog.md b/publish/changeLog.md index 0404aa0..434846a 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -3,6 +3,10 @@ - 新增设置-桌面歌词-单行歌词设置,默认关闭,启用后只显示一行歌词,超出窗口宽度自动滚动到末尾 - 新增设置-桌面歌词-显示歌词切换动画,默认启用,如果你觉得切换动画影响视觉可以将其关闭 +### 优化 + +- 支持mg源的歌词翻译(之前添加的歌曲需要去设置清空缓存才会刷新歌词) + ### 修复 - 修复桌面歌词转繁体设置不立即生效的问题 diff --git a/src/utils/music/mg/leaderboard.js b/src/utils/music/mg/leaderboard.js index 21b3647..2ac4e25 100644 --- a/src/utils/music/mg/leaderboard.js +++ b/src/utils/music/mg/leaderboard.js @@ -148,6 +148,8 @@ export default { img: item.albumImgs && item.albumImgs.length ? item.albumImgs[0].img : null, lrc: null, lrcUrl: item.lrcUrl, + mrcUrl: item.mrcUrl, + trcUrl: item.trcUrl, otherSource: null, types, _types, diff --git a/src/utils/music/mg/lyric.js b/src/utils/music/mg/lyric.js index 308ceae..73abeb7 100644 --- a/src/utils/music/mg/lyric.js +++ b/src/utils/music/mg/lyric.js @@ -1,23 +1,50 @@ import { httpFetch } from '../../request' +import musicSearch from './musicSearch' export default { + getText(url, tryNum = 0) { + const requestObj = httpFetch(url, { + headers: { + Referer: 'https://app.c.nf.migu.cn/', + 'User-Agent': 'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36', + channel: '0146921', + }, + }) + return requestObj.promise.then(({ statusCode, body }) => { + if (statusCode == 200) return body + if (tryNum > 5 || statusCode == 404) return Promise.reject('歌词获取失败') + return this.getText(url, ++tryNum) + }) + }, + getLrc(url) { + return this.getText(url) + }, + getTrc(url) { + if (!url) return Promise.resolve('') + return this.getText(url) + }, + getMusicInfo(songInfo) { + return songInfo.mrcUrl == null + ? musicSearch.search(`${songInfo.name} ${songInfo.singer || ''}`.trim(), 1, { limit: 25 }).then(({ list }) => { + const targetSong = list.find(s => s.songmid == songInfo.songmid) + return targetSong ? { lrcUrl: targetSong.lrcUrl, mrcUrl: targetSong.mrcUrl, trcUrl: targetSong.trcUrl } : Promise.reject('获取歌词失败') + }) + : Promise.resolve({ lrcUrl: songInfo.lrcUrl, mrcUrl: songInfo.mrcUrl, trcUrl: songInfo.trcUrl }) + }, getLyric(songInfo, tryNum = 0) { // console.log(songInfo.copyrightId) if (songInfo.lrcUrl) { - let requestObj = httpFetch(songInfo.lrcUrl) - requestObj.promise = requestObj.promise.then(({ body, statusCode }) => { - if (statusCode !== 200) { - if (tryNum > 5) return Promise.reject('歌词获取失败') - let tryRequestObj = this.getLyric(songInfo, ++tryNum) - requestObj.cancelHttp = tryRequestObj.cancelHttp.bind(tryRequestObj) - return tryRequestObj.promise - } - return { - lyric: body, - tlyric: '', - } - }) - return requestObj + return { + promise: this.getMusicInfo(songInfo).then(info => { + return Promise.all([this.getLrc(info.lrcUrl), this.getTrc(info.trcUrl)]).then(([lyric, tlyric]) => { + return { + lyric, + tlyric, + } + }) + }), + cancelHttp() {}, + } } else { let requestObj = httpFetch(`http://music.migu.cn/v3/api/music/audioPlayer/getLyric?copyrightId=${songInfo.copyrightId}`, { headers: { diff --git a/src/utils/music/mg/musicSearch.js b/src/utils/music/mg/musicSearch.js index 47ce6b5..43bc8c9 100644 --- a/src/utils/music/mg/musicSearch.js +++ b/src/utils/music/mg/musicSearch.js @@ -89,7 +89,7 @@ export default { name: item.name, albumName: albumNInfo.name, albumId: albumNInfo.id, - songmid: item.id, + songmid: item.copyrightId, songId: item.songId, copyrightId: item.copyrightId, source: 'mg', @@ -97,6 +97,8 @@ export default { img: item.imgItems && item.imgItems.length ? item.imgItems[0].img : null, lrc: null, lrcUrl: item.lyricUrl, + mrcUrl: item.mrcurl, + trcUrl: item.trcUrl, otherSource: null, types, _types, diff --git a/src/utils/music/mg/songList.js b/src/utils/music/mg/songList.js index c02a289..3c9ba56 100644 --- a/src/utils/music/mg/songList.js +++ b/src/utils/music/mg/songList.js @@ -214,6 +214,8 @@ export default { img: item.albumImgs && item.albumImgs.length ? item.albumImgs[0].img : null, lrc: null, lrcUrl: item.lrcUrl, + mrcUrl: item.mrcUrl, + trcUrl: item.trcUrl, otherSource: null, types, _types,