Browse Source

评论

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

427
pages/notes/detail.vue

@ -89,23 +89,90 @@
>评论 ({{ noteDetail.commentCount || 0 }})</text
>
</view>
<view
class="comment-item"
v-for="comment in noteDetail.comments"
:key="comment.id"
>
<view class="main-comment" @click="replyToComment(comment)">
<image
class="comment-avatar"
:src="showImg(comment.headImg)"
:src="comment.headImg"
mode="aspectFill"
/>
<view class="comment-content">
<view class="comment-header">
<text class="comment-user">{{ comment.nickname }}</text>
<text class="comment-time">{{ comment.formatTime }}</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>
</view>
</view>
@ -129,17 +196,32 @@
<!-- 底部占位 -->
<view class="bottom-placeholder"></view>
</view>
<!-- 底部评论输入框 -->
<view class="comment-input-section-box">
<view class="comment-input-section">
<input
class="comment-input"
v-model="commentText"
placeholder="写下你的想法..."
:placeholder="toNickname ? `回复 @${toNickname}` : '写下你的想法...'"
ref="inputComment"
:focus="inputFocusState"
@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
class="send-btn"
@click="submitComment"
@ -192,6 +274,12 @@ export default {
pageSize: 10,
hasMoreComments: true,
loadingComments: false,
parentId: null, // ID
toUserId: null, // ID
toHeadImg: "", //
toNickname: "", //
inputFocusState: false, //
showRepliesMap: {}, //
noteDetail: {
id: "",
title: "",
@ -202,7 +290,6 @@ export default {
isLiked: false,
createTime: "",
tagNames: [],
comments: [],
},
};
@ -225,6 +312,131 @@ export default {
swiperChange(e) {
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() {
try {
@ -285,6 +497,11 @@ export default {
);
if (res.code === 200) {
if (res.rows) {
res.rows.forEach((comment) => {
this.$set(comment, "replies", []);
});
}
//
if (isLoadMore) {
//
@ -296,10 +513,8 @@ export default {
//
this.noteDetail.comments = res.rows || [];
}
//
this.noteDetail.commentCount = res.total || 0;
//
this.hasMoreComments = this.noteDetail.comments.length < res.total;
@ -307,6 +522,7 @@ export default {
if (this.hasMoreComments) {
this.pageNum++;
}
console.log(this.noteDetail.comments);
} else {
console.error("加载评论列表失败:", res.msg);
uni.showToast({
@ -446,12 +662,22 @@ export default {
try {
uni.showLoading({ title: "提交中..." });
const res = await this.Post(
{
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(
params,
"/framework/comment/addComment",
"DES"
);
@ -459,8 +685,27 @@ export default {
//
this.commentText = "";
//
//
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({
title: "评论成功",
@ -516,6 +761,34 @@ export default {
// 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>
@ -659,9 +932,11 @@ export default {
}
.comment-item {
display: flex;
margin-bottom: 32rpx;
.main-comment {
display: flex;
.comment-avatar {
width: 64rpx;
height: 64rpx;
@ -694,6 +969,123 @@ export default {
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;
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;
.reply-header {
display: flex;
align-items: center;
margin-bottom: 8rpx;
.reply-user {
font-size: 28rpx;
font-weight: bold;
color: #000000;
margin-right: 8rpx;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
max-width: 250rpx;
}
.reply-to {
font-size: 26rpx;
color: #666;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
max-width: 250rpx;
}
}
.reply-text {
font-size: 26rpx;
line-height: 1.5;
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);
}
.cancel-reply {
font-size: 28rpx;
color: #666;
margin-right: 16rpx;
margin-bottom: max(env(safe-area-inset-bottom), 24rpx);
}
.send-btn {
width: 140rpx;
height: 67rpx;

Loading…
Cancel
Save