CountDown.vue 4.85 KB
<template>
  <div class="count-down">
    <div class="bg">
      <img src="https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/timer-bg.png" />
    </div>
    <div :class="['timer-title', { 'timer-not-started': detail.activityStatus == 1 }]">
      <img :src="`https://img02.glsx.com.cn/weapp/resource/scrm-mobile/crowdfunding/timer-${detail.activityStatus == 1 ? 1 : 2}.png`" />
    </div>
    <div :class="['time-box text-center', { 'not-started': detail.activityStatus == 1 }, { finished: finished }]">
      <template v-if="timeFlag">
        <template v-if="finished">{{ finished }}</template>
        <template v-else>
          <template v-if="detail.activityStatus == 1">{{ new Date(iosTimeFormat(detail.startTime)).format('M月d') }}日开始,还差</template>
          <template v-if="activityTime.day">
            <div class="time flex">
              <span class="text-center flex">{{ activityTime.day }}</span>
              <div class="unit">天</div>
            </div>
          </template>
          <template v-for="(i, index) in activityTime.dayList">
            <div class="time flex" :key="index">
              <span class="text-center">{{ i.text }}</span>
              <div class="unit">{{ i.unit }}</div>
            </div>
          </template>
        </template>
      </template>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
export default {
  name: 'CountDown',
  props: {
    detail: {
      type: Object,
      default: () => { }
    },
  },
  data () {
    return {
      activityTime: {},
      timeFlag: false,
      finished: ''
    }
  },

  mounted () {
    this.init()
  },
  methods: {
    init () {
      this.timeFlag = false
      let { detail } = this
      this.countDown([detail.startTime, detail.endTime])
      this.hideLoading()
    },
    countDown (params) {
      let {
        activityStatus
      } = this.detail
      // activityStatus 1:未开始,2进行中,3已结束
      const addZero = i => i < 10 ? `0${i}` : `${i}`
      let [activityStartTime, activityEndTime] = params
      const start = this.iosTimeFormat(activityStartTime)
      const end = this.iosTimeFormat(activityEndTime)
      if ((new Date().getTime() > new Date(end).getTime()) || activityStatus == 3) {
        clearTimeout(this.time)
        this.timeFlag = true
        this.finished = '活动已结束'
        if (activityStatus != 3) this.$emit('refresh', 3)
        return
      }
      if (new Date().getTime() >= new Date(start).getTime() && activityStatus != 2) {
        this.$emit('refresh', 2)
        return
      }
      let starTime = new Date()
      let endTime = new Date(activityStatus == 2 ? end : start)
      let lefttime = parseInt((endTime.getTime() - starTime.getTime()) / 1000)
      let d = parseInt(lefttime / (24 * 60 * 60))
      let h = parseInt(lefttime / (60 * 60) % 24)
      let m = parseInt(lefttime / 60 % 60)
      let s = parseInt(lefttime % 60)
      h = addZero(h)
      m = addZero(m)
      s = addZero(s)
      let type = {
        1: '距开始',
        2: '距结束'
      }[activityStatus]
      if (this.time) clearTimeout(this.time)
      this.time = setTimeout(() => {
        let dayList = [{
          text: h,
          unit: '时'
        }, {
          text: m,
          unit: '分'
        }, {
          text: s,
          unit: '秒'
        }]
        let activityTime = {
          dayList,
          type
        }
        if (d > 0) activityTime.day = addZero(d)
        this.activityTime = activityTime
        this.timeFlag = true
        this.countDown(params)
      }, 1 * 1000)
    }
  }
}
</script>

<style scoped lang="scss">
@import '@/assets/css/crowdfunding.scss';
.count-down {
  position: relative;
  .bg {
    position: absolute;
    width: calc(100% + 32px);
    left: -16px;
    top: 0;
    z-index: -1;
    img {
      width: 100%;
      height: 200px;
    }
  }
}
.timer-title {
  padding-top: 14px;
  height: 19px;
  img {
    width: 105px;
  }
}
.timer-not-started {
  img {
    width: 128px;
  }
}
.time-box {
  height: 56px;
  border-radius: 4px;
  margin: 11.5px 0;
  .time {
    span {
      width: 34px;
      height: 32px;
      background: #fff;
      border-radius: 4px;
      font-size: 20px;
      color: #fa2c18;
      font-weight: 600;
    }

    .unit {
      font-weight: 500;
      font-size: 20px;
      color: #fff;
      margin: 0 5px 0 3px;
    }
  }
}
.not-started {
  font-size: 14px;
  font-weight: 600;
  color: #fff;
  border: 1px dashed #fff;
  box-sizing: border-box;
  .time {
    margin-left: 2px;
    span {
      width: 26px;
      height: 26px;
      background: #ae0404;
      box-shadow: inset 1px 1px 2px 0px rgba(111, 0, 0, 0.5);
      border-radius: 3px;
      color: #fff;
      font-size: 18px;
    }
    .unit {
      font-size: 14px;
    }
  }
}
.finished {
  font-size: 20px;
  font-weight: 600;
  color: #fff;
  border: 1px dashed #ffffff;
  box-sizing: border-box;
}
</style>