修复小芸源音乐搜索结果最多只有20条搜索结果的问题

pull/389/head
lyswhut 2020-11-01 14:36:53 +08:00
parent 19817249db
commit 784d2fda6d
2 changed files with 45 additions and 17 deletions

View File

@ -6,3 +6,7 @@
- 修改播放详情页的歌曲图片的显示效果 - 修改播放详情页的歌曲图片的显示效果
### 修复
- 修复小芸源音乐搜索结果最多只有20条搜索结果的问题

View File

@ -1,6 +1,7 @@
import { httpFetch } from '../../request' import { httpFetch } from '../../request'
import { weapi } from './utils/crypto' import { weapi } from './utils/crypto'
import { sizeFormate, formatPlayTime } from '../../index' // import { sizeFormate, formatPlayTime } from '../../index'
import musicDetailApi from './musicDetail'
let searchRequest let searchRequest
export default { export default {
@ -10,8 +11,12 @@ export default {
allPage: 1, allPage: 1,
musicSearch(str, page) { musicSearch(str, page) {
if (searchRequest && searchRequest.cancelHttp) searchRequest.cancelHttp() if (searchRequest && searchRequest.cancelHttp) searchRequest.cancelHttp()
searchRequest = httpFetch('http://music.163.com/weapi/cloudsearch/get/web?csrf_token=', { searchRequest = httpFetch('https://music.163.com/weapi/search/get', {
method: 'post', method: 'post',
headers: {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',
origin: 'https://music.163.com',
},
form: weapi({ form: weapi({
s: str, s: str,
type: 1, // 1: 单曲, 10: 专辑, 100: 歌手, 1000: 歌单, 1002: 用户, 1004: MV, 1006: 歌词, 1009: 电台, 1014: 视频 type: 1, // 1: 单曲, 10: 专辑, 100: 歌手, 1000: 歌单, 1002: 用户, 1004: MV, 1006: 歌词, 1009: 电台, 1014: 视频
@ -19,7 +24,24 @@ export default {
offset: this.limit * (page - 1), offset: this.limit * (page - 1),
}), }),
}) })
return searchRequest.promise.then(({ body }) => body) return searchRequest.promise.then(({ body }) =>
body && body.code === 200
? musicDetailApi.getList(body.result.songs.map(s => s.id)).then(({ list }) => {
this.total = body.result.songCount || 0
this.page = page
this.allPage = Math.ceil(this.total / this.limit)
return {
code: 200,
data: {
list,
allPage: this.allPage,
limit: this.limit,
total: this.total,
source: 'wy',
},
}
})
: body)
}, },
getSinger(singers) { getSinger(singers) {
let arr = [] let arr = []
@ -28,7 +50,7 @@ export default {
}) })
return arr.join('、') return arr.join('、')
}, },
handleResult(rawList) { /* handleResult(rawList) {
// console.log(rawList) // console.log(rawList)
if (!rawList) return [] if (!rawList) return []
return rawList.map(item => { return rawList.map(item => {
@ -78,27 +100,29 @@ export default {
typeUrl: {}, typeUrl: {},
} }
}) })
}, }, */
search(str, page = 1, { limit } = {}, retryNum = 0) { search(str, page = 1, { limit } = {}, retryNum = 0) {
if (++retryNum > 3) return Promise.reject(new Error('try max num')) if (++retryNum > 3) return Promise.reject(new Error('try max num'))
if (limit != null) this.limit = limit if (limit != null) this.limit = limit
return this.musicSearch(str, page).then(result => { return this.musicSearch(str, page).then(result => {
// console.log(result)
if (!result || result.code !== 200) return this.search(str, page, { limit }, retryNum) if (!result || result.code !== 200) return this.search(str, page, { limit }, retryNum)
let list = this.handleResult(result.result.songs || []) // let list = this.handleResult(result.result.songs || [])
if (list == null) return this.search(str, page, { limit }, retryNum) // if (list == null) return this.search(str, page, { limit }, retryNum)
this.total = result.result.songCount || 0 // this.total = result.result.songCount || 0
this.page = page // this.page = page
this.allPage = Math.ceil(this.total / this.limit) // this.allPage = Math.ceil(this.total / this.limit)
return Promise.resolve({ // return Promise.resolve({
list, // list,
allPage: this.allPage, // allPage: this.allPage,
limit: this.limit, // limit: this.limit,
total: this.total, // total: this.total,
source: 'wy', // source: 'wy',
}) // })
return result.data
}) })
}, },
} }