article-item-comment.vue 3.53 KB
<template>
  <div class="article-item-comment">
    <div v-if="data.comments && data.comments.length" class="comment-list">
      <CommentPlaceholder
        :share="share"
        tag="p"
        class="comment-item"
        v-for="(comment, inx) in data.comments"
        :key="inx"
        :dest-id="comment.id"
        :pos-id="posId"
        :article-id="articleId"
        :add-type="1"
        :column-type="comment.columnType"
        :user="comment.userName">
        <span class="user-name">{{comment.userName}}</span>
        <span class="comment-content">: {{getCommentContent(comment.content)}}</span>
      </CommentPlaceholder>
    </div>
    <div v-if="!invisible" class="total-comment">
      <div class="total hover-opacity" @click="onShowComment">查看{{data.commentCount}}条评论</div>
    </div>
    <div class="comment">
      <WidgetAvatar :lazy="true" class="widget-avatar" :src="userHeadIco" :width="70" :height="70" @click.native="commentClick"></WidgetAvatar>
      <CommentPlaceholder
        ref="commentInput"
        :share="share"
        class="comment-input hover-opacity"
        :dest-id="data.articleId"
        :add-type="0"
        :article-id="articleId"
        :pos-id="posId"
        :column-type="1001"
        @on-comment="onComment">
        添加评论...
      </CommentPlaceholder>
      <div class="last-time">{{data.date}}</div>
    </div>
  </div>
</template>
<script>
import {Input} from 'cube-ui';
import {createNamespacedHelpers} from 'vuex';
const {mapMutations} = createNamespacedHelpers('article');

export default {
  name: 'ArticleItemComment',
  props: {
    data: {
      type: Object,
      default() {
        return {};
      }
    },
    type: String,
    share: Boolean,
    thumb: Boolean,
    posId: Number,
    articleId: [Number, String],
    userHeadIco: String
  },
  computed: {
    invisible() {
      return this.thumb || (this.data.commentCount <= 2);
    }
  },
  methods: {
    ...mapMutations(['ASYNC_ARTICLE_COMMENT']),
    onShowComment() {
      if (this.share) {
        return this.$links.toDownloadApp();
      }
      if (this.data.commentCount) {
        this.$emit('on-show-comment', {commentCount: this.data.commentCount});
      }
    },
    getCommentContent(content = '') {
      if (content.length < 50) {
        return content;
      } else {
        return `${content.substring(0, 50)}...`;
      }
    },
    commentClick() {
      this.$refs.commentInput.$el.click();
    },
    onComment() {
      this.ASYNC_ARTICLE_COMMENT({articleId: this.data.articleId, type: this.type});
      this.$emit('on-resize');
    }
  },
  components: {CubeInput: Input}
};
</script>

<style lang="scss" scoped>
.article-item-comment {
  padding: 30px;

  .comment-list {
    margin-bottom: 26px;
  }

  .comment-item {
    font-size: 24px;
    color: #4a4a4a;
    letter-spacing: 0.06PX;
    line-height: 38px;

    > * {
      vertical-align: text-bottom;
    }
  }

  .user-name {
    font-weight: bold;
    max-width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: inline-block;
  }

  .total-comment {
    font-size: 24px;
    color: #b0b0b0;
    letter-spacing: 0.06px;
    margin-bottom: 26px;
  }

  .comment {
    display: flex;
    align-items: center;
    margin-top: 10px;

    .widget-avatar {
      width: 50px;
      height: 50px;
    }

    .comment-input {
      flex-grow: 1;
      padding-left: 12px;
      height: 50px;
      line-height: 50px;
      color: #b0b0b0;
      font-size: 24px;
    }

    .last-time {
      color: #b0b0b0;
    }
  }
}
</style>