修改删除评论函数(评论数问题)

main
lee-zt 4 weeks ago
parent a467856a7c
commit f0ac926e7e

@ -2,20 +2,6 @@ import { defineStore } from "pinia";
import request from "@/utils/request"; import request from "@/utils/request";
import { ElMessage } from "element-plus"; 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", { export const usePostDetailStore = defineStore("postDetail", {
state: () => ({ state: () => ({
post: null, // 帖子主要信息 post: null, // 帖子主要信息
@ -244,16 +230,34 @@ export const usePostDetailStore = defineStore("postDetail", {
} }
}, },
// 删除评论或回复 // 删除评论或回复
async deleteComment(commentId, parentCommentId = null) { async deleteComment(commentObj) {
try { try {
const commentId = commentObj.id;
const topId = commentObj.topId;
const res = await request.delete('/comment', { params: { id: commentId} }); const res = await request.delete('/comment', { params: { id: commentId} });
if (res.code === 200) { if (res.code === 200) {
// 一级评论 // 一级评论
if (!parentCommentId) { if (commentObj.id === commentObj.topId) {
this.comments = this.comments.filter(c => c.id !== commentId); // 删除一级评论及其所有回复
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 { } else {
// 回复评论,递归删除 const topComment = this.comments.find(c => c.id === topId);
removeReply(this.comments,commentId); 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) { if (this.post && this.post.commentCount > 0) {

@ -86,7 +86,7 @@
<button <button
class="delete-btn" class="delete-btn"
v-if="String(reply.userId) === String(userStore.userInfo?.userid)" v-if="String(reply.userId) === String(userStore.userInfo?.userid)"
@click="handleDelete(reply, comment.id)" @click="handleDelete(reply)"
>删除</button> >删除</button>
</div> </div>
</div> </div>
@ -201,7 +201,7 @@ const sendComment = () => {
} }
}; };
function handleDelete(comment, parentCommentId = null) { function handleDelete(comment) {
ElMessageBox.confirm( ElMessageBox.confirm(
'确定要删除这条评论吗?', '确定要删除这条评论吗?',
'提示', '提示',
@ -211,7 +211,7 @@ function handleDelete(comment, parentCommentId = null) {
type: 'warning', type: 'warning',
} }
).then(() => { ).then(() => {
postDetailStore.deleteComment(comment.id, parentCommentId); postDetailStore.deleteComment(comment);
}).catch(() => {}); }).catch(() => {});
} }
// postDetailStore.post // postDetailStore.post

Loading…
Cancel
Save