From 1c585dc47976ef14547de55fb41da01d5373e6ab Mon Sep 17 00:00:00 2001 From: lyswhut Date: Mon, 5 Apr 2021 19:30:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E7=A6=BB=E6=AD=8C=E6=9B=B2URL?= =?UTF-8?q?=E7=9A=84=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/ipcNames.js | 3 +++ src/main/rendererEvents/index.js | 1 + src/main/rendererEvents/lyric.js | 6 ++--- src/main/rendererEvents/musicUrl.js | 10 +++++++++ src/renderer/components/core/Player.vue | 2 +- src/renderer/store/modules/download.js | 29 +++++++++++++++---------- src/renderer/store/modules/list.js | 3 ++- src/renderer/store/modules/player.js | 28 ++++++++++++++---------- src/renderer/utils/index.js | 7 ++++++ 9 files changed, 61 insertions(+), 28 deletions(-) create mode 100644 src/main/rendererEvents/musicUrl.js diff --git a/src/common/ipcNames.js b/src/common/ipcNames.js index 56387d5e..aa68e8d1 100644 --- a/src/common/ipcNames.js +++ b/src/common/ipcNames.js @@ -63,6 +63,9 @@ const names = { get_lyric: 'get_lyric', save_lyric: 'save_lyric', clear_lyric: 'clear_lyric', + get_music_url: 'get_music_url', + save_music_url: 'save_music_url', + clear_music_url: 'clear_music_url', }, winLyric: { close: 'close', diff --git a/src/main/rendererEvents/index.js b/src/main/rendererEvents/index.js index 89228062..9fafd6df 100644 --- a/src/main/rendererEvents/index.js +++ b/src/main/rendererEvents/index.js @@ -18,6 +18,7 @@ require('./showDialog') require('./playList') require('./data') require('./lyric') +require('./musicUrl') require('./kw_decodeLyric') diff --git a/src/main/rendererEvents/lyric.js b/src/main/rendererEvents/lyric.js index 8cbbe1a6..839a33f0 100644 --- a/src/main/rendererEvents/lyric.js +++ b/src/main/rendererEvents/lyric.js @@ -2,9 +2,9 @@ const { mainOn, NAMES: { mainWindow: ipcMainWindowNames }, mainHandle } = requir const getStore = require('@common/store') -mainHandle(ipcMainWindowNames.get_lyric, async(event, id) => getStore('lyrics').get(id) || {}) +mainHandle(ipcMainWindowNames.get_lyric, async(event, id) => getStore('lyrics', true, false).get(id) || {}) -mainOn(ipcMainWindowNames.save_lyric, (event, { id, lyrics }) => getStore('lyrics').set(id, lyrics)) +mainOn(ipcMainWindowNames.save_lyric, (event, { id, lyrics }) => getStore('lyrics', true, false).set(id, lyrics)) -mainOn(ipcMainWindowNames.clear_lyric, () => getStore('lyrics').clear()) +mainOn(ipcMainWindowNames.clear_lyric, () => getStore('lyrics', true, false).clear()) diff --git a/src/main/rendererEvents/musicUrl.js b/src/main/rendererEvents/musicUrl.js new file mode 100644 index 00000000..5bac64b8 --- /dev/null +++ b/src/main/rendererEvents/musicUrl.js @@ -0,0 +1,10 @@ +const { mainOn, NAMES: { mainWindow: ipcMainWindowNames }, mainHandle } = require('../../common/ipc') +const getStore = require('@common/store') + + +mainHandle(ipcMainWindowNames.get_music_url, async(event, id) => getStore('musicUrls', true, false).get(id) || '') + + +mainOn(ipcMainWindowNames.save_music_url, (event, { id, url }) => getStore('musicUrls', true, false).set(id, url)) + +mainOn(ipcMainWindowNames.clear_music_url, () => getStore('musicUrls', true, false).clear()) diff --git a/src/renderer/components/core/Player.vue b/src/renderer/components/core/Player.vue index 508f13e6..2d659c51 100644 --- a/src/renderer/components/core/Player.vue +++ b/src/renderer/components/core/Player.vue @@ -607,7 +607,7 @@ export default { if (!retryedSource.includes(targetSong.source)) retryedSource.push(targetSong.source) let type = this.getPlayType(this.setting.player.highQuality, targetSong) - this.musicInfo.url = targetSong.typeUrl[type] + // this.musicInfo.url = await getMusicUrl(targetSong, type) this.status = this.statusText = this.$t('core.player.geting_url') return this.getUrl({ musicInfo: targetSong, originMusic, type, isRefresh }).then(url => { diff --git a/src/renderer/store/modules/download.js b/src/renderer/store/modules/download.js index 19eedfcf..d4e3c2f8 100644 --- a/src/renderer/store/modules/download.js +++ b/src/renderer/store/modules/download.js @@ -3,7 +3,7 @@ import fs from 'fs' import path from 'path' import music from '../../utils/music' import { getMusicType } from '../../utils/music/utils' -import { setMeta, saveLrc, getLyric, setLyric } from '../../utils' +import { setMeta, saveLrc, getLyric, setLyric, getMusicUrl, setMusicUrl } from '../../utils' // state const state = { @@ -147,15 +147,20 @@ const pauseTasks = async(store, list, runs = []) => { await pauseTasks(store, list, runs) } -const getUrl = (downloadInfo, isRefresh) => { - const url = downloadInfo.musicInfo.typeUrl[downloadInfo.type] +const getUrl = async(downloadInfo, isRefresh) => { + const cachedUrl = await getMusicUrl(downloadInfo.musicInfo, downloadInfo.type) if (!downloadInfo.musicInfo._types[downloadInfo.type]) { // 兼容旧版酷我源搜索列表过滤128k音质的bug - if (!(downloadInfo.musicInfo.source == 'kw' && downloadInfo.type == '128k')) return Promise.reject(new Error('该歌曲没有可下载的音频')) + if (!(downloadInfo.musicInfo.source == 'kw' && downloadInfo.type == '128k')) throw new Error('该歌曲没有可下载的音频') // return Promise.reject(new Error('该歌曲没有可下载的音频')) } - return url && !isRefresh ? Promise.resolve({ url }) : music[downloadInfo.musicInfo.source].getMusicUrl(downloadInfo.musicInfo, downloadInfo.type).promise + return cachedUrl && !isRefresh + ? cachedUrl + : music[downloadInfo.musicInfo.source].getMusicUrl(downloadInfo.musicInfo, downloadInfo.type).promise.then(({ url }) => { + setMusicUrl(downloadInfo.musicInfo, downloadInfo.type, url) + return url + }) } // 修复 1.1.x版本 酷狗源歌词格式 @@ -228,12 +233,12 @@ const downloadLyric = (downloadInfo, filePath) => { const refreshUrl = function(commit, downloadInfo) { commit('setStatusText', { downloadInfo, text: '链接失效,正在刷新链接' }) - getUrl(downloadInfo, true).then(result => { - commit('updateUrl', { downloadInfo, url: result.url }) + getUrl(downloadInfo, true).then(url => { + commit('updateUrl', { downloadInfo, url }) commit('setStatusText', { downloadInfo, text: '链接刷新成功' }) const dl = dls[downloadInfo.key] if (!dl) return - dl.refreshUrl(result.url) + dl.refreshUrl(url) dl.start().catch(err => { commit('onError', { downloadInfo, errorMsg: err.message }) commit('setStatusText', { downloadInfo, text: err.message }) @@ -389,10 +394,10 @@ const actions = { commit('setStatusText', { downloadInfo, text: '获取URL中...' }) let p = options.url ? Promise.resolve() - : getUrl(downloadInfo).then(result => { - commit('updateUrl', { downloadInfo, url: result.url }) - if (!result.url) return Promise.reject(new Error('获取URL失败')) - options.url = result.url + : getUrl(downloadInfo).then(url => { + commit('updateUrl', { downloadInfo, url }) + if (!url) return Promise.reject(new Error('获取URL失败')) + options.url = url }) p.then(() => { tryNum[downloadInfo.key] = 0 diff --git a/src/renderer/store/modules/list.js b/src/renderer/store/modules/list.js index ad9b537f..4228b2c2 100644 --- a/src/renderer/store/modules/list.js +++ b/src/renderer/store/modules/list.js @@ -1,5 +1,5 @@ import musicSdk from '../../utils/music' -import { clearLyric } from '../../utils' +import { clearLyric, clearMusicUrl } from '../../utils' let allList = {} window.allList = allList @@ -215,6 +215,7 @@ const mutations = { if (item.tlrc) delete item.tlrc } } + clearMusicUrl() clearLyric() }, setOtherSource(state, { musicInfo, otherSource }) { diff --git a/src/renderer/store/modules/player.js b/src/renderer/store/modules/player.js index 47894d1b..d926b204 100644 --- a/src/renderer/store/modules/player.js +++ b/src/renderer/store/modules/player.js @@ -1,6 +1,13 @@ import path from 'path' import music from '../../utils/music' -import { getRandom, checkPath, getLyric as getStoreLyric, setLyric } from '../../utils' +import { + getRandom, + checkPath, + getLyric as getStoreLyric, + setLyric, + setMusicUrl, + getMusicUrl, +} from '../../utils' // state const state = { @@ -147,20 +154,19 @@ const getters = { // actions const actions = { - getUrl({ commit, state }, { musicInfo, originMusic, type, isRefresh }) { + async getUrl({ commit, state }, { musicInfo, originMusic, type, isRefresh }) { if (!musicInfo._types[type]) { // 兼容旧版酷我源搜索列表过滤128k音质的bug - if (!(musicInfo.source == 'kw' && type == '128k')) return Promise.reject(new Error('该歌曲没有可播放的音频')) + if (!(musicInfo.source == 'kw' && type == '128k')) throw new Error('该歌曲没有可播放的音频') // return Promise.reject(new Error('该歌曲没有可播放的音频')) } if (urlRequest && urlRequest.cancelHttp) urlRequest.cancelHttp() - if (musicInfo.typeUrl[type] && !isRefresh) return Promise.resolve(musicInfo.typeUrl[type]) - try { - urlRequest = music[musicInfo.source].getMusicUrl(musicInfo, type) - } catch (err) { - return Promise.reject(err) - } + const cachedUrl = await getMusicUrl(musicInfo, type) + if (cachedUrl && !isRefresh) return cachedUrl + + urlRequest = music[musicInfo.source].getMusicUrl(musicInfo, type) + return urlRequest.promise.then(({ url }) => { if (originMusic) commit('setUrl', { musicInfo: originMusic, url, type }) commit('setUrl', { musicInfo, url, type }) @@ -328,8 +334,8 @@ const actions = { // mitations const mutations = { - setUrl(state, datas) { - datas.musicInfo.typeUrl = Object.assign({}, datas.musicInfo.typeUrl, { [datas.type]: datas.url }) + setUrl(state, { musicInfo, type, url }) { + setMusicUrl(musicInfo, type, url) }, getPic(state, datas) { datas.musicInfo.img = datas.url diff --git a/src/renderer/utils/index.js b/src/renderer/utils/index.js index c044d2d8..1228b68b 100644 --- a/src/renderer/utils/index.js +++ b/src/renderer/utils/index.js @@ -414,3 +414,10 @@ export const setLyric = (musicInfo, { lyric, tlyric, lxlyric }) => rendererSend( lyrics: { lyric, tlyric, lxlyric }, }) export const clearLyric = () => rendererSend(NAMES.mainWindow.clear_lyric) + +export const getMusicUrl = (musicInfo, type) => rendererInvoke(NAMES.mainWindow.get_music_url, `${musicInfo.source}_${musicInfo.songmid}_${type}`) +export const setMusicUrl = (musicInfo, type, url) => rendererSend(NAMES.mainWindow.save_music_url, { + id: `${musicInfo.source}_${musicInfo.songmid}_${type}`, + url, +}) +export const clearMusicUrl = () => rendererSend(NAMES.mainWindow.clear_music_url)