comment-list.vue 9.36 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" :scroll-events="['scroll', 'scroll-end']" :options="scrollOption" @scroll="onScrollHandle" @scroll-end="onScrollEndHandle" @pulling-up="onPullingUp">
          <CommentEmpty v-if="empty"></CommentEmpty>
          <div v-else ref="commentList" class="comment-list">
            <div ref="commentListTop" class="comment-list-top">
              <div ref="commentPre" class="comment-pre-list">
                <div
                v-for="(comments, index) in commentPreList"
                :key="commentPreList.length - index">
                  <CommentItem
                    v-for="comment in comments"
                    :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>
                </div>
              </div>
            </div>
            <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="posId"
              :article-id="articleId"
              @on-reply="onReply"
              @on-close="onClose">
            </CommentItem>
          </div>
        </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, throttle} 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,
    commentId: Number
  },
  data() {
    return {
      page: 1,
      prePage: 1,
      totalPage: 1,
      commentPaddingTop: 0,
      commentList: [],
      commentPreList: [],
      firstLoading: true,
      empty: false,
      scrollOption: {
        bounce: false,
        pullUpLoad: {
          threshold: 200,
          txt: {
            more: '上拉加载',
            noMore: '- 已经到底了 -'
          }
        }
      },
      show: false
    };
  },
  mounted() {
    this.fetchCommentsEvent = throttle(this.fetchComments.bind(this), 1500);
  },
  methods: {
    ...mapActions(['fetchCommentList', 'fetchReplayList', 'postComment']),
    async fetchCommentsAsync(pre) {
      let params = {
        destId: this.destId,
        columnType: this.columnType,
        page: pre ? this.prePage : this.page
      };

      if (params.page < 1 || params.page > this.totalPage) {
        return {code: 200};
      }

      if (this.commentId && (this.prePage === this.page)) {
        params.rootCommentId = this.commentId;
      }

      const result = await this.fetchCommentList(params);

      if (result.code === 200) {
        if (params.rootCommentId) {
          this.page = get(result, 'data.page', 1);
          this.prePage = this.page - 1;
        } else {
          this.prePage = 0;
        }

        this.totalPage = get(result, 'data.totalPage', 1);

        if (this.page === 1) {
          await new Promise((r) => {
            setTimeout(() => {
              r();
            }, 400);
          });
        }

        if (pre) {
          this.prePage--;
        } else {
          this.page++;
        }
      }

      if (!get(result, 'data.total')) {
        this.empty = true;
      } else {
        this.empty = false;
      }

      return result;
    },
    async fetchComments(pre) {
      const result = await this.fetchCommentsAsync(pre);

      let dirty = true;

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

        if (comments.length) {
          this.$emit('on-page-change', {
            page: result.data.page,
            size: result.data.total
          });

          if (pre) {
            this.commentPreList.unshift(comments);
            this.$nextTick(() => {
              this.loadPreComment();
            });
          } else {
            if (this.page <= 2) {
              this.commentList = comments;

              // 将评论滚动到可视区域
              if (this.commentId) {
                setTimeout(() => {
                  let scrollHeight = this.$refs.scroll.$el.offsetHeight;

                  let dom = this.$refs.commentList.getElementsByClassName('comment-' + this.commentId);

                  if (scrollHeight && dom.length && (dom[0].offsetHeight + dom[0].offsetTop > scrollHeight)) {
                    this.$refs.scroll.scrollTo(0, scrollHeight - this.$refs.commentList.offsetHeight);
                  }
                }, 500);
              }
            } else {
              this.commentList = this.commentList.concat(comments);
            }
          }
        } 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();
    },
    onScrollHandle(e) {
      this.scrollY = e.y;
    },
    onScrollEndHandle(e) {
      this.scrollY = e.y;

      if (!this.commentId || this.prePage < 1) {
        return;
      }

      if (!this.loadPreComment() && this.scrollY < 1000) {
        this.fetchCommentsEvent(true);
      }
    },
    loadPreComment() {
      let offsetHeight = this.$refs.commentPre.offsetHeight;

      if (this.commentPaddingTop !== offsetHeight) {
        this.$refs.commentListTop.style.height = offsetHeight + 'px';
        this.scrollY = this.scrollY - offsetHeight + this.commentPaddingTop;
        this.commentPaddingTop = offsetHeight;
        this.$refs.scroll.scrollTo(0, this.scrollY);
        this.$refs.scroll.forceUpdate(true);

        return true;
      }

      return false;
    },
    async onComment() {
      this.page = 1;
      this.totalPage = 1;
      this.$refs.scroll.scrollTo(0, 0, 200);
      this.fetchComments();
      this.$emit('on-comment', {destId: this.destId});
    },
    async init() {
      this.page = 1;
      this.perPage = 1;
      this.totalPage = 1;
      this.commentList = [];
      this.firstLoading = true;
      this.fetchComments();
    },
    async onReply({destId, parentId}) {
      const commentId = parentId || destId;

      if (!commentId) {
        return;
      }

      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-list-top {
    margin-bottom: 40px;
    height: 0;
    position: relative;
  }

  .comment-pre-list {
    position: absolute;
    bottom: 0;

    > * {
      padding-bottom: 40px;
    }
  }

  .comment-pre-hide {
    visibility: hidden;
    position: absolute;
    z-index: -1;
  }

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

    .comment-list {
      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>