App({
  onLaunch: function () {
    // 登录
    var that = this;
    wx.getSystemInfo({
      success: res => {
        console.log(res)
        var system = res.system.trim().split(/\s+/)[1];
        var intNumber = system.substring(0, system.indexOf("."));
        if (res.platform == 'ios' && intNumber < 13 || res.SDKVersion < '2.1.0') {
          console.log('低版本')
          that.globalData.compatible = true;
          wx.reLaunch({
            url: '/pages/compatible/index/index',
          })
        }
        that.globalData.platform = res.platform
        that.globalData.version = res.version
        console.log(res.safeArea.bottom)
        console.log('手机信息res' + res.model)
        if (res.model == 'iPhone X' || res.model == 'iPhone XR' || res.model == 'iPhone XS Max') {
          that.globalData.screenHeight = 'xSeries'
        } else if (res.safeArea.bottom >= 780) {
          that.globalData.screenHeight = 'xSeries'
        }
      }
    })
    if (wx.getStorageSync('midTimes') != '') {
      var time = wx.getStorageSync('midTimes');
      var timestamp = Date.parse(new Date()) / 1000;
      if (time <= timestamp) {
        console.log('midtimes过期')
        wx.setStorageSync('midTimes', '')
      }
    }
    var time = wx.getStorageSync('token_time');
    var timestamp = Date.parse(new Date());
    if (time <= timestamp / 1000) {
      console.log('token过期')
      wx.removeStorageSync('token_time')
    }
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                if (res.confirm) {
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
            })
          })
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  globalData: {
    statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
    windowHeight: wx.getSystemInfoSync()['windowHeight'],
    imgUrl: 'http://resource.bike.hanyiyun.com/',
    screenHeight: '',
    login: false,
    platform: '',
    version: '',
    req: true,
    id: 0,
    times: 60,
    invalid: false,
    compatible: '',
    title: '闪现出行', //全局修改此小程序名称
    logoUrl: '/img/shanxianLg.png', //logo图片路径 网络图片/本地图片 dejin.png  shanxianLg.png  yuzhouLg.png
    appid: 'wxd47b0ef6600fbe24', //小程序的appid 
    merchant_id: 1,
    //  url : 'http://api.weikemu.dev.hanyiyun.com/api', //威科姆测试域名
    //  url : 'https://api.xiaobaichuxing.hanyiyun.com/api', //小白出行域名sa
    //  url: 'https://api.bike.hanyiyun.com/api', //闪现出行域名
    //  url : 'https://api.weilai.hanyiyun.com/api', //太比特测试域名
      // url : 'https://api.weilaibike.com/api',          //线上域名
     url: 'https://api.bike.saas.yufengjiayun.com/api', //商户端域名
    // url:'http://dev.api.saas.yufengjiayun.com/api',//测试域名
   // url:'https://api.dev.saas.yufengjiayun.com/api',//支付宝测试域名
    mapKey:'C3YBZ-SLSKU-TYWVP-2NEUJ-JNDO3-KCFBH' //地图key值 德劲：PM2BZ-CS7L3-MPR35-3A5GW-HGW66-QLBXA ,闪现出行：C3YBZ-SLSKU-TYWVP-2NEUJ-JNDO3-KCFBH,禹州闪现出行 GTLBZ-F6BKO-5EDWH-SDHIX-K2ART-O2FRQ
  },
  request(api, params, method, req1) {
    var that = this;
    if (req1 == false) return;
    if (req1) {
      that.globalData.req = false;
    }
    if (wx.getStorageSync('token') != '' || wx.getStorageSync('token') != undefined) {
      if (req1 == true || req1 == undefined) {
        return new Promise((resolve, reject) => {
          wx.request({
            url: this.globalData.url + api,
            data: params,
            header: {
              'content-type': 'application/x-www-form-urlencoded',
              'Cache-Control': 'no-cache',
              'Authorization': wx.getStorageSync('token'),
              'merchant-id': this.globalData.merchant_id
            },
            method: method,
            success: (res) => {
              resolve(res)
              // console.log(res)
              if (res.statusCode == 450) {
                return;
              }
              if (res.data.statusCode != 200 ) {
                if (res.data.message == '订单不存在或订单已结算') {
                  if (this.globalData.compatible) {
                    wx.reLaunch({
                      url: '/pages/compatible/index/index',
                    })
                  } else {
                    wx.reLaunch({
                      url: '/pages/index/index',
                    })
                  }
                  return false;
                } else if (res.data.message == '运动中不能关锁') {
                  return false;
                }
                if (res.message != undefined) {
                    wx.showToast({
                      title: res.message,
                      icon: 'none',
                      duration: 3000,
                      mask: true
                    })
                } else if (res.data.message != undefined) {
                    wx.showToast({
                      title: res.data.message,
                      icon: 'none',
                      duration: 3000,
                      mask: true
                    })
                }
              } else if (res.statusCode != 200) {
                if (res.data.message == '订单不存在或订单已结算') {
                  if (this.globalData.compatible) {
                    wx.reLaunch({
                      url: '/pages/compatible/index/index',
                    })
                  } else {
                    wx.reLaunch({
                      url: '/pages/index/index',
                    })
                  }
                  return false;
                } else if (res.data.message == '运动中不能关锁') {
                  return false;
                }
                if (res.message != undefined) {
                    wx.showToast({
                      title: res.message,
                      icon: 'none',
                      duration: 3000,
                      mask: true
                    })
                } else if (res.data.message != undefined) {
                    wx.showToast({
                      title: res.data.message,
                      icon: 'none',
                      duration: 3000,
                      mask: true
                    })
                }
              }
              if (res.statusCode == 401) {
                var timeout;
                wx.removeStorageSync('token')
                wx.removeStorageSync('token_time')
                that.globalData.invalid = true
                console.log(that.globalData.login)
                if (that.globalData.login) {
                  return false;
                }
                if (that.globalData.login == false) {
                  that.globalData.login = true
                  wx.showToast({
                    title: '登陆过期请重新登陆~',
                    icon: 'none',
                    duration: 1000,
                    mask: true,
                    success: function () {
                      wx.reLaunch({
                        url: '/pages/authentication/authentication',
                      })
                    }
                  })
                  timeout = setTimeout(function () {
                    that.globalData.login = false
                    if (that.globalData.login == false) {
                      clearTimeout(timeout)
                    }
                  }, 3000)
                }
              } else if (res.statusCode == 404) {
                wx.navigateTo({
                  url: '/pages/undefind/undefind',
                })
              }
            },
            fail: (err) => {
              wx.showToast({
                title: err,
                icon: 'none',
                mask: true
              });
              reject("请求失败")
            },
            complete: () => {
              that.globalData.req = true
            }
          })
        })
      }
    }
  },
})