Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

Commit

Permalink
fix: 播放链接403问题
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden committed Nov 4, 2021
1 parent 3454934 commit 3796b66
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/background/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ let isForcePlay = false
let persistData = null
// 静音前音量
let volumeMute = 0
// 上次重启时间
let rebootAt = 0

const store = proxy({ ...COMMON_PROPS })

Expand All @@ -45,6 +47,7 @@ export async function bootstrap () {
refreshLogin()
])
await refreshPlaylists()
rebootAt = Date.now()
logger.info('bootstrap', store)
}

Expand Down Expand Up @@ -261,13 +264,19 @@ function getPopupData () {
}

async function refreshLogin () {
await api.loginRefresh()
const res = await api.getUser()
if (res.code === 200 && res?.profile) {
const { userId, vipType } = res.profile
Object.assign(store, { userId, vip: vipType > 0 })
} else {
const res = await api.loginRefresh()
if (res.code !== 200) {
await reset()
return
}
if (store.userId === null) {
const res = await api.getUser()
if (res.code === 200 && res?.profile) {
const { userId, vipType } = res.profile
Object.assign(store, { userId, vip: vipType > 0 })
} else {
await reset()
}
}
}

Expand Down Expand Up @@ -393,12 +402,13 @@ async function loadSongDetail (playlistDetail, songId, retry) {
songsMap = tracksToSongsMap(tracks)
}
const song = songsMap[songId]
let url
try {
if (!song || !song.valid) {
throw new Error('歌曲无法播放')
} else if (song.miss || (song.vip && !store.vip)) {
try {
song.url = await race([
url = await race([
getKuWoSong(song.name, song.artists),
getMiGuSong(song.name, song.artists)
])
Expand All @@ -410,11 +420,10 @@ async function loadSongDetail (playlistDetail, songId, retry) {
if (res.code !== 200) {
throw new Error(res.message)
}
const url = res.data.map(v => v.url)[0]
url = res.data.map(v => v.url)[0]
if (!url) {
throw new Error('获取资源失败')
}
song.url = url
}
} catch (err) {
logger.error('loadSongDetail.err', err)
Expand All @@ -429,7 +438,7 @@ async function loadSongDetail (playlistDetail, songId, retry) {
const newSongId = getNextSongId(playlistDetail, songId)
return loadSongDetail(playlistDetail, newSongId, retry)
}
return song
return { ...song, url }
}

async function loadTracks (ids) {
Expand Down Expand Up @@ -513,7 +522,7 @@ function createAudio (url) {
sendToPopup({ topic: 'sync', change })
}
audio.onerror = async () => {
logger.error('audio.error', url, audio.error.message)
logger.error('audio.error', store.selectedSong, audio.error.message)
if (isForcePlay) {
sendToPopup({ topic: 'error', message: '无法该播放' })
} else {
Expand Down Expand Up @@ -588,13 +597,12 @@ subscribeKey(store, 'playing', playing => {
}
})

setInterval(() => {
bootstrap()
}, 10 * 60 * 60 * 1000)

setInterval(() => {
refreshLogin()
}, 51 * 60 * 1000)
setInterval(async () => {
await refreshLogin()
if (Date.now() - rebootAt > 13 * 60 * 60 * 1000) {
await bootstrap()
}
}, 33 * 60 * 1000)

api.code301 = reset

Expand Down
1 change: 1 addition & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const logger = {
log (level, ...args) {
if (level === 'error') {
console.error(...args)
return
}
const levelValue = LOG_LEVEL[level]
const configLevelValue = LOG_LEVEL[logger.level]
Expand Down

0 comments on commit 3796b66

Please sign in to comment.