|
|
@ -59,7 +59,7 @@ export const usePostDetailStore = defineStore("postDetail", {
|
|
|
|
this.commentsLoading = false;
|
|
|
|
this.commentsLoading = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// 获取某条评论的子评论(多级结构,分页)
|
|
|
|
// 获取某条评论的子评论
|
|
|
|
async fetchReplies(parentCommentId, commentObj) {
|
|
|
|
async fetchReplies(parentCommentId, commentObj) {
|
|
|
|
if (commentObj.repliesLoading || commentObj.repliesFinished) return;
|
|
|
|
if (commentObj.repliesLoading || commentObj.repliesFinished) return;
|
|
|
|
commentObj.repliesLoading = true;
|
|
|
|
commentObj.repliesLoading = true;
|
|
|
@ -76,12 +76,7 @@ export const usePostDetailStore = defineStore("postDetail", {
|
|
|
|
if (res.code === 200) {
|
|
|
|
if (res.code === 200) {
|
|
|
|
const records = (res.data.records || []).map(item => ({
|
|
|
|
const records = (res.data.records || []).map(item => ({
|
|
|
|
...item,
|
|
|
|
...item,
|
|
|
|
replies: [],
|
|
|
|
replyUserName: item.replyUserName,
|
|
|
|
repliesLastVal: 0,
|
|
|
|
|
|
|
|
repliesOffset: 0,
|
|
|
|
|
|
|
|
repliesSize: 5,
|
|
|
|
|
|
|
|
repliesFinished: false,
|
|
|
|
|
|
|
|
repliesLoading: false,
|
|
|
|
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
commentObj.replies.push(...records);
|
|
|
|
commentObj.replies.push(...records);
|
|
|
|
commentObj.repliesLastVal = res.data.lastVal;
|
|
|
|
commentObj.repliesLastVal = res.data.lastVal;
|
|
|
@ -138,7 +133,7 @@ export const usePostDetailStore = defineStore("postDetail", {
|
|
|
|
likeCount,
|
|
|
|
likeCount,
|
|
|
|
favoriteCount,
|
|
|
|
favoriteCount,
|
|
|
|
viewCount,
|
|
|
|
viewCount,
|
|
|
|
author: { // 新增 author 字段
|
|
|
|
author: {
|
|
|
|
userId,
|
|
|
|
userId,
|
|
|
|
userName,
|
|
|
|
userName,
|
|
|
|
userAvatar,
|
|
|
|
userAvatar,
|
|
|
@ -173,7 +168,7 @@ export const usePostDetailStore = defineStore("postDetail", {
|
|
|
|
id: null,
|
|
|
|
id: null,
|
|
|
|
postId: newCommentData.postId, // 帖子ID
|
|
|
|
postId: newCommentData.postId, // 帖子ID
|
|
|
|
content: newCommentData.content, // 评论内容
|
|
|
|
content: newCommentData.content, // 评论内容
|
|
|
|
parentCommentId: newCommentData.parentCommentId, // 如果是一级评论则为0
|
|
|
|
parentCommentId: newCommentData.parentCommentId,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
const res = await request.post('/comment', RequestData);
|
|
|
|
const res = await request.post('/comment', RequestData);
|
|
|
@ -189,28 +184,25 @@ export const usePostDetailStore = defineStore("postDetail", {
|
|
|
|
replyCount: 0,
|
|
|
|
replyCount: 0,
|
|
|
|
postId: this.post.postId,
|
|
|
|
postId: this.post.postId,
|
|
|
|
parentCommentId: newCommentData.parentCommentId,
|
|
|
|
parentCommentId: newCommentData.parentCommentId,
|
|
|
|
|
|
|
|
replyUserName: newCommentData.replyUserName,
|
|
|
|
topId: newCommentData.topId,
|
|
|
|
topId: newCommentData.topId,
|
|
|
|
isLike: 0,
|
|
|
|
isLike: 0,
|
|
|
|
replies: [],
|
|
|
|
|
|
|
|
repliesLastVal: 0,
|
|
|
|
|
|
|
|
repliesOffset: 0,
|
|
|
|
|
|
|
|
repliesSize: 5,
|
|
|
|
|
|
|
|
repliesFinished: false,
|
|
|
|
|
|
|
|
repliesLoading: false,
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// 新增评论后刷新评论列表或插入到对应位置
|
|
|
|
// 新增评论后刷新评论列表或插入到对应位置
|
|
|
|
if (!newCommentData.parentCommentId) {
|
|
|
|
if (!newCommentData.parentCommentId) {
|
|
|
|
// 一级评论,插入到最前面
|
|
|
|
// 一级评论,插入到最前面
|
|
|
|
|
|
|
|
commentObj.topId = commentObj.id; // 一级评论的顶级ID就是自己的ID
|
|
|
|
|
|
|
|
commentObj.replies = [];
|
|
|
|
|
|
|
|
commentObj.repliesLastVal = Date.now();
|
|
|
|
|
|
|
|
commentObj.repliesOffset = 0;
|
|
|
|
|
|
|
|
commentObj.repliesSize = 5;
|
|
|
|
|
|
|
|
commentObj.repliesLoading = false;
|
|
|
|
|
|
|
|
commentObj.repliesFinished = false;
|
|
|
|
this.comments.unshift(commentObj);
|
|
|
|
this.comments.unshift(commentObj);
|
|
|
|
this.post.commentCount = (this.post.commentCount || 0) + 1; // 更新帖子评论数
|
|
|
|
this.post.commentCount = (this.post.commentCount || 0) + 1; // 更新帖子评论数
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// 回复评论,插入到对应父评论的 replies
|
|
|
|
// 回复,只插入到一级评论的 replies
|
|
|
|
// 先找顶级父评论
|
|
|
|
let parent = this.comments.find(c => c.id === newCommentData.parentCommentId);
|
|
|
|
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) {
|
|
|
|
if (parent) {
|
|
|
|
parent.replies.unshift(commentObj);
|
|
|
|
parent.replies.unshift(commentObj);
|
|
|
|
parent.replyCount = (parent.replyCount || 0) + 1;
|
|
|
|
parent.replyCount = (parent.replyCount || 0) + 1;
|
|
|
|