add-date.js 2.66 KB
// pack-book/components/book/time/add-date.js
import util from '../../../../utils/util'
import PubSub from '../../../../utils/PubSub';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    type:Number,
    time:Number,
  },
  lifetimes: {
    // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
    attached: function () {},
    moved: function () { },
    detached: function () { },
  },
  observers:{
    time:function(time){
      if(time>0){
        this.initView()
      }
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    checked:-1,
    list:[
      {
        time:new Date()/1000,
      },
      {
        time:new Date()/1000+86400,
      },
      {
        time:0,
      }
    ],

    calendarMaskLoad: false,
    calendarMask: false,
  },
  checked:-1,
  /**
   * 组件的方法列表
   */
  methods: {
    initView(){
      //console.log('add-date',this.data.nowtime,this.data.today)
      setTimeout(() => {
        this.setData({
          calendarMaskLoad: true,
        })
      }, 1000);
      this.HHIISS=util.unixtoTime(this.data.time,'HH:II:SS')
      this.initChecked(this.data.time)
      setTimeout(() => {
        this.trigger()
      }, 800);
    },
    initChecked(nowtime){
      let nowStr=util.unixtoTime(nowtime,"YYYY-MM-DD");
      let list=this.data.list
      this.checked=2
      this.checkedTime=nowtime
      if(nowStr==util.unixtoTime(list[0].time,"YYYY-MM-DD")){
        this.checked=0
      }else if(nowStr==util.unixtoTime(list[1].time,"YYYY-MM-DD")){
        this.checked=1
      }
      this.setData({
        checked:this.checked,
        [`list[2].time`]:nowtime,
      })

    },
    selectedValue(e){
      this.checked=util.attr(e,'value')
      this.checkedTime=this.data.list[this.checked].time
      this.setData({
        checked:this.checked,
      })
      this.trigger()
    },
    trigger(){
      let date=util.unixtoTime(this.checkedTime,"YYYY-MM-DD")
      let dur_time=util.timetouUnix(date+' '+this.HHIISS);
      let data={
        type:this.data.type,
        date:date,
        dur_time:dur_time,
        dateIndex:this.checked,
      }
      console.log('add-date.trigger',data)
      // this.triggerEvent('event',{
      //   checked:this.checked,
      //   checkedTime:this.checkedTime,
      // })
      PubSub.emit('add-time-dur',data)
    },
    calendarShow(e){
      this.setData({
        calendarMask: true
      })
    },
    calendarClose(){
      this.setData({
        calendarMask: false,
      })
    },
    dateChange(event){
      let cktime = util.timetouUnix(event.detail.date)
      this.initChecked(cktime)
      this.trigger()
      this.calendarClose();
    },
  }
})