From 48cfebd5aec75e8daf363421f982b24941ffff6a Mon Sep 17 00:00:00 2001 From: lyswhut Date: Thu, 25 Jun 2020 19:37:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E8=81=9A=E5=90=88=E6=90=9C?= =?UTF-8?q?=E7=B4=A2=E7=9A=84=E6=90=9C=E7=B4=A2=E7=BB=93=E6=9E=9C=E6=8E=92?= =?UTF-8?q?=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/material/Pagination.vue | 6 +- src/renderer/store/modules/search.js | 86 ++++++++++++++++++- src/renderer/utils/music/wy/musicSearch.js | 2 + src/renderer/views/Search.vue | 4 +- 4 files changed, 91 insertions(+), 7 deletions(-) diff --git a/src/renderer/components/material/Pagination.vue b/src/renderer/components/material/Pagination.vue index 53ca3bf3..fe68cbcd 100644 --- a/src/renderer/components/material/Pagination.vue +++ b/src/renderer/components/material/Pagination.vue @@ -56,6 +56,10 @@ export default { type: Number, default: 7, }, + maxPage: { + type: Number, + default: null, + }, }, data() { return { @@ -65,7 +69,7 @@ export default { computed: { ...mapGetters(['userInfo']), allPage() { - return Math.ceil(this.count / this.limit) || 1 + return this.maxPage == null ? Math.ceil(this.count / this.limit) || 1 : this.maxPage }, pageEvg() { return Math.floor(this.btnLength / 2) diff --git a/src/renderer/store/modules/search.js b/src/renderer/store/modules/search.js index 18ce121b..4490e824 100644 --- a/src/renderer/store/modules/search.js +++ b/src/renderer/store/modules/search.js @@ -27,6 +27,67 @@ for (const source of music.sources) { sourceMaxPage[source.id] = 0 } +// https://blog.csdn.net/xcxy2015/article/details/77164126#comments +const similar = (a, b) => { + if (!a || !b) return 0 + if (a.length > b.length) { // 保证 a <= b + let t = b + b = a + a = t + } + let al = a.length + let bl = b.length + let mp = [] // 一个表 + let i, j, ai, lt, tmp // ai:字符串a的第i个字符。 lt:左上角的值。 tmp:暂存新的值。 + for (i = 0; i <= bl; i++) mp[i] = i + for (i = 1; i <= al; i++) { + ai = a.charAt(i - 1) + lt = mp[0] + mp[0] = mp[0] + 1 + for (j = 1; j <= bl; j++) { + tmp = Math.min(mp[j] + 1, mp[j - 1] + 1, lt + (ai == b.charAt(j - 1) ? 0 : 1)) + lt = mp[j] + mp[j] = tmp + } + } + return 1 - (mp[bl] / bl) +} + +const sortInsert = (arr, data) => { + let key = data.num + let left = 0 + let right = arr.length - 1 + + while (left <= right) { + let middle = parseInt((left + right) / 2) + if (key == arr[middle]) { + left = middle + break + } else if (key < arr[middle].num) { + right = middle - 1 + } else { + left = middle + 1 + } + } + while (left > 0) { + if (arr[left - 1].num != key) break + left-- + } + + arr.splice(left, 0, data) +} + +const handleSortList = (list, keyword) => { + let arr = [] + for (const item of list) { + sortInsert(arr, { + num: similar(keyword, `${item.name} ${item.singer}`), + data: item, + }) + } + return arr.map(item => item.data).reverse() +} + sources.push({ id: 'all', name: '聚合搜索', @@ -65,12 +126,29 @@ const actions = { let task = [] for (const source of sources) { if (source.id == 'all') continue - task.push(music[source.id].musicSearch.search(text, page)) + task.push(music[source.id].musicSearch.search(text, page).catch(error => { + console.log(error) + return { + allPage: 1, + limit: 30, + list: [], + source: source.id, + total: 0, + } + })) } Promise.all(task).then(results => commit('setLists', { results, page })) } else { - return music[rootState.setting.search.searchSource].musicSearch.search(text, page, limit) - .then(data => commit('setList', { page, ...data })) + return music[rootState.setting.search.searchSource].musicSearch.search(text, page, limit).catch(error => { + console.log(error) + return { + allPage: 1, + limit: 30, + list: [], + source: rootState.setting.search.searchSource, + total: 0, + } + }).then(data => commit('setList', { page, ...data })) } }, } @@ -101,7 +179,7 @@ const mutations = { total += source.total limit += source.limit } - list.sort((a, b) => a.name.charCodeAt(0) - b.name.charCodeAt(0)) + list = handleSortList(list, state.text) state.allPage = Math.max(...pages) state.total = total state.limit = limit diff --git a/src/renderer/utils/music/wy/musicSearch.js b/src/renderer/utils/music/wy/musicSearch.js index 9d31cd8d..78ef0125 100644 --- a/src/renderer/utils/music/wy/musicSearch.js +++ b/src/renderer/utils/music/wy/musicSearch.js @@ -30,6 +30,7 @@ export default { }, handleResult(rawList) { // console.log(rawList) + if (!rawList) return [] return rawList.map(item => { const types = [] const _types = {} @@ -83,6 +84,7 @@ export default { if (limit != null) this.limit = limit return this.musicSearch(str, page).then(result => { if (!result || result.code !== 200) return this.search(str, page, { limit }, retryNum) + // console.log(result.result) let list = this.handleResult(result.result.songs) if (list == null) return this.search(str, page, { limit }, retryNum) diff --git a/src/renderer/views/Search.vue b/src/renderer/views/Search.vue index dbf44adc..98909784 100644 --- a/src/renderer/views/Search.vue +++ b/src/renderer/views/Search.vue @@ -36,7 +36,7 @@ :download-btn="assertApiSupport(item.source)" @btn-click="handleListBtnClick") div(:class="$style.pagination") - material-pagination(:count="listInfo.total" :limit="listInfo.limit" :page="page" @btn-click="handleTogglePage") + material-pagination(:max-page="listInfo.allPage" :limit="listInfo.limit" :page="page" @btn-click="handleTogglePage") div(v-else :class="$style.noitem") div.scroll(:class="$style.noitemListContainer" v-if="setting.search.isShowHotSearch || setting.search.isShowHistorySearch") dl(:class="[$style.noitemList, $style.noitemHotSearchList]" v-if="setting.search.isShowHotSearch") @@ -115,7 +115,7 @@ export default { // console.log('mounted') // 处理搜索源不存在时页面报错的问题 - if (!this.sourceList[this.setting.search.searchSource]) { + if (!this.sourceList[this.setting.search.searchSource] && this.setting.search.searchSource != 'all') { this.setSearchSource({ searchSource: 'kw', })