新增音乐sdk初始化方法

pull/96/head
lyswhut 2019-10-30 22:29:14 +08:00
parent c881f8e886
commit 0df3ca3c5f
5 changed files with 41 additions and 14 deletions

View File

@ -22,6 +22,7 @@ import dnscache from 'dnscache'
import { mapMutations, mapGetters, mapActions } from 'vuex'
import { rendererOn } from '../common/icp'
import { isLinux } from '../common/utils'
import music from './utils/music'
window.ELECTRON_DISABLE_SECURITY_WARNINGS = process.env.ELECTRON_DISABLE_SECURITY_WARNINGS
dnscache({
enable: true,
@ -145,6 +146,9 @@ export default {
this.globalObj.apiSource = this.setting.apiSource
this.globalObj.proxy = Object.assign({}, this.setting.network.proxy)
window.globalObj = this.globalObj
// sdk
music.init()
},
enableIgnoreMouseEvents() {
if (isLinux) return

View File

@ -4,7 +4,7 @@ import tx from './tx'
import wy from './wy'
import mg from './mg'
import bd from './bd'
export default {
const sources = {
sources: [
{
name: '酷我音乐',
@ -38,3 +38,12 @@ export default {
mg,
bd,
}
export default {
...sources,
init() {
for (let source of sources.sources) {
let sm = sources[source.id]
sm && sm.init && sm.init()
}
},
}

View File

@ -1,7 +1,7 @@
import { httpGet, cancelHttp } from '../../request'
import tempSearch from './tempSearch'
import musicSearch from './musicSearch'
import { formatSinger } from './util'
import { formatSinger, getToken } from './util'
import leaderboard from './leaderboard'
import lyric from './lyric'
import pic from './pic'
@ -96,6 +96,10 @@ const kw = {
getPic(songInfo) {
return pic.getPic(songInfo)
},
init() {
getToken()
},
}
export default kw

View File

@ -2,12 +2,11 @@ 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, token) {
this.cancelTempSearch()
@ -20,7 +19,7 @@ export default {
})
return this.requestObj.promise.then(({ statusCode, body, headers }) => {
if (statusCode != 200) return Promise.reject(new Error('请求失败'))
this.token = matchToken(headers)
window.kw_token.token = matchToken(headers)
if (body.code !== 200) return Promise.reject(new Error('请求失败'))
return body
})
@ -35,13 +34,8 @@ export default {
if (this.requestObj && this.requestObj.cancelHttp) this.requestObj.cancelHttp()
},
async search(str) {
let token = this.token
if (this.isGetingToken) throw new Error('正在获取token')
if (!this.token) {
this.isGetingToken = true
token = await getToken()
this.isGetingToken = false
}
let token = window.kw_token.token
if (!token) token = await getToken()
return this.tempSearch(str, token).then(result => this.handleResult(result.data))
},
}

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)
})
})