article-item2.vue 5.52 KB
<template>
  <div class="article-item">
    <slot>
      <div class="article-item-main">
        <a v-if="actionUrl" class="action-article" :href="actionUrl" target="_blank"></a>
        <div class="layer-image" :style="`height: ${Math.floor(width * coverImage.scale)}px`" @click="toArticlePage">
          <div v-if="data.sort === 2" class="article-long-icon"></div>
          <ImageFormat :mode="1" :src="coverImage.contentData" :width="coverImage.width" :height="coverImage.height"></ImageFormat>
        </div>
        <div v-if="intro" class="description" @click="toArticlePage">{{intro}}</div>
        <div class="attribution">
          <div class="auther" @click="toUserPage">
            <WidgetAvatar class="avatar" :src="data.authorHeadIco" :group="data.authGroupId" :small="true" :width="70" :height="70"></WidgetAvatar>
            <span class="name">{{data.authorName}}</span>
          </div>
          <div class="fav">
            <WidgetFav :articleId="data.articleId" :num="data.praiseCount" :share="share" :option="favOption"></WidgetFav>
          </div>
        </div>
      </div>
    </slot>
  </div>
</template>

<script>
import {get} from 'lodash';
import YAS from 'utils/yas-constants';
import {getArticleImageSize} from 'utils/image-handler';

import {createNamespacedHelpers} from 'vuex';
const {mapState} = createNamespacedHelpers('article');

export default {
  name: 'ArticleItem',
  data() {
    return {
      imgWidth: 500
    };
  },
  props: {
    data: {
      type: Object,
      default() {
        return {};
      }
    },
    width: Number,
    index: Number,
    share: Boolean,
    type: String,
    thumb: Boolean,
    posId: Number,
    articleId: Number
  },
  computed: {
    ...mapState(['articleStates']),
    articleState() {
      return this.articleStates[this.data.articleId] || this.data;
    },
    coverImage() {
      let img = get(this.data, 'blockList', []).filter(block => block.templateKey === 'image')[0] || {
        contentData: this.data.coverImage,
        width: this.data.imageWidth,
        height: this.data.imageHeight
      };

      img = Object.assign({...img}, getArticleImageSize({
        width: img.width,
        height: img.height,
        maxWidth: 500
      }));

      img.scale = img.height / img.width;

      return img;
    },
    intro() {
      return get(get(this.data, 'blockList', []).filter(block => block.templateKey === 'text'), '[0].contentData', this.data.content);
    },
    actionUrl() {
      if (!this.share) {
        if (this.data.sort === 3) {
          return this.data.actionUrl;
        }
      }

      return '';
    },
    favOption() {
      return {
        selected: this.articleState.hasPraise === 'Y',
        iconBold: true,
        iconFontSize: 30,
        textAlign: 'normal'
      };
    }
  },
  methods: {
    toUserPage() {
      if (this.share) {
        return this.$links.toDownloadApp();
      }

      this.$router.push({
        name: 'author',
        params: {
          type: this.data.authorType,
          id: this.data.authorUid
        }
      });

      this.reportClickAvatar();
    },
    toArticlePage() {
      if (this.share) {
        return this.$links.toDownloadApp();
      }

      this.$router.push({
        name: 'article',
        params: {
          id: this.data.articleId
        },
        query: {
          report_yas: YAS.eventName.detailShow,
          report_param: {
            ART_ID: this.data.articleId,
            ATR_TYPE: this.data.sort === 2 ? 1 : 2
          }
        }
      });
      this.reportClickArticle();
    },
    reportClickArticle() {
      this.$store.dispatch('reportYas', {
        params: {
          appop: YAS.eventName.articleExpand,
          param: {
            I_INDEX: this.index,
            ARTICLE_ID: this.data.articleId,
            POS_ID: this.posId
          }
        }
      });
    },
    reportClickAvatar() {
      this.$store.dispatch('reportYas', {
        params: {
          appop: YAS.eventName.avatar,
          param: {
            AUTH_ID: this.data.authorUid,
            POS_ID: this.posId
          }
        }
      });
    }
  }
};
</script>

<style scoped lang="scss">
.article-item {
  width: 100%;
  padding: 5px;

  .article-item-main {
    background-color: #fff;
    border-radius: 2PX;
    position: relative;
    overflow: hidden;
  }

  .action-article {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1;
  }

  .article-long-icon {
    width: 36px;
    height: 36px;
    position: absolute;
    top: 18px;
    right: 18px;
    background-image: url("~statics/image/article/article-long-icon.png");
    background-size: 100% 100%;
  }

  .layer-image img {
    width: 100%;
    height: 100%;
  }

  .description {
    margin: 14px 20px;
    font-size: 24px;
    line-height: 36px;
    max-height: 72px;
    word-break: break-all;
    /* stylelint-disable */
    text-overflow: -o-ellipsis-lastline;
    text-overflow: ellipsis;
    /* stylelint-enable */
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    line-clamp: 2;
    -webkit-box-orient: vertical;
  }

  .attribution {
    height: 48px;
    margin: 20px;
    display: flex;
    justify-content: space-between;
  }

  .auther,
  .fav {
    display: flex;
    align-items: center;
  }

  .avatar {
    width: 20PX;
    height: 20PX;
    margin-right: 12px;
    display: block;
  }

  .name {
    max-width: 170px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .line {
    height: 10PX;
    width: 100%;
    background-color: #f0f0f0;
  }
}
</style>