add-date.js 2.07 KB
// pack-book/components/book/time/add-date.js
import util from '../../../../utils/util'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    nowtime:Number,
  },
  lifetimes: {
    // 生命周期函数,可以为函数,或一个在methods段中定义的方法名
    attached: function () {
      this.initView()
    },
    moved: function () { },
    detached: function () { },
  },
  /**
   * 组件的初始数据
   */
  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.initChecked(this.data.nowtime)

    },
    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,
      })
      this.trigger()
    },
    selectedValue(e){
      this.checked=util.attr(e,'value')
      this.checkedTime=this.data.list[this.checked].time
      this.setData({
        checked:this.checked,
      })
      this.trigger()
    },
    trigger(){
      this.triggerEvent('event',{
        checked:this.checked,
        checkedTime:this.checkedTime,
      })
    },
    calendarShow(e){
      this.setData({
        calendarMask: true
      })
    },
    calendarClose(){
      this.setData({
        calendarMask: false,
      })
    },
    dateChange(event){
      let cktime = util.timetouUnix(event.detail.date)
      this.initChecked(cktime)
      this.calendarClose();
    },
  }
})