新增热搜词、历史搜索记录

This commit is contained in:
lyswhut
2020-03-15 17:17:41 +08:00
parent 9d1270374e
commit 670533807a
29 changed files with 460 additions and 18 deletions
+36 -8
View File
@@ -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 {