comment-list.vue 8.7 KB

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