瑶都万能墙
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
6.3 KiB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
  1. <template>
  2. <view class="commemt">
  3. <view class="comment-list">
  4. <commentItem v-for="(item, index) in list" :key="index" :parentId="item.id" :sourceType="params.type"
  5. :sourceId="params.orderId" :item="item" :subComments="getSubComments(item.id)"
  6. @loadSubComments="handleLoadSubComments" @reply="handleReply" />
  7. </view>
  8. <view class="submit-box">
  9. <view class="top">
  10. <button class="share" open-type="share">
  11. <uv-icon color="#00cf05" size="50rpx" name="weixin-fill"></uv-icon>
  12. </button>
  13. <input type="text" disabled @click="openCommentPublish"
  14. :placeholder="'评论给' + params.name || '他'" />
  15. </view>
  16. </view>
  17. <!-- 评论发布组件 -->
  18. <commentPublish ref="commentPublish" :params="params" :placeholder="'评论给' + params.name || '他'" @success="handleCommentSuccess" />
  19. </view>
  20. </template>
  21. <script>
  22. import commentItem from './commentItem.vue'
  23. import commentPublish from './commentPublish.vue'
  24. export default {
  25. components: {
  26. commentItem,
  27. commentPublish,
  28. },
  29. props: ['list', 'params'],
  30. data() {
  31. return {
  32. currentReplyComment: null, // 当前回复的评论
  33. subCommentsMap: {} // 存储子评论数据的映射表,key为父评论ID,value为子评论数组
  34. }
  35. },
  36. methods: {
  37. // 获取指定父评论的子评论列表
  38. getSubComments(parentId) {
  39. return this.subCommentsMap[parentId] || [];
  40. },
  41. // 打开评论发布弹窗
  42. openCommentPublish() {
  43. this.currentReplyComment = null
  44. this.$refs.commentPublish.open()
  45. },
  46. // 评论发布成功回调
  47. handleCommentSuccess(successData) {
  48. console.log('handleCommentSuccess 接收到的数据:', successData);
  49. // 如果是回复评论,需要重新加载对应的子评论列表
  50. if (successData && successData.isReply && successData.parentId) {
  51. console.log('准备重新加载子评论,parentId:', successData.parentId);
  52. // 重新加载子评论数据
  53. this.loadSubCommentsData(successData.parentId);
  54. } else {
  55. console.log('不是回复评论或缺少parentId,successData:', successData);
  56. }
  57. // 触发父组件刷新主评论列表
  58. this.$emit('getData')
  59. },
  60. // 处理加载子评论
  61. handleLoadSubComments(comment) {
  62. // 模拟加载子评论数据的API调用
  63. this.loadSubCommentsData(comment.id);
  64. },
  65. // 加载子评论数据
  66. async loadSubCommentsData(parentId) {
  67. try {
  68. // 显示加载提示
  69. uni.showLoading({
  70. title: '加载中...'
  71. });
  72. const apiParams = {
  73. pid: parentId,
  74. type: this.params.type,
  75. orderId: this.params.orderId,
  76. page: 1,
  77. pageSize: 99999999
  78. };
  79. // 使用实际的API获取子评论数据
  80. this.$api('getCommentPage', apiParams, res => {
  81. uni.hideLoading();
  82. if (res.code === 200) {
  83. // 将子评论数据存储到映射表中
  84. const subComments = res.result.records || [];
  85. this.$set(this.subCommentsMap, parentId, subComments);
  86. }
  87. });
  88. } catch (error) {
  89. uni.hideLoading();
  90. }
  91. },
  92. // 处理回复评论
  93. handleReply({item, level}) {
  94. let comment = item
  95. this.currentReplyComment = comment
  96. // 判断是一级评论还是二级评论
  97. let replyParams
  98. let parentCommentId // 用于标识真正的父评论ID(一级评论ID)
  99. if (comment.pid && comment.pid !== 0) {
  100. // 这是二级评论,回复二级评论时仍然使用原始一级评论的ID作为pid
  101. parentCommentId = comment.pid // 二级评论的pid就是一级评论的ID
  102. replyParams = {
  103. ...this.params,
  104. pid: comment.pid, // 使用原始一级评论的ID
  105. parentCommentId: parentCommentId // 添加父评论ID用于更新子评论列表
  106. }
  107. if (level == 0) {
  108. }else{
  109. replyParams.replyToId = comment.replyToId
  110. replyParams.replyToName = comment.userName
  111. replyParams.replyToAvatar = comment.userHead
  112. }
  113. } else {
  114. // 这是一级评论,回复一级评论时使用该评论的ID作为pid
  115. parentCommentId = comment.id // 一级评论的ID就是父评论ID
  116. replyParams = {
  117. ...this.params,
  118. pid: comment.id, // 使用一级评论的ID作为pid
  119. replyToId: comment.id,
  120. replyToName: comment.userName,
  121. replyToAvatar: comment.userHead,
  122. parentCommentId: parentCommentId // 添加父评论ID用于更新子评论列表
  123. }
  124. }
  125. this.$refs.commentPublish.setReplyParams(replyParams)
  126. this.$refs.commentPublish.open()
  127. }
  128. }
  129. }
  130. </script>
  131. <style scoped lang="scss">
  132. .commemt {
  133. padding-bottom: env(safe-area-inset-bottom);
  134. }
  135. .comment-list {
  136. margin-top: 20rpx;
  137. padding-bottom: 150rpx;
  138. }
  139. .submit-box {
  140. position: fixed;
  141. bottom: 0;
  142. left: 0;
  143. background-color: #fff;
  144. width: 100%;
  145. box-shadow: 0 0 6rpx 6rpx #00000011;
  146. padding-bottom: env(safe-area-inset-bottom);
  147. .top {
  148. align-items: center;
  149. display: flex;
  150. justify-content: center;
  151. input {
  152. background-color: #f3f3f3;
  153. width: 460rpx;
  154. height: 40rpx;
  155. border-radius: 40rpx;
  156. margin: 20rpx;
  157. padding: 20rpx 30rpx;
  158. font-size: 28rpx;
  159. }
  160. }
  161. }
  162. </style>