article-item-slide.vue 5.58 KB
<template>
  <div class="article-item-slide" :style="slideItemStyle" @touchend="praiseArticle">
    <Slide
      class="article-item-slide-com"
      ref="slide"
      :key="`slide${data.articleId}`"
      :data="data.blockList"
      :refresh-reset-current="false"
      :initial-index="slideIndex - 1"
      :threshold="0.2"
      :auto-play="false"
      :loop="false"
      :options="scrollOption"
      @change="onChange">
      <SlideItem class="slide-item" :style="slideItemStyle" v-for="(item, inx) in data.blockList" :key="inx">
        <ArticleItemSlideImage
          v-if="!thumb || inx === 0"
          :lazy="true"
          :data="item"
          :thumb-size="firstBlockSize"
          :share="share">
        </ArticleItemSlideImage>
      </SlideItem>
    </Slide>
    <div class="pages" v-if="data.blockList.length > 1">
      {{slideIndex}}/{{data.blockList.length}}
    </div>
    <div v-show="onAnimation" class="praise-lottie">
      <WidgetLottie ref="lottie" :options="lottieOptions"></WidgetLottie>
    </div>
  </div>
</template>

<script>
import {first, round, findIndex} from 'lodash';
import {Slide} from 'cube-ui';
import ArticleItemSlideImage from './article-item-slide-image';
import {mapState, createNamespacedHelpers} from 'vuex';
import lottieJson from '../../../../statics/lottie/detail-heart.json';
const {mapMutations} = createNamespacedHelpers('article');

const dblclickDdelay = 200;

export default {
  name: 'ArticleItemSlide',
  props: {
    data: {
      type: Object,
      default() {
        return {};
      }
    },
    slideIndex: Number,
    lazy: {
      type: Boolean,
      default: true
    },
    type: String,
    thumb: Boolean,
    share: Boolean
  },
  data() {
    return {
      scrollOption: {
        eventPassthrough: 'vertical'
      },
      lottieOptions: {
        animationData: lottieJson
      },
      onAnimation: false,
      singleImage: {},
      showSingleImage: true,
      articleId: 0
    };
  },
  computed: {
    ...mapState(['yoho']),
    slideItemStyle() {
      const {width, height} = this.firstBlockSize;

      if (width && height) {
        return {
          width: `${round(width, 2)}rem`,
          height: `${round(height, 2)}rem`,
        };
      }
    },
    firstBlockSize() {
      const allowScale = [4 / 3, 3 / 4];
      const {width, height} = first(this.data.blockList) || {};

      if (width && height) {
        let scale = width / height;

        const index = findIndex(allowScale, o => {
          return Math.abs(scale - o) < 0.01;
        });

        if (index < 0) {
          scale = 1;
        }

        return {
          width: 750 / 40,
          height: (750 / 40) / scale,
        };
      }
      return {};
    }
  },
  created() {
    this.showSingleImage = this.data.blockList.length === 1;
  },
  methods: {
    ...mapMutations(['CHANGE_ARTICLE_LIST_SLIDE']),
    onChange(inx) {
      if (this.type) {
        this.CHANGE_ARTICLE_LIST_SLIDE({articleId: this.data.articleId, index: inx + 1, type: this.type});
      } else {
        this.$emit('change', {index: inx + 1});
      }
    },
    onOriSuccess() {
      if (this.data.blockList.length > 1) {
        this.showSingleImage = false;
      }
    },
    dotClass(current, inx) {
      const offset = 1;
      const len = this.data.blockList.length;

      let left = current - offset;

      let right = current + offset;

      if (left < 0) {
        const offsetRight = 0 - left;

        if (right + offsetRight > len - 1) {
          right = len - 1;
        } else {
          right += offsetRight;
        }
        left = 0;
      } else if (right > len - 1) {
        const offsetLeft = right - (len - 1);

        if (left - offsetLeft < 0) {
          left = 0;
        } else {
          left -= offsetLeft;
        }
        right = len - 1;
      }

      return {
        active: current === inx,
        more: !(inx >= left && inx <= right),
        hide: left - inx > 1 || inx - right > 1
      };
    },
    async praiseArticle(e) {
      if (e.timeStamp - this._preTimeStamp < dblclickDdelay) {
        if (this.share) {
          return this.$links.toDownloadApp();
        }

        const user = await this.$sdk.getUser();

        if (user && user.uid) {
          this.onAnimation = true;
          this._lottieTimer && clearTimeout(this._lottieTimer);
          this.$refs.lottie.play();
          this._lottieTimer = setTimeout(() => {
            this.$refs.lottie.stop();
            this.onAnimation = false;
          }, 1000);

          this.$emit('on-praise');
        } else {
          this.$sdk.goLogin();
        }
      }

      this._preTimeStamp = e.timeStamp;
    }
  },
  components: {Slide, SlideItem: Slide.Item, ArticleItemSlideImage}
};
</script>

<style lang="scss" scoped>
.article-item-slide-wrap {
  position: relative;
}

.article-item-slide {
  position: relative;
  width: 750px;
  overflow: hidden;

  & /deep/ .cube-slide-dots {
    display: none;
  }

  .article-item-slide-com {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
  }
}

.slide-item {
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
}

.pages {
  position: absolute;
  z-index: 1;
  left: 30px;
  bottom: 30px;
  width: 80px;
  height: 48px;
  background: rgba($color: #000, $alpha: 0.5);
  border-radius: 24px;
  padding-left: 2px;
  color: #fff;
  font-size: 24px;
  box-sizing: border-box;
  letter-spacing: 4px;
  display: flex;
  line-height: 24px;
  justify-content: center;
  align-items: center;
}

.praise-lottie {
  width: 200px;
  height: 388px;
  position: absolute;
  left: calc(50% - 100px);
  top: calc(50% - 260px);
}
</style>