//app.js
const util = require("util/util.js")
App({
  onLaunch: function() {
    // 初始化配置
    this.autoUpgrade()
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        let capsule = wx.getMenuButtonBoundingClientRect();
        if (capsule) {
          this.globalData.Custom = capsule;
          this.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight;
        } else {
          this.globalData.CustomBar = e.statusBarHeight + 50;
        }
        this.globalData.CustomRate = e.screenHeight / e.screenWidth;
      }
    })
  },
  autoUpgrade: function(){
    if (wx.canIUse('getUpdateManager')) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          console.log('res.hasUpdate====')
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: function (res) {
                console.log('success====', res)
                if (res.confirm) {
                  updateManager.applyUpdate()
                }
              }
            })
          })
          updateManager.onUpdateFailed(function () {
            // 新的版本下载失败
            wx.showModal({
              title: '已经有新版本了哟~',
              content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
            })
          })
        }
      })
    }
  },
  // 微信登入
  doLogin: function( cb ){
    let that = this
    wx.login({
      success: res => {
        if (res.code) {
          let param = { code: res.code };
          //发起网络请求
          util.http("/base/wxLogin", param, (errCode, data) => {
            if( errCode == 0){
              let {user, token} = data
              user.token = token;
              wx.setStorageSync('@yunyuanqu', user);
            }else{
              util.showMsg( data )
            }
            cb&&cb(errCode, data );
          });
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  },
  checkLogin:function( cb ){
    var info = wx.getStorageSync('@yunyuanqu')||{};
    let isLogin =  !!info.token;
    this.globalData.userInfo = info;
    if (!isLogin){
      this.doLogin( cb )
    }else{
      cb&&cb( )
    }
  },
  getSysInfo: function (cb) {
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
        typeof cb == "function" && cb(res);
      }
    })
  },
  goHome() {
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  getLocation: function (cb) {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res)
        typeof cb == "function" && cb(res);
      }
    })
  },
  checkPermission( cb ){
    wx.getSetting({
      success: function (res) {
        if (!res.authSetting['scope.userLocation']) {
          //申请授权
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              console.log("authorize success")
              cb&&cb()
            },
            fail(){
              console.log("authorize fail")
            }
          })
        }else{
          cb&&cb()
        }
      },
      fail: function(res){
        console.log("fail", res)
      }
    })
  },
  getLocation(){
    return this.globalData.location;
  },
  setLocaltion( location ){
    this.globalData.location = location;
  },
  getUserInfo( ){
    return this.globalData.userInfo
  },
  setUserInfo( userInfo ){
    userInfo = Object.assign(this.globalData.userInfo, userInfo )
    wx.setStorageSync('@flogin', userInfo);
    this.globalData.userInfo = userInfo;
  },
  setMakerInfo( maker ){
    maker = Object.assign(this.globalData.maker, maker )
    this.globalData.maker = maker;
  },
  getMakerInfo( cb ){
    if( this.globalData.maker.id ){
      cb&&cb( this.globalData.maker);
    }else{
      util.http( '/wx/getMakerInfo', {}, (err,res)=>{
        if( err == 0 ) {
          this.globalData.maker = res.info
          cb&&cb( res.info );
         }else{
          cb&&cb( {} );
         }
      })
    }
  },
  globalData: {
    userInfo: {},
    location: {},
    maker: {},
    Custom:0,
    CustomBar:0,
    CustomRate:0,
    StatusBar:0
  }
})