datePicker.js 7.63 KB
// components/datePicker/datePicker.js

const util = require('../../utils/util.js')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    dateList: {
      type: Array,
      value: [],
      observer: function(newVal, oldVal) {
        if (newVal) {
          this.setData({
            time: this.pushDay(this.data.today.getMonth() + 1)
          })
        }
      }
    },
    show: {
      type: Boolean,
      value: false,
      observer: function(newVal, oldVal) {
        if (newVal) {
          this.open()
        }
      }
    },
    count: {
      type: Number,
      value: 3
    },
    date: {
      type: String,
      value: new Date().format('yyyy-MM-dd'),
      observer: function(newVal, oldVal) {
        let today = this.data.date.split('-');
        if(today[0] == ""){
          today=(new Date()).format('yyyy-MM-dd').split("-")
        }
        this.setData({
          today: new Date(today[0], today[1] - 1, today[2]),
          day: `${today[0]}年\n${today[1]}月`
        })
        wx.canIUse('nextTick') ? wx.nextTick(() => this.setData({
          time: this.pushDay(this.data.today.getMonth() + 1)
        })) : setTimeout(() => this.setData({
          time: this.pushDay(this.data.today.getMonth() + 1)
        }), 100)
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    up: false,
    height: 0,
    day: '',
    time: [],
    today: null,
    cookie: null,
    showRight: false,
    stop: ''
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 禁止滑动
     */
    noScroll() {},
    /**
     * 显示
     */
    open() {
      const query = wx.createSelectorQuery().in(this)
      wx.canIUse('nextTick') ? wx.nextTick(() => this.setHeight(query)) : setTimeout(() => this.setHeight(query), 100)
      if (this.data.today.format('yyyy-MM') != new Date().format('yyyy-MM')) this.setData({showRight: true})
      let time = new Date()
      time.setMonth(time.getMonth() - (this.data.count - 1))
      this.setData({stop: time.format('yyyy-MM')})
    },
    /**
     * 设置日期高度
     */
    setHeight(el) {
      el.select('.day').boundingClientRect(res => {
        this.setData({
          up: true,
          height: res.width * 2 * 0.84
        })
      }).exec()
    },
    /**
     * 关闭
     */
    close() {
      this.triggerEvent('close', false)
      this.setData({up: false})
    },
    /**
     * 选中日期
     */
    chooseDay(e) {
      let dataset = e.currentTarget.dataset
      let index = dataset.index
      let day = dataset.date.split('-')
      let time = this.data.time
      let cookie = new Date(day[0], day[1] - 1, day[2])
      time.forEach(e => e.active = false)
      time[index].active = true
      this.setData({
        time: time,
        cookie: cookie
      })
      this.triggerEvent('choose',cookie.format('yyyy-MM-dd'))
      this.close()
    },
    /**
     * 上个月
     */
    left() {
      if (this.data.today.format('yyyy-MM') == this.data.stop) {
        wx.showModal({
          content: `只支持查看近${ this.data.count}个月的记录`,
          showCancel:false,
          confirmText: '知道了'
        })
      } else {
        this.setData({
          day: this.getLastMonth(this.data.today)
        })
        if (this.data.today.format('yyyy-MM') < new Date().format('yyyy-MM') && this.data.showRight == false) {
          this.setData({
            showRight: true
          })
        }
      }
    },
    /**
     * 下个月
     */
    right() {
      let today = this.data.today
      let time = new Date()
      if (today.format('yyyy-MM') < time.format('yyyy-MM')) {
        this.setData({
          day: this.getNextMonth(this.data.today),
        })
        if (this.data.today.format('yyyy-MM') == time.format('yyyy-MM')) this.setData({
          showRight: false
        })
      }
    },
    /**
     * 获取上个月
     */
    getLastMonth(date) {
      let year = date.getFullYear()
      let month = date.getMonth()
      if (month == 0) {
        year -= 1
        month = 12
      }
      this.setData({
        today: new Date(year, month - 1),
      })
      wx.canIUse('nextTick') ? wx.nextTick(() => this.setData({
        time: this.pushDay(month)
      })) : setTimeout(() => this.setData({
        time: this.pushDay(month)
      }), 100)
      let m = month
      if (m < 10) m = '0' + m
      return `${year}年\t${m}月`
    },
    /**
     * 获取下个月
     */
    getNextMonth(date) {
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      if (month == 12) {
        year += 1
        month = 0
      }
      this.setData({
        today: new Date(year, month),
      })
      wx.canIUse('nextTick') ? wx.nextTick(() => this.setData({
        time: this.pushDay(month + 1)
      })) : setTimeout(() => this.setData({
        time: this.pushDay(month + 1)
      }), 100)
      let m = month + 1
      if (m < 10) m = '0' + m
      return `${year}年\t${m}月`
    },
    /**
     * 获取月份天数
     */
    getDayNumber(year, time) {
      let day = 0
      if (time == 2) {
        day = year % 4 == 0 ? 29 : 28
      } else if (time == 4 || time == 6 || time == 9 || time == 11) {
        day = 30
      } else {
        day = 31
      }
      return day
    },
    /**
     * 填充数据
     */
    pushDay(month, arr = [], day = new Date()) {
      let last = null
      let next = null
      let today = this.data.today
      let week = new Date(today.format('yyyy'), today.format('MM') - 1, 1).getDay()
      last = today.format('MM') == 1 ? new Date(today.getFullYear() - 1, 11, 1) : new Date(today.getFullYear(), today.getMonth() - 1, 1)
      next = today.format('MM') == 12 ? new Date(today.getFullYear() + 1, 0, 1) : new Date(today.getFullYear(), today.format('MM'), 1)
      let lastNumber = this.getDayNumber(last, parseInt(last.format('MM')))
      for (let i = 1; i <= this.getDayNumber(today, month); i++) {
        let d = i
        if (d < 10) d = '0' + d
        arr.push({
          value: i,
          date: `${today.format('yyyy')}-${today.format('MM')}-${d}`,
          active: false
        })
      }
      if (today.format('yyyy-MM') != this.data.stop) {
        for (let i = lastNumber; i > lastNumber - week; i--) {
          let item = [{
            value: i,
            date: `${last.format('yyyy')}-${last.format('MM')}-${i}`,
            active: false,
            opacity: true
          }]
          arr = item.concat(arr)
        }
      }

      let length = arr.length
      let sum = 42
      if (length < 35) sum = 35
      for (let i = 1; i <= sum - length; i++) {
        let d = i
        if (d < 10) d = '0' + d
        arr.push({
          value: i,
          date: `${next.format('yyyy')}-${next.format('MM')}-${d}`,
          active: false,
          opacity: true
        })
      }
      if (this.data.cookie !== null) {
        let cookie = this.data.cookie
        arr.forEach((e, index) => {
          if (e.date == cookie.format('yyyy-MM-dd')) {
            arr[index].active = true
          }
        })
      }
      if (this.data.cookie === null) {
        arr.forEach((e, index) => {
          if (e.date == this.data.date) {
            arr[index].active = true
          }
        })
      }
      const res = new Map()
      let dateList = this.data.dateList
      dateList = dateList.filter(a => !res.has(a) && res.set(a, 1))
      arr.forEach((e, index) => {
        dateList.forEach(i => {
          if (e.date == i) arr[index].have = true
        })
      })
      if (today.format('yyyy-MM') == new Date().format('yyyy-MM')) {
        arr.forEach((e, index) => {
          if (e.date > new Date().format('yyyy-MM-dd')) arr.splice(index, arr.length)
        })
      }
      return arr
    }
  }
})