//app.js
// import md5 from 'utils/md5.js';
const App = require('./utils/ald-stat.js').App;

const ald = require('./utils/ald-stat.js');
App({
  onLaunch: function () {
   let that=this;
  wx.login({
   success: (res) => {
    let url = 'public/getSessionKey';
    let params = {
     code: res.code
    }
    that.post(url, params).then((res) => {
    
     wx.setStorageSync('openid', res.openid);
     wx.setStorageSync('session_key', res.session_key);
      //记录小程序打开次数
     
     // 调用sendSession方法,将session_key以参数形式传入
 
    }).catch((errMsg) => {
     console.log(errMsg)
    })
   }
  })
 },
 
 onShow: function () { 
 },
  /**
     * 自定义post函数,返回Promise
     * +-------------------
     * @param {String}      url 接口网址
     * @param {arrayObject} data 要传的数组对象 like: {name: 'name', age: 32}
     * +-------------------
     * @return {Promise}    promise 返回promise供后续操作
     */
  post: function (url, data, headerParams) {
    // wx.showNavigationBarLoading()
    // wx.showLoading({
    //   title: '加载中',
    // })    
    var promise = new Promise((resolve, reject) => {
      let that = this;
      let postData = data;
     let baseUrl = 'https://shuhou.superkangaroo.net/api/wxapp/';
     // let baseUrl = 'http://postoperative.w.bronet.cn/api/wxapp/';//测试域名
      //网络请求
      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) {//返回取得的数据
        console.log(res.data.data)
          if (res.data.code == '20000') {
            resolve(res.data.data);
          } else if (res.data.code == '40001') {//返回错误提示信息
            wx.showModal({
              title: '提示',
             content: res.data.msg,
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  wx.removeStorageSync('token');
                  wx.navigateTo({
                    url: '/pages/start/start',
                  })
                }
              }
            })
          } else if (res.data.code == '40000') {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false,
              success: function (res) { }
            })
          } else if (res.data.code == '40005'){

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

          }else {
            wx.showModal({
              title: '提示',
              content: res.data.msg,
              showCancel: false,
            })
            reject(res.data)
          }
          // wx.hideLoading()
          // wx.hideNavigationBarLoading()
        },
        error: function (e) {
         console.log(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();//当前时间点
  console.log(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;
 },
 //记录打开小程序日志
 setStatisticsOpenTimes(openid){
  let that=this;
  let url = 'index/setStatisticsOpenTimes';
  let params={
   'doctor_id': that.globalData.doctor_id,
   'openid':openid
  }
  that.post(url,params).then((res)=>{
  
  }).catch((errMsg)=>{
   console.log(errMsg)
  })
 },
 //记录页面访问日志
 setStatisticsVisitorLog(page){
  let that = this;
  wx.login({
   success: (res) => {
    let url = 'public/getSessionKey';
    let params = {
     code: res.code
    }
    that.post(url, params ).then((res) => {
     let url = 'index/setStatisticsVisitorLog';
     let params = {
      'doctor_id': that.globalData.doctor_id,
      'page': page,
      'openid': res.openid
     }
     that.post(url, params).then((res) => {

     }).catch((errMsg) => {
      console.log(errMsg)
     })
    }).catch((errMsg) => {
     console.log(errMsg)
    })
   }
  })
 },
  globalData: {
   userInfo: '',
   doctorid: 0,//字符串id,用于获取医生详情
   doctor_id: 0,//本小程序中使用的id
   is_register:0,//是否注册过,
   is_bind_doctor_id:0//是否绑定医生id
  }
})