新增热搜词、历史搜索记录
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import music from '../../utils/music'
|
||||
const sources = []
|
||||
const sourceList = {}
|
||||
for (const source of music.sources) {
|
||||
const hotSearch = music[source.id].hotSearch
|
||||
if (!hotSearch) continue
|
||||
sources.push(source)
|
||||
sourceList[source.id] = []
|
||||
}
|
||||
|
||||
sources.push({
|
||||
id: 'all',
|
||||
name: '聚合搜索',
|
||||
})
|
||||
|
||||
// state
|
||||
const state = {
|
||||
list: {
|
||||
...sourceList,
|
||||
all: [],
|
||||
},
|
||||
currentSource: null,
|
||||
}
|
||||
|
||||
// getters
|
||||
const getters = {
|
||||
sources(state, getters, rootState, { sourceNames }) {
|
||||
return sources.map(item => ({ id: item.id, name: sourceNames[item.id] }))
|
||||
},
|
||||
list: state => state.list,
|
||||
}
|
||||
|
||||
// actions
|
||||
const actions = {
|
||||
getList({ state, commit }, source) {
|
||||
if (source == 'all') {
|
||||
let task = []
|
||||
for (const source of sources) {
|
||||
if (source.id == 'all') continue
|
||||
task.push(
|
||||
state.list[source.id].length
|
||||
? Promise.resolve({ source: source.id, list: state.list[source.id] })
|
||||
: music[source.id].hotSearch.getList(),
|
||||
)
|
||||
}
|
||||
Promise.all(task).then(results => commit('setLists', results))
|
||||
} else {
|
||||
if (!music[source].hotSearch) {
|
||||
commit('setList', { source, data: null })
|
||||
return Promise.resolve()
|
||||
}
|
||||
return music[source].hotSearch.getList()
|
||||
.then(data => commit('setList', { source, data }))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// mitations
|
||||
const mutations = {
|
||||
setList(state, { source, data }) {
|
||||
state.list[source] = data ? data.list.slice(0, 20) : []
|
||||
},
|
||||
setLists(state, lists) {
|
||||
let list = []
|
||||
for (const source of lists) {
|
||||
if (!state.list[source.source].length) state.list[source.source] = source.list.slice(0, 20)
|
||||
const sourceList = source.list.slice(0, 10)
|
||||
for (const item of sourceList) {
|
||||
list.push(item.trim())
|
||||
}
|
||||
}
|
||||
list = Array.from(new Set(list))
|
||||
list.sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0))
|
||||
state.list.all = list
|
||||
},
|
||||
clearList(state, source) {
|
||||
state.list[source] = []
|
||||
},
|
||||
}
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state,
|
||||
getters,
|
||||
actions,
|
||||
mutations,
|
||||
}
|
||||
@@ -85,7 +85,7 @@ const mutations = {
|
||||
listClear(state, id) {
|
||||
let targetList = getList(state, id)
|
||||
if (!targetList) return
|
||||
targetList.list.length = 0
|
||||
targetList.list.length = []
|
||||
},
|
||||
updateMusicInfo(state, { id, index, data }) {
|
||||
let targetList = getList(state, id)
|
||||
|
||||
@@ -1,4 +1,15 @@
|
||||
import Store from 'electron-store'
|
||||
import music from '../../utils/music'
|
||||
|
||||
const electronStore_data = window.electronStore_data = new Store({
|
||||
name: 'data',
|
||||
})
|
||||
let historyList = electronStore_data.get('searchHistoryList')
|
||||
if (historyList == null) {
|
||||
historyList = []
|
||||
electronStore_data.set('searchHistoryList', historyList)
|
||||
}
|
||||
|
||||
const sources = []
|
||||
const sourceList = {}
|
||||
const sourceMaxPage = {}
|
||||
@@ -31,6 +42,7 @@ const state = {
|
||||
allPage: 1,
|
||||
total: 0,
|
||||
sourceMaxPage,
|
||||
historyList,
|
||||
}
|
||||
|
||||
// getters
|
||||
@@ -40,28 +52,34 @@ const getters = {
|
||||
},
|
||||
sourceList: state => state.sourceList || [],
|
||||
searchText: state => state.text,
|
||||
historyList: state => state.historyList,
|
||||
allList: state => ({ list: state.list, allPage: state.allPage, page: state.page, total: state.total, limit: state.limit, sourceMaxPage: state.sourceMaxPage }),
|
||||
}
|
||||
|
||||
// actions
|
||||
const actions = {
|
||||
search({ commit, rootState }, { text, page, limit }) {
|
||||
commit('setText', text)
|
||||
commit('addHistory', text)
|
||||
if (rootState.setting.search.searchSource == 'all') {
|
||||
let task = []
|
||||
for (const source of sources) {
|
||||
if (source.id == 'all') continue
|
||||
task.push(music[source.id].musicSearch.search(text, page))
|
||||
}
|
||||
Promise.all(task).then(results => commit('setLists', { results, text, page }))
|
||||
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', { text, page, ...data }))
|
||||
.then(data => commit('setList', { page, ...data }))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// mitations
|
||||
const mutations = {
|
||||
setText(state, text) {
|
||||
state.text = text
|
||||
},
|
||||
setList(state, datas) {
|
||||
let source = state.sourceList[datas.source]
|
||||
source.list = datas.list
|
||||
@@ -69,9 +87,8 @@ const mutations = {
|
||||
source.allPage = datas.allPage
|
||||
source.page = datas.page
|
||||
source.limit = datas.limit
|
||||
state.text = datas.text
|
||||
},
|
||||
setLists(state, { results, text, page }) {
|
||||
setLists(state, { results, page }) {
|
||||
let pages = []
|
||||
let total = 0
|
||||
let limit = 0
|
||||
@@ -84,28 +101,39 @@ const mutations = {
|
||||
total += source.total
|
||||
limit += source.limit
|
||||
}
|
||||
list.sort((a, b) => b.name.charCodeAt(0) - a.name.charCodeAt(0))
|
||||
list.sort((a, b) => a.name.charCodeAt(0) - b.name.charCodeAt(0))
|
||||
state.allPage = Math.max(...pages)
|
||||
state.total = total
|
||||
state.limit = limit
|
||||
state.page = page
|
||||
state.text = text
|
||||
state.list = list
|
||||
},
|
||||
clearList(state) {
|
||||
for (const source of Object.keys(state.sourceList)) {
|
||||
state.sourceList[source].list.length = 0
|
||||
state.sourceList[source].list = []
|
||||
state.sourceList[source].page = 0
|
||||
state.sourceList[source].allPage = 0
|
||||
state.sourceList[source].total = 0
|
||||
state.sourceMaxPage[source] = 0
|
||||
}
|
||||
state.list.length = 0
|
||||
state.list.length = []
|
||||
state.page = 0
|
||||
state.allPage = 0
|
||||
state.total = 0
|
||||
state.text = ''
|
||||
},
|
||||
addHistory(state, text) {
|
||||
let index = state.historyList.indexOf(text)
|
||||
if (index > -1) state.historyList.splice(index, 1)
|
||||
if (state.historyList.length >= 15) state.historyList = state.historyList.slice(0, 14)
|
||||
state.historyList.unshift(text)
|
||||
},
|
||||
removeHistory(state, index) {
|
||||
state.historyList.splice(index, 1)
|
||||
},
|
||||
clearHistory(state) {
|
||||
state.historyList = []
|
||||
},
|
||||
}
|
||||
|
||||
export default {
|
||||
|
||||
Reference in New Issue
Block a user