You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.4 KiB
103 lines
2.4 KiB
6 months ago
|
import musicApi from '../../api/music'
|
||
|
|
||
|
Page({
|
||
|
data: {
|
||
|
playlist: null,
|
||
|
currentSong: {},
|
||
|
isPlaying: false,
|
||
|
isExpanded: false,
|
||
|
showExpandBtn: false,
|
||
|
loading: false
|
||
|
},
|
||
|
|
||
|
onLoad(options) {
|
||
|
if (options.id) {
|
||
|
this.loadPlaylistDetail(options.id)
|
||
|
}
|
||
|
},
|
||
|
|
||
|
async loadPlaylistDetail(id) {
|
||
|
try {
|
||
|
this.setData({ loading: true })
|
||
|
const res = await musicApi.getPlaylistDetail(id)
|
||
|
if (res.data.code === 200) {
|
||
|
this.setData({
|
||
|
playlist: res.data.playlist
|
||
|
}, () => {
|
||
|
this.checkDescriptionHeight()
|
||
|
})
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error('加载歌单详情失败:', error)
|
||
|
wx.showToast({
|
||
|
title: '加载失败',
|
||
|
icon: 'none'
|
||
|
})
|
||
|
} finally {
|
||
|
this.setData({ loading: false })
|
||
|
}
|
||
|
},
|
||
|
|
||
|
async handlePlay(e) {
|
||
|
const { song } = e.detail
|
||
|
try {
|
||
|
const res = await musicApi.getSongUrl(song.id)
|
||
|
if (res.data.code === 200 && res.data.data[0]?.url) {
|
||
|
const songWithUrl = {
|
||
|
...song,
|
||
|
url: res.data.data[0].url
|
||
|
}
|
||
|
this.setData({
|
||
|
currentSong: songWithUrl,
|
||
|
isPlaying: true
|
||
|
})
|
||
|
|
||
|
// 更新全局播放状态
|
||
|
const app = getApp()
|
||
|
app.globalData.currentSong = songWithUrl
|
||
|
app.globalData.isPlaying = true
|
||
|
|
||
|
// 播放音乐
|
||
|
const bgAudio = wx.getBackgroundAudioManager()
|
||
|
bgAudio.title = song.name
|
||
|
bgAudio.singer = this.getArtists(song.ar)
|
||
|
bgAudio.coverImgUrl = song.al?.picUrl
|
||
|
bgAudio.src = songWithUrl.url
|
||
|
}
|
||
|
} catch (error) {
|
||
|
console.error('播放失败:', error)
|
||
|
wx.showToast({
|
||
|
title: '播放失败',
|
||
|
icon: 'none'
|
||
|
})
|
||
|
}
|
||
|
},
|
||
|
|
||
|
toggleExpand() {
|
||
|
this.setData({
|
||
|
isExpanded: !this.data.isExpanded
|
||
|
})
|
||
|
},
|
||
|
|
||
|
checkDescriptionHeight() {
|
||
|
const query = wx.createSelectorQuery()
|
||
|
query.select('.description').boundingClientRect()
|
||
|
query.exec(res => {
|
||
|
if (res[0]) {
|
||
|
this.setData({
|
||
|
showExpandBtn: res[0].height >= 144
|
||
|
})
|
||
|
}
|
||
|
})
|
||
|
},
|
||
|
|
||
|
getArtists(artists) {
|
||
|
return artists?.map(a => a.name).join(' / ') || '未知歌手'
|
||
|
},
|
||
|
|
||
|
formatDate(timestamp) {
|
||
|
if (!timestamp) return ''
|
||
|
const date = new Date(timestamp)
|
||
|
return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`
|
||
|
}
|
||
|
})
|