|
|
|
@ -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, // 帖子主要信息
|
|
|
|
@ -16,67 +30,67 @@ export const usePostDetailStore = defineStore("postDetail", {
|
|
|
|
|
commentsFinished: false, // 是否加载完全部评论
|
|
|
|
|
}),
|
|
|
|
|
actions: {
|
|
|
|
|
// ...existing code...
|
|
|
|
|
async fetchComments() {
|
|
|
|
|
if (this.commentsLoading || this.commentsFinished) return;
|
|
|
|
|
this.commentsLoading = true;
|
|
|
|
|
// 拼接参数到URL
|
|
|
|
|
const params = [
|
|
|
|
|
`lastVal=${this.lastVal}`,
|
|
|
|
|
`offset=${this.offset}`,
|
|
|
|
|
`size=${this.size}`,
|
|
|
|
|
`postId=${this.post.postId}`
|
|
|
|
|
].join('&');
|
|
|
|
|
const url = `/comment/list?${params}`;
|
|
|
|
|
try {
|
|
|
|
|
const response = await request.get(url);
|
|
|
|
|
if (response.code === 200) {
|
|
|
|
|
// 初始化每条评论的子评论分页状态
|
|
|
|
|
const comments = (response.data.records || []).map(item => ({
|
|
|
|
|
...item,
|
|
|
|
|
replies: [],
|
|
|
|
|
repliesLastVal: Date.now(),
|
|
|
|
|
repliesOffset: 0,
|
|
|
|
|
repliesSize: 5,
|
|
|
|
|
repliesLoading: false,
|
|
|
|
|
repliesFinished: false,
|
|
|
|
|
}));
|
|
|
|
|
this.comments.push(...comments);
|
|
|
|
|
this.lastVal = response.data.lastVal;
|
|
|
|
|
this.offset = response.data.offset;
|
|
|
|
|
if (comments.length < this.size) {
|
|
|
|
|
this.commentsFinished = true; // 如果评论数少于每页大小,标记为已加载完
|
|
|
|
|
async fetchComments() {
|
|
|
|
|
if (this.commentsLoading || this.commentsFinished) return;
|
|
|
|
|
this.commentsLoading = true;
|
|
|
|
|
// 拼接参数到URL
|
|
|
|
|
const params = [
|
|
|
|
|
`lastVal=${this.lastVal}`,
|
|
|
|
|
`offset=${this.offset}`,
|
|
|
|
|
`size=${this.size}`,
|
|
|
|
|
`postId=${this.post.postId}`
|
|
|
|
|
].join('&');
|
|
|
|
|
const url = `/comment/list?${params}`;
|
|
|
|
|
try {
|
|
|
|
|
const response = await request.get(url);
|
|
|
|
|
if (response.code === 200) {
|
|
|
|
|
// 初始化每条评论的子评论分页状态
|
|
|
|
|
const comments = (response.data.records || []).map(item => ({
|
|
|
|
|
...item,
|
|
|
|
|
replies: [],
|
|
|
|
|
showReplies: false, // 是否显示子评论
|
|
|
|
|
repliesLastVal: Date.now(),
|
|
|
|
|
repliesOffset: 0,
|
|
|
|
|
repliesSize: 5,
|
|
|
|
|
repliesLoading: false,
|
|
|
|
|
repliesFinished: false,
|
|
|
|
|
}));
|
|
|
|
|
this.comments.push(...comments);
|
|
|
|
|
this.lastVal = response.data.lastVal;
|
|
|
|
|
this.offset = response.data.offset;
|
|
|
|
|
if (comments.length < this.size) {
|
|
|
|
|
this.commentsFinished = true; // 如果评论数少于每页大小,标记为已加载完
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ElMessage({
|
|
|
|
|
message: '获取评论失败,请稍后重试',
|
|
|
|
|
type: 'error',
|
|
|
|
|
duration: 500
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("获取评论失败:", error);
|
|
|
|
|
alert(error.response?.message || '获取评论失败,请稍后重试');
|
|
|
|
|
}finally {
|
|
|
|
|
this.commentsLoading = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ElMessage({
|
|
|
|
|
message: '获取评论失败,请稍后重试',
|
|
|
|
|
type: 'error',
|
|
|
|
|
duration: 500
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("获取评论失败:", error);
|
|
|
|
|
alert(error.response?.message || '获取评论失败,请稍后重试');
|
|
|
|
|
}finally {
|
|
|
|
|
this.commentsLoading = false;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
// ...existing code...
|
|
|
|
|
},
|
|
|
|
|
// 获取某条评论的子评论
|
|
|
|
|
async fetchReplies(parentCommentId, commentObj) {
|
|
|
|
|
if (commentObj.repliesLoading || commentObj.repliesFinished) return;
|
|
|
|
|
commentObj.repliesLoading = true;
|
|
|
|
|
// 请求子评论数据
|
|
|
|
|
const RequestReplyData = {
|
|
|
|
|
lastVal: commentObj.repliesLastVal,
|
|
|
|
|
offset: commentObj.repliesOffset,
|
|
|
|
|
size: commentObj.repliesSize,
|
|
|
|
|
postId: this.post?.postId,
|
|
|
|
|
parentCommentId: parentCommentId,
|
|
|
|
|
};
|
|
|
|
|
/// 拼接参数到URL
|
|
|
|
|
const params = [
|
|
|
|
|
`lastVal=${commentObj.repliesLastVal}`,
|
|
|
|
|
`offset=${commentObj.repliesOffset}`,
|
|
|
|
|
`size=${commentObj.repliesSize}`,
|
|
|
|
|
`postId=${this.post?.postId}`,
|
|
|
|
|
`parentCommentId=${parentCommentId}`
|
|
|
|
|
].join('&');
|
|
|
|
|
const url = `/comment/list/reply?${params}`;
|
|
|
|
|
try {
|
|
|
|
|
const res = await request.post('/comment/list/reply', RequestReplyData);
|
|
|
|
|
const res = await request.get(url);
|
|
|
|
|
if (res.code === 200) {
|
|
|
|
|
const records = (res.data.records || []).map(item => ({
|
|
|
|
|
...item,
|
|
|
|
@ -106,6 +120,12 @@ async fetchComments() {
|
|
|
|
|
|
|
|
|
|
async fetchPostDetail(postId) {
|
|
|
|
|
this.detailLoading = true;
|
|
|
|
|
// 先重置评论相关状态,防止串数据
|
|
|
|
|
this.comments = [];
|
|
|
|
|
this.lastVal = Date.now();
|
|
|
|
|
this.offset = 0;
|
|
|
|
|
this.commentsFinished = false;
|
|
|
|
|
this.commentsLoading = false;
|
|
|
|
|
try {
|
|
|
|
|
// 获取帖子详情
|
|
|
|
|
const postRes = await request.get('/post/detail', { params: { id: postId } });
|
|
|
|
@ -169,15 +189,17 @@ async fetchComments() {
|
|
|
|
|
async sendComment(newCommentData) {
|
|
|
|
|
if (!newCommentData.content || !this.post?.postId) return;
|
|
|
|
|
const RequestData = {
|
|
|
|
|
id:null,
|
|
|
|
|
postId: newCommentData.postId, // 帖子ID
|
|
|
|
|
content: newCommentData.content, // 评论内容
|
|
|
|
|
parentCommentId: newCommentData.parentCommentId,
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
const res = await request.post('/comment', RequestData);
|
|
|
|
|
console.log("发送评论返回:", res.data);
|
|
|
|
|
if (res.code === 200) {
|
|
|
|
|
const commentObj = {
|
|
|
|
|
id: res.data.id,
|
|
|
|
|
id: res.data,
|
|
|
|
|
content: newCommentData.content,
|
|
|
|
|
userId: newCommentData.userId,
|
|
|
|
|
userName: newCommentData.userName,
|
|
|
|
@ -191,6 +213,7 @@ async fetchComments() {
|
|
|
|
|
topId: newCommentData.topId,
|
|
|
|
|
isLike: 0,
|
|
|
|
|
};
|
|
|
|
|
console.log("发送评论数据:", commentObj.id);
|
|
|
|
|
// 新增评论后刷新评论列表或插入到对应位置
|
|
|
|
|
if (!newCommentData.parentCommentId) {
|
|
|
|
|
// 一级评论,插入到最前面
|
|
|
|
@ -202,16 +225,16 @@ async fetchComments() {
|
|
|
|
|
commentObj.repliesLoading = false;
|
|
|
|
|
commentObj.repliesFinished = false;
|
|
|
|
|
this.comments.unshift(commentObj);
|
|
|
|
|
this.post.commentCount = (this.post.commentCount || 0) + 1; // 更新帖子评论数
|
|
|
|
|
} else {
|
|
|
|
|
// 回复,只插入到一级评论的 replies
|
|
|
|
|
let parent = this.comments.find(c => c.id === newCommentData.parentCommentId);
|
|
|
|
|
let parent = this.comments.find(c => c.id === newCommentData.topId);
|
|
|
|
|
if (parent) {
|
|
|
|
|
parent.replies.unshift(commentObj);
|
|
|
|
|
parent.replies.push(commentObj);
|
|
|
|
|
parent.replyCount = (parent.replyCount || 0) + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.log("评论成功:", res);
|
|
|
|
|
this.post.commentCount = (this.post.commentCount || 0) + 1; // 更新帖子评论数
|
|
|
|
|
console.log("评论成功:", commentObj);
|
|
|
|
|
}else {
|
|
|
|
|
console.error("评论失败:", res);
|
|
|
|
|
ElMessage.error(res.message || '评论失败');
|
|
|
|
@ -220,5 +243,29 @@ async fetchComments() {
|
|
|
|
|
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 || '删除失败,请稍后重试');
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
});
|