comment-list.vue 4.39 KB
<template>
  <div class="comment-list">
    <div class="comment-content-flex">
      <div class="comment-content">
        <Scroll 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"
            @on-reply="onReply">
          </CommentItem>
        </Scroll>
      </div>
    </div>
    <div class="comment-footer">
      <CommentPlaceholder
        class="comment-input"
        :dest-id="destId"
        :add-type="0"
        :column-type="columnType"
        @on-comment="onComment">
        参与评论
      </CommentPlaceholder>
    </div>
  </div>
</template>

<script>
import CommentItem from './comment-item.vue';
import {Scroll} 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
    }
  },
  data() {
    return {
      page: 1,
      commentList: [],
      scrollOption: {
        bounce: false,
        pullUpLoad: {
          threshold: 200,
          txt: {
            more: '上拉加载',
            noMore: '- 已经到底了 -'
          }
        }
      },
      show: false
    };
  },
  methods: {
    ...mapActions(['fetchCommentList', 'fetchReplayList', 'postComment']),
    async fetchComments(lazy) {
      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', []);

        setTimeout(() => {
          if (this.page === 1) {
            this.commentList = comments;
          } else {
            this.commentList = this.commentList.concat(comments);
          }
          if (comments.length) {
            this.page++;
          } else {
            dirty = false;
          }
          this.$emit('on-page-change', {
            page: this.page,
            size: result.data.total
          });
          this.$refs.scroll.forceUpdate(dirty);
        }, lazy ? 200 : 0);
      } else {
        this.$createToast && this.$createToast({
          txt: result.message || '服务器开小差了',
          type: 'warn',
          time: 1000
        }).show();
      }
      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.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();
      }
    }
  },
  components: {Scroll, CommentItem}
};
</script>

<style scoped lang="scss">
.comment-list {
  width: 100%;
  height: 100%;
  overflow: hidden;
  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>