timepicker.js 5.7 KB
// component/time-picker/index.js
let utils = require("../../utils/util.js")
let method = require("../../utils/reuqest.js")
const app = getApp()
Component({
  data: {
    deliveryTime: "",
    selfData: [], //自提日期
    showDelivery: false,
    list: [], //自提时间数组
    nowDay: 0,
    selectTime: '', //自提时间
    selectTimeIndex: '',
    selectDay: 0,
  },
  pageLifetimes: {
    show: function () {
      this.getDliveryTime()
    }
  },
  properties: {
    timeLists: {
      type: Array,
      value: true
    }
  },
  /**
   * 组件的方法列表
   */
  lifetimes: {
    created() {
      let _this = this;
      if (_this.data.nowDay == 0) {
        method.getRequest("/myUser/period/" + '1', data => {
          //let list=this
          if (data.statusCode == 0) {
            _this.setData({
              list: data.data
            })
          }
          console.log(_this.data.nowDay, 'now')
          console.log(_this.data.deliveryTimeList, 'list')
        })
      } else {
        method.getRequest("/myUser/period/" + '2', data => {
          if (data.statusCode == 0) {
            _this.setData({
              list: data.data
            })
          }
          console.log(_this.data.nowDay, 'now')
          console.log(_this.data.deliveryTimeList, 'list')
        })
      }

    },
    attached() {
      console.log("在组件实例进入页面节点树时执行")
    },
    ready() {

      console.log("在组件在视图层布局完成后执行")
    },
    moved() {
      console.log("在组件实例被移动到节点树另一个位置时执行")
    },
    detached() {
      console.log("在组件实例被从页面节点树移除时执行")
    },
    error() {
      console.log("每当组件方法抛出错误时执行")
    },
  },
  methods: {
    getnowDay(e) {
      this.setData({
        nowDay: e.currentTarget.dataset.index
      })
      console.log(this.data.nowDay, 'nowday')
      let _this = this;
      if (_this.data.nowDay == 0) {
        method.getRequest("/myUser/period/" + '1', data => {
          if (data.statusCode == 0) {
            _this.setData({
              list: data.data
            })
          }
          console.log(_this.data.nowDay, 'now')
          console.log(_this.data.deliveryTimeList, 'list')
        })
      } else {
        method.getRequest("/myUser/period/" + '2', data => {
          //let list=this
          if (data.statusCode == 0) {
            _this.setData({
              list: data.data
            })
          }
          console.log(_this.data.nowDay, 'now')
          console.log(_this.data.deliveryTimeList, 'list')
        })
      }
    },
    clickTime(e) {
      let selfData = this.data.selfData[this.data.nowDay]
      let id = e.currentTarget.dataset.id;
      let index = e.currentTarget.dataset.index;
      let _this = this;
      let deliveryTime = selfData.year + '-' + selfData.month + '-' + selfData.day + ' '
      let nowDay = this.data.nowDay
      this.setData({
        selectTimeIndex: index,
        // selectTime: selectTime,
        deliveryTime: deliveryTime,
        selectDay: nowDay
      })
      this.data.list.map((item, index) => {
        if (item.id === id) {
          let times = item.start + '-' + item.end
          _this.triggerEvent('selectTime', {
            time: times,
            flag: false,
            timeId: item.id,
            deliveryTime: this.data.deliveryTime
          })
          wx.setStorageSync('selecClick', true)
        }
      })
    },
    //计算配送时间
    getDliveryTime() {
      let that = this;
      let deliveryTimeList = [];
      let date = new Date();
      this.setData({
        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"
      //   //         );
      //   //       }
      //   //     }
      //   //   }
      //   // }
      //   method.getRequest("/myUser/period/" + '1', data => {
      //     if (data.statusCode == 0) {
      //       that.setData({
      //         deliveryTimeList: data.data
      //       })
      //       timeList = data.data
      //     }
      //   })
      //   return timeList;
      // }
    }
  }
})