//app.js
import {
  LoginToken
} from '/lib/RequestingCenter.js'
import  {
  Toastsuccess,ToastError
}  from '/lib/Toast.js'
let sum = 0;
App({
  onLaunch: function() {
    wx.removeStorage({
      key: 'oldHotelinfo',
    })
     // 获取系统状态栏信息
     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.userLogin();
  },
  globalData: {
    ImgUrl : "https://auth.blv-oa.com/ImgServer/FileImage?url=ImgUrl&&ftp=11",
    userinfo: null,
    MapKey: '6JMBZ-BSAWP-D2NDJ-LFL4J-QJQQ3-3TBTV',
    MapSk:'MHAGZ4S3aYeAD1pFoMTy6HAFL7nG18',
    autho: null,
    authotype:{
       no:1,
       ok:3
    }
  },
  toast:function(type,title,success,time){
    if(type==1){
      Toastsuccess(title,success,time)
    }
    if(type==2){
     ToastError(title,success,time)
    }
    // Toast(title,icon,success,time)
  },
  //TOKEN登录
  userLogin:async function (back) {
    let that = this;
    // 读取本地信息尝试登录
    const xiaoxi_token = wx.getStorageSync('xiaoxi_token') || null
    if (xiaoxi_token != null && xiaoxi_token!="")   {
   await LoginToken({
        token: xiaoxi_token
      }).then(res => {
        if(res.Status!=200){
          that.toast(2,res.Message||"网络繁忙",function () {
            wx.reLaunch({
              url: "../login/login",
            })
          })
          return;
        }
          res.Data.userinfo.error = res.Data.error
          res.Data.userinfo.HeadImg =  that.globalData.ImgUrl.replace("ImgUrl",res.Data.userinfo.HeadImg)
           that.globalData.userinfo = res.Data.userinfo
           that.globalData.autho = res.Data.autho
           //由于这里是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
      //   if (that.checkLoginReadyCallback){
      //     that.checkLoginReadyCallback();
      //  }
       wx.setStorageSync('xiaoxi_token', res.Data.token)
       back();
      },
      err=>{
        that.toast(2,'网络繁忙',function () {
          wx.reLaunch({
            url: "/pages/login/login",
          })
        })
      }
      ).catch(
        err => {
        console.log(err)
          that.toast(2,'网络繁忙', ()=> {
            wx.reLaunch({
              url:  "/pages/login/login",
            })
          })
        }
      );
    } else {
      wx.reLaunch({
        url:  "/pages/login/login",
      })
    }
  }, 
  //检测授权信息
  SQ: function (back) {
    let that = this;
    wx.getSetting({
      success: (res) => {
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          //以前被拒绝授权指引用户授权
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                console.log('0')
                back(false) 
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      back(true);
                    } else {
                      back(false) 
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          back(false) 
        } else {
          // 已经授权
          back(true);
        }
      }
    })
  },
  // 微信api，获取经纬度
  getLocation(back,err) {
    wx.getLocation({
      type: 'gcj02',
      success: (params) =>{
        back();
      },
      fail: (e) => {
       err();
      }
    })
  }
})