修复酷我源搜索提示、排行榜接口挂掉的问题

pull/96/head
lyswhut 2019-10-28 22:42:01 +08:00
parent 1bd8694262
commit 63a5f94a67
4 changed files with 54 additions and 9 deletions

View File

@ -1,3 +1,7 @@
#### 优化
- 大幅减少程序播放时对CPU与GPU的使用
#### 修复
- 修复酷我源**搜索提示、排行榜**接口挂掉的问题

View File

@ -1,7 +1,6 @@
import { httpGet, cancelHttp } from '../../request'
import { formatPlayTime, decodeName } from '../../index'
import { formatSinger } from './util'
import { formatSinger, getToken, matchToken } from './util'
export default {
list: [
@ -67,6 +66,8 @@ export default {
},
limit: 30,
token: null,
isGetingToken: false,
_cancelRequestObj: null,
_cancelPromiseCancelFn: null,
_cancelRequestObj2: null,
@ -89,20 +90,34 @@ export default {
})
})
},
getData2(url) {
async getData2(url) {
if (this._cancelRequestObj2 != null) {
cancelHttp(this._cancelRequestObj2)
this._cancelPromiseCancelFn2(new Error('取消http请求'))
}
if (this.isGetingToken) return Promise.reject(new Error('正在获取token'))
let token = this.token
if (!token) {
this.isGetingToken = true
token = await getToken()
this.isGetingToken = false
}
return new Promise((resolve, reject) => {
this._cancelPromiseCancelFn2 = reject
this._cancelRequestObj2 = httpGet(url, (err, resp, body) => {
this._cancelRequestObj2 = httpGet(url, {
headers: {
Referer: 'http://www.kuwo.cn/',
csrf: token,
cookie: 'kw_token=' + token,
},
}, (err, resp, body) => {
this._cancelRequestObj2 = null
this._cancelPromiseCancelFn2 = null
if (err) {
console.log(err)
reject(err)
}
this.token = matchToken(resp.headers)
resolve(body)
})
})

View File

@ -1,20 +1,27 @@
import { httpFetch } from '../../request'
import { decodeName } from '../../index'
import { getToken, matchToken } from './util'
export default {
regExps: {
relWord: /RELWORD=(.+)/,
},
token: null,
isGetingToken: false,
requestObj: null,
tempSearch(str) {
tempSearch(str, token) {
this.cancelTempSearch()
this.requestObj = httpFetch(`http://www.kuwo.cn/api/www/search/searchKey?key=${encodeURIComponent(str)}`, {
headers: {
Referer: 'http://www.kuwo.cn/',
csrf: token,
cookie: 'kw_token=' + token,
},
})
return this.requestObj.promise.then(({ statusCode, body }) => {
if (statusCode != 200 || body.code !== 200) return Promise.reject(new Error('请求失败'))
return this.requestObj.promise.then(({ statusCode, body, headers }) => {
if (statusCode != 200) return Promise.reject(new Error('请求失败'))
this.token = matchToken(headers)
if (body.code !== 200) return Promise.reject(new Error('请求失败'))
return body
})
},
@ -27,7 +34,14 @@ export default {
cancelTempSearch() {
if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp()
},
search(str) {
return this.tempSearch(str).then(result => this.handleResult(result.data))
async search(str) {
let token = this.token
if (this.isGetingToken) return Promise.reject(new Error('正在获取token'))
if (!this.token) {
this.isGetingToken = true
token = await getToken()
this.isGetingToken = false
}
return this.tempSearch(str, token).then(result => this.handleResult(result.data))
},
}

View File

@ -1,2 +1,14 @@
import { httpGet } from '../../request'
export const formatSinger = rawData => rawData.replace(/&/g, '、')
export const matchToken = headers => headers['set-cookie'][0].match(/kw_token=(\w+)/)[1]
export const getToken = () => new Promise((resolve, reject) => {
httpGet('http://www.kuwo.cn', (err, resp) => {
if (err) return reject(err)
if (resp.statusCode != 200) return reject(new Error('获取失败'))
const token = matchToken(resp.headers)
resolve(token)
})
})