feat(playlist): 增加歌单播放次数功能
- 在前端 PlaylistDetail.vue 中添加播放全部歌曲时增加播放次数的逻辑- 在后端 PlaylistServiceImpl 中实现 playPlaylist 方法,增加歌单播放次数 - 优化了播放全部歌曲的功能,包括处理播放次数增加失败的情况
This commit is contained in:
@@ -519,13 +519,17 @@ public class PlaylistServiceImpl implements PlaylistService {
|
||||
@Override
|
||||
@Transactional
|
||||
public PlaylistDTO playPlaylist(Long id) {
|
||||
System.out.println("接收到播放歌单请求,歌单ID: " + id);
|
||||
// 获取歌单
|
||||
Playlist playlist = playlistRepository.findById(id)
|
||||
.orElseThrow(() -> new BusinessException("歌单不存在"));
|
||||
|
||||
// 增加播放次数
|
||||
playlist.setPlayCount(playlist.getPlayCount() + 1);
|
||||
long currentPlayCount = playlist.getPlayCount() == null ? 0 : playlist.getPlayCount();
|
||||
System.out.println("当前播放次数: " + currentPlayCount);
|
||||
playlist.setPlayCount(currentPlayCount + 1);
|
||||
playlistRepository.save(playlist);
|
||||
System.out.println("播放次数已增加,更新后为: " + playlist.getPlayCount());
|
||||
|
||||
return convertToDTO(playlist);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import { useRoute, useRouter } from 'vue-router'
|
||||
import { usePlayerStore } from '../stores/player'
|
||||
import { useUserStore } from '../stores/user'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import { getPlaylistById, toggleCollectPlaylist, deletePlaylist as apiDeletePlaylist, isPlaylistCollected, updatePlaylist } from '../api/playlist'
|
||||
import { getPlaylistById, toggleCollectPlaylist, deletePlaylist as apiDeletePlaylist, isPlaylistCollected, updatePlaylist, playPlaylist } from '../api/playlist'
|
||||
import { getMusicStatus } from '../api/music'
|
||||
import { getCommentList, addComment, deleteComment as apiDeleteComment } from '../api/comment'
|
||||
import { connect, subscribe, unsubscribe, disconnect } from '../utils/websocket' // 导入 WebSocket 工具
|
||||
@@ -127,12 +127,25 @@ const fetchComments = async () => {
|
||||
}
|
||||
|
||||
// 播放全部
|
||||
const playAll = () => {
|
||||
const playAll = async () => {
|
||||
if (!playlist.value || playlist.value.songs.length === 0) {
|
||||
ElMessage.warning('没有可播放的音乐')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// 调用 API 增加播放次数
|
||||
await playPlaylist(playlistId.value)
|
||||
|
||||
// 更新前端显示的播放次数
|
||||
if (playlist.value) {
|
||||
playlist.value.playCount++
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('增加播放次数失败:', error)
|
||||
// 即使增加播放次数失败,也继续播放
|
||||
}
|
||||
|
||||
// 清空当前播放列表
|
||||
playerStore.clearPlaylist()
|
||||
|
||||
@@ -142,7 +155,7 @@ const playAll = () => {
|
||||
})
|
||||
|
||||
// 播放第一首
|
||||
playerStore.setCurrentMusic(playlist.value.songs[0])
|
||||
playerStore.setCurrentMusic(playlist.value.songs)
|
||||
|
||||
ElMessage.success('开始播放全部歌曲')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user