daySignIn.js 4.4 KB
// pages/signIn/daySignIn/daySignIn.js
var util = require('../../../utils/util.js');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sign: '',
    popup_state: true,
    currentTime: '',
    is_week: '',
    hint: '尚未到达签到时间',
    state: false
  },

  //签到
  getData() {
    let url = 'portal/Sign/daySign'
    let params = {}
    let header = {
      "XX-Token": wx.getStorageSync('token')
    }
    app.post(url, params, header).then((res) => {
      console.log(res)
      // if (res.data.data.is_week == 1) {
      //   wx.navigateTo({
      //     url: '../../signIn/Weight/Weight',
      //   })
      // }
      if (res.data.code == 20000) {
        this.setData({
          all: res.data.data.all,
          last: res.data.data.last,
          longest: res.data.data.longest,
          morning: res.data.data.morning,
          noon: res.data.data.noon,
          night: res.data.data.night,
          sleep: res.data.data.sleep,
        })
      }
    })
  },

  //签到
  signIn() {
    if (this.data.sign == 0) {
      wx.showToast({
        title: '未到打卡时间',
        icon: 'none'
      })
      this.setData({
        state: false
      })
    } else if (this.data.sign == -1) {
      wx.showToast({
        title: '当前时段已打卡',
        icon: 'none'
      })
      this.setData({
        state: false
      })
    } else {
      let url = 'portal/Sign/daySignPost'
      let params = {
        status: this.data.sign
      }
      let header = {
        "XX-Token": wx.getStorageSync('token')
      }
      app.post(url, params, header).then((res) => {
        console.log(res)
        if (res.data.code == 20000) {
          this.getData()
          this.setData({
            state: false
          })
          if (res.data.data.day_sign == 1) {
            var sign = this.data.sign
            wx.navigateTo({
              url: '../dailySignIn/dailySignIn?sign=' + sign,
            })
          }else{
            wx.showToast({
              title: '打卡成功',
            })
          }
        } else if (res.data.code == 40000) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          this.setData({
            state: false
          })
        } else if (res.data.code == 40002) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          this.setData({
            state: false
          })
        }
      })
    }
  },

  //
  popupConfirm() {
    this.setData({
      popup_state: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options.sign)
    this.setData({
      sign: options.sign,
    })
    if (this.data.sign == 1) {
      this.setData({
        hint: '已进入早打卡时间范围',
        state: true
      })
    } else if (this.data.sign == 2) {
      this.setData({
        hint: '已进入午打卡时间范围',
        state: true
      })
    } else if (this.data.sign == 3) {
      this.setData({
        hint: '已进入晚打卡时间范围',
        state: true
      })
    } else if (this.data.sign == 4) {
      this.setData({
        hint: '已进入睡前打卡时间范围',
        state: true
      })
    }
    this.getData()
    var hour = new Date().getHours()
    var minute = new Date().getMinutes()
    var second = new Date().getSeconds()
    this.setData({
      currentTime: hour + ':' + minute + ":" + second
    })
    // 调用函数时,传入new Date()参数,返回值是日期和时间  
    var time = util.formatTime(new Date());
    console.log(time)
    // 再通过setData更改Page()里面的data,动态更新页面的数据  
    this.setData({
      time: time
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})