diff --git a/music-houduan/src/main/java/com/test/musichouduan/service/impl/CommentServiceImpl.java b/music-houduan/src/main/java/com/test/musichouduan/service/impl/CommentServiceImpl.java index fdfa260..a19ab52 100644 --- a/music-houduan/src/main/java/com/test/musichouduan/service/impl/CommentServiceImpl.java +++ b/music-houduan/src/main/java/com/test/musichouduan/service/impl/CommentServiceImpl.java @@ -28,7 +28,9 @@ import org.springframework.transaction.annotation.Transactional; import jakarta.persistence.criteria.Predicate; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; @@ -221,6 +223,14 @@ public class CommentServiceImpl implements CommentService { } } + // 通过 WebSocket 广播删除事件 + String destination = String.format("/topic/comments/%d/%d", comment.getTargetType(), comment.getTargetId()); + Map deleteEvent = new HashMap<>(); + deleteEvent.put("deleted", true); + deleteEvent.put("id", id); + deleteEvent.put("parentId", comment.getParentId()); + messagingTemplate.convertAndSend(destination, deleteEvent); + return true; } @@ -343,6 +353,14 @@ public class CommentServiceImpl implements CommentService { } } + // 通过 WebSocket 广播删除事件 + String destination = String.format("/topic/comments/%d/%d", comment.getTargetType(), comment.getTargetId()); + Map deleteEvent = new HashMap<>(); + deleteEvent.put("deleted", true); + deleteEvent.put("id", id); + deleteEvent.put("parentId", comment.getParentId()); + messagingTemplate.convertAndSend(destination, deleteEvent); + return true; } diff --git a/music-qianduan/src/views/MusicDetail.vue b/music-qianduan/src/views/MusicDetail.vue index 17c2935..57fec97 100644 --- a/music-qianduan/src/views/MusicDetail.vue +++ b/music-qianduan/src/views/MusicDetail.vue @@ -490,7 +490,25 @@ const setupWebSocket = () => { if (!music.value) return; connect(() => { const destination = `/topic/comments/1/${music.value.id}`; // 1 代表音乐类型 - commentSubscription = subscribe(destination, (newComment) => { + commentSubscription = subscribe(destination, (message) => { + // 处理删除事件 + if (message && message.deleted) { + const deletedId = message.id; + if (message.parentId) { + // 删除子评论 + const parentComment = comments.value.find(c => c.id === message.parentId); + if (parentComment && parentComment.children) { + parentComment.children = parentComment.children.filter(c => c.id !== deletedId); + } + } else { + // 删除父评论 + comments.value = comments.value.filter(c => c.id !== deletedId); + } + return; + } + + // 处理新增评论 + const newComment = message; if (newComment && newComment.id) { if (newComment.user && newComment.user.avatar) { newComment.user.avatar = processImageUrl(newComment.user.avatar); diff --git a/music-qianduan/src/views/PlaylistDetail.vue b/music-qianduan/src/views/PlaylistDetail.vue index 75264f7..fc3ee24 100644 --- a/music-qianduan/src/views/PlaylistDetail.vue +++ b/music-qianduan/src/views/PlaylistDetail.vue @@ -525,7 +525,28 @@ onMounted(() => { // 连接 WebSocket 并订阅评论 connect(() => { const destination = `/topic/comments/2/${playlistId.value}`; // 2 代表歌单类型 - commentSubscription = subscribe(destination, (newComment) => { + commentSubscription = subscribe(destination, (message) => { + // 处理删除事件 + if (message && message.deleted) { + const deletedId = message.id; + if (message.parentId) { + // 删除子评论 + const parentComment = comments.value.find(c => c.id === message.parentId); + if (parentComment && parentComment.children) { + parentComment.children = parentComment.children.filter(c => c.id !== deletedId); + } + } else { + // 删除父评论 + comments.value = comments.value.filter(c => c.id !== deletedId); + } + if (playlist.value && playlist.value.commentCount > 0) { + playlist.value.commentCount--; + } + return; + } + + // 处理新增评论 + const newComment = message; if (newComment && newComment.id) { if (newComment.user && newComment.user.avatar) { newComment.user.avatar = processImageUrl(newComment.user.avatar); diff --git a/music-qianduan/src/views/SingerDetail.vue b/music-qianduan/src/views/SingerDetail.vue index 8a17f7e..49fabfd 100644 --- a/music-qianduan/src/views/SingerDetail.vue +++ b/music-qianduan/src/views/SingerDetail.vue @@ -441,7 +441,28 @@ onMounted(() => { // 连接 WebSocket 并订阅评论 connect(() => { const destination = `/topic/comments/3/${singerId.value}`; // 3 代表歌手类型 - commentSubscription = subscribe(destination, (newComment) => { + commentSubscription = subscribe(destination, (message) => { + // 处理删除事件 + if (message && message.deleted) { + const deletedId = message.id; + if (message.parentId) { + // 删除子评论 + const parentComment = comments.value.find(c => c.id === message.parentId); + if (parentComment && parentComment.children) { + parentComment.children = parentComment.children.filter(c => c.id !== deletedId); + } + } else { + // 删除父评论 + comments.value = comments.value.filter(c => c.id !== deletedId); + } + if (singer.value && singer.value.commentCount > 0) { + singer.value.commentCount--; + } + return; + } + + // 处理新增评论 + const newComment = message; if (newComment && newComment.id) { if (newComment.user && newComment.user.avatar) { newComment.user.avatar = processImageUrl(newComment.user.avatar);