当前位置:   article > 正文

uniapp评论列表插件获取_hb-comment

hb-comment

评论列表,回复,点赞,删除,留言板 - DCloud 插件市场导入,并使用。

代码样式优化及接入如下:

  1. <template>
  2. <view class="hb-comment">
  3. <!-- 阅读数-start -->
  4. <view v-if="false">
  5. <img style="width: 14px; height: 14px;"
  6. src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAA0tJREFUWEftVk2IHUUQ/qp311NOksRT8JBDkARCfrxEDyLK+pe/DQkmJpBlZ7ofDwQ15iRocjYSSWAzU/OWd4hCMJiQH43xBz2YXCRivIgHQQ8SRPGUU/btfNLPnuU5O7OZlWz2kobh8aq/qvq6qrq6BEu8ZIn94wGBBUWg3W6v6vV62wFsILlGRNYA6AH4AcCPJH8mebPT6dxomtpGBJxz4wD2kny2oWHN81ybEJmXQKvVWpfn+VsAXi45vgPgj/D5rUfC91AJ54m83el0PLZy1RKw1u4GkAB4OGj+LSJXRORMkiSXq6xFUfSUiHi93SKyImB+BeBU9fMqnUoCcRy/IiIfFAoicp7kYVX9pUkKrLWrReRdkjsLvNfPsuxYWX8OAWvtOwCODACPqOrRJo7LmApbe1T17CDuPwSccy2SpwZYj2VZdr7K+fj4+IqRkZFVIjJD8ndV/asKdzcSswTiON4iIlcBLPOGROTFNE0/rTHqQ3motHdWVffU1YYx5utizxjzXJIk3te/nbDdbi/r9XpesCWAasNureV86VDVurqKRUSD7k8zMzPPT01N/dYHW2uPA3gtnDxL09TWnPwjX+F+j+Q1EfkCwC0ALwHYGnTeU9U3q/Sdc0oyDnvnVHWXOOdeIPlJEN4E8ExVPq21ywH8GXCXVHXboJM4jr8VkSe8bHp6emW32y2ws7Bg40sA68NhD3gCXZIHA2q07r4659aT9C3XL3+vi3D2BaVi26Sq31dFIY7jgyLSDXs3xFp7rci9MebJJEn8/zlrMQj00+ic20/ydAjJhTRNd9Tk/56ngOT2ogg/BjAWHB9X1TfuQxG+r6qv9wlMTEw8OjQ0dAXAY8HxYl/D68PDw6OTk5O3Z+9sq9UazfP8s+Lki9iIbpMczbLsej/tg6F2zr1K8kQhI3n/WnHhdDEfI5L7syz7cPDQlW2zHIl78RwDqCzu2oEkiqKnjTFfDbD9vwOJb9vHsiw7XHWz5h3JoijaZIzxz/PjJeVGIxnJb4wxJ9M0PVflfE4R1oGcc2Mk9wHYVYcpyS8BuKiqnbvhG03FhRHn3GaSG0muM8as9b8AjB/HRcR/35G8oKr+hWy0FkSgkcUFgh4QWPII/ANy0cUvD9WbkgAAAABJRU5ErkJggg==" />
  7. <span class="top-read">{{commentData.readNumer}}</span>
  8. </view>
  9. <!-- 阅读数-end -->
  10. <!-- 阅读数下边那条线-start -->
  11. <view class="seg_line_box">
  12. <view class="seg_line"></view>
  13. <view class="seg_dot"></view>
  14. <view class="seg_line"></view>
  15. </view>
  16. <!-- 阅读数下边那条线-end -->
  17. <!-- 评论主体-start -->
  18. <view class="comment-list" v-if="commentData.comment.length != 0">
  19. <!-- 评论主体-顶部数量及发表评论按钮-start -->
  20. <view class="comment-num">
  21. <view>共 {{commentData.commentSize}} 条评论</view>
  22. <view class="add-btn">
  23. <button class="publishcomment" type="primary" size="mini" @click="commentInput(true)">发表评论</button>
  24. </view>
  25. </view>
  26. <!-- 评论主体-顶部数量及发表评论按钮-end -->
  27. <!-- 评论列表-start -->
  28. <view class="comment-box" v-for="(item, index) in commentData.comment">
  29. <view class="comment-box-item">
  30. <view>
  31. <image :src="baseUrl+item.avatar || emptyAvatar" mode="aspectFill" class="avatar"></image>
  32. </view>
  33. <view class="comment-main">
  34. <!-- 父评论体-start -->
  35. <view class="comment-main-top">
  36. <view class="nick-name-box">
  37. <view class="comLogo com1" v-if="index == 0">沙发</view>
  38. <view class="comLogo com2" v-if="index == 1">板凳</view>
  39. <view class="comLogo com3" v-if="index == 2">地板</view>
  40. <view class="comLogo com4" v-if="index > 2">{{index + 1}}楼</view>
  41. <view class="nick-name">{{item.nickName}}</view>
  42. </view>
  43. <view v-if="false" class="zan-box" @click="like(item.id)">
  44. <span :class="item.hasLike ? 'isLike' : 'notLike'">{{item.likeNum == 0 ? '抢首赞' : item.likeNum}}</span>
  45. <img style="width: 14px; height: 14px;" v-if="!item.hasLike"
  46. src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAApVJREFUWEfFlz9oFEEUxr93SWEhFkIaBUljo4WgATEaiI0GwVLBQg2EzLd3hSBqLI2lRgWLY+ftSSQgggoWNv5pImLUwjQBg1goqCCIIBqwueSeTLg7Ln9Mcmdub7vdnZnvN9+8fe+toMWXtFgfDQGMjo52zM7OHgMwIyLTg4ODk41upG4A7/0OEbkPYGdFVEQuOOeuNQJRN4CqBvGw+3dm9kZEBsrCPSRf1gvRCIAFETPrjqLodZIkY2Z2SkQmnHMHUgMgOQ8fx3FvJpMZB/CDZEfqACEgi8XidwA/SW5uKkCN2AzJTYscmCTZ1VQA7/2AiNwSkefOuYNBLEmSnJnlATwgebypAKr6CMBRANdJng9i3vu8iORE5Mrc3NyTlQDM7HMul/tYO2bNX0GhUDhRKpXumtlvAPuiKJouOzBuZr117HyBU6sCqOpeAH0AhssiwyQvVwTjOO5va2s7vRaAGtCrJC+GOVUAVb1UTjDVDLd4URHJOuf8WsSWG5MkSbeZTYR3lc+4CpAkyUpWht3fI/m+UfEwL5/Pb2xvb59ZFkBV5zNchazsSBBeYPn/ABQKhZ5SqfQCwBTJXYuPoOkAqnoGwE0Ad0ieTB3Ae39bRPoBDJEcSR1AVd8C2CMifc65p6kCmJkkSfIHwAYAW0h+SxVAVXcDmDSzL1EUbasEc20eaGoQVuoIgMckj6QOoKo3AJwFMEJyqBUAIegOAegnOdYKgK8AtmYyma7aLjqVGPDebxeRDwB+AeggWUzVAVV1ABTAK5L7l+0HVPUTgM7Q7VQGhPJZe99oHagpw4dJPvsXQPixONeoyErzRGTKzB7W9hFLjiA8iOO4M7iw3hDZbLbq6pIeY73F6l1v1Zas3gXrHf8XhhNvMGSmtPYAAAAASUVORK5CYII=" />
  47. <img style="width: 14px; height: 14px;" v-else
  48. src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAfVJREFUWEfFl79LHEEUx7/vclr4g51TLFQQW9FNDFhFBDvjtcF0gXSp5eYuWJlUAW9NIP9BIFUEOxFtTKF2FrezKFgkkEBS3h4SAtHbJ6vu5W5d1F3cm20WZt+b7+e9mTfzlqD5Ic36SATw+OPJQP2ft+ABJwQ+VFIcJA0kEYBpVfcAehKIMqHoFISVBCI2wOS76mi9g76HxTLwZiqybzcuRGwAc7WWB/NGWIhBu440ZlIHmFitLRPzm+tC9EtJY1gjAP9VMtelEQC2kuJR6gCm5X4BsBAWImDdluJZqgBjH/4MZr3TYzB6IoRWmHnzJgDK0g+1KL4128SqgnD9x432yn5NSfE88L0VYPx9rS/jYR6EJTCPJxQNu60oKV77gw2Ai/Ly1/ZKRElx8c20XL4n0ZZpgvkbAKbl7gCYDax0ALREmjJAo2SbM9A+AKLPqmC8aNkD4bVOMwPEKNlFUdYGkAE9rUhjSxvA2YOOoaPF7t+6AH4qKUauHURt3AObSoq8NgAGyo4UJW0ABH5py9wnbQAAppq76HYfRLXOXmPg4BWdasoA7yuZm47sB0zL9Vvt0TQvI/bqc06pfzsS4KHlWgwUUgKwmWjdKRhvI1q5/0P+T8dZ9jILTjH31X9PlKuNKzppXxDMFeV/a0eUVPSuftoBzgHKR/ohZwAugwAAAABJRU5ErkJggg==" />
  49. </view>
  50. </view>
  51. <view class="comment-main-content">
  52. {{item.content.length > 60 ? item.content.slice(0, 59) : item.content}}
  53. <span v-if="item.content.length > 60">
  54. {{item.hasShowMore ? item.content.slice(59) : '...'}}
  55. <span class="foot-btn" @click="showMore(item.id)">
  56. {{item.hasShowMore ? '收起' : '展开'}}
  57. </span>
  58. </span>
  59. </view>
  60. <view class="comment-main-foot">
  61. <view class="foot-time">{{item.createTime}}</view>
  62. <view class="foot-btn" @click="reply(item.nickName,item.nickName,item.id)">回复</view>
  63. <view class="foot-btn" v-if="item.owner" @click="confirmDelete(item.id)">删除</view>
  64. </view>
  65. <!-- 父评论体-end -->
  66. <!-- 子评论列表-start -->
  67. <view class="comment-sub-box">
  68. <view class="comment-sub-item" v-for="each in item.children">
  69. <view>
  70. <image :src="baseUrl+each.avatar || emptyAvatar" mode="aspectFill" class="avatar">
  71. </image>
  72. </view>
  73. <view class="comment-main">
  74. <view class="sub-comment-main-top">
  75. <view class="nick-name">{{each.nickName}}</view>
  76. <view v-if="false" class="zan-box" @click="like(each.id)">
  77. <span :class="each.hasLike ? 'isLike' : 'notLike'">{{each.likeNum == 0 ? '抢首赞' : each.likeNum}}</span>
  78. <img style="width: 14px; height: 14px;" v-if="!each.hasLike"
  79. src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAApVJREFUWEfFlz9oFEEUxr93SWEhFkIaBUljo4WgATEaiI0GwVLBQg2EzLd3hSBqLI2lRgWLY+ftSSQgggoWNv5pImLUwjQBg1goqCCIIBqwueSeTLg7Ln9Mcmdub7vdnZnvN9+8fe+toMWXtFgfDQGMjo52zM7OHgMwIyLTg4ODk41upG4A7/0OEbkPYGdFVEQuOOeuNQJRN4CqBvGw+3dm9kZEBsrCPSRf1gvRCIAFETPrjqLodZIkY2Z2SkQmnHMHUgMgOQ8fx3FvJpMZB/CDZEfqACEgi8XidwA/SW5uKkCN2AzJTYscmCTZ1VQA7/2AiNwSkefOuYNBLEmSnJnlATwgebypAKr6CMBRANdJng9i3vu8iORE5Mrc3NyTlQDM7HMul/tYO2bNX0GhUDhRKpXumtlvAPuiKJouOzBuZr117HyBU6sCqOpeAH0AhssiwyQvVwTjOO5va2s7vRaAGtCrJC+GOVUAVb1UTjDVDLd4URHJOuf8WsSWG5MkSbeZTYR3lc+4CpAkyUpWht3fI/m+UfEwL5/Pb2xvb59ZFkBV5zNchazsSBBeYPn/ABQKhZ5SqfQCwBTJXYuPoOkAqnoGwE0Ad0ieTB3Ae39bRPoBDJEcSR1AVd8C2CMifc65p6kCmJkkSfIHwAYAW0h+SxVAVXcDmDSzL1EUbasEc20eaGoQVuoIgMckj6QOoKo3AJwFMEJyqBUAIegOAegnOdYKgK8AtmYyma7aLjqVGPDebxeRDwB+AeggWUzVAVV1ABTAK5L7l+0HVPUTgM7Q7VQGhPJZe99oHagpw4dJPvsXQPixONeoyErzRGTKzB7W9hFLjiA8iOO4M7iw3hDZbLbq6pIeY73F6l1v1Zas3gXrHf8XhhNvMGSmtPYAAAAASUVORK5CYII=" />
  80. <img style="width: 14px; height: 14px;" v-else
  81. src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAfVJREFUWEfFl79LHEEUx7/vclr4g51TLFQQW9FNDFhFBDvjtcF0gXSp5eYuWJlUAW9NIP9BIFUEOxFtTKF2FrezKFgkkEBS3h4SAtHbJ6vu5W5d1F3cm20WZt+b7+e9mTfzlqD5Ic36SATw+OPJQP2ft+ABJwQ+VFIcJA0kEYBpVfcAehKIMqHoFISVBCI2wOS76mi9g76HxTLwZiqybzcuRGwAc7WWB/NGWIhBu440ZlIHmFitLRPzm+tC9EtJY1gjAP9VMtelEQC2kuJR6gCm5X4BsBAWImDdluJZqgBjH/4MZr3TYzB6IoRWmHnzJgDK0g+1KL4128SqgnD9x432yn5NSfE88L0VYPx9rS/jYR6EJTCPJxQNu60oKV77gw2Ai/Ly1/ZKRElx8c20XL4n0ZZpgvkbAKbl7gCYDax0ALREmjJAo2SbM9A+AKLPqmC8aNkD4bVOMwPEKNlFUdYGkAE9rUhjSxvA2YOOoaPF7t+6AH4qKUauHURt3AObSoq8NgAGyo4UJW0ABH5py9wnbQAAppq76HYfRLXOXmPg4BWdasoA7yuZm47sB0zL9Vvt0TQvI/bqc06pfzsS4KHlWgwUUgKwmWjdKRhvI1q5/0P+T8dZ9jILTjH31X9PlKuNKzppXxDMFeV/a0eUVPSuftoBzgHKR/ohZwAugwAAAABJRU5ErkJggg==" />
  82. </view>
  83. </view>
  84. <view class="comment-main-content">
  85. {{each.content.length > 60 ? each.content.slice(0, 59) : each.content}}
  86. <span v-if="each.content.length > 60">
  87. {{each.hasShowMore ? each.content.slice(59) : '...'}}
  88. <span class="foot-btn" @click="showMore(each.id)">
  89. {{each.hasShowMore ? '收起' : '展开'}}
  90. </span>
  91. </span>
  92. </view>
  93. <view class="comment-main-foot">
  94. <view class="foot-time">{{each.createTime}}</view>
  95. <view class="foot-btn" @click="reply(item.nickName,each.nickName,item.id)">
  96. 回复</view>
  97. <view class="foot-btn" v-if="each.owner" @click="confirmDelete(each.id)">删除
  98. </view>
  99. </view>
  100. </view>
  101. </view>
  102. </view>
  103. <!-- 子评论列表-end -->
  104. </view>
  105. </view>
  106. </view>
  107. <!-- 评论列表-end -->
  108. </view>
  109. <!-- 评论主体-end -->
  110. <!-- 无评论-start -->
  111. <view class="comment-none" v-else>
  112. 暂无评论,<span @click="commentInput(true)" style="color: #007AFF;">抢沙发</span>
  113. </view>
  114. <!-- 无评论-end -->
  115. <!-- 新增评论-start -->
  116. <view class="comment-submit-box" v-if="submit" @click="closeInput">
  117. <!-- 下边的click.stop.prevent用于让上边的click不传下去,以防点到下边的空白处触发closeInput方法 -->
  118. <view class="comment-add" @click.stop.prevent="stopPrevent" :style="'bottom:' + KeyboardHeight + 'px'">
  119. <view class="comment-submit">
  120. <view class="btn-click cancel" @click="closeInput">取消</view>
  121. <view>
  122. <view class="replayTag" v-show="showTag">
  123. <view v-if="pUser">回复在 {{pUser}} 的评论下</view>
  124. <view v-else>发表评论</view>
  125. <view @click="tagClose" v-if="false" class="replyTagClose">×</view>
  126. </view>
  127. </view>
  128. <view>
  129. <view class="btn-click" @click="add">发布</view>
  130. </view>
  131. </view>
  132. <textarea class="textarea" v-model="commentReq.content" :placeholder="placeholder" :adjust-position="false" :show-confirm-bar="false"
  133. @blur="blur" @focus="focusOn" :focus="focus" maxlength="800"></textarea>
  134. </view>
  135. </view>
  136. <!-- 新增评论-end -->
  137. </view>
  138. </template>
  139. <script>
  140. import config from '@/config'
  141. export default {
  142. name: 'hb-comment',
  143. props: {
  144. cmData: {
  145. type: Object,
  146. default: () => {
  147. return null;
  148. }
  149. },
  150. deleteTip: {
  151. type: String,
  152. default: () => {
  153. return '操作不可逆,如果评论下有子评论,也将被一并删除,确认?';
  154. }
  155. },
  156. },
  157. watch: {
  158. cmData: {
  159. handler: function(newVal, oldVal) {
  160. this.init(newVal);
  161. },
  162. immediate: true
  163. }
  164. },
  165. data() {
  166. return {
  167. "emptyAvatar": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAABF5JREFUWEfFl11oHFUUx//nbmKwxdJ9qFL7YPEhRJC2gljjF60WG0WsoNkWfSnZ7Jy7FVPF0AoKpmBFqyIG3Jk7G1iIQXHbBz+KbbGtRfBbMe2LseKjiVDoiqIhwZ0jV3fb3cnM7kQCuU+7M+ec/+9+nHPPEJZ50DLrY1EApVJp9fz8/BYRuZ2INgDYWJvAWRE5R0RnZmZmPh4ZGZlPOrFEAMVi8e4gCPYSUZ+IXGGFgiCYIaJpKyQi1yql1orIFgAXARxRSvm5XO67diBtAYwxRQCDAE4RUUkpdWxwcNCKLBiu665TSj0kIpqIbgTgMzO3gmgJYIz5CMB9AIaZ+bXGQMVi8RoRuZeI/lZKHc1ms3/U34+Pj6+cnZ3dC+AggGPMfH8cRCyAMUZqThlmPlwPUCgU0qlUyq7Kww1BrbjHzPsahYwx/QDK9hkzR2pFPjTGnAWwIexkjOkRkRIR3Rozo5Miskdr/VMIxE7mHDPXD+2l1wsA6nseBMHmfD7/dSjQOwB2tTlYC/bddd1blFJfARhj5lyjfxNA7bSfitpz3/d3iYgFaDuUUjeHM8AY8zSAV5VS9+RyudP1IE0Axpj3Aaxk5m1hFc/zPiWiO9uq/2dwiJn3h22NMScB/MnMOxYAjI6Orurq6rpgU0hrXYpw/hFAd0KAD5n5wbCt7/t7ROT1ubm5NUNDQ7/b95dWwHXd7Uqp452dnVcPDAxciACwDlclAbAFynGcdRGr2EtEnwdB0JfP5080ARhjDhLRbY7jbI0SMcZUAKxOAgDgN2ZOx8SxGfEiMz/bBOD7/lgQBCu01o/GOH4PYFNCgElmvinK1vf9X2xxchzHVtfLW2CMOQpgipmHYwBeBtBUaOJgRKSgtX48Js63AH5l5geaADzPe1cpddFxnHyM42YAXyZYAXt+epn557iVFJHzWuudYYDnlFJ9juPcESfi+35JRHa3gdjPzIfibIwxNtPe0Fq/EAZ4hIhcZl4T5+y67nql1CcA1kfZENFnrSZQ6ycqItKvtT4SBthIRJMdHR092WzW5nzk8H1/WEReiQHY4TjOB3G+nuf9qyEim7TW9r65fAjL5fKVlUrlPICXmPnNFquwWym1oFBZ+yAItubz+TMtAJ4gon3pdLo7k8nMNgHYP7ZeE5EWkbuYeaYx0NjY2HXValUDeApAV4zIJIADzPxe+H2hULg+lUp9U6sBl3qLprugXC6nKpXKF0R02nGcZ2wQ3/e3ichOEckQ0aoEWQDbsgF4a3p6eqLeH3qeN0FE3el0ujeTyVTrcaKuY1uIjIg8CaCfiLYnEW1hY4WPi8gEgMeY+e1G27iGxHYxtptZynGYmTPhgK1asqWEiBRfcAjDdMaYpYCIFW8LUMuM54nIsb3/YvbDXskiYtuzA6382n4X1CDWAnCSgNSFa98ETakcWbwWMytjzAoAPUEQ3JBKpXrs75r/VLVanVJK/VC7Uf9KGjfRCiQN9n/slh3gHz9i4jC+FVL5AAAAAElFTkSuQmCC",
  168. "commentData": null,
  169. "placeholder": "请输入评论",
  170. "commentReq": {
  171. "pId": null, // 评论父id
  172. "content": null // 评论内容
  173. },
  174. "pUser": null, // 标签-回复人
  175. "showTag": false, // 标签展示与否
  176. "focus": false, // 输入框自动聚焦
  177. "submit": false, // 弹出评论
  178. "KeyboardHeight": 0 ,// 键盘高度
  179. baseUrl:config.baseUrl,
  180. };
  181. },
  182. mounted: function() {
  183. uni.onKeyboardHeightChange(res => {
  184. this.KeyboardHeight = res.height;
  185. })
  186. },
  187. methods: {
  188. // 初始化评论
  189. init(cmData) {
  190. // for (var i in cmData.comment) {
  191. // cmData.comment[i].hasShowMore = false;
  192. // for (var j in cmData.comment[i].children) {
  193. // cmData.comment[i].children[j].hasShowMore = false;
  194. // }
  195. // }
  196. this.commentData = cmData;
  197. },
  198. // 没用的方法,但不要删
  199. stopPrevent() {},
  200. // 回复评论
  201. reply(pUser, reUser, pId) {
  202. this.pUser = pUser;
  203. this.commentReq.pId = pId;
  204. if (reUser) {
  205. this.commentReq.content = '@' + reUser + ' ';
  206. } else {
  207. this.commentReq.content = '';
  208. }
  209. this.showTag = true;
  210. this.commentInput(false);
  211. },
  212. // 删除评论前确认
  213. confirmDelete(commentId) {
  214. var that = this;
  215. uni.showModal({
  216. title: '警告',
  217. content: that.deleteTip,
  218. confirmText: '确认删除',
  219. success: function(res) {
  220. if (res.confirm) {
  221. that.$emit('del', commentId);
  222. }
  223. }
  224. });
  225. },
  226. // 新增评论
  227. add() {
  228. if (this.commentReq.content == null || this.commentReq.content.length < 2) {
  229. uni.showToast({
  230. title: '评论内容过短',
  231. duration: 2000
  232. });
  233. return
  234. }
  235. this.$emit('add', this.commentReq);
  236. },
  237. // 点赞评论
  238. like(commentId) {
  239. this.$emit('like', commentId);
  240. },
  241. // 新增完成
  242. addComplete() {
  243. this.commentReq.content = null;
  244. this.tagClose();
  245. this.closeInput();
  246. },
  247. // 点赞完成-本地修改点赞结果
  248. likeComplete(commentId) {
  249. for (var i in this.commentData.comment) {
  250. if (this.commentData.comment[i].id == commentId) {
  251. this.commentData.comment[i].hasLike ? this.commentData.comment[i].likeNum-- : this.commentData
  252. .comment[i].likeNum++;
  253. this.commentData.comment[i].hasLike = !this.commentData.comment[i].hasLike;
  254. return
  255. }
  256. for (var j in this.commentData.comment[i].children) {
  257. if (this.commentData.comment[i].children[j].id == commentId) {
  258. this.commentData.comment[i].children[j].hasLike ? this.commentData.comment[i].children[j]
  259. .likeNum-- : this.commentData.comment[i].children[j].likeNum++;
  260. this.commentData.comment[i].children[j].hasLike = !this.commentData.comment[i].children[j]
  261. .hasLike;
  262. return
  263. }
  264. }
  265. }
  266. },
  267. // 删除完成-本地删除评论
  268. deleteComplete(commentId) {
  269. for (var i in this.commentData.comment) {
  270. for (var j in this.commentData.comment[i].children) {
  271. if (this.commentData.comment[i].children[j].id == commentId) {
  272. this.commentData.comment[i].children.splice(Number(j), 1);
  273. return
  274. }
  275. }
  276. if (this.commentData.comment[i].id == commentId) {
  277. this.commentData.comment.splice(Number(i), 1);
  278. return
  279. }
  280. }
  281. },
  282. // 展开评论
  283. showMore(commentId) {
  284. for (var i in this.commentData.comment) {
  285. if (this.commentData.comment[i].id == commentId) {
  286. this.commentData.comment[i].hasShowMore = !this.commentData.comment[i].hasShowMore;
  287. this.$forceUpdate();
  288. return
  289. }
  290. for (var j in this.commentData.comment[i].children) {
  291. if (this.commentData.comment[i].children[j].id == commentId) {
  292. this.commentData.comment[i].children[j].hasShowMore = !this.commentData.comment[i].children[j]
  293. .hasShowMore;
  294. this.$forceUpdate();
  295. return
  296. }
  297. }
  298. }
  299. },
  300. // 输入框失去焦点
  301. blur() {
  302. this.focus = false;
  303. },
  304. // 输入框聚焦
  305. focusOn() {
  306. this.$emit('focusOn');
  307. },
  308. // 标签关闭
  309. tagClose() {
  310. this.showTag = false;
  311. this.pUser = null;
  312. this.commentReq.pId = null;
  313. },
  314. // 输入评论
  315. commentInput(data) {
  316. if(data){
  317. this.commentReq = {
  318. "pId": null, // 评论父id
  319. "content": null // 评论内容
  320. };
  321. }
  322. // TODO 调起键盘方法
  323. this.submit = true;
  324. setTimeout(() => {
  325. this.focus = true;
  326. }, 50)
  327. },
  328. // 关闭输入评论
  329. closeInput() {
  330. this.focus = false;
  331. this.submit = false;
  332. }
  333. }
  334. };
  335. </script>
  336. <style lang="scss" scoped>
  337. .hb-comment {
  338. padding: 10rpx;
  339. background-color: #f4ffff;
  340. width: 95%;
  341. border-radius: 5px;
  342. border-color: lightskyblue;
  343. padding-left: 5px;
  344. margin-left: 10px;
  345. }
  346. .top-read {
  347. font-size: 28rpx;
  348. padding-left: 10rpx;
  349. color: #999999;
  350. }
  351. .seg_line_box {
  352. display: flex;
  353. height: 5rpx;
  354. justify-content: space-between;
  355. margin: 5rpx 0;
  356. }
  357. .seg_line {
  358. width: 45%;
  359. border-bottom: 1rpx solid #e1e1e1;
  360. }
  361. .seg_dot {
  362. width: 8%;
  363. border-bottom: 5rpx dotted #dbdbdb;
  364. }
  365. .comment-num {
  366. display: flex;
  367. justify-content: space-between;
  368. align-items: center;
  369. padding: 20rpx 0;
  370. .publishcomment{
  371. background-color: lightgreen;
  372. }
  373. }
  374. .comment-box {
  375. padding: 10rpx 0;
  376. }
  377. .comment-box-item {
  378. display: flex;
  379. }
  380. .comment-main {
  381. padding-left: 20rpx;
  382. }
  383. .comment-main-top {
  384. width: 600rpx;
  385. padding-top: 6rpx;
  386. display: flex;
  387. justify-content: space-between;
  388. }
  389. .sub-comment-main-top {
  390. width: 510rpx;
  391. padding-top: 6rpx;
  392. display: flex;
  393. justify-content: space-between;
  394. }
  395. .avatar {
  396. width: 70rpx;
  397. height: 70rpx;
  398. border-radius: 50%;
  399. }
  400. .nick-name-box {
  401. display: flex;
  402. align-items: center;
  403. }
  404. .comLogo {
  405. margin-right: 18rpx;
  406. font-size: 22rpx;
  407. border-radius: 10rpx;
  408. padding: 5rpx 15rpx;
  409. color: #FFFFFF;
  410. }
  411. .com1 {
  412. background-color: #d218b1;
  413. }
  414. .com2 {
  415. background-color: #f19c0b;
  416. }
  417. .com3 {
  418. background-color: #c8da85;
  419. }
  420. .com4 {
  421. background-color: #bfd0da;
  422. }
  423. .nick-name {
  424. color: #2d8cf0;
  425. }
  426. .isLike {
  427. font-size: 28rpx;
  428. padding-right: 10rpx;
  429. color: #2d8cf0;
  430. }
  431. .notLike {
  432. font-size: 28rpx;
  433. padding-right: 10rpx;
  434. color: #999999;
  435. }
  436. .comment-main-content {
  437. padding: 10rpx 10rpx 10rpx 0;
  438. }
  439. .comment-main-foot {
  440. display: flex;
  441. font-size: 22rpx;
  442. }
  443. .replayTag {
  444. color: #909399;
  445. margin-bottom: 5px;
  446. border: 1px solid #c8c9cc;
  447. background-color: #f4f4f5;
  448. border-radius: 3px;
  449. display: flex;
  450. justify-content: space-between;
  451. align-items: center;
  452. font-size: 16rpx;
  453. padding: 5px 10px;
  454. }
  455. .replyTagClose {
  456. font-size: 20px;
  457. line-height: 12px;
  458. padding: 0 0 2px 5px;
  459. }
  460. .foot-btn {
  461. padding-left: 10rpx;
  462. color: #007AFF;
  463. }
  464. .comment-sub-box {
  465. padding: 20rpx 0;
  466. }
  467. .comment-sub-item {
  468. display: flex;
  469. }
  470. .comment-none {
  471. padding: 20rpx;
  472. width: 100%;
  473. text-align: center;
  474. color: #999999;
  475. }
  476. .comment-submit-box {
  477. position: fixed;
  478. display: flex;
  479. align-items: flex-end;
  480. z-index: 9900;
  481. left: 0;
  482. top: var(--window-top);
  483. bottom: 0;
  484. background-color: rgba($color: #000000, $alpha: 0.5);
  485. width: 100%;
  486. }
  487. .comment-add {
  488. position: fixed;
  489. background-color: #FFFFFF;
  490. width: 100%;
  491. padding: 5rpx;
  492. border: 1px solid #ddd;
  493. transition: .3s;
  494. -webkit-transition: .3s;
  495. }
  496. .btn-click {
  497. color: #007AFF;
  498. font-size: 28rpx;
  499. padding: 10rpx;
  500. }
  501. .cancel {
  502. color: #606266;
  503. }
  504. .textarea {
  505. height: 100px;
  506. padding: 16rpx;
  507. width: 100%;
  508. }
  509. .comment-submit {
  510. padding: 5rpx 20rpx 0 20rpx;
  511. border-bottom: 1px dashed #ddd;
  512. width: calc(100% - 40rpx);
  513. display: flex;
  514. justify-content: space-between;
  515. align-items: center;
  516. }
  517. </style>

页面接入:

  1. <view class="comment">
  2. <hb-comment ref="hbComment" @add="sendComment" @del="delcomment" @like="like" @focusOn="focusOn" :deleteTip="'确认删除?'"
  3. :cmData="commentData" v-if="commentData"></hb-comment>
  4. </view>
  5. getTree(data) {
  6. let result = [];
  7. let map = {};
  8. data.forEach(item => {
  9. map[item.id] = item;
  10. });
  11. data.forEach(item => {
  12. let parent = map[item.parentId];
  13. if (parent) {
  14. (parent.children || (parent.children = [])).push(item);
  15. } else {
  16. result.push(item);
  17. }
  18. });
  19. return result;
  20. },
  21. bindTextAreaBlur: function (e) {
  22. console.log(e.detail.value)
  23. },
  24. focusOn(){
  25. this.$refs.hbComment.focusOn();
  26. },
  27. sendComment(data){
  28. var type=0
  29. if(data.pId){
  30. type = 1;
  31. }
  32. var form = {"content":data.content,"blogId":this.item.id,"type":type,"parentId":data.pId};
  33. addCommentBlog(form).then(response=>{
  34. this.$refs.hbComment.closeInput();
  35. this.getCommentList();
  36. })
  37. },
  38. delcomment(data){
  39. delcomment(data).then(response=>{
  40. this.getCommentList();
  41. })
  42. },
  43. getCommentList(){
  44. listCommentBlogMinApp({"blogId":this.item.id}).then(res=>{
  45. // res.readNumer = 193;
  46. res.commentList=res.data.rows;
  47. this.commentData = {
  48. "readNumer": res.readNumer,
  49. "commentSize": res.commentList.length,
  50. "comment": this.getTree(res.commentList)
  51. }
  52. })
  53. },

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/500870
推荐阅读
相关标签
  

闽ICP备14008679号