//app.js
App({
  onLaunch: function () {
    //自动更新版本
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
    })

    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好,是否重启应用?',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })

    })

    updateManager.onUpdateFailed(function () {
      // 新的版本下载失败
      wx.showModal({
        title: '更新提示',
        content: '新版本下载失败',
        showCancel: false
      })
    })
  },

  post: function (url, data, headerParams) {
    wx.showNavigationBarLoading()
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var promise = new Promise((resolve, reject) => {
      let that = this;
      let postData = data;
      let baseUrl = 'http://volunteer.w.bronet.cn/api/';
      // let baseUrl = 'https://volunteer.cnpu.org/api/';
      //网络请求
      let header = {
        'content-type': 'application/x-www-form-urlencoded'
      }
      header = Object.assign(header, headerParams)
      wx.request({
        url: baseUrl + url,
        data: postData,
        method: 'POST',
        header: header,
        success: function (res) { //返回取得的数据
          if (res.data.code == '20000') {
            resolve(res.data.data);
          } else if (res.data.code == '10001') { //用户未登录

            wx.reLaunch({
              url: "/pages/start/start"
            })

          } else if (res.data.code == '40000') {

            
            if (url == '/index/second/commitAnswer') {
              resolve(res.data);
            } else  {
              wx.showModal({
                title: '提示',
                content: res.data.msg,
                showCancel: false,
                success: function (res) {

                }
              })
            }




          } else if (res.data.code == '40005') {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '/pages/start/start'
                  })
                } else if (res.cancel) {

                }
              }
            })
          } else if (res.data.code == '40006') {

          } else {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false,
            })
            reject(res.data)
          }
          wx.hideLoading()
          wx.hideNavigationBarLoading()
        },
        fail: function (e) {
          reject('网络出错');
          // wx.hideLoading()
          wx.hideNavigationBarLoading()
        }
      })
    });

    return promise;
  },
  //根据年月日获取(星期等信息)////later=0 当前  later=1明天  later=2后天
  dateLater(later) {
    let dateObj = {};
    let show_day = new Array('星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
    let date = new Date();
    date.setDate(date.getDate() + later);
    let day = date.getDay();
    dateObj.year = date.getFullYear();
    dateObj.month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
    dateObj.day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
    dateObj.week = show_day[day];
    dateObj.dataday = date.getFullYear() + '-' + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + '-' + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
    dateObj.houser = date.getHours(); //当前时间点
    dateObj.housers = (date.getHours() < 10 ? ("0" + date.getHours()) : date.getHours()) + ':00:00'; //当前时间点
    dateObj.datadayhouser = date.getFullYear() + '/' + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + '/' + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate()) + ' ' + (date.getHours() < 10 ? ("0" + date.getHours()) : date.getHours()) + ':00'; //当前时间并时间点
    return dateObj;
  },
  globalData: {
    userInfo: null
  }
})