var ald = require('./utils/ald-stat.js');
//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
   
  },
  post: function (url, data) {
    /** 
  * 自定义post函数,返回Promise
  * +-------------------
  * @param {String}      url 接口网址
  * @param {arrayObject} data 要传的数组对象 like: {name: 'name', age: 32}
  * +-------------------
  * @return {Promise}    promise 返回promise供后续操作
  */
    // wx.showLoading({
    //   title: '加载中',
    // })
    wx.showNavigationBarLoading()
    var promise = new Promise((resolve, reject) => {
      //init
      let that = this;
      let postData = data;
      let baseUrl = 'http://zhouli.w.bronet.cn/api/zhouli';
      // let baseUrl = ' https://tzzl.superkangaroo.net';
      // postData.signature = that.makeSign(postData);
      //网络请求  
      wx.request({
        url: baseUrl + url,
        data: postData,
        method: 'POST', 
        header: { 
          'content-type': 'application/x-www-form-urlencoded',
          'XX-Token': wx.getStorageSync('token')
        },
        success: function (res) {//返回取得的数据
          if (res.data.code == '20000') {
            resolve(res.data.data);
          } else if (res.data.code == '20001') {//返回错误提示信息
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              success: function (res) {
                if (res.confirm) {
                  wx.removeStorageSync('token');
                  wx.navigateTo({
                    url: '/pages/startRecord/startRecord',
                  })
                }
              }
            })
          }else if(res.data.code == '40001'){//返回错误提示信息
            wx.showModal({
              title: '提示',
              content: '登录已过期请重新登录!',
              showCancel: false,
              success: function(res){
                if(res.confirm){
                  wx.removeStorageSync('token');
                  wx.navigateTo({ 
                    url: '/pages/startRecord/startRecord',
                  }) 
                }
              } 
            })
          } else if (res.data.code == '40000'){
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false,
              success: function (res) {}
            })
          } else if (res.data.code == '44004') {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false,
              success: function (res) { 
                wx.navigateBack()
              }
            })
          } else if (res.data.code == '44002'){
            // 初始化用户信息判断 
            let usetrInfoType = {
              hasUserInfo: false
            }
            resolve(usetrInfoType);
            // wx.showModal({  
            //   title: '提示',
            //   content: res.data.msg,
            //   showCancel: false
            // })
          }else{
              wx.showModal({
                title: '提示',
                content: res.data.msg,
                showCancel: false
              })
              reject(res.data)
          }
          setTimeout(function () {
            // wx.hideLoading()
            wx.hideNavigationBarLoading()
          }, 600)
        },
        error: function (e) {
          reject('网络出错');
          // wx.hideLoading()
          wx.hideNavigationBarLoading()
        } 
      })
    });
    return promise;
  },
  nowDate(){
      let date = new Date();
      let month = date.getMonth()+1;
      let day = date.getDate();
      return date.getFullYear()+'-'+(month>9?month:('0'+month))+'-'+(day>9?day:('0'+day));
  },
  minDate(){
    let date = new Date();
    date.setDate(date.getDate() - 287); 
    let m = date.getMonth() + 1; 
    return date.getFullYear() + '-' + m + '-' + date.getDate(); 
  },
  globalData: {
    userInfo: null,
    appInfo:{
      
    }
  }
})