index.js 4 KB
// pages/crowdfunding/components/countdown/index.js
import { iosTimeFormat, centsToyuan } from "~/utils/format";

var timer = null;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    detail: {
      type: Object,
      value: {},
      observer: function (n) {
        if (n.startTime || n.endTime) {
          let expressFee = n.expressFee || 0,
            [price, decimal] = centsToyuan(n.retailPrice + expressFee)
              .toString()
              .split(".");
          if (!decimal) decimal = "00";
          n.price = price;
          n.decimal = decimal;
          this.setData({ activityDate: [n.startTime, n.endTime], detail: n });
          this.countDown();
        }
      },
    },
    groupInfo: {
      type: Object,
      value: () => {},
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    activityTime: "",
    timeFlag: false,
    // time: "",
    finished: "",
    classList: {
      1: "activity_no_start",
      2: "",
      3: "activity_end",
    },
    startDate: "",
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 倒计时
    countDown(params) {
      let _this = this;
      let { activityStatus } = _this.data.detail;
      // activityStatus 1:未开始,2进行中,3已结束
      let [activityStartTime, activityEndTime] = this.data.activityDate; //params
      const start = iosTimeFormat(activityStartTime);
      const end = iosTimeFormat(activityEndTime);
      if (
        new Date().getTime() > new Date(end).getTime() ||
        activityStatus == 3
      ) {
        clearTimeout(timer);
        _this.setData({
          timeFlag: true,
          finished: "活动已结束",
        });
        // if (activityStatus != 3) {
        //   _this.$emit('refresh', 3)
        // }
        return false;
      }
      // if (new Date().getTime() >= new Date(start).getTime() && activityStatus != 2) {
      //   this.$emit('refresh', 2)
      //   return false
      // }

      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 = _this.addZero(h);
      m = _this.addZero(m);
      s = _this.addZero(s);
      // let type = {
      //   1: "距开始",
      //   2: "距结束",
      // }[activityStatus];
      // if (_this.data.time) {
      //   clearTimeout(this.data.time);
      // }

      // let time = setTimeout(() => {
      // let dayList = [
      //   {
      //     text: h,
      //     unit: "时",
      //   },
      //   {
      //     text: m,
      //     unit: "分",
      //   },
      //   {
      //     text: s,
      //     unit: "秒",
      //   },
      // ];
      let activityTime = {
        h,
        m,
        s,
        // type,
      };
      if (d > 0) {
        activityTime.day = _this.addZero(d);
      }

      _this.setData({
        activityTime: activityTime,
        timeFlag: true,
        // time: time,
        startDate: `${endTime.getMonth() + 1}月${endTime.getDate()}日`,
      });
      this.countSeconds();
      // _this.countDown(params)
      // }, 1 * 1000)
    },

    countSeconds() {
      timer = setTimeout(() => {
        var activityTime = this.data.activityTime;
        var seconds = activityTime.s - 1;
        activityTime.s = this.addZero(seconds);
        this.setData({ activityTime });
        if (seconds <= 0) {
          clearTimeout(timer);
          this.countDown();
        } else {
          this.countSeconds();
        }
      }, 1 * 1000);
    },
    // // 兼容ios时间
    // iosTimeFormat(time) {
    //   if (!time) {
    //     return ''
    //   }
    //   return time.replace(/-/g, '/')
    // },

    // 时间补0
    addZero(i) {
      return i < 10 ? `0${i}` : `${i}`;
    },
  },
  detached() {
    if (timer) {
      clearTimeout(timer);
      timer = null;
    }
  },
});