comment-list.vue 5.13 KB
<template>
  <div class="comment-list">
    <div class="comment-content-flex">
      <div class="comment-content">
        <div class="loading" v-if="firstLoading">
          <Loading></Loading>
        </div>
        <Scroll v-else ref="scroll" :data="commentList" :options="scrollOption" @pulling-up="onPullingUp">
          <CommentItem
            v-for="comment in commentList"
            :key="comment.parentComment.id"
            :parent-comment="comment.parentComment"
            :children-comments="comment.childrenComments"
            :column-type="columnType"
            :pos-id="posId"
            :article-id="articleId"
            @on-reply="onReply"
            @on-close="onClose">
          </CommentItem>
        </Scroll>
      </div>
    </div>
    <div class="comment-footer" v-if="!firstLoading">
      <CommentPlaceholder
        class="comment-input"
        :dest-id="destId"
        :pos-id="posId"
        :article-id="articleId"
        :add-type="0"
        :column-type="columnType"
        @on-comment="onComment">
        参与评论
      </CommentPlaceholder>
    </div>
  </div>
</template>

<script>
import CommentItem from './comment-item.vue';
import {Scroll, Loading} from 'cube-ui';
import {get} from 'lodash';
import {createNamespacedHelpers} from 'vuex';
const {mapActions} = createNamespacedHelpers('comment');

export default {
  name: 'CommentList',
  props: {
    destId: Number,
    columnType: {
      type: Number,
      default: 1001
    },
    posId: Number,
    articleId: Number
  },
  data() {
    return {
      page: 1,
      commentList: [],
      firstLoading: true,
      scrollOption: {
        bounce: false,
        pullUpLoad: {
          threshold: 200,
          txt: {
            more: '上拉加载',
            noMore: '- 已经到底了 -'
          }
        }
      },
      show: false
    };
  },
  methods: {
    ...mapActions(['fetchCommentList', 'fetchReplayList', 'postComment']),
    async fetchComments() {
      const result = await this.fetchCommentList({
        destId: this.destId,
        columnType: this.columnType,
        page: this.page,
      });
      let dirty = true;

      if (result.code === 200) {
        const comments = get(result, 'data.commentInfos', []);

        this.$emit('on-page-change', {
          page: this.page,
          size: result.data.total
        });
        if (this.page === 1) {
          await new Promise((r) => {
            setTimeout(() => {
              r();
            }, 400);
          });
          this.commentList = comments;
        } else {
          this.commentList = this.commentList.concat(comments);
        }
        if (comments.length) {
          this.page++;
        } else {
          dirty = false;
        }
        this.firstLoading = false;
        this.$nextTick(() => {
          this.$refs.scroll.forceUpdate(dirty);
        });
      } else {
        this.$createToast && this.$createToast({
          txt: result.message || '服务器开小差了',
          type: 'warn',
          time: 1000
        }).show();
        this.$emit('on-page-ready', {success: false});
      }
      return result;
    },
    onPullingUp() {
      this.fetchComments();
    },
    async onComment() {
      this.page = 1;
      this.$refs.scroll.scrollTo(0, 0, 200);
      this.fetchComments(false);
      this.$emit('on-comment', {destId: this.destId});
    },
    async init() {
      this.page = 1;
      this.commentList = [];
      this.firstLoading = true;
      this.fetchComments(true);
    },
    async onReply({commentId}) {
      const result = await this.fetchReplayList({
        commentId
      });

      if (result.code === 200) {
        this.commentList.forEach(comment => {
          if (comment.parentComment.id === commentId) {
            comment.childrenComments = get(result, 'data.childrenComments');
          }
        });
      } else {
        this.$createToast({
          txt: result.message || '服务器开小差了',
          type: 'warn',
          time: 1000
        }).show();
      }
    },
    onClose() {
      this.$emit('on-close');
    }
  },
  components: {Scroll, CommentItem, Loading}
};
</script>

<style scoped lang="scss">
.loading {
  width: 100%;
  height: 200px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.comment-list {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #fff;
  display: flex;
  flex-direction: column;

  .comment-content-flex {
    flex: 1;
    overflow: hidden;
    position: relative;

    /deep/ .before-trigger {
      color: #b0b0b0;
    }

    .comment-content {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      padding-top: 40px;
      padding-left: 30px;
      padding-right: 30px;
    }
  }

  .comment-footer {
    width: 100%;
    height: 100px;
    overflow: hidden;
    padding: 14px 30px;
    background-color: #fff;
    border-top: solid 1px #e0e0e0;

    .comment-input {
      width: 100%;
      height: 100%;
      background: #f0f0f0;
      border: 1px solid #e0e0e0;
      border-radius: 4PX;
      color: #b0b0b0;
      padding-left: 22px;
      display: flex;
      align-items: center;
    }
  }
}
</style>