Browse Source

评论

dev_des
1054425342@qq.com 4 weeks ago
parent
commit
138b3bc47d
  1. 513
      pages/notes/detail.vue

513
pages/notes/detail.vue

@ -89,57 +89,139 @@
>评论 ({{ noteDetail.commentCount || 0 }})</text >评论 ({{ noteDetail.commentCount || 0 }})</text
> >
</view> </view>
<view <view
class="comment-item" class="comment-item"
v-for="comment in noteDetail.comments" v-for="comment in noteDetail.comments"
:key="comment.id" :key="comment.id"
> >
<image <view class="main-comment" @click="replyToComment(comment)">
class="comment-avatar" <image
:src="showImg(comment.headImg)" class="comment-avatar"
mode="aspectFill" :src="comment.headImg"
/> mode="aspectFill"
<view class="comment-content"> />
<view class="comment-header"> <view class="comment-content">
<text class="comment-user">{{ comment.nickname }}</text> <view class="comment-header">
<text class="comment-time">{{ comment.formatTime }}</text> <text class="comment-user">{{ comment.nickname }}</text>
</view>
<text class="comment-text">{{ comment.content }}</text>
<view class="comment-footer">
<text class="comment-time">{{ comment.formatTime }}</text>
<text class="reply-btn">回复</text>
</view>
<!-- 展开/收起二级评论按钮 -->
<!-- 二级评论列表 -->
<view
class="replies-container"
v-if="
showRepliesMap[comment.id] &&
comment.replies &&
comment.replies.length > 0
"
>
<view
class="reply-item"
v-for="reply in comment.replies"
:key="reply.id"
@click.stop="replyToComment(reply)"
>
<image
class="reply-avatar"
:src="showImg(reply.headImg)"
mode="aspectFill"
/>
<view class="reply-content">
<view class="reply-header">
<text class="reply-user">{{ reply.nickname }}</text>
<text class="reply-to" v-if="reply.toNickname"
>回复 @{{ reply.toNickname }}</text
>
</view>
<text class="reply-text">{{ reply.content }}</text>
<view class="reply-footer">
<text class="reply-time">{{ reply.formatTime }}</text>
<text class="reply-btn">回复</text>
</view>
</view>
</view>
<!-- 展开更多按钮 -->
</view>
<view
class="toggle-replies flex-start"
v-if="comment.commentCount > 0"
@click.stop="toggleReplies(comment.id)"
>
<view
class="load-more-replies flex-start"
v-if="showRepliesMap[comment.id] && comment.hasMoreReplies"
@click.stop="loadReplies(comment.id, true)"
>
<text>展开更多</text
><uni-icons type="down" color="#666" size="18"></uni-icons>
</view>
<view>
<view v-if="!showRepliesMap[comment.id]" class="flex-start">
<text>展开{{ comment.commentCount }}条回复</text>
<uni-icons type="down" color="#666" size="18"></uni-icons>
</view>
<view class="flex-start" v-else>
<text>收起</text>
<uni-icons type="up" color="#666" size="18"></uni-icons>
</view>
</view>
</view>
</view> </view>
<text class="comment-text">{{ comment.content }}</text>
</view> </view>
</view> </view>
<!-- 加载更多提示 -->
<view class="loading-more" v-if="loadingComments">
<text>加载中...</text>
</view>
<view
class="no-more-comments"
v-if="!hasMoreComments && noteDetail.comments.length > 0"
>
<text>没有更多评论了</text>
</view>
<view
class="no-comments"
v-if="!hasMoreComments && noteDetail.comments.length === 0"
>
<text>暂无评论快来发表第一条评论吧</text>
</view>
</view> </view>
<!-- 底部占位 --> <!-- 加载更多提示 -->
<view class="bottom-placeholder"></view> <view class="loading-more" v-if="loadingComments">
<text>加载中...</text>
</view>
<view
class="no-more-comments"
v-if="!hasMoreComments && noteDetail.comments.length > 0"
>
<text>没有更多评论了</text>
</view>
<view
class="no-comments"
v-if="!hasMoreComments && noteDetail.comments.length === 0"
>
<text>暂无评论快来发表第一条评论吧</text>
</view>
</view> </view>
<!-- 底部占位 -->
<view class="bottom-placeholder"></view>
<!-- 底部评论输入框 --> <!-- 底部评论输入框 -->
<view class="comment-input-section-box"> <view class="comment-input-section-box">
<view class="comment-input-section"> <view class="comment-input-section">
<input <input
class="comment-input" class="comment-input"
v-model="commentText" v-model="commentText"
placeholder="写下你的想法..." :placeholder="toNickname ? `回复 @${toNickname}` : '写下你的想法...'"
ref="inputComment"
:focus="inputFocusState"
@confirm="submitComment" @confirm="submitComment"
@focus="showTextarea = true"
@blur="inputFocusState = false"
/> />
<!-- 文本域用于多行输入 -->
<!-- <textarea
v-else
class="comment-textarea"
v-model="commentText"
:placeholder="
toNickname ? `回复 @${toNickname}` : '写下你的想法...'
"
auto-height
:focus="showTextarea"
@blur="onTextareaBlur"
@confirm="submitComment"
/> -->
<button <button
class="send-btn" class="send-btn"
@click="submitComment" @click="submitComment"
@ -192,6 +274,12 @@ export default {
pageSize: 10, pageSize: 10,
hasMoreComments: true, hasMoreComments: true,
loadingComments: false, loadingComments: false,
parentId: null, // ID
toUserId: null, // ID
toHeadImg: "", //
toNickname: "", //
inputFocusState: false, //
showRepliesMap: {}, //
noteDetail: { noteDetail: {
id: "", id: "",
title: "", title: "",
@ -202,7 +290,6 @@ export default {
isLiked: false, isLiked: false,
createTime: "", createTime: "",
tagNames: [], tagNames: [],
comments: [], comments: [],
}, },
}; };
@ -225,6 +312,131 @@ export default {
swiperChange(e) { swiperChange(e) {
this.swiperIndex = e.detail.current; this.swiperIndex = e.detail.current;
}, },
//
async loadReplies(commentId, isLoadMore = false, isNewComment = false) {
try {
uni.showLoading({ title: "加载中..." });
//
const index = this.noteDetail.comments.findIndex(
(item) => item.id === commentId
);
let comment = this.noteDetail.comments[index];
if (!comment) return;
// ""1
if (!isLoadMore) {
// 1
if (!comment.replyPageNum) {
this.$set(comment, "replyPageNum", 1);
} else {
comment.replyPageNum = 1;
}
if (!comment.replies) {
this.$set(comment, "replies", []);
} else if (!isNewComment) {
//
//
comment.replies = [];
}
// hasMoreRepliestrue
this.$set(comment, "hasMoreReplies", true);
}
//
if (isLoadMore && !comment.hasMoreReplies) return;
const res = await this.Post(
{
parentId: commentId,
noteId: this.noteId,
pageNum: comment.replyPageNum,
pageSize: 5, // 5
},
"/framework/comment/pageList",
"DES"
);
if (res.code === 200) {
const newReplies = res.rows || [];
//
if (isLoadMore) {
comment.replies = [...comment.replies, ...newReplies];
} else {
// 使
// 1
comment.replies = newReplies;
}
//
const newCommentCount = res.total || 0;
comment.commentCount = newCommentCount;
//
comment.hasMoreReplies =
comment.replies.length < comment.commentCount;
// 1
if (comment.hasMoreReplies) {
comment.replyPageNum++;
}
this.$forceUpdate();
//
if (isNewComment) {
//
const mainComment = this.noteDetail.comments.find(
(item) => item.id === commentId
);
if (mainComment) {
mainComment.commentCount = newCommentCount;
}
}
} else {
uni.showToast({
title: res.msg || "加载回复失败",
icon: "none",
});
}
} catch (error) {
console.error("加载二级评论失败:", error);
uni.showToast({
title: "加载回复失败",
icon: "none",
});
} finally {
uni.hideLoading();
}
},
// /
toggleReplies(commentId) {
const comment = this.noteDetail.comments.find(
(item) => item.id === commentId
);
if (!comment) return;
//
if (this.showRepliesMap[commentId] === undefined) {
this.$set(this.showRepliesMap, commentId, true);
//
this.loadReplies(commentId);
} else if (this.showRepliesMap[commentId]) {
//
this.$set(this.showRepliesMap, commentId, false);
} else {
//
this.$set(this.showRepliesMap, commentId, true);
//
if (!comment.replies || comment.replies.length === 0) {
this.loadReplies(commentId);
}
}
},
// //
async loadNoteDetail() { async loadNoteDetail() {
try { try {
@ -285,6 +497,11 @@ export default {
); );
if (res.code === 200) { if (res.code === 200) {
if (res.rows) {
res.rows.forEach((comment) => {
this.$set(comment, "replies", []);
});
}
// //
if (isLoadMore) { if (isLoadMore) {
// //
@ -296,10 +513,8 @@ export default {
// //
this.noteDetail.comments = res.rows || []; this.noteDetail.comments = res.rows || [];
} }
// //
this.noteDetail.commentCount = res.total || 0; this.noteDetail.commentCount = res.total || 0;
// //
this.hasMoreComments = this.noteDetail.comments.length < res.total; this.hasMoreComments = this.noteDetail.comments.length < res.total;
@ -307,6 +522,7 @@ export default {
if (this.hasMoreComments) { if (this.hasMoreComments) {
this.pageNum++; this.pageNum++;
} }
console.log(this.noteDetail.comments);
} else { } else {
console.error("加载评论列表失败:", res.msg); console.error("加载评论列表失败:", res.msg);
uni.showToast({ uni.showToast({
@ -446,12 +662,22 @@ export default {
try { try {
uni.showLoading({ title: "提交中..." }); uni.showLoading({ title: "提交中..." });
const params = {
noteId: this.noteId,
content: this.commentText,
method: "POST",
};
//
if (this.parentId) {
params.parentId = this.parentId;
params.toUserId = this.toUserId;
params.toHeadImg = this.toHeadImg;
params.toNickname = this.toNickname;
}
const res = await this.Post( const res = await this.Post(
{ params,
noteId: this.noteId,
content: this.commentText,
method: "POST",
},
"/framework/comment/addComment", "/framework/comment/addComment",
"DES" "DES"
); );
@ -459,8 +685,27 @@ export default {
// //
this.commentText = ""; this.commentText = "";
// //
await this.loadCommentList(); const parentIdCopy = this.parentId;
this.parentId = null;
this.toUserId = null;
this.toHeadImg = "";
this.toNickname = "";
//
this.showTextarea = false;
//
if (parentIdCopy) {
// isNewComment=true
// isLoadMore=false
await this.loadReplies(parentIdCopy, false, true);
//
this.$set(this.showRepliesMap, parentIdCopy, true);
} else {
//
await this.loadCommentList();
}
uni.showToast({ uni.showToast({
title: "评论成功", title: "评论成功",
@ -516,6 +761,34 @@ export default {
// url: `/pages/tags/detail?tag=${encodeURIComponent(tag)}` // url: `/pages/tags/detail?tag=${encodeURIComponent(tag)}`
// }); // });
}, },
//
replyToComment(comment) {
// parentIdID
if (comment.parentId) {
// parentIdID
this.parentId = comment.parentId;
} else {
// 使ID
this.parentId = comment.id;
}
this.toUserId = comment.userId;
this.toHeadImg = comment.headImg;
this.toNickname = comment.nickname;
// uni-appfalsetrue
this.inputFocusState = false;
// DOM
this.$nextTick(() => {
console.log(this.$refs.inputComment);
//
setTimeout(() => {
this.inputFocusState = true;
}, 100);
});
},
}, },
}; };
</script> </script>
@ -659,41 +932,160 @@ export default {
} }
.comment-item { .comment-item {
display: flex;
margin-bottom: 32rpx; margin-bottom: 32rpx;
.comment-avatar { .main-comment {
width: 64rpx; display: flex;
height: 64rpx;
border-radius: 32rpx; .comment-avatar {
margin-right: 24rpx; width: 64rpx;
height: 64rpx;
border-radius: 32rpx;
margin-right: 24rpx;
}
.comment-content {
flex: 1;
.comment-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12rpx;
.comment-user {
font-size: 30rpx;
font-weight: bold;
color: #000000;
}
.comment-time {
font-size: 26rpx;
color: #999;
}
}
.comment-text {
font-size: 28rpx;
line-height: 1.5;
color: #000000;
margin-bottom: 12rpx;
}
.comment-footer {
display: flex;
justify-content: space-between;
align-items: center;
.comment-time {
font-size: 26rpx;
color: #999;
}
.reply-btn {
font-size: 26rpx;
color: #666;
padding: 6rpx 12rpx;
border-radius: 4rpx;
&:active {
background-color: #f0f0f0;
}
}
}
.toggle-replies {
margin-top: 16rpx;
text {
font-size: 28rpx;
color: #666;
padding: 6rpx 0;
}
}
}
} }
.load-more-replies {
margin-right: 20rpx;
.comment-content { text {
font-size: 28rpx;
color: #666;
padding: 6rpx 0;
}
}
}
}
.replies-container {
margin-top: 16rpx;
.reply-item {
display: flex;
margin-bottom: 20rpx;
.reply-avatar {
width: 56rpx;
height: 56rpx;
border-radius: 28rpx;
margin-right: 16rpx;
}
.reply-content {
flex: 1; flex: 1;
.comment-header { .reply-header {
display: flex; display: flex;
justify-content: space-between;
align-items: center; align-items: center;
margin-bottom: 12rpx; margin-bottom: 8rpx;
.comment-user { .reply-user {
font-size: 30rpx; font-size: 28rpx;
font-weight: bold; font-weight: bold;
color: #000000; color: #000000;
margin-right: 8rpx;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
max-width: 250rpx;
} }
.comment-time { .reply-to {
font-size: 26rpx; font-size: 26rpx;
color: #999; color: #666;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
max-width: 250rpx;
} }
} }
.comment-text { .reply-text {
font-size: 28rpx; font-size: 26rpx;
line-height: 1.5; line-height: 1.5;
color: #000000; color: #000000;
margin-bottom: 8rpx;
}
.reply-footer {
display: flex;
justify-content: space-between;
align-items: center;
.reply-time {
font-size: 24rpx;
color: #999;
}
.reply-btn {
font-size: 26rpx;
color: #666;
padding: 6rpx 12rpx;
border-radius: 4rpx;
&:active {
background-color: #f0f0f0;
}
}
} }
} }
} }
@ -726,6 +1118,13 @@ export default {
margin-bottom: max(env(safe-area-inset-bottom), 24rpx); margin-bottom: max(env(safe-area-inset-bottom), 24rpx);
} }
.cancel-reply {
font-size: 28rpx;
color: #666;
margin-right: 16rpx;
margin-bottom: max(env(safe-area-inset-bottom), 24rpx);
}
.send-btn { .send-btn {
width: 140rpx; width: 140rpx;
height: 67rpx; height: 67rpx;

Loading…
Cancel
Save