From 0fc3a5e109b8070d270fbb7fcf57cfe10f91df2f Mon Sep 17 00:00:00 2001 From: lee-zt <2960166273@qq.com> Date: Thu, 29 May 2025 23:24:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E9=80=81=E8=AF=84=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vue-frontend/src/stores/postdetail.js | 59 ++++++++++ .../vue-frontend/src/views/PostDetail.vue | 101 +++++++++++++----- 2 files changed, 135 insertions(+), 25 deletions(-) diff --git a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js index 45f66cb..87083b6 100644 --- a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js +++ b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js @@ -169,5 +169,64 @@ export const usePostDetailStore = defineStore("postDetail", { this.detailLoading = false; } }, + // 发送评论或回复 + async sendComment(newCommentData) { + if (!content || !this.post?.postId) return; + const RequestData = { + id: null, + postId: newCommentData.postId, // 帖子ID + content: newCommentData.content, // 评论内容 + parentCommentId: newCommentData.parentCommentId, // 如果是一级评论则为0 + }; + try { + const res = await request.post('/comment', RequestData); + if (res.code === 200) { + const commentObj = { + id: res.data.id, + content: newCommentData.content, + userId: newCommentData.userId, + userName: newCommentData.userName, + userAvatar: newCommentData.userAvatar, + createTime: new Date().toISOString(), + likeCount: 0, + replyCount: 0, + postId: this.post.postId, + parentCommentId: newCommentData.parentCommentId, + topId: newCommentData.topId, + isLike: 0, + replies: [], + repliesLastVal: 0, + repliesOffset: 0, + repliesSize: 5, + repliesFinished: false, + repliesLoading: false, + }; + // 新增评论后刷新评论列表或插入到对应位置 + if (!parentCommentId) { + // 一级评论,插入到最前面 + this.comments.unshift(commentObj); + this.post.commentCount = (this.post.commentCount || 0) + 1; // 更新帖子评论数 + } else { + // 回复评论,插入到对应父评论的 replies + // 先找顶级父评论 + let parent = this.comments.find(c => c.id === commentObj.parentCommentId || c.id === commentObj.topId); + // 如果是二级及以上回复,需递归查找 + if (!parent && commentObj.topId) { + parent = this.comments.find(c => c.id === commentObj.topId); + } + if (parent) { + parent.replies.unshift(commentObj); + parent.replyCount = (parent.replyCount || 0) + 1; + } + } + console.log("评论成功:", res); + }else { + console.error("评论失败:", res); + ElMessage.error(res.message || '评论失败'); + } + } catch (e) { + alert(e.response?.message || '发送评论失败,请稍后重试'); + } + }, }, }); \ No newline at end of file diff --git a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/views/PostDetail.vue b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/views/PostDetail.vue index 0b079f4..86b5f46 100644 --- a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/views/PostDetail.vue +++ b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/views/PostDetail.vue @@ -35,25 +35,34 @@ 评论者头像

{{ comment.userName }}

-

{{ comment.content }}

-
- {{ comment.createTime ? formatTime(comment.createTime) : '' }} - 赞 {{ comment.likeCount ?? 0 }} - - - -
- - +

{{ comment.content }}

+
+ {{ comment.createTime ? formatTime(comment.createTime) : '' }} + 赞 {{ comment.likeCount ?? 0 }} + + + + +
+ +
@@ -61,6 +70,10 @@
+
+ 正在回复 @{{ replyingComment.userName }} + +