瑶都万能墙
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.

191 lines
6.6 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year 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. // 检查用户是否已登录
  44. if (!this.userInfo || !this.userInfo.id) {
  45. console.log('用户未登录,跳转到登录页面')
  46. // 使用utils中的toLogin方法跳转到登录页面
  47. this.$utils.toLogin()
  48. return
  49. }
  50. this.currentReplyComment = null
  51. this.$refs.commentPublish.open()
  52. },
  53. // 评论发布成功回调
  54. handleCommentSuccess(successData) {
  55. console.log('handleCommentSuccess 接收到的数据:', successData);
  56. // 如果是回复评论,需要重新加载对应的子评论列表
  57. if (successData && successData.isReply && successData.parentId) {
  58. console.log('准备重新加载子评论,parentId:', successData.parentId);
  59. // 重新加载子评论数据
  60. this.loadSubCommentsData(successData.parentId);
  61. } else {
  62. console.log('不是回复评论或缺少parentId,successData:', successData);
  63. }
  64. // 触发父组件刷新主评论列表
  65. this.$emit('getData')
  66. },
  67. // 处理加载子评论
  68. handleLoadSubComments(comment) {
  69. // 模拟加载子评论数据的API调用
  70. this.loadSubCommentsData(comment.id);
  71. },
  72. // 加载子评论数据
  73. async loadSubCommentsData(parentId) {
  74. try {
  75. // 显示加载提示
  76. uni.showLoading({
  77. title: '加载中...'
  78. });
  79. const apiParams = {
  80. pid: parentId,
  81. type: this.params.type,
  82. orderId: this.params.orderId,
  83. page: 1,
  84. pageSize: 99999999
  85. };
  86. // 使用实际的API获取子评论数据
  87. this.$api('getCommentPage', apiParams, res => {
  88. uni.hideLoading();
  89. if (res.code === 200) {
  90. // 将子评论数据存储到映射表中
  91. const subComments = res.result.records || [];
  92. this.$set(this.subCommentsMap, parentId, subComments);
  93. }
  94. });
  95. } catch (error) {
  96. uni.hideLoading();
  97. }
  98. },
  99. // 处理回复评论
  100. handleReply({item, level}) {
  101. let comment = item
  102. this.currentReplyComment = comment
  103. // 判断是一级评论还是二级评论
  104. let replyParams
  105. let parentCommentId // 用于标识真正的父评论ID(一级评论ID)
  106. if (comment.pid && comment.pid !== 0) {
  107. // 这是二级评论,回复二级评论时仍然使用原始一级评论的ID作为pid
  108. parentCommentId = comment.pid // 二级评论的pid就是一级评论的ID
  109. replyParams = {
  110. ...this.params,
  111. pid: comment.pid, // 使用原始一级评论的ID
  112. parentCommentId: parentCommentId // 添加父评论ID用于更新子评论列表
  113. }
  114. if (level == 0) {
  115. }else{
  116. replyParams.replyToId = comment.replyToId
  117. replyParams.replyToName = comment.userName
  118. replyParams.replyToAvatar = comment.userHead
  119. }
  120. } else {
  121. // 这是一级评论,回复一级评论时使用该评论的ID作为pid
  122. parentCommentId = comment.id // 一级评论的ID就是父评论ID
  123. replyParams = {
  124. ...this.params,
  125. pid: comment.id, // 使用一级评论的ID作为pid
  126. replyToId: comment.id,
  127. replyToName: comment.userName,
  128. replyToAvatar: comment.userHead,
  129. parentCommentId: parentCommentId // 添加父评论ID用于更新子评论列表
  130. }
  131. }
  132. this.$refs.commentPublish.setReplyParams(replyParams)
  133. this.$refs.commentPublish.open()
  134. }
  135. }
  136. }
  137. </script>
  138. <style scoped lang="scss">
  139. .commemt {
  140. padding-bottom: env(safe-area-inset-bottom);
  141. }
  142. .comment-list {
  143. margin-top: 20rpx;
  144. padding-bottom: 150rpx;
  145. }
  146. .submit-box {
  147. position: fixed;
  148. bottom: 0;
  149. left: 0;
  150. background-color: #fff;
  151. width: 100%;
  152. box-shadow: 0 0 6rpx 6rpx #00000011;
  153. padding-bottom: env(safe-area-inset-bottom);
  154. .top {
  155. align-items: center;
  156. display: flex;
  157. justify-content: center;
  158. input {
  159. background-color: #f3f3f3;
  160. width: 460rpx;
  161. height: 40rpx;
  162. border-radius: 40rpx;
  163. margin: 20rpx;
  164. padding: 20rpx 30rpx;
  165. font-size: 28rpx;
  166. }
  167. }
  168. }
  169. </style>