comment-list.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <view class="comment-list">
  3. <!-- 顶部区域 -->
  4. <view class="comment-list_head">
  5. <view class="title">
  6. <text>评论列表</text>
  7. <text class="count">({{ (result && result.total) || 0 }}条)</text>
  8. </view>
  9. <view class="refresh">
  10. <text class="icon iconfont icon-sync-alt text-weight-s text-grey-darken-1"></text>
  11. <text class="text-weight-s text-grey-darken-1 ml-6" style="font-size: 26rpx;" @click="fnGetData">刷新</text>
  12. </view>
  13. </view>
  14. <!-- 内容区域 -->
  15. <view class="comment-list_content">
  16. <view v-if="loading !== 'success'" class="loading-wrap flex">
  17. <view v-if="loading === 'loading'" class="loading flex flex-center flex-col">
  18. <text class="e-loading-icon iconfont icon-loading text-blue"></text>
  19. <view class="text-size-n text-grey-lighten-1 py-12 mt-12">加载中,请稍等...</view>
  20. </view>
  21. <view v-else-if="loading === 'error'" class="error">
  22. <tm-empty icon="icon-wind-cry" label="加载失败">
  23. <tm-button theme="bg-gradient-light-blue-accent" size="m" v-if="!disallowComment"
  24. @click="fnToComment()">刷新试试
  25. </tm-button>
  26. </tm-empty>
  27. </view>
  28. </view>
  29. <block v-else>
  30. <tm-alerts v-if="disallowComment && dataList.length !== 0" color="red" text :margin="[0, 0]" :shadow="0"
  31. label="Ծ‸Ծ博主已设置该文章禁止评论!" :round="3"></tm-alerts>
  32. <view class="empty pt-50" v-if="dataList.length === 0">
  33. <tm-empty v-if="disallowComment" icon="icon-shiliangzhinengduixiang-" label="暂无评论">
  34. <text class="text-red text-size-s">- 文章已开启禁止评论 -</text>
  35. </tm-empty>
  36. <tm-empty v-else icon="icon-shiliangzhinengduixiang-" label="暂无评论">
  37. <tm-button theme="light-blue" :dense="true" :shadow="0" size="m" @click="fnToComment(null)">抢沙发
  38. </tm-button>
  39. </tm-empty>
  40. </view>
  41. <block v-else>
  42. <!-- 评论内容 : 目前仅支持二级评论 -->
  43. <block v-for="(comment, index) in dataList" :key="comment.metadata.name">
  44. <comment-item :useContentBg="false" :isChild="false" :comment="comment" :postName="postName"
  45. :disallowComment="disallowComment" @on-copy="fnCopyContent" @on-comment="fnToComment"
  46. @on-todo="fnToDo" @on-detail="fnShowCommentDetail"></comment-item>
  47. <!-- 二级评论 -->
  48. <block v-if="comment.replies && comment.replies.items.length !== 0">
  49. <block v-for="(childComment, childIndex) in comment.replies.items"
  50. :key="childComment.metadata.name">
  51. <comment-item :useContentBg="false" :isChild="true" :comment="childComment"
  52. :postName="postName" :disallowComment="disallowComment" @on-copy="fnCopyContent"
  53. @on-comment="fnToComment" @on-todo="fnToDo"
  54. @on-detail="fnShowCommentDetail"></comment-item>
  55. </block>
  56. </block>
  57. </block>
  58. <view v-if="false" class="to-more-comment">
  59. <tm-button item-class="btn" :block="true" width="90vw" theme="bg-gradient-light-blue-lighten"
  60. size="m">点击查看全部评论
  61. </tm-button>
  62. </view>
  63. </block>
  64. </block>
  65. </view>
  66. </view>
  67. </template>
  68. <script>
  69. import tmEmpty from '@/tm-vuetify/components/tm-empty/tm-empty.vue';
  70. import tmButton from '@/tm-vuetify/components/tm-button/tm-button.vue';
  71. import tmAlerts from '@/tm-vuetify/components/tm-alerts/tm-alerts.vue';
  72. export default {
  73. name: 'comment-list',
  74. components: {
  75. tmEmpty,
  76. tmButton,
  77. tmAlerts
  78. },
  79. props: {
  80. // 是否禁用评论
  81. disallowComment: {
  82. type: Boolean,
  83. default: false
  84. },
  85. postName: {
  86. type: String,
  87. default: ""
  88. },
  89. post: {
  90. type: Object,
  91. default: () => {
  92. }
  93. }
  94. },
  95. data() {
  96. return {
  97. loading: 'loading',
  98. queryParams: {
  99. group: "content.halo.run",
  100. kind: "Post",
  101. version: "v1alpha1",
  102. name: "",
  103. page: 1,
  104. size: 50,
  105. withReplies: true,
  106. replySize: 10
  107. },
  108. result: null,
  109. dataList: []
  110. };
  111. },
  112. created() {
  113. this.queryParams.name = this.postName;
  114. this.fnGetData();
  115. uni.$on('comment_list_refresh', () => {
  116. this.fnOnSort(true);
  117. });
  118. },
  119. methods: {
  120. fnOnSort(refresh = false) {
  121. if (refresh === false) return;
  122. this.fnGetData();
  123. },
  124. fnGetData() {
  125. this.loading = 'loading';
  126. this.$httpApi.v2.getPostCommentList(this.queryParams)
  127. .then(res => {
  128. console.log("日志:", res)
  129. this.result = res;
  130. this.dataList = res.items;
  131. this.loading = 'success';
  132. this.$emit('on-loaded', this.dataList);
  133. })
  134. .catch(err => {
  135. this.loading = 'error';
  136. })
  137. .finally(() => {
  138. uni.hideLoading();
  139. });
  140. },
  141. fnToDo() {
  142. uni.$tm.toast('Halo暂未支持!');
  143. },
  144. fnToComment(data) {
  145. if (this.disallowComment) {
  146. return uni.$tm.toast('文章已禁止评论!');
  147. }
  148. let _comment = {};
  149. console.log('data', data)
  150. if (data) {
  151. let {type, comment} = data;
  152. // 来自用户
  153. _comment = {
  154. isComment: false,
  155. postName: comment.metadata.name,
  156. title: comment.owner.displayName,
  157. from: 'posts',
  158. formPage: 'comment_list',
  159. type: 'user'
  160. };
  161. } else {
  162. // 来自文章
  163. _comment = {
  164. isComment: true,
  165. postName: this.post.metadata.name,
  166. title: '新增评论',
  167. formPage: 'comment_list',
  168. from: 'posts',
  169. type: 'post'
  170. };
  171. }
  172. this.$emit("on-comment", _comment)
  173. },
  174. fnCopyContent(content) {
  175. uni.$tm.u.setClipboardData(content);
  176. uni.$tm.toast('内容已复制成功!');
  177. },
  178. fnShowCommentDetail(comment) {
  179. this.$emit('on-comment-detail', {
  180. postName: this.postName,
  181. comment: comment
  182. });
  183. }
  184. }
  185. };
  186. </script>
  187. <style lang="scss" scoped>
  188. .comment-list {
  189. &_head {
  190. display: flex;
  191. align-items: center;
  192. justify-content: space-between;
  193. position: relative;
  194. box-sizing: border-box;
  195. padding-left: 24rpx;
  196. font-size: 30rpx;
  197. font-weight: bold;
  198. &:before {
  199. content: '';
  200. position: absolute;
  201. left: 0rpx;
  202. top: 8rpx;
  203. width: 8rpx;
  204. height: 26rpx;
  205. background-color: rgb(3, 174, 252);
  206. border-radius: 6rpx;
  207. }
  208. .title {
  209. .count {
  210. font-size: 28rpx;
  211. font-weight: normal;
  212. }
  213. }
  214. .filter {
  215. font-size: 26rpx;
  216. font-weight: normal;
  217. &-item {
  218. margin-left: 20rpx;
  219. color: #666;
  220. &.active {
  221. font-weight: bold;
  222. color: rgb(255, 152, 0);
  223. font-size: 26rpx;
  224. }
  225. }
  226. }
  227. }
  228. &_content {
  229. margin-top: 24rpx;
  230. padding-bottom: 36rpx;
  231. }
  232. }
  233. .loading-wrap {
  234. width: 100%;
  235. height: 506rpx;
  236. .loading {
  237. width: 100%;
  238. }
  239. .e-loading-icon {
  240. font-size: 100rpx;
  241. }
  242. }
  243. .to-more-comment {
  244. width: 100%;
  245. display: flex;
  246. align-items: center;
  247. justify-content: center;
  248. margin-top: 80rpx;
  249. ::v-deep {
  250. .tm-button .tm-button-btn uni-button {
  251. height: 70rpx;
  252. }
  253. }
  254. }
  255. </style>