feat(music): 添加音乐状态获取功能并优化音乐列表展示

- 新增 getMusicStatus API 接口,用于获取音乐的收藏和点赞状态
- 在音乐列表、歌单详情、搜索结果等页面中集成音乐状态获取功能
- 优化音乐播放器,主动获取并更新音乐状态
- 为每首歌曲添加收藏和点赞状态字段
This commit is contained in:
ikmkj
2025-07-26 00:02:03 +08:00
parent 4435315acf
commit 439d4b7588
11 changed files with 205 additions and 27 deletions

View File

@@ -78,7 +78,7 @@
@click="toggleLike"
>
<el-icon><component :is="music.liked ? 'Check' : 'Pointer'" /></el-icon>
{{ music.liked ? '已点赞' : '点赞' }} ({{ music.likeCount || 0 }})
{{ music.liked ? '已点赞' : '点赞' }}
</el-button>
<el-button
@@ -202,7 +202,7 @@ import { ref, computed, onMounted, watch, onUnmounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { usePlayerStore } from '../stores/player'
import { useUserStore } from '../stores/user'
import { getMusicById, toggleCollectMusic, getHotMusic, toggleLikeMusic } from '../api/music'
import { getMusicById, toggleCollectMusic, getHotMusic, toggleLikeMusic, getMusicStatus } from '../api/music'
import { getCommentList, addComment, deleteComment as apiDeleteComment } from '../api/comment'
import { connect, subscribe, unsubscribe, disconnect } from '../utils/websocket'
import { ElMessage, ElMessageBox } from 'element-plus'
@@ -246,6 +246,8 @@ const fetchMusicDetail = async () => {
if (res.code === 200) {
if (res.data) {
music.value = res.data
// 获取音乐状态
fetchMusicStatus()
} else {
// 如果音乐不存在,显示提示并返回音乐列表页
ElMessage.warning('音乐不存在或已被删除')
@@ -264,6 +266,22 @@ const fetchMusicDetail = async () => {
}
}
// 获取音乐状态
const fetchMusicStatus = async () => {
if (!music.value || !userStore.isLoggedIn) {
return
}
try {
const res = await getMusicStatus(music.value.id)
if (res.code === 200 && res.data) {
music.value.collected = res.data.collected
music.value.liked = res.data.liked
}
} catch (error) {
console.error('获取音乐状态失败:', error)
}
}
// 获取相关音乐
const fetchRelatedMusic = async () => {
try {