新增音乐sdk初始化方法

This commit is contained in:
lyswhut
2019-10-30 22:29:14 +08:00
parent c881f8e886
commit 0df3ca3c5f
5 changed files with 41 additions and 14 deletions
+18 -2
View File
@@ -1,14 +1,30 @@
import { httpGet } from '../../request'
if (!window.kw_token) {
window.kw_token = {
token: null,
isGetingToken: false,
}
}
export const formatSinger = rawData => rawData.replace(/&/g, '、')
export const matchToken = headers => headers['set-cookie'][0].match(/kw_token=(\w+)/)[1]
export const matchToken = headers => {
try {
return headers['set-cookie'][0].match(/kw_token=(\w+)/)[1]
} catch (err) {
return null
}
}
export const getToken = () => new Promise((resolve, reject) => {
if (window.kw_token.isGetingToken) reject(new Error('正在获取token'))
window.kw_token.isGetingToken = true
httpGet('http://www.kuwo.cn', (err, resp) => {
window.kw_token.isGetingToken = false
if (err) return reject(err)
if (resp.statusCode != 200) return reject(new Error('获取失败'))
const token = matchToken(resp.headers)
const token = window.kw_token.token = matchToken(resp.headers)
resolve(token)
})
})