From f0ac926e7ebef2a0ed187b073e5b75fa6e2ac17b Mon Sep 17 00:00:00 2001 From: lee-zt <2960166273@qq.com> Date: Tue, 3 Jun 2025 22:24:02 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E8=AF=84=E8=AE=BA=E5=87=BD=E6=95=B0=EF=BC=88=E8=AF=84=E8=AE=BA?= =?UTF-8?q?=E6=95=B0=E9=97=AE=E9=A2=98=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vue-frontend/src/stores/postdetail.js | 42 ++++++++++--------- .../vue-frontend/src/views/PostDetail.vue | 6 +-- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js index 110a493..da07d51 100644 --- a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js +++ b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js @@ -2,20 +2,6 @@ import { defineStore } from "pinia"; import request from "@/utils/request"; import { ElMessage } from "element-plus"; -// 递归删除子评论的工具函数 -function removeReply(list, commentId) { - for (let i = 0; i < list.length; i++) { - if (list[i].id === commentId) { - list.splice(i, 1); - return true; - } - if (list[i].replies && list[i].replies.length > 0) { - if (removeReply(list[i].replies, commentId)) return true; - } - } - return false; -} - export const usePostDetailStore = defineStore("postDetail", { state: () => ({ post: null, // 帖子主要信息 @@ -244,16 +230,34 @@ export const usePostDetailStore = defineStore("postDetail", { } }, // 删除评论或回复 - async deleteComment(commentId, parentCommentId = null) { + async deleteComment(commentObj) { try { + const commentId = commentObj.id; + const topId = commentObj.topId; const res = await request.delete('/comment', { params: { id: commentId} }); if (res.code === 200) { // 一级评论 - if (!parentCommentId) { - this.comments = this.comments.filter(c => c.id !== commentId); + if (commentObj.id === commentObj.topId) { + // 删除一级评论及其所有回复 + const idx = this.comments.findIndex(c => c.id === commentId); + if (idx !== -1) { + const comment = this.comments[idx]; + // 总评论数 -= replyCount + if (this.post && this.post.commentCount > 0) { + this.post.commentCount -= (comment.replyCount || 0); + if (this.post.commentCount < 0) this.post.commentCount = 0; + } + this.comments.splice(idx, 1); + } } else { - // 回复评论,递归删除 - removeReply(this.comments,commentId); + const topComment = this.comments.find(c => c.id === topId); + if (topComment) { + const replyIdx = topComment.replies.findIndex(r => r.id === commentId); + if (replyIdx !== -1) { + topComment.replies.splice(replyIdx, 1); + if (topComment.replyCount > 0) topComment.replyCount--; + } + } } // 更新评论数 if (this.post && this.post.commentCount > 0) { diff --git a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/views/PostDetail.vue b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/views/PostDetail.vue index 1f0bbbf..467e4a0 100644 --- a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/views/PostDetail.vue +++ b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/views/PostDetail.vue @@ -86,7 +86,7 @@ @@ -201,7 +201,7 @@ const sendComment = () => { } }; -function handleDelete(comment, parentCommentId = null) { +function handleDelete(comment) { ElMessageBox.confirm( '确定要删除这条评论吗?', '提示', @@ -211,7 +211,7 @@ function handleDelete(comment, parentCommentId = null) { type: 'warning', } ).then(() => { - postDetailStore.deleteComment(comment.id, parentCommentId); + postDetailStore.deleteComment(comment); }).catch(() => {}); } // 监听 postDetailStore.post 加载完成后再允许滚动加载评论 From 3eaa20c80393aa0a740bcfa15e43cedea35f9b02 Mon Sep 17 00:00:00 2001 From: lee-zt <2960166273@qq.com> Date: Wed, 4 Jun 2025 10:39:34 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E7=82=B9=E8=B5=9E=E5=B8=96=E5=AD=90?= =?UTF-8?q?=E5=92=8C=E8=AF=84=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../vue-frontend/src/components/PostPage.vue | 6 +- .../vue-frontend/src/stores/postdetail.js | 60 +++++++++++++++++++ .../vue-frontend/src/views/PostDetail.vue | 45 ++++++++++++-- 3 files changed, 103 insertions(+), 8 deletions(-) diff --git a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/components/PostPage.vue b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/components/PostPage.vue index ef90945..41edd69 100644 --- a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/components/PostPage.vue +++ b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/components/PostPage.vue @@ -27,9 +27,9 @@

{{ post.summary }}

- 浏览量 {{ post.viewCount }} - 评论 {{ post.comments }} - 赞 {{ post.likes }} + 👁 {{ post.viewCount }} + 🗨 {{ post.comments }} + ♥ {{ post.likes }}
diff --git a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js index da07d51..53c4fc0 100644 --- a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js +++ b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/stores/postdetail.js @@ -271,5 +271,65 @@ export const usePostDetailStore = defineStore("postDetail", { ElMessage.error(e.response?.message || '删除失败,请稍后重试'); } }, + //帖子点赞和取消 + async PostLike() { + if (!this.post || !this.post.postId) return; + try { + const url = `/post/like/${this.post.postId}`; + if (!this.isLike) { + // 点赞 + const res = await request.put(url); + if (res.code === 200) { + this.isLike = true; + this.post.likeCount = (this.post.likeCount || 0) + 1; + ElMessage.success('点赞成功'); + } else { + ElMessage.error(res.message || '点赞失败'); + } + } else { + // 取消点赞 + const res = await request.put(url); + if (res.code === 200) { + this.isLike = false; + this.post.likeCount = Math.max((this.post.likeCount || 1) - 1, 0); + ElMessage.success('已取消点赞'); + } else { + ElMessage.error(res.message || '取消点赞失败'); + } + } + } catch (e) { + ElMessage.error(e.response?.message || '操作失败,请稍后重试'); + } + }, + // 评论点赞和取消 + async CommentLike(commentObj) { + if (!commentObj || !commentObj.id) return; + try { + const url = `/comment/like/${commentObj.id}`; + if (!commentObj.isLike) { + // 点赞 + const res = await request.put(url); + if (res.code === 200) { + commentObj.isLike = true; + commentObj.likeCount = (commentObj.likeCount || 0) + 1; + ElMessage.success('点赞成功'); + } else { + ElMessage.error(res.message || '点赞失败'); + } + } else { + // 取消点赞 + const res = await request.put(url); + if (res.code === 200) { + commentObj.isLike = false; + commentObj.likeCount = Math.max((commentObj.likeCount || 1) - 1, 0); + ElMessage.success('已取消点赞'); + } else { + ElMessage.error(res.message || '取消点赞失败'); + } + } + } catch (e) { + ElMessage.error(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 467e4a0..72d11ed 100644 --- a/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/views/PostDetail.vue +++ b/珞珈岛-项目相关文件/luojia-island/vue-frontend/src/views/PostDetail.vue @@ -24,9 +24,19 @@

{{ postDetailStore.post?.title || '' }}

{{ postDetailStore.post?.content || '' }}

- 浏览量 {{ postDetailStore.post?.viewCount ?? 0 }} - 点赞 {{ postDetailStore.post?.likeCount ?? 0 }} - 评论 {{ postDetailStore.post?.commentCount ?? 0 }} + 👁 {{ postDetailStore.post?.viewCount ?? 0 }} + 🗨 {{ postDetailStore.post?.commentCount ?? 0 }} + + +
发布时间:{{ postDetailStore.post?.createTime ? formatTime(postDetailStore.post.createTime) : '' }} @@ -50,7 +60,14 @@

{{ comment.content || '' }}

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