删除评论

main
lee-zt 4 weeks ago
parent 54afda80e1
commit c467599302

@ -185,22 +185,27 @@ public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> impl
@Transactional(rollbackFor = Exception.class)
public void deleteComment(Long id) {
validatePostUtil.validateCommentOwnership(id);
LambdaQueryWrapper<Comment> queryWrapper = Wrappers.lambdaQuery(Comment.class)
.eq(Comment::getTopId, id);
int delete = commentMapper.delete(queryWrapper);
if(delete <= 0) {
throw new PostException("删除评论失败");
}
Comment comment = commentMapper.selectById(id);
if(comment.getId().equals(comment.getTopId())) {
LambdaQueryWrapper<Comment> queryWrapper = Wrappers.lambdaQuery(Comment.class)
.eq(Comment::getTopId, id);
int delete = commentMapper.delete(queryWrapper);
if(delete <= 0) {
throw new PostException("删除根评论失败");
}
redisUtil.zRemove("post:comment_by_time:" + comment.getPostId(), comment.getId());
redisUtil.zRemove("post:comment_by_hot:" + comment.getPostId(), comment.getId());
redisUtil.delete("comment:reply_by_time:" + comment.getTopId());
redisUtil.delete("comment:reply_by_hot:" + comment.getTopId());
}else{
int delete = commentMapper.deleteById(id);
if(delete <= 0){
throw new PostException("删除评论失败");
}
redisUtil.zRemove("comment:reply_by_time:" + comment.getTopId(), comment.getId());
redisUtil.zRemove("comment:reply_by_hot:" + comment.getTopId(), comment.getId());
}
// TODO 如果根评论删除,那么其他评论怎么办,目前做法是删除其下所有的子评论
}

@ -17,38 +17,38 @@
# accessKey: forely
# secretKey: Forely123!
lj:
db:
host: 192.168.125.128
password: MySQL@5678
redis:
host: 192.168.125.128
port: 6379
password: Redis@9012
rabbitmq:
host: 192.168.125.128
port: 5672
username: rabbit_admin
password: Rabbit@3456
minio:
endpoint: http://192.168.125.128:9000
accessKey: minio_admin
secretKey: Minio@1234
#lj:
# db:
# host: localhost
# password: 123456
# host: 192.168.125.128
# password: MySQL@5678
# redis:
# host: localhost
# host: 192.168.125.128
# port: 6379
# password: 123456
# password: Redis@9012
# rabbitmq:
# host: localhost
# host: 192.168.125.128
# port: 5672
# username: guest
# password: guest
# username: rabbit_admin
# password: Rabbit@3456
# minio:
# endpoint: http://localhost:9005
# accessKey: leezt
# secretKey: lzt264610
# endpoint: http://192.168.125.128:9000
# accessKey: minio_admin
# secretKey: Minio@1234
lj:
db:
host: localhost
password: 123456
redis:
host: localhost
port: 6379
password: 123456
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
minio:
endpoint: http://localhost:9005
accessKey: leezt
secretKey: lzt264610

@ -92,7 +92,7 @@ const colleges = ref([
]);
//
const defaultAvatar = require("@/assets/default-avatar/boy_4.png");
const defaultAvatar = require("@/assets/default-avatar/boy_1.png");
//
const isLoggedIn = computed(() => userStore.isLoggedIn);

@ -180,17 +180,6 @@ async function sendCode() {
}
//
//function Login1() {
// userStore.login({
// avatar:require('@/assets/default-avatar/boy_1.png'),
// userName: '',
// userid:1
// });
// emit('LoginSuccess');
//}
//
async function login() {
if (!isvalidForm.value) return;

@ -2,6 +2,20 @@ 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, // 帖子主要信息
@ -229,5 +243,29 @@ export const usePostDetailStore = defineStore("postDetail", {
alert(e.response?.message || '发送评论失败,请稍后重试');
}
},
// 删除评论或回复
async deleteComment(commentId, parentCommentId = null) {
try {
const res = await request.delete('/comment', { params: { id: commentId} });
if (res.code === 200) {
// 一级评论
if (!parentCommentId) {
this.comments = this.comments.filter(c => c.id !== commentId);
} else {
// 回复评论,递归删除
removeReply(this.comments,commentId);
}
// 更新评论数
if (this.post && this.post.commentCount > 0) {
this.post.commentCount--;
}
ElMessage.success('删除成功');
} else {
ElMessage.error(res.message || '删除失败');
}
} catch (e) {
ElMessage.error(e.response?.message || '删除失败,请稍后重试');
}
},
},
});

@ -57,6 +57,11 @@
</button>
</span>
<button class="reply-btn" @click="startReply(comment)"></button>
<button
class="delete-btn"
v-if="String(comment.userId) === String(userStore.userInfo?.userid)"
@click="handleDelete(comment)"
>删除</button>
</div>
<!-- 子评论列表 -->
<ul v-if="comment.showReplies && comment.replies && comment.replies.length > 0" class="replies-list">
@ -78,6 +83,11 @@
<span class="comment-time">{{ reply.createTime ? formatTime(reply.createTime) : '' }}</span>
<span class="comment-likes"> {{ reply.likeCount ?? 0 }}</span>
<button class="reply-btn" @click="startReply(reply)"></button>
<button
class="delete-btn"
v-if="String(reply.userId) === String(userStore.userInfo?.userid)"
@click="handleDelete(reply, comment.id)"
>删除</button>
</div>
</div>
</li>
@ -108,6 +118,7 @@
<script setup lang="js" name="PostDetail">
import { ref, computed, onMounted , onUnmounted, watch } from 'vue';
import { useRoute,useRouter } from 'vue-router';
import { ElMessageBox } from 'element-plus';
import { usePostDetailStore } from '@/stores/postdetail.js';
import { useUserStore } from '@/stores/user.js';
@ -185,13 +196,24 @@ const sendComment = () => {
topId: replyingComment.value ? replyingComment.value.topId : null,
isLike: false,
}
console.log('回复:', replyingComment.value);
console.log('发送评论:', newCommentData);
postDetailStore.sendComment(newCommentData);
newComment.value = '';
}
};
function handleDelete(comment, parentCommentId = null) {
ElMessageBox.confirm(
'确定要删除这条评论吗?',
'提示',
{
confirmButtonText: '删除',
cancelButtonText: '取消',
type: 'warning',
}
).then(() => {
postDetailStore.deleteComment(comment.id, parentCommentId);
}).catch(() => {});
}
// postDetailStore.post
watch(
() => postDetailStore.post,
@ -507,4 +529,15 @@ onUnmounted(() => {
.reply-btn:hover {
text-decoration: underline;
}
.delete-btn {
background: none;
border: none;
color: #f56c6c;
cursor: pointer;
font-size: 13px;
margin-left: 10px;
}
.delete-btn:hover {
text-decoration: underline;
}
</style>

@ -214,7 +214,7 @@ const privacySettings = ref({
const recommendations = ref([]);
//
const defaultAvatar = require('@/assets/default-avatar/boy_4.png');
const defaultAvatar = require('@/assets/default-avatar/boy_1.png');
//
const handleAvatarError = "this.onerror=null;this.src='" + defaultAvatar + "'";

Loading…
Cancel
Save