grass-artilces.vue 3.86 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,
      imagePreview: null,
      isShowPreview: false,
    };
  },
  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.imagePreview = this.$createImagePreview({
        imgs: this.imgs,
        initialIndex: this.initialIndex,
        loop: false,
        speed: 500,
        onChange: (i) => {
          this.initialIndex = i;
        },
        onHide: () => {
          console.log('hide');
        }
      }, (h) => {
        return h('a', {
          class: {
            'image-preview-custom-close': true
          },
          slot: 'header',
          on: {
            click: () => {
              if (this.imagePreview) {
                this.imagePreview.hide();
                this.imagePreview = null;
                this.isShowPreview = false;
              }

            }
          }
        }, '关 闭',);
      });

      this.imagePreview.show();
      this.isShowPreview = true;

      // 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_ID: articleId,
        I_INDEX: +index + 1
      };

      this.$store.dispatch('reportYas', {
        params: {
          appop: 'XY_GDS_DT_ARTICLE_C',
          param,
        }
      });
    },
    closeImagePreview() {
      if (this.imagePreview && this.isShowPreview) {
        this.imagePreview.hide();
        this.imagePreview = null;
        this.isShowPreview = false;
      }
    }
  }
};
</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>