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.

78 lines
1.8 KiB

import musicApi from '../../api/music'
const app = getApp()
Page({
data: {
musicApi,
uid: null,
playlists: [],
loading: false
},
async onLoad() {
const uid = app.globalData.uid
if (!uid) {
try {
const res = await app.getUserAccount()
if (res.data.code === 200 && res.data.account?.id) {
app.globalData.uid = res.data.account.id
this.setData({ uid: res.data.account.id })
await this.loadPlaylists(res.data.account.id)
}
} catch (error) {
console.error('获取用户信息失败:', error)
wx.showToast({
title: '获取用户信息失败',
icon: 'none'
})
}
} else {
this.setData({ uid })
await this.loadPlaylists(uid)
}
},
onShow() {
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
selected: 1 // 我的歌单 tab 索引为 1
})
}
},
async loadPlaylists(uid) {
if (!uid) return
this.setData({ loading: true })
try {
const res = await musicApi.getUserPlaylists(uid)
if (res.data.code === 200) {
const playlists = res.data.playlist.filter(item =>
item.userId === uid && !item.specialType
)
this.setData({ playlists })
}
} catch (error) {
console.error('获取用户歌单失败:', error)
wx.showToast({
title: '获取歌单失败',
icon: 'none'
})
} finally {
this.setData({ loading: false })
}
},
goToPlaylist(e) {
const { id } = e.currentTarget.dataset
wx.navigateTo({
url: `/pages/playlist/index?id=${id}`
})
},
onPullDownRefresh() {
this.loadPlaylists(this.data.uid).then(() => {
wx.stopPullDownRefresh()
})
}
})