community-comment-popup.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <template>
  2. <view>
  3. <u-popup v-model="show" mode="bottom" height="900rpx" :duration="100" closeable border-radius="14">
  4. <view class="content">
  5. <!-- 评论头 -->
  6. <view class="xl p-24 text-center bold bb">评论</view>
  7. <!-- 评论内容 -->
  8. <view class="content-wrapper">
  9. <scroll-view scroll-y="true" @scrolltolower="toLower">
  10. <template v-if="isFirstLoading">
  11. <view class="text-center flex row-center p-50">
  12. <u-loading :color="colorConfig.primary" :size="40" mode="circle"></u-loading>
  13. <text class="m-l-20">加载中</text>
  14. </view>
  15. </template>
  16. <template v-else>
  17. <view class="text-center p-50" v-if="!commentData.length">
  18. <view class="flex row-center">
  19. <u-image :src="'/static/images/news_null.png'" width="300" height="300"></u-image>
  20. </view>
  21. <view class="muted m-t-40">
  22. 还没有人评论呢, 快来抢沙发~
  23. </view>
  24. </view>
  25. <template v-else>
  26. <block v-for="(commentItem, commentIndex) in commentData" :key="commentItem.id">
  27. <community-comment-list :comment="commentItem" @reply="onReply">
  28. </community-comment-list>
  29. </block>
  30. <!-- 加载 -->
  31. <view class="flex row-center primary nd p-50" v-if="more === 1 && loading">
  32. <u-loading :color="colorConfig.primary" :size="40" mode="circle"></u-loading>
  33. <text class="m-l-20">加载中</text>
  34. </view>
  35. <!-- 加载底部 -->
  36. <view class="text-center muted nd p-50" v-else>
  37. <text>没有更多了~</text>
  38. </view>
  39. </template>
  40. </template>
  41. </scroll-view>
  42. </view>
  43. <!-- 评论输入 -->
  44. <view class="comment-footer m-t-8 flex row-between">
  45. <view class="flex-1 flex" @click="showCommentPopup=true">
  46. <u-image :src="userInfo.avatar" width="60" height="60" borderRadius="50%"></u-image>
  47. <view class="input nr muted">发表你的想法吧</view>
  48. </view>
  49. </view>
  50. </view>
  51. </u-popup>
  52. <!-- 一级评论 -->
  53. <community-comment v-model="showCommentPopup" :communityId="id" @change="changeComment">
  54. </community-comment>
  55. <!-- 二级评论 -->
  56. <community-comment v-model="showCommentChildPopup" :communityId="id" :pid="childPid"
  57. @change="changeCommentChild" :placeholder="childPlaceholder">
  58. </community-comment>
  59. </view>
  60. </template>
  61. <script>
  62. import {
  63. mapGetters
  64. } from "vuex"
  65. import {
  66. getCommunityCommentLists
  67. } from '@/api/community.js';
  68. import CommunityCommentList from "../community-comment-list/community-comment-list.vue"
  69. export default {
  70. name: "community-comment-popup",
  71. components: {
  72. CommunityCommentList
  73. },
  74. props: {
  75. value: {
  76. type: Boolean
  77. },
  78. communityId: {
  79. type: [Number, String]
  80. }
  81. },
  82. data() {
  83. return {
  84. id: '',
  85. commentData: [],
  86. page: 1, // 评论分页页数
  87. pageSize: 10, // 评论分页数量
  88. more: 0, // 是否有下一页分页
  89. loading: false, // 加载动画 底部
  90. showCommentPopup: false, // 一级评论
  91. showCommentChildPopup: false, // 二级评论
  92. childPid: '', // 选择二级评论回复ID
  93. childIndex: '', // 选择的二级评论第几条
  94. childPlaceholder: '', // 选择的二级评论回复人名字: 回复@xxx
  95. // 是否初始化加载中
  96. isFirstLoading: true
  97. }
  98. },
  99. computed: {
  100. ...mapGetters(['userInfo']),
  101. // 弹窗Popup显示状态
  102. show: {
  103. get: function() {
  104. return this.value
  105. },
  106. set: function(value) {
  107. !value ? this.showCommentPopup = false : '';
  108. this.$emit('input', value)
  109. }
  110. }
  111. },
  112. watch: {
  113. communityId: {
  114. handler: function(val) {
  115. this.id = val
  116. this.page = 1;
  117. this.commentData = []
  118. this.getCommentData()
  119. },
  120. immediate: true
  121. }
  122. },
  123. methods: {
  124. // 评论
  125. changeComment(event) {
  126. if (!event.hasOwnProperty('child')) {
  127. event.child = []
  128. }
  129. this.commentData.unshift(event)
  130. },
  131. onReply(event) {
  132. this.childIndex = this.commentData.findIndex(item => item.id == event.parentId)
  133. this.childPid = event.commentId
  134. this.childPlaceholder = '回复@' + event.commentUserName;
  135. this.showCommentChildPopup = true
  136. },
  137. // 子评论
  138. changeCommentChild(event) {
  139. this.commentData[this.childIndex].child.push(event)
  140. },
  141. // 获取评论
  142. getCommentData() {
  143. getCommunityCommentLists({
  144. article_id: this.communityId,
  145. page_no: this.page,
  146. page_size: this.pageSize,
  147. }).then(res => {
  148. setTimeout(() => this.isFirstLoading = false, 1000)
  149. if(res.code == 1) {
  150. res.data.list.forEach(item => item.loading = false)
  151. if (res.data.more === 1) {
  152. this.page += 1
  153. }
  154. this.commentData.push(...res.data.list)
  155. this.more = res.data.more
  156. this.loading = false
  157. } else {
  158. this.$toast({ title: res.msg })
  159. }
  160. })
  161. },
  162. // 触底加载
  163. toLower() {
  164. if (this.more) {
  165. this.loading = true
  166. this.getCommentData()
  167. }
  168. },
  169. }
  170. }
  171. </script>
  172. <style lang="scss" scoped>
  173. .bb {
  174. border-bottom: 1px solid $-color-body;
  175. }
  176. .content {
  177. width: 100%;
  178. height: 100%;
  179. position: relative;
  180. >view {
  181. width: 100%;
  182. position: fixed;
  183. }
  184. .content-wrapper {
  185. top: 46px;
  186. /* #ifdef H5 */
  187. height: 600rpx;
  188. /* #endif */
  189. /* #ifndef H5 */
  190. height: 700rpx;
  191. /* #endif */
  192. scroll-view {
  193. height: 100%;
  194. }
  195. }
  196. .comment-footer {
  197. bottom: var(--window-bottom);
  198. height: 92rpx;
  199. padding: 0 30rpx;
  200. box-shadow: 0 -4rpx 10rpx rgba(#000000, .1);
  201. .input {
  202. width: 100%;
  203. margin-left: 16rpx;
  204. border-radius: 30rpx;
  205. background: #f8f8f8;
  206. padding: 10rpx 30rpx;
  207. }
  208. image {
  209. width: 44rpx;
  210. height: 44rpx;
  211. }
  212. }
  213. }
  214. </style>