MaterialIndex.vue 9.4 KB
<template>
  <view class="material-box">
    <template v-if="(moduleList && moduleList.length) || (imagelist && imagelist.length)">
      <!-- 轮播容器 -->
      <view class="swiper-carousel" @touchstart="starts" @touchend="ends">
        <view v-for="(item, index) in imagelist" :key="index" :class="['carousel-block', item.class]"
          @click.stop="handleClick(item)">
          <image class="image-block" :src="item.imgUrl" mode="aspectFill" />
          <view class="mask"></view>
        </view>
      </view>
      <!-- 标题 -->
      <view class="module-title flex">
        {{ moduleTitle}}
      </view>
      <!-- 指示点 -->
      <view class="module-dots flex">
        <view v-for="(i, index) in moduleList" :key="index" :class="['dot', { 'active': index == currentIndex }]">
        </view>
      </view>
    </template>
    <template v-else>
      <view class="empty-box">
        <image
          class="empty-img"
          src="@/assets/images/common/nothing.webp"
          mode="aspectFit"
        />
        <view class="empty-text">暂无数据</view>
      </view>
    </template>
  </view>
</template>

<script>
export default {
  name: 'MaterialIndex',
  props: {
    moduleList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      datalist: [],
      imagelist: [],
      // 定义位置样式:左、中、右
      style_object: [
        { index: 0, class: 'block-left' },
        { index: 1, class: 'block-center' },
        { index: 2, class: 'block-right' }
      ],
      ismove: 0,
      autoplay: true,
      interval: 4000,
      autoPlayTimer: null,
      endTimer: null,
      playStatus: true,
      currentIndex: 0
    }
  },
  computed: {
    moduleTitle() {
      return this.moduleList[this.currentIndex]?.materialName || ''
    }
  },
  watch: {
    moduleList: {
      handler(val) {
        this.getDatalist()
      },
      deep: true
    }
  },
  mounted() {
    this.getDatalist()
    if (this.autoplay) this.autoPlayFunction()
  },
  methods: {
    getDatalist() {
      this.datalist = this.moduleList
      const list = JSON.parse(JSON.stringify(this.style_object))
      const len = this.datalist.length

      if (len > 1 && this.autoplay) {
        this.autoPlayFunction()
      } else {
        if (this.autoPlayTimer) {
          clearTimeout(this.autoPlayTimer)
          this.autoPlayTimer = null
        }
      }

      if (!len) {
        this.imagelist = []
        return
      }
      if (len === 1) {
        this.imagelist = [
          { isToor: 1, class: 'block-center', imgUrl: this.datalist[0].materialUrl, isIndex: 0, index: 1 }
        ]
        return
      }
      if (len === 2) {
        this.imagelist = [
          { isToor: 1, class: 'block-center', imgUrl: this.datalist[0].materialUrl, isIndex: 0, index: 1 },
          { isToor: 2, class: 'block-right', imgUrl: this.datalist[1].materialUrl, isIndex: 1, index: 2 }
        ]
        this.currentIndex = 0
        return
      }
      
      const count = Math.min(3, len)
      this.imagelist = Array.from({ length: count }).map((_, i) => {
        let isIndex
        if (i === 0) isIndex = len - 1
        else if (i === 1) isIndex = 0
        else isIndex = 1
        
        const cfg = list[i]
        return {
          isToor: cfg.index,
          class: cfg.class,
          imgUrl: this.datalist[isIndex].materialUrl,
          isIndex: isIndex,
          index: cfg.index
        }
      })
      const ci = this.imagelist.findIndex(item => item.index === 1)
      if (ci !== -1) this.currentIndex = this.imagelist[ci].isIndex
    },
    autoPlayFunction() {
      if (!this.autoplay || this.datalist.length <= 1) return
      if (this.autoPlayTimer) clearTimeout(this.autoPlayTimer)
      this.autoPlayTimer = setTimeout(() => {
        if (!this.playStatus) {
          clearTimeout(this.autoPlayTimer)
          this.autoPlayTimer = null
          return
        }
        this.updateCarouselPositions('left')
        this.autoPlayFunction()
      }, this.interval)
    },
    updateCarouselPositions(direction = 'left') {
      const { imagelist, datalist } = this
      const len = datalist.length
      const list = JSON.parse(JSON.stringify(this.style_object))
      
      if (len === 2) {
        // 两张图特殊处理
        const a = this.imagelist[0]
        const b = this.imagelist[1]
        const aWasCenter = a.class === 'block-center'
        a.class = aWasCenter ? 'block-right' : 'block-center'
        a.isToor = a.class === 'block-center' ? 1 : 2
        
        b.class = aWasCenter ? 'block-center' : 'block-right'
        b.isToor = b.class === 'block-center' ? 1 : 2
        
        const centerItem = a.class === 'block-center' ? a : b
        this.currentIndex = centerItem.isIndex
      } else {
        if (direction === 'left') {
          this.currentIndex = (this.currentIndex + 1) % len
        } else {
          this.currentIndex = (this.currentIndex - 1 + len) % len
        }

        this.imagelist.forEach(item => {
          // 计算新的位置索引 0(Left), 1(Center), 2(Right)
          // Left: isToor --;
          const toor = direction === 'left'
            ? (item.isToor - 1) < 0 ? 2 : item.isToor - 1
            : (item.isToor + 1) > 2 ? 0 : item.isToor + 1
          
          item.isToor = toor
          item.class = list[toor].class
          
          // 动态更新图片逻辑:
          // 只有当卡片移动到"替补"位置时才更新内容,以保持动画连贯
          // 左滑 (Next): Right(2) -> Center(1) -> Left(0) -> Right(2)...
          // 原 Left(0) 变成 Right(2),它需要加载下下张图
          if (direction === 'left' && item.class === 'block-right') {
             item.isIndex = (this.currentIndex + 1) % len
             item.imgUrl = datalist[item.isIndex].materialUrl
          }
          // 右滑 (Prev): Left(0) -> Center(1) -> Right(2) -> Left(0)...
          // 原 Right(2) 变成 Left(0),它需要加载上上张图
          if (direction === 'right' && item.class === 'block-left') {
             item.isIndex = (this.currentIndex - 1 + len) % len
             item.imgUrl = datalist[item.isIndex].materialUrl
          }
        })
      }
    },
    starts(e) {
      this.ismove = e.touches[0].pageX
      this.playStatus = false
      if (this.endTimer) {
        clearTimeout(this.endTimer)
        this.endTimer = null
      }
    },
    ends(e) {
      if (this.moduleList.length <= 1) return
      const pageX = e.changedTouches[0].pageX
      const nums = this.ismove - pageX
      if (Math.abs(nums) < 30) return // 阈值防误触
      
      if (nums > 30) this.updateCarouselPositions('left')
      if (nums < -30) this.updateCarouselPositions('right')
      
      this.endTimer = setTimeout(() => {
        this.playStatus = true
        this.autoPlayFunction()
      }, this.interval)
    },
    handleClick(item) {
      if (item.class !== 'block-center') {
        // 点击两侧卡片也可以切换
        if (item.class === 'block-right') this.updateCarouselPositions('left')
        if (item.class === 'block-left') this.updateCarouselPositions('right')
        return
      }
      const data = this.moduleList[item.isIndex]
      this.$emit('itemClick', data, item.isIndex)
    }
  }
}
</script>

<style scoped lang="scss">
.material-box {
  width: 100%;
  height: 100%;
  text-align: center;
  position: relative;
  overflow: hidden;
}

.empty-box {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  color: #999;
  font-size: toRpx(28);
  
  .empty-img {
    width: toRpx(160);
    height: toRpx(160);
    margin-bottom: toRpx(16);
  }
}

.swiper-carousel {
  width: 100%;
  height: calc(100% - #{toRpx(90)});
  position: relative;
  margin-top: toRpx(10);
  perspective: 2000rpx; /* 3D透视 */
}

.carousel-block {
  position: absolute;
  top: 50%;
  width: 65%;
  height: 96%;
  border-radius: toRpx(32);
  background: #fff;
  overflow: hidden;
  /* 使用带回弹的贝塞尔曲线 */
  transition: all 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
  transform-style: preserve-3d;
  box-shadow: 0 toRpx(10) toRpx(30) rgba(0, 0, 0, 0.05);
  
  .image-block {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: toRpx(32);
    display: block;
  }
  
  .mask {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.3);
    opacity: 0;
    transition: opacity 0.6s;
    pointer-events: none;
  }
}

/* 中心卡片 */
.block-center {
  left: 50%;
  transform: translate3d(-50%, -50%, 0) scale(1);
  z-index: 20;
  opacity: 1;
  box-shadow: 0 toRpx(30) toRpx(60) rgba(44, 102, 247, 0.35); /* 品牌色投影 */
  
  .mask {
    opacity: 0;
  }
}

/* 左侧卡片 */
.block-left {
  left: 30%;
  transform: translate3d(-50%, -50%, -100rpx) scale(0.85) rotateY(15deg);
  z-index: 10;
  opacity: 0.8;
  
  .mask {
    opacity: 1;
  }
}

/* 右侧卡片 */
.block-right {
  left: 70%;
  transform: translate3d(-50%, -50%, -100rpx) scale(0.85) rotateY(-15deg);
  z-index: 10;
  opacity: 0.8;
  
  .mask {
    opacity: 1;
  }
}

.module-title {
  color: #333;
  font-size: toRpx(28);
  font-weight: 600;
  margin-top: toRpx(10);
  justify-content: center;
}

.module-dots {
  position: absolute;
  bottom: toRpx(20);
  right: toRpx(98);
  gap: toRpx(12);

  .dot {
    width: toRpx(12);
    height: toRpx(12);
    opacity: 1;
    background: #0007400d;
    border-radius: 100%;
  }

  .active {
    background: #000740;
  }
}
</style>