comment-list.vue 2.73 KB
<template>
  <div class="comment-context">
    <p class="comment-title">共{{commentCount}}条评论</p>
    <div class="comment-list">
      <CommentItem
        v-for="comment in commentList"
        :key="comment.parentComment.id"
        :class="'comment-' + comment.parentComment.id"
        :parent-comment="comment.parentComment"
        :children-comments="comment.childrenComments"
        :column-type="columnType"
        :pos-id="0"
        :article-id="articleId"
        @on-reply="onReply">
      </CommentItem>
    </div>
    <div v-show="showMore" class="more-comment hover-opacity" @click="onFetch">
      <p class="loading" v-if="onFetching">
        <Loading :size="20"></Loading>
        &nbsp;&nbsp;加载中
      </p>
      <span v-else>展开{{page > 2 ? '更多' : commentCount + '条'}}评论</span>

    </div>
  </div>
</template>

<script>
import {Loading} from 'cube-ui';
import {createNamespacedHelpers} from 'vuex';

const {mapActions} = createNamespacedHelpers('comment');

export default {
  name: 'ArticleDetailCommentList',
  props: {
    commentCount: [Number, String],
    articleId: [Number, String],
    columnType: {
      type: Number,
      default: 1001
    },
    share: Boolean
  },
  data() {
    return {
      commentList: [],
      page: 1,
      onFetching: false,
      showMore: false
    }
  },
  mounted() {
    this.init();
  },
  methods: {
    ...mapActions(['fetchCommentList', 'fetchReplayList', 'postComment']),
    init() {
      this.commentList.length = 0;
      this.page = 1;
      this.showMore = false;

      this.onFetch();
    },
    async onFetch() {
      if (this.onFetching || (this.page > 1 && !this.showMore)) {
        return;
      }

      this.onFetching = true;

      const result = await this.fetchCommentList({
        destId: this.articleId,
        columnType: this.columnType,
        page: this.page,
        limit: 10
      });

      setTimeout(() => {
        if (result.code === 200) {
          this.showMore = result.data.totalPage > this.page;

          this.page++;
          this.commentList = this.commentList.concat(result.data.commentInfos || []);
        }

        this.onFetching = false;
      }, 800);
    },
    onReply() {

    }
  },
  components: {
    Loading
  }
};
</script>

<style :lang="scss" scoped>
.comment-title {
  font-size: 28px;
  margin: 0 30px;
  color: #9b9b9b;
  line-height: 80px;
  border-top: 1px solid #f0f0f0;
}

.comment-list {
  padding: 20px 30px;
}

.more-comment {
  height: 100px;
  line-height: 1;
  font-size: 28px;
  color: #4a90e2;
  border-top: 1px solid #f0f0f0;
  display: flex;
  align-items: center;
  justify-content: center;

  .loading {
    display: flex;
    align-items: center;
    justify-content: center;
    color: #999;
  }
}
</style>