diff --git a/src/renderer/components/layout/Toolbar/SearchInput.vue b/src/renderer/components/layout/Toolbar/SearchInput.vue index 24da7200..9adda756 100644 --- a/src/renderer/components/layout/Toolbar/SearchInput.vue +++ b/src/renderer/components/layout/Toolbar/SearchInput.vue @@ -22,7 +22,7 @@ export default { const visibleList = ref(false) const tipList = ref([]) let isFocused = false - let prevTempSearchSource = '' + let prevTipSearchSource = '' const route = useRoute() const router = useRouter() @@ -44,19 +44,19 @@ export default { handleTipSearch() }) - const tipSearch = debounce(async() => { - if (searchText.value === '' && prevTempSearchSource) { + if (searchText.value === '' && prevTipSearchSource) { tipList.value = [] - music[prevTempSearchSource].tempSearch.cancelTempSearch() + music[prevTipSearchSource].tipSearch.cancelTipSearch() return } - const { temp_source } = await getSearchSetting() - prevTempSearchSource = temp_source - music[prevTempSearchSource].tempSearch.search(searchText.value).then(list => { + const { source, temp_source } = await getSearchSetting() + prevTipSearchSource = source + if (source === 'all') prevTipSearchSource = temp_source + music[prevTipSearchSource].tipSearch.search(searchText.value).then(list => { tipList.value = list }).catch(() => {}) - }, 50) + }, 100) const handleTipSearch = () => { if (!visibleList.value && isFocused) visibleList.value = true diff --git a/src/renderer/utils/musicSdk/kg/index.js b/src/renderer/utils/musicSdk/kg/index.js index 4d4ac3da..142dd9b1 100644 --- a/src/renderer/utils/musicSdk/kg/index.js +++ b/src/renderer/utils/musicSdk/kg/index.js @@ -6,8 +6,10 @@ import pic from './pic' import lyric from './lyric' import hotSearch from './hotSearch' import comment from './comment' +import tipSearch from './tipSearch' const kg = { + tipSearch, leaderboard, songList, musicSearch, diff --git a/src/renderer/utils/musicSdk/kg/tipSearch.js b/src/renderer/utils/musicSdk/kg/tipSearch.js new file mode 100644 index 00000000..0268676d --- /dev/null +++ b/src/renderer/utils/musicSdk/kg/tipSearch.js @@ -0,0 +1,25 @@ +import { createHttpFetch } from './util' + +export default { + requestObj: null, + cancelTipSearch() { + if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp() + }, + tipSearchBySong(str) { + this.cancelTipSearch() + this.requestObj = createHttpFetch(`https://searchtip.kugou.com/getSearchTip?MusicTipCount=10&keyword=${encodeURIComponent(str)}`, { + headers: { + referer: 'https://www.kugou.com/', + }, + }) + return this.requestObj.then(body => { + return body[0].RecordDatas + }) + }, + handleResult(rawData) { + return rawData.map(info => info.HintInfo) + }, + async search(str) { + return this.tipSearchBySong(str).then(result => this.handleResult(result)) + }, +} diff --git a/src/renderer/utils/musicSdk/kw/index.js b/src/renderer/utils/musicSdk/kw/index.js index b943f18a..5c89fba7 100644 --- a/src/renderer/utils/musicSdk/kw/index.js +++ b/src/renderer/utils/musicSdk/kw/index.js @@ -1,5 +1,5 @@ import { httpFetch } from '../../request' -import tempSearch from './tempSearch' +import tipSearch from './tipSearch' import musicSearch from './musicSearch' import { formatSinger, getToken } from './util' import leaderboard from './leaderboard' @@ -32,7 +32,7 @@ const kw = { // // }) // }, - tempSearch, + tipSearch, musicSearch, leaderboard, songList, diff --git a/src/renderer/utils/musicSdk/kw/tempSearch.js b/src/renderer/utils/musicSdk/kw/tipSearch.js similarity index 79% rename from src/renderer/utils/musicSdk/kw/tempSearch.js rename to src/renderer/utils/musicSdk/kw/tipSearch.js index f1f958f3..007478c6 100644 --- a/src/renderer/utils/musicSdk/kw/tempSearch.js +++ b/src/renderer/utils/musicSdk/kw/tipSearch.js @@ -1,15 +1,13 @@ -// import { httpFetch } from '../../request' import { decodeName } from '../../index' import { tokenRequest } from './util' - export default { regExps: { relWord: /RELWORD=(.+)/, }, requestObj: null, - async tempSearch(str) { - this.cancelTempSearch() + async tipSearchBySong(str) { + this.cancelTipSearch() this.requestObj = await tokenRequest(`http://www.kuwo.cn/api/www/search/searchKey?key=${encodeURIComponent(str)}`) return this.requestObj.promise.then(({ body }) => { // console.log(body) @@ -23,10 +21,10 @@ export default { return matchResult ? decodeName(matchResult[1]) : '' }) }, - cancelTempSearch() { + cancelTipSearch() { if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp() }, async search(str) { - return this.tempSearch(str).then(result => this.handleResult(result.data)) + return this.tipSearchBySong(str).then(result => this.handleResult(result.data)) }, } diff --git a/src/renderer/utils/musicSdk/mg/index.js b/src/renderer/utils/musicSdk/mg/index.js index 72ccde6a..8cd3aa63 100644 --- a/src/renderer/utils/musicSdk/mg/index.js +++ b/src/renderer/utils/musicSdk/mg/index.js @@ -6,8 +6,10 @@ import pic from './pic' import lyric from './lyric' import hotSearch from './hotSearch' import comment from './comment' +import tipSearch from './tipSearch' const mg = { + tipSearch, songList, musicSearch, leaderboard, diff --git a/src/renderer/utils/musicSdk/mg/tipSearch.js b/src/renderer/utils/musicSdk/mg/tipSearch.js new file mode 100644 index 00000000..41486d8d --- /dev/null +++ b/src/renderer/utils/musicSdk/mg/tipSearch.js @@ -0,0 +1,25 @@ +import { createHttpFetch } from './util' + +export default { + requestObj: null, + cancelTipSearch() { + if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp() + }, + tipSearchBySong(str) { + this.cancelTipSearch() + this.requestObj = createHttpFetch(`https://music.migu.cn/v3/api/search/suggest?keyword=${encodeURIComponent(str)}`, { + headers: { + referer: 'https://music.migu.cn/v3', + }, + }) + return this.requestObj.then(body => { + return body.songs + }) + }, + handleResult(rawData) { + return rawData.map(info => `${info.name} - ${info.singerName}`) + }, + async search(str) { + return this.tipSearchBySong(str).then(result => this.handleResult(result)) + }, +} diff --git a/src/renderer/utils/musicSdk/mg/util.js b/src/renderer/utils/musicSdk/mg/util.js index 8d3934db..efcf7548 100644 --- a/src/renderer/utils/musicSdk/mg/util.js +++ b/src/renderer/utils/musicSdk/mg/util.js @@ -15,6 +15,15 @@ export const createHttpFetch = async(url, options, retryNum = 0) => { console.log(err) return createHttpFetch(url, options, ++retryNum) } - if (result.statusCode !== 200 || result.body.code !== '000000') return createHttpFetch(url, options, ++retryNum) - if (result.body) return result.body + if (result.statusCode !== 200 || + ( + (result.body.code !== undefined + ? result.body.code + : result.body.returnCode !== undefined + ? result.body.returnCode + : result.body.code + ) !== '000000') + ) return createHttpFetch(url, options, ++retryNum) + if (result.body.data) return result.body.data + return result.body } diff --git a/src/renderer/utils/musicSdk/tx/index.js b/src/renderer/utils/musicSdk/tx/index.js index 73b4cdee..1a5bcc1d 100644 --- a/src/renderer/utils/musicSdk/tx/index.js +++ b/src/renderer/utils/musicSdk/tx/index.js @@ -5,10 +5,10 @@ import musicSearch from './musicSearch' import { apis } from '../api-source' import hotSearch from './hotSearch' import comment from './comment' -import tempSearch from './tempSearch' +import tipSearch from './tipSearch' const tx = { - tempSearch, + tipSearch, leaderboard, songList, musicSearch, diff --git a/src/renderer/utils/musicSdk/tx/tempSearch.js b/src/renderer/utils/musicSdk/tx/tipSearch.js similarity index 79% rename from src/renderer/utils/musicSdk/tx/tempSearch.js rename to src/renderer/utils/musicSdk/tx/tipSearch.js index 18e67c65..f0d42234 100644 --- a/src/renderer/utils/musicSdk/tx/tempSearch.js +++ b/src/renderer/utils/musicSdk/tx/tipSearch.js @@ -1,16 +1,12 @@ import { httpFetch } from '../../request' - export default { - regExps: { - relWord: /RELWORD=(.+)/, - }, requestObj: null, - cancelTempSearch() { + cancelTipSearch() { if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp() }, - tempSearch(str) { - this.cancelTempSearch() + tipSearchBySong(str) { + this.cancelTipSearch() this.requestObj = httpFetch(`https://c.y.qq.com/splcloud/fcgi-bin/smartbox_new.fcg?_=1682514997109&cv=4747474&ct=24&format=json&inCharset=utf-8&outCharset=utf-8¬ice=0&platform=yqq.json&needNewCode=1&uin=0&g_tk_new_20200303=5381&g_tk=5381&hostUin=0&is_xml=0&key=${encodeURIComponent(str)}`, { headers: { origin: 'https://y.qq.com/', @@ -19,13 +15,13 @@ export default { }) return this.requestObj.promise.then(({ statusCode, body }) => { if (statusCode != 200 || body.code != 0) return Promise.reject(new Error('请求失败')) - return body.data + return body.data.song.itemlist }) }, handleResult(rawData) { return rawData.map(info => `${info.name} - ${info.singer}`) }, async search(str) { - return this.tempSearch(str).then(result => this.handleResult(result.song.itemlist)) + return this.tipSearchBySong(str).then(result => this.handleResult(result)) }, } diff --git a/src/renderer/utils/musicSdk/wy/index.js b/src/renderer/utils/musicSdk/wy/index.js index 5fdc0251..95217c9c 100644 --- a/src/renderer/utils/musicSdk/wy/index.js +++ b/src/renderer/utils/musicSdk/wy/index.js @@ -6,8 +6,10 @@ import musicSearch from './musicSearch' import songList from './songList' import hotSearch from './hotSearch' import comment from './comment' +import tipSearch from './tipSearch' const wy = { + tipSearch, leaderboard, musicSearch, songList, diff --git a/src/renderer/utils/musicSdk/wy/tipSearch.js b/src/renderer/utils/musicSdk/wy/tipSearch.js new file mode 100644 index 00000000..1c1c9050 --- /dev/null +++ b/src/renderer/utils/musicSdk/wy/tipSearch.js @@ -0,0 +1,33 @@ +import { httpFetch } from '../../request' +import { weapi } from './utils/crypto' +import { getSingerName } from '../../index' + +export default { + requestObj: null, + cancelTipSearch() { + if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp() + }, + tipSearchBySong(str) { + this.cancelTipSearch() + this.requestObj = httpFetch('https://music.163.com/weapi/search/suggest/web', { + method: 'POST', + headers: { + referer: 'https://music.163.com/', + origin: 'https://music.163.com/', + }, + form: weapi({ + s: str, + }), + }) + return this.requestObj.promise.then(({ statusCode, body }) => { + if (statusCode != 200 || body.code != 200) return Promise.reject(new Error('请求失败')) + return body.result.songs + }) + }, + handleResult(rawData) { + return rawData.map(info => `${info.name} - ${getSingerName(info.artists, 'name')}`) + }, + async search(str) { + return this.tipSearchBySong(str).then(result => this.handleResult(result)) + }, +}