timepicker.js 3.0 KB
// component/time-picker/index.js
let utils=require("../../utils/util.js")
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
 
  },
 
  /**
   * 组件的初始数据
   */
  data: {
    deliveryTime: "",
    selfData: [], //自提日期
    showDelivery: false,
    deliveryTimeList: [{
      day: "",
      timeList: ""
    }],
    nowDay: 0,
    selectTime: '',
    selectTimeIndex: '',
    selectDay: 0
  },
  pageLifetimes: {
    show: function() {
      this.getDliveryTime()
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getnowDay(e) {
      this.setData({
        nowDay: e.currentTarget.dataset.index
      })
    },
    clickTime(e) {
      let selfData = this.data.selfData[this.data.nowDay]
      let index = e.currentTarget.dataset.index
      let selectTime = this.data.deliveryTimeList[this.data.nowDay].timeList[index]
      let deliveryTime = selfData.year + '-' + selfData.month + '-' + selfData.day + ' ' + selectTime
      let nowDay = this.data.nowDay
      this.setData({
        selectTimeIndex: index,
        selectTime: selectTime,
        deliveryTime: deliveryTime,
        selectDay: nowDay
      })
      this.triggerEvent('selectTime', {
        deliveryTime: this.data.deliveryTime
      })
    },
    //计算配送时间
    getDliveryTime() {
      let deliveryTimeList = [];
      let date = new Date();
        this.setData({
          selfData: utils.getDates(5)
          // selfData: utils.getDates(5)
        })
      //获取时间段
      let timeDate = new Date(date.getTime());
      let todayList = getTimeList(timeDate.getHours(), 1);
      deliveryTimeList.push({
        timeList: todayList
      });
      let nextDayList = getTimeList(9, 0);
      for (let i = 1; i < 5; i++) {
        deliveryTimeList.push({
          timeList: nextDayList
        });
      }
      this.triggerEvent('selectTime', {
        deliveryTime: this.data.deliveryTime
      })
      this.setData({
        deliveryTimeList: []
      })
      this.setData({
        deliveryTimeList: deliveryTimeList
      })
      // today 1 是今天 0 不是今天
      function getTimeList(hour, today) {
        let timeList = [];
        let startTime = hour <= 9 ? 9 : hour;
        for (let i = 0; i < 8; i++) {
          if (((hour <= 9) && (startTime + 2 * i >21)) || ((hour > 9) && (startTime + 2 * i >= 21))) {
            break;
          } else {
            if (hour <= 9) {
              timeList.push(
                startTime + 2 * i + ":00" + "-" + (startTime + 2 * (i + 1)) + ":00"
              );
            } else {
              if (startTime % 2 === 0) {
                timeList.push(
                  startTime + 2 * i + 1 + ":00" + "-" + (startTime + 2 * i + 3) + ":00"
                );
              } else {
                console.log(i,'i')
                timeList.push(
                  startTime + 2 * i +2  + ":00" + "-" + (startTime + 2 * i + 4) + ":00"
                );
              }
            }
          }
        }
        return timeList;
      }
    }
  }
})