|
|
|
@ -1,39 +1,117 @@
|
|
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
import request from "@/utils/request";
|
|
|
|
|
import { ElMessage } from "element-plus";
|
|
|
|
|
|
|
|
|
|
export const usePostDetailStore = defineStore("postDetail", {
|
|
|
|
|
state: () => ({
|
|
|
|
|
post: null, // 帖子主要信息
|
|
|
|
|
comments: [], // 评论列表
|
|
|
|
|
userInfo: null, // 用户信息
|
|
|
|
|
totalComments: 0, // 评论总数
|
|
|
|
|
likeCount: 0, // 点赞数
|
|
|
|
|
commentCount: 0, // 评论数
|
|
|
|
|
favoriteCount: 0, // 收藏数
|
|
|
|
|
viewCount: 0, // 浏览数
|
|
|
|
|
isLike: false, // 是否点赞
|
|
|
|
|
loading: false, // 加载状态
|
|
|
|
|
lastVal: 0, // 用于滚动分页的时间戳
|
|
|
|
|
offset: 0, // 偏移量
|
|
|
|
|
size: 5, // 每页评论数
|
|
|
|
|
detailLoading: false, // 加载状态
|
|
|
|
|
commentsLoading: false, // 评论加载状态
|
|
|
|
|
commentsFinished: false, // 是否加载完全部评论
|
|
|
|
|
}),
|
|
|
|
|
actions: {
|
|
|
|
|
setPost(post) {
|
|
|
|
|
this.post = post;
|
|
|
|
|
},
|
|
|
|
|
setComments(comments) {
|
|
|
|
|
this.comments = comments;
|
|
|
|
|
this.totalComments = comments.length;
|
|
|
|
|
},
|
|
|
|
|
setUserInfo(userInfo) {
|
|
|
|
|
this.userInfo = userInfo;
|
|
|
|
|
async fetchComments() {
|
|
|
|
|
if (this.commentsLoading || this.commentsFinished) return;
|
|
|
|
|
this.commentsLoading = true;
|
|
|
|
|
const RequestCommentData = {
|
|
|
|
|
lastVal: this.lastVal,
|
|
|
|
|
offset: this.offset,
|
|
|
|
|
size: this.size,
|
|
|
|
|
postId: this.post.postId
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
const response = await request.post('/comment/list', RequestCommentData);
|
|
|
|
|
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; // 如果评论数少于每页大小,标记为已加载完
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ElMessage({
|
|
|
|
|
message: '获取评论失败,请稍后重试',
|
|
|
|
|
type: 'error',
|
|
|
|
|
duration: 500
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("获取评论失败:", error);
|
|
|
|
|
alert(error.response?.message || '获取评论失败,请稍后重试');
|
|
|
|
|
}finally {
|
|
|
|
|
this.commentsLoading = false;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
addComment(comment) {
|
|
|
|
|
this.comments.push(comment);
|
|
|
|
|
this.totalComments += 1;
|
|
|
|
|
// 获取某条评论的子评论(多级结构,分页)
|
|
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
try {
|
|
|
|
|
const res = await request.post('/comment/list/reply', RequestReplyData);
|
|
|
|
|
if (res.code === 200) {
|
|
|
|
|
const records = (res.data.records || []).map(item => ({
|
|
|
|
|
...item,
|
|
|
|
|
replies: [],
|
|
|
|
|
repliesLastVal: 0,
|
|
|
|
|
repliesOffset: 0,
|
|
|
|
|
repliesSize: 5,
|
|
|
|
|
repliesFinished: false,
|
|
|
|
|
repliesLoading: false,
|
|
|
|
|
}));
|
|
|
|
|
commentObj.replies.push(...records);
|
|
|
|
|
commentObj.repliesLastVal = res.data.lastVal;
|
|
|
|
|
commentObj.repliesOffset = res.data.offset;
|
|
|
|
|
if (records.length < commentObj.repliesSize) {
|
|
|
|
|
commentObj.repliesFinished = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ElMessage({
|
|
|
|
|
message: '获取子评论失败,请稍后重试',
|
|
|
|
|
type: 'error',
|
|
|
|
|
duration: 500
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("获取子评论失败:", error);
|
|
|
|
|
alert(error.response?.message || '获取子评论失败,请稍后重试');
|
|
|
|
|
} finally {
|
|
|
|
|
commentObj.repliesLoading = false;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
async fetchPostDetail(postId, { lastVal = Date.now(), offset = 0, size = 10 } = {}) {
|
|
|
|
|
this.loading = true;
|
|
|
|
|
async fetchPostDetail(postId) {
|
|
|
|
|
this.detailLoading = true;
|
|
|
|
|
try {
|
|
|
|
|
// 获取帖子详情
|
|
|
|
|
const postRes = await request.get(`/post/detail/${postId}`);
|
|
|
|
|
const RequestPostDetailData = {
|
|
|
|
|
id: postId,
|
|
|
|
|
};
|
|
|
|
|
const postRes = await request.get('/post/detail/', RequestPostDetailData);
|
|
|
|
|
if (postRes.code === 200 && postRes.data) {
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
@ -58,6 +136,10 @@ export const usePostDetailStore = defineStore("postDetail", {
|
|
|
|
|
title,
|
|
|
|
|
content,
|
|
|
|
|
createTime,
|
|
|
|
|
commentCount,
|
|
|
|
|
likeCount,
|
|
|
|
|
favoriteCount,
|
|
|
|
|
viewCount,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 用户信息
|
|
|
|
@ -67,27 +149,24 @@ export const usePostDetailStore = defineStore("postDetail", {
|
|
|
|
|
userAvatar: userAvatar,
|
|
|
|
|
followers:1234,//先预设粉丝占位
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 其余字段
|
|
|
|
|
this.likeCount = likeCount;
|
|
|
|
|
this.commentCount = commentCount;
|
|
|
|
|
this.favoriteCount = favoriteCount;
|
|
|
|
|
this.viewCount = viewCount;
|
|
|
|
|
this.isLike = isLike;
|
|
|
|
|
// 获取评论列表
|
|
|
|
|
this.lastVal = Date.now();
|
|
|
|
|
this.fetchComments();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取评论列表
|
|
|
|
|
const commentRes = await request.post('/comment/list', {
|
|
|
|
|
lastVal,
|
|
|
|
|
offset,
|
|
|
|
|
size,
|
|
|
|
|
postId
|
|
|
|
|
});
|
|
|
|
|
if (commentRes.code === 200) {
|
|
|
|
|
this.setComments(commentRes.data.records || []);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
this.loading = false;
|
|
|
|
|
else {
|
|
|
|
|
ElMessage({
|
|
|
|
|
message: '获取帖子详情失败,请稍后重试',
|
|
|
|
|
type: 'error',
|
|
|
|
|
duration: 500
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("获取帖子详情失败:", error);
|
|
|
|
|
alert(error.response?.message || '获取帖子详情失败,请稍后重试');
|
|
|
|
|
}finally {
|
|
|
|
|
this.detailLoading = false;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|