//app.js

App({
  onLaunch: function () {
  
  },
  //判断小程序是否注册
 ifregister(e){
  console.log(e);
  let that=this;
  wx.login({
   success: function (s) {
    let url = 'wxapp/public/getSessionKey';
    if (s.code) {
     var code = s.code;
     var param = {
      code: code
     }
     that.post(url, param).then((res) => {
      console.log(res);
          
     }).catch((errMsg) => {
      console.log(errMsg);
     })
    }
   }
  });

 },
 //获取sessionKey和openid 
 start(e) {
  let that = this;
  that.globalData.userInfo = e.detail.userInfo;
  wx.login({
   success: function (s) {
    let url = 'wxapp/public/getSessionKey';
    if (s.code) {
     var code = s.code;
     var param = {
      code: code
     }
     that.post(url, param).then((res) => {
       let url = 'wxapp/public/is_register';
       let params = {
        openid: res.openid
       }
       that.post(url, params).then((res) => {
        if (res.code == '40000') {
         wx.setStorageSync('token', res.data);
         return true;
        } else {
         let param = {
          openid: res.openid,
          session_key: res.session_key,
          encrypted_data: e.detail.encryptedData,
          iv: e.detail.iv
         }
         let url = 'wxapp/public/login';
         that.post(url, param).then((res) => {
          wx.setStorageSync('token', res.token);
          return true;
         }).catch((errMsg) => {
          return false;
          console.log(errMsg);
         })
        }
       }).catch((err) => {
       
        return false;
        console.log(err);
       })

     }).catch((errMsg) => {
      return false;
      console.log(errMsg);
     })
    }
   },
   
  });
 },
//检测小程序是否登陆
 is_register(openid, session_key, encryptedData, iv){
  console.log('is_register');
  let that=this;
  let url ='wxapp/public/is_register';
  let params={
   openid:openid
  }
  that.post(url, params).then((res)=>{

   if (res.code=='40000'){
    wx.setStorageSync('token', res.data);
    
   }else{
    let param = {
     openid: openid,
     session_key: session_key,
     encrypted_data: encrypted_data,
     iv: iv
    }
    let url = 'wxapp/public/login';
    that.post(url, param).then((res) => {
     wx.setStorageSync('token', res.token);
     let token = {
      'token': res.token
     }
     return token;

    }).catch((errMsg) => {
     console.log(errMsg);
    })
   }
  }).catch((err)=>{
   console.log(err)
  })
 },
 //小程序登录注册 
 login(openid, session_key, encrypted_data, iv) {
  let that = this;
  let param = {
   openid: openid,
   session_key: session_key,
   encrypted_data: encrypted_data,
   iv: iv
  }
  let url = 'wxapp/public/login';
  that.post(url, param).then((res) => {
   wx.setStorageSync('token', res.token);
   let token={
    'token': res.token
   }
   return token;
   
  }).catch((errMsg) => {
   console.log(errMsg);
   let tem={};
   return tem;
  })
 },
 /**
    * 自定义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://wxapp.xianghejia.cn/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.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()
    },
    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();//当前时间点
  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;
 },
  globalData: {
    userInfo: null,
    city:'西安',//预约城市,
   district: '未央区',//预约城市"
  }
})