grass-artilces.vue 3.06 KB
<template>
  <!--推荐搭配9宫格图片-->
  <div class="articles-wrapper" v-if="articleList">
    <div class="articles-container">
      <p class="title">推荐搭配({{articleTotal}})</p>
      <ul>
        <li v-for="(item, index) in articleList"
            @click="showAricleImages"
            :data-id="item.articleId"
            :data-index="item.imageindex"
            :data-idx="index">
          <img v-lazy="item.coverImage">
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import {createNamespacedHelpers} from 'vuex';

const {mapActions} = createNamespacedHelpers('product');

export default {
  name: 'grassArtilces',
  props: {
    listdata: {
      type: Object,
      default: {}
    },
    productId: {
      type: Number
    }
  },
  data() {
    return {
      imgs: [],
      initialIndex: 0,
      articleTotal: 0
    };
  },
  computed: {
    articleList() { // 获取到listdata之后处理数据
      if (this.listdata && this.listdata.list) {
        this.imgs = this.listdata.imageList || [];
        this.articleTotal = this.listdata.list.length || 0;
      }
      return this.listdata.list;
    }
  },
  methods: {
    ...mapActions(['fetchGrassArticleImages']),
    showAricleImages(e) { // 点击推荐小图展示文章内大图
      this.initialIndex = parseInt(e.currentTarget.dataset.index, 10) || 0;

      this.$createImagePreview({
        imgs: this.imgs,
        initialIndex: this.initialIndex,
        loop: false,
        speed: 500,
        onChange: (i) => {
          this.initialIndex = i;
        }
      }).show();

      // yas
      /**
       * 图片点击时
       * XY_GDS_DT_ARTICLE_C
       * 1.PRD_SKN:商品SKN
       * 2.ARTICLE:文章ID
       * 3.I_INDEX:顺序号
       */
      let articleId = e.currentTarget.dataset.id;
      let index = e.currentTarget.dataset.idx;
      let param = {
        PRD_SKN: this.productId,
        ARTICLE: articleId,
        I_INDEX: +index + 1
      };

      this.$store.dispatch('reportYas', {
        params: {
          appop: 'XY_GDS_DT_ARTICLE_C',
          param,
        }
      });
    },
  }
};
</script>

<style lang="scss" scoped>
  .articles-wrapper {
    position: relative;
    width: 100%;
    margin: 40px 0 0 0;

    .articles-container {
      position: relative;
      padding: 0 40px;
      overflow: hidden;

      .title {
        font-size: 36px;
        line-height: 44px;
        font-weight: 600;
      }

      ul {
        position: relative;
        width: 100%;
        margin: 20px auto;
        list-style: none;
        padding: 0;
        overflow: hidden;

        li {
          display: flex;
          justify-content: center;
          align-items: center;
          float: left;
          width: 222px;
          height: 222px;
          margin: 0 0 2px 0;
          box-sizing: border-box;
          border-radius: 8px;
          overflow: hidden;

          img {
            width: 100%;
            float: left;
            margin: 0 auto;
          }
        }

        li:nth-child(3n + 2) {
          margin: 0 2px;
        }
      }
    }
  }

</style>