优化逻辑

This commit is contained in:
lyswhut
2021-04-24 17:22:28 +08:00
parent 895198b7b8
commit a8dcb0b973
3 changed files with 39 additions and 36 deletions
+30 -11
View File
@@ -6,7 +6,8 @@ import {
getLyric as getStoreLyric,
setLyric,
setMusicUrl,
getMusicUrl,
getMusicUrl as getStoreMusicUrl,
assertApiSupport,
} from '../../utils'
// state
@@ -66,6 +67,31 @@ const filterList = async({ playedList, listInfo, savePath, commit }) => {
return list
}
const getMusicUrl = function(musicInfo, type, retryedSource = [], originMusic) {
// console.log(musicInfo.source)
if (!originMusic) originMusic = musicInfo
let reqPromise
try {
reqPromise = music[musicInfo.source].getMusicUrl(musicInfo, type).promise
} catch (err) {
reqPromise = Promise.reject(err)
}
return reqPromise.catch(err => {
if (!retryedSource.includes(musicInfo.source)) retryedSource.push(musicInfo.source)
return this.dispatch('list/getOtherSource', originMusic).then(otherSource => {
console.log('find otherSource', otherSource)
if (otherSource.length) {
for (const item of otherSource) {
if (retryedSource.includes(item.source) || !assertApiSupport(item.source)) continue
console.log('try toggle to: ', item.source, item.name, item.singer, item.interval)
return getMusicUrl.call(this, item, type, retryedSource, originMusic)
}
}
return Promise.reject(err)
})
})
}
const getPic = function(musicInfo, retryedSource = [], originMusic) {
// console.log(musicInfo.source)
if (!originMusic) originMusic = musicInfo
@@ -163,24 +189,17 @@ const getters = {
// actions
const actions = {
async getUrl({ commit, state }, { musicInfo, originMusic, type, isRefresh }) {
async getUrl({ commit, state }, { musicInfo, type, isRefresh }) {
if (!musicInfo._types[type]) {
// 兼容旧版酷我源搜索列表过滤128k音质的bug
if (!(musicInfo.source == 'kw' && type == '128k')) throw new Error('该歌曲没有可播放的音频')
// return Promise.reject(new Error('该歌曲没有可播放的音频'))
}
const cachedUrl = await getMusicUrl(musicInfo, type)
const cachedUrl = await getStoreMusicUrl(musicInfo, type)
if (cachedUrl && !isRefresh) return cachedUrl
let reqPromise
try {
reqPromise = music[musicInfo.source].getMusicUrl(musicInfo, type).promise
} catch (err) {
reqPromise = Promise.reject(err)
}
return reqPromise.then(({ url }) => {
if (originMusic) commit('setUrl', { musicInfo: originMusic, url, type })
return getMusicUrl.call(this, musicInfo, type).then(({ url }) => {
commit('setUrl', { musicInfo, url, type })
return url
}).catch(err => {