diff --git a/src/components/song-table.vue b/src/components/song-table.vue index be3d6e5..f9bd5aa 100644 --- a/src/components/song-table.vue +++ b/src/components/song-table.vue @@ -151,7 +151,7 @@ export default { ['row-click']: this.onRowClick }, props: { - ['table-cell-class-name']: this.tableCellClassName, + ['cell-class-name']: this.tableCellClassName, data: this.songs }, style: { width: '99.9%' } diff --git a/src/store/actions.js b/src/store/actions.js index 9335f60..6c9e0ff 100644 --- a/src/store/actions.js +++ b/src/store/actions.js @@ -4,30 +4,30 @@ import { HISTORY_KEY } from '@/utils/config' export default { // 整合歌曲信息 并且开始播放 - async startSong({ commit, state }, song) { - const { data } = await getSongUrl(song.id) - const [resultSong] = data - const canPlay = !!resultSong.url - if (canPlay) { - commit('setCurrentSong', { - ...song, - url: resultSong.url - }) + async startSong({ commit, state, dispatch }, song) { - // 历史记录 - const { playHistory } = state - const playHistoryCopy = playHistory.slice() - const findedIndex = playHistoryCopy.findIndex(({ id }) => song.id === id) - if (findedIndex !== -1) { - // 删除旧那一项, 插入到最前面 - playHistoryCopy.splice(findedIndex, 1) - } - playHistoryCopy.unshift(song) - commit('setPlayHistory', playHistoryCopy) - commit('setPlayingState', true) - storage.set(HISTORY_KEY, playHistoryCopy) - } else { - alert('暂时无法播放') + commit('setCurrentSong', { + ...song, + url: genSongPlayUrl(song.id) + }) + + // 历史记录 + const { playHistory } = state + const playHistoryCopy = playHistory.slice() + const findedIndex = playHistoryCopy.findIndex(({ id }) => song.id === id) + if (findedIndex !== -1) { + // 删除旧那一项, 插入到最前面 + playHistoryCopy.splice(findedIndex, 1) + } + playHistoryCopy.unshift(song) + commit('setPlayHistory', playHistoryCopy) + commit('setPlayingState', true) + storage.set(HISTORY_KEY, playHistoryCopy) + // 检查是否能播放 + const canPlay = await checkCanPlay(song.id) + if (!canPlay) { + alert('播放失败') + dispatch('clearCurrentSong') } }, clearCurrentSong({ commit }) { @@ -48,3 +48,13 @@ export default { } } } + +function genSongPlayUrl(id) { + return `https://music.163.com/song/media/outer/url?id=${id}.mp3` +} + +async function checkCanPlay(id) { + const { data } = await getSongUrl(id) + const [resultSong] = data + return !!resultSong.url +}