comment-list.vue 4.24 KB
<template>
  <div class="comment-list">
    <div class="comment-content">
      <Scroll ref="scroll" :data="commentList" :options="scrollOption" @pulling-up="onPullingUp">
        <CommentItem
          v-for="(comment, index) in commentList"
          :key="index"
          :parent-comment="comment.parentComment"
          :children-comments="comment.childrenComments"
          :column-type="columnType"
          @on-reply="onReply">
        </CommentItem>
      </Scroll>
    </div>
    <div class="comment-footer">
      <div class="comment-input" @click="onComment">参与评论</div>
    </div>
  </div>
</template>

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

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

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

        if (refresh) {
          this.commentList = appendComments;
        } else {
          this.commentList = this.commentList.concat(appendComments);
        }

        if (appendComments.length) {
          this.page++;
        } else {
          dirty = false;
        }
        this.$emit('on-page-change', {
          page: this.page,
          size: result.data.total
        });
      } else {
        this.$createToast && this.$createToast({
          txt: result.message || '服务器开小差了',
          type: 'warn',
          time: 1000
        }).show();
      }

      this.$refs.scroll.forceUpdate(dirty);
      return result;
    },
    onPullingUp() {
      this.fetchComments();
    },
    async onComment() {
      const result = await this.postComment({
        content: '这还是一条测试回复1',
        destId: this.destId,
        addType: 0,
        columnType: this.columnType
      });

      if (result.code === 200) {
        this.onRefresh();
      } else {
        this.$createToast({
          txt: result.message || '服务器开小差了',
          type: 'warn',
          time: 1000
        }).show();
      }
    },
    async onRefresh() {
      this.page = 1;
      await this.fetchComments(true);
      this.$refs.scroll.forceUpdate(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: 1;
    padding-top: 40px;
    padding-left: 30px;
    padding-right: 30px;
    overflow: hidden;

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

  .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>