addWeight.js 4.6 KB
// pages/addWeight/addWeight.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiIndex: [0, 0],
    weeksArry: [],
    weight: ''
  },
  getWeeks() {
    let url = '/user/getWeekDay'
    app.post(url, {}).then((res) => {
      console.log(res)
      this.setData({
        multiIndex: [res.default.week, res.default.day],
        week: res.default.week,
        day: res.default.day
      })
      console.log(this.data.multiIndex)
      let tempData = []
      let tempWeeks = []
      let tempDays = []
      for (let i = 0; i <= this.data.week; i++) {
        let tempText = '孕期' + i + '周'
        let tempw = {
          id: i,
          text: tempText
        }
        tempWeeks.push(tempw)
      }
      for (let i = 0; i <= this.data.day; i++) {
        let tempText = '加' + i + '天'
        let tempD = {
          id: i,
          text: tempText
        }
        tempDays.push(tempD)
      }
      tempData[0] = tempWeeks;
      tempData[1] = tempDays;
      this.setData({
        weeksArry: tempData
      })
    }).catch((errMsg) => {

    })
  },
  bindMultiPickerChange: function (e) {
    console.log(e)
    let chooseW = this.data.weeksArry[0][e.detail.value[0]]
    let chooseD = this.data.weeksArry[1][e.detail.value[1]]
    this.setData({
      multiIndex: e.detail.value,
      week: chooseW.id,
      day: chooseD.id
    })
  },
  bindMultiPickerColumnChange: function (e) {
    console.log(e)
    let row = e.detail.column;
    let value = e.detail.value;
    let baseWeek = this.data.week;
    let weeksArry = this.data.weeksArry;
    let tempDays = [];
    let nowDays = []
    if (row == 0) {
      if (value < baseWeek) {
        for (let i = 0; i < 7; i++) {
          let tempText = '加' + i + '天'
          let tempD = {
            id: i,
            text: tempText
          }
          tempDays.push(tempD)
        }
        weeksArry[1] = tempDays;
        this.setData({
          weeksArry: weeksArry
        })
      } else {
        for (let i = 0; i <= this.data.day; i++) {
          let tempText = '加' + i + '天'
          let tempD = {
            id: i,
            text: tempText
          }
          nowDays.push(tempD)
        }
        weeksArry[1] = nowDays;
        this.setData({
          weeksArry: weeksArry
        })
      }
    }
  },
  getWeight(e) {
    console.log(e)
    this.setData({
      weight: e.detail.value
    })
  },
  addWeight() {
    let url = '/user/addWeightLog'
    let params = {
      create_date: this.data.create_date,
      weight: this.data.weight
    }
    if (params.weight == '' || params.weight == null || params.weight == undefined) {
      wx.showToast({
        title: '请输入体重', 
        icon: 'none'
      })
    } else {
      app.post(url, params).then((res) => {
        wx.showToast({
          title: '添加成功',
        })
        wx.setStorageSync('newWeight','true')
        setTimeout(function(){
          wx.switchTab({
            url: '/pages/weight/weight',
          })
        },1000)
      }).catch((errMsg) => {

      })
    }
  },
  inputDate: function (e) {
    this.setData({
      create_date: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // let tempData = []
    // let tempWeeks = []
    // let tempDays = []
    // for (let i = 0; i <= 42; i++) {
    //   let tempText = '孕期' + i + '周'
    //   let tempw = {
    //     id: i,
    //     text: tempText 
    //   }
    //   tempWeeks.push(tempw)
    // }
    // for (let i = 0; i < 7; i++) {
    //   let tempText = '加' + i + '天'
    //   let tempD = {
    //     id: i,
    //     text: tempText
    //   }
    //   tempDays.push(tempD)
    // }
    // tempData[0] = tempWeeks;
    // tempData[1] = tempDays;
    // this.setData({
    //   weeksArry: tempData
    // })
    // this.getWeeks();
    this.setData({
      create_date:app.nowDate(),
      endDate: app.nowDate(),
      startDate: app.minDate()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})