gree_leran/components/CommentsBox/CommentsBox.vue

218 lines
4.7 KiB
Vue
Raw Normal View History

2024-06-22 07:20:39 +00:00
<template>
<view class="comment-container">
<view class="comment-main" @click="addComment(info)">
<up-avatar :src="info.imageUrl" size="30"></up-avatar>
<up-text class="username" :text="info.replyName"></up-text>
<up-text class="content" :text="info.commentContent" size="36rpx"></up-text>
</view>
<view v-if="isZkai" class="comment-children">
<view
class="comment-child"
v-for="(item, index) in props.data.children"
:key="item.id"
@click="addComment(item)"
>
<up-text class="child-username" :text="item.replyName + '>' + item.replyUserName"></up-text>
<up-text class="child-content" :text="item.commentContent" size="36rpx"></up-text>
</view>
<!-- 更多评论 -->
</view>
<view v-if="props.data.children.length > 0" class="toggle-button" @click="zhankai">
{{ isZkai ? "收起" : "展开" }}
</view>
</view>
</template>
<script setup>
import { onMounted, reactive, ref } from "vue";
import {getImageById }from "@/apis/user.js"
const props = defineProps({
data: {
type: Object,
default: () => ({
id: null,
commentId: null,
relatedPostId: null,
userId: null,
replyUserName: "名称",
replyUserId: null,
replyName: null,
commentContent: "内容",
type: null,
children: [
{
id: null,
relatedPostId: null,
userId: null,
replyUserName: "名称",
replyUserId: null,
replyName: "名称2",
commentContent: "内容",
type: null,
},
{
id: null,
relatedPostId: null,
userId: null,
replyUserName: "名称",
replyUserId: null,
replyName: "名称2",
commentContent: "内容",
type: null,
},
{
id: null,
relatedPostId: null,
userId: null,
replyUserName: "名称",
replyUserId: null,
replyName: "名称2",
commentContent: "内容",
type: null,
},
],
}),
},
});
const emit = defineEmits(['addComment']);
const addComment = (itme) => {
let data = {
//帖子id
relatedPostId: info.relatedPostId,
//
parentCommentId:info.id | null,
replyUseId:itme.userId|null
};
emit('addComment', data);
};
const isZkai = ref(false);
const zhankai = () => {
isZkai.value = !isZkai.value;
};
const info = reactive({
id: null,
commentId: null,
relatedPostId: null,
imageUrl:null,
userId: null,
replyUserName: "名称",
replyUserId: null,
replyName: null,
commentContent: "内容",
type: null,
children: [
{
id: null,
relatedPostId: null,
commentId: null,
userId: null,
replyUserName: "名称",
replyUserId: null,
replyName: "名称2",
commentContent: "内容",
type: null,
},
{
id: null,
relatedPostId: null,
userId: null,
replyUserName: "名称",
replyUserId: null,
replyName: "名称2",
commentContent: "内容",
type: null,
},
{
id: null,
relatedPostId: null,
userId: null,
replyUserName: "名称",
replyUserId: null,
replyName: "名称2",
commentContent: "内容",
type: null,
},
],
});
const getUserImage=async(id)=>{
let res=await getImageById(id);
if(res.code==200){
info.imageUrl=res.msg;
}
}
onMounted(() => {
console.log("评论数据",props.data);
//把props所有都拷贝过来
info.id = props.data.id;
info.commentId = props.data.commentId;
info.relatedPostId = props.data.relatedPostId;
info.userId = props.data.userId;
info.replyUserName = props.data.replyUserName;
info.replyUserId = props.data.replyUserId;
info.replyName = props.data.replyName;
info.commentContent = props.data.commentContent;
info.type = props.data.type;
info.children = props.data.children|[];
getUserImage(info.userId);
console.log("长度",info.children,props.data.children)
});
</script>
<style scoped>
.comment-container {
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
margin-bottom: 10px;
}
.comment-main {
display: flex;
flex-direction: column;
padding: 10px;
cursor: pointer;
background-color: #f9f9f9;
}
.username {
font-weight: bold;
margin-bottom: 5px;
}
.content {
margin-left: 10px;
color: #333;
}
.comment-children {
margin-left: 20px;
border-left: 2px solid #eee;
padding-left: 10px;
}
.comment-child {
margin-top: 10px;
cursor: pointer;
}
.child-username {
font-weight: bold;
color: #555;
}
.child-content {
margin-left: 10px;
color: #666;
}
.toggle-button {
margin-top: 10px;
color: #1e90ff;
cursor: pointer;
text-align: right;
}
</style>