From 1af3cd1d77822374961b8588adf32267ab69beba Mon Sep 17 00:00:00 2001 From: lyswhut Date: Mon, 18 Jan 2021 16:21:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=92=AD=E6=94=BE=E6=AD=8C?= =?UTF-8?q?=E6=9B=B2=E6=8D=A2=E6=BA=90=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- publish/changeLog.md | 4 ++ src/renderer/store/modules/list.js | 14 ++++++ src/renderer/utils/music/index.js | 68 +++++++++++++----------------- 3 files changed, 47 insertions(+), 39 deletions(-) diff --git a/publish/changeLog.md b/publish/changeLog.md index 71ac33d3..06db02d5 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -1,3 +1,7 @@ ### 新增 - 搜索界面新增搜索状态的提示 + +### 优化 + +- 优化播放歌曲换源匹配 diff --git a/src/renderer/store/modules/list.js b/src/renderer/store/modules/list.js index ce6ce500..209eacdc 100644 --- a/src/renderer/store/modules/list.js +++ b/src/renderer/store/modules/list.js @@ -57,6 +57,20 @@ const mutations = { if (defaultList != null) Object.assign(state.defaultList, { list: defaultList.list, location: defaultList.location }) if (loveList != null) Object.assign(state.loveList, { list: loveList.list, location: loveList.location }) if (userList != null) state.userList = userList + if (window.localStorage.getItem('isResetOtherSource') != '1') { + for (const item of defaultList.list) { + if (item.otherSource) item.otherSource = null + } + for (const item of loveList.list) { + if (item.otherSource) item.otherSource = null + } + for (const list of userList) { + for (const item of list.list) { + if (item.otherSource) item.otherSource = null + } + } + window.localStorage.setItem('isResetOtherSource', '1') + } allListInit(state.defaultList, state.loveList, state.userList) state.isInitedList = true }, diff --git a/src/renderer/utils/music/index.js b/src/renderer/utils/music/index.js index 9653e637..54d708a3 100644 --- a/src/renderer/utils/music/index.js +++ b/src/renderer/utils/music/index.js @@ -59,25 +59,37 @@ export default { async findMusic(musicInfo) { const tasks = [] + const sortSingle = singer => singer.includes('、') ? singer.split('、').sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0)).join('、') : singer + const sortMusic = (arr, callback) => { + const tempResult = [] + for (let i = arr.length - 1; i > -1; i--) { + const item = arr[i] + if (callback(item)) { + delete item.sortedSinger + tempResult.push(item) + arr.splice(i, 1) + } + } + return tempResult.reverse() + } + const trimStr = str => typeof str == 'string' ? str.trim() : str + const sortedSinger = sortSingle(musicInfo.singer) + const musicName = trimStr(musicInfo.name) for (const source of sources.sources) { if (!sources[source.id].musicSearch || source.id === musicInfo.source || source.id === 'xm') continue - const sortedSinger = musicInfo.singer.includes('、') ? musicInfo.singer.split('、').sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0)).join('、') : null - tasks.push(sources[source.id].musicSearch.search(`${musicInfo.name} ${musicInfo.singer || ''}`.trim(), 1, { limit: 10 }).then(res => { + tasks.push(sources[source.id].musicSearch.search(`${musicName} ${musicInfo.singer || ''}`.trim(), 1, { limit: 10 }).then(res => { for (const item of res.list) { + item.sortedSinger = sortSingle(item.singer) + item.name = trimStr(item.name) if ( ( - item.singer === musicInfo.singer && - (item.name === musicInfo.name || item.interval === musicInfo.interval) + item.sortedSinger === sortedSinger && + (item.name === musicName || item.interval === musicInfo.interval) ) || ( - item.interval === musicInfo.interval && item.name === musicInfo.name && - (item.singer.includes(musicInfo.singer) || musicInfo.singer.includes(item.singer)) - ) || - ( - sortedSinger && - item.singer.includes('、') && - item.singer.split('、').sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0)).join('、') === sortedSinger + item.interval === musicInfo.interval && item.name === musicName && + (item.sortedSinger.includes(sortedSinger) || sortedSinger.includes(item.sortedSinger)) ) ) { return item @@ -89,35 +101,13 @@ export default { const result = (await Promise.all(tasks)).filter(s => s) const newResult = [] if (result.length) { - for (let i = result.length - 1; i > -1; i--) { - const item = result[i] - if (item.singer === musicInfo.singer && item.name === musicInfo.name && item.interval === musicInfo.interval) { - newResult.push(item) - result.splice(i, 1) - } + newResult.push(...sortMusic(result, item => item.sortedSinger === sortedSinger && item.name === musicName && item.interval === musicInfo.interval)) + newResult.push(...sortMusic(result, item => item.sortedSinger === sortedSinger && item.interval === musicInfo.interval)) + newResult.push(...sortMusic(result, item => item.name === musicName && item.sortedSinger === sortedSinger && item.albumName === musicInfo.albumName)) + newResult.push(...sortMusic(result, item => item.sortedSinger === sortedSinger && item.name === musicName)) + for (const item of result) { + delete item.sortedSinger } - for (let i = result.length - 1; i > -1; i--) { - const item = result[i] - if (item.singer === musicInfo.singer && item.interval === musicInfo.interval) { - newResult.push(item) - result.splice(i, 1) - } - } - for (let i = result.length - 1; i > -1; i--) { - const item = result[i] - if (item.name === musicInfo.name && item.singer === musicInfo.singer && item.albumName === musicInfo.albumName) { - newResult.push(item) - result.splice(i, 1) - } - } - for (let i = result.length - 1; i > -1; i--) { - const item = result[i] - if (item.singer === musicInfo.singer && item.name === musicInfo.name) { - newResult.push(item) - result.splice(i, 1) - } - } - newResult.reverse() newResult.push(...result) } // console.log(newResult)