product-group-item.vue 5.18 KB
<template>
  <div
    class="product-item"
    :class="{single}">
    <div class="product-content" @click="onClick">
      <ImageFormat :lazy="lazy" class="product-image" :src="product.productImage" :width="136" :height="180"></ImageFormat>
      <div class="product-info">
        <p class="product-name">{{product.productName}}</p>
        <div class="price">
          ¥{{product.salesPrice}}
          <div class="btn-right btn-buy hover-opacity">
            <span class="iconfont icon-cart"></span>购买
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {createNamespacedHelpers} from 'vuex';
import YAS from 'utils/yas-constants';
const {mapActions} = createNamespacedHelpers('product');

export default {
  name: 'ProductGroupItem',
  props: {
    single: Boolean,
    lazy: Boolean,
    product: Object,
    share: Boolean,
    thumb: Boolean,
    posId: Number,
    articleId: [String, Number],
    index: Number
  },
  data() {
    return {
      posting: false,
      favorite: this.product.favorite
    };
  },
  watch: {
    'product.favorite'(val) {
      this.favorite = val;
    }
  },
  computed: {
    favClass() {
      return {
        'btn-is-fav': this.favorite,
        loading: this.posting,
        invisible: this.thumb
      };
    },
    favText() {
      return this.favorite ? '已收藏' : '收藏';
    },
  },
  methods: {
    ...mapActions(['postProductFav']),
    async onFav() {
      if (this.share) {
        return this.$links.toDownloadApp();
      }
      if (this.posting) {
        return;
      }

      this.posting = true;
      const favorite = !this.favorite;

      const result = await this.postProductFav({
        productId: this.product.productId,
        productSkn: this.product.productSkn,
        favorite,
        productType: this.product.productType
      });

      this.posting = false;

      if (result.code === 200) {
        if (favorite) {
          this.reportFavProduct(this.product.productSkn, this.product.productType);
          this.prompt = this.$grassPrompt({
            img: this.product.productImage,
            title: '收藏成功',
            desc: '可在 [我的-商品收藏] 页面查看',
            onClick: () => {
              this.$yoho.goPage('go.fav', {
                favType: 0
              });
            }
          }, 3000);
        } else {
          this.prompt && this.prompt.hide();
          this.$createToast({
            txt: '取消收藏成功',
            type: 'success',
            time: 1000
          }).show();
        }

        this.favorite = favorite;
      } else {
        this.$createToast({
          txt: result.message || '服务器开小差了',
          type: 'warn',
          time: 1000
        }).show();
      }
    },
    onClick(e) {
      if (e.timeStamp - this._lastTime < 1200) {
        return;
      }

      this._lastTime = e.timeStamp;

      let skn = this.product.productSkn;

      if (!skn) {
        return;
      }

      if (this.product.productType === 1) {
        this.$yoho.goProductDetail(skn);
      } else if (this.product.productType === 2) {
        this.$yoho.goUfoProductDetail(skn);
      }

      this.reportProduct(skn, this.product.productType);
    },
    reportProduct(skn, type) {
      this.$store.dispatch('reportYas', {
        params: {
          appop: YAS.eventName.productClick,
          param: {
            POS_ID: this.posId,
            ARTICLE_ID: this.articleId,
            PRD_SKN: skn,
            PRD_TYPE: type
          }
        }
      });
    },
    reportFavProduct(skn, type) {
      this.$store.dispatch('reportYas', {
        params: {
          appop: YAS.eventName.productFav,
          param: {
            POS_ID: this.posId,
            ARTICLE_ID: this.articleId,
            PRD_SKN: skn,
            PRD_TYPE: type,
            I_INDEX: this.index
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.product-item {
  margin-top: 20px;
  margin-right: 20px;
  margin-left: 30px;
  height: 180px;
  width: 580px;
  background-color: #fff;
  box-shadow: 0 5px 10px 0 rgba(107, 95, 95, 0.2);
  display: inline-block;
  padding: 10px 20px;
  white-space: initial;

  &:last-child {
    margin-right: 50px;
  }

  &.single {
    width: 670px;
  }
}

.product-content {
  display: flex;
  width: 100%;
  height: 100%;
}

.product-image {
  width: 136px;
  height: 180px;
  margin-top: -30px;
}

.product-info {
  padding-left: 20px;
  flex: 1;

  .product-name {
    font-size: 24px;
    color: #9b9b9b;
    letter-spacing: -0.25PX;
    height: 104px;
    display: flex;
    align-content: center;
  }

  .price {
    font-size: 32px;
    color: #d0021b;
    letter-spacing: -0.34PX;
  }
}

.btn-right {
  float: right;
  margin-right: -40px;
  width: 120px;
  height: 50px;
  padding: 0;
  font-size: 24px;
  background-size: contain;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  letter-spacing: 0.2PX;
}

.btn-buy {
  background-color: #ce0a24;

  .iconfont {
    font-size: 30px;
    margin-right: 5px;
    font-weight: 700;
  }
}

.btn-fav {
  background-color: #444;

  &.btn-is-fav {
    background-color: #b0b0b0;
  }

  & /deep/ .iconfont {
    margin: 0;
  }
}
</style>