//app.js
App({
  url: 'https://www.koala-online.cn/compass/if/',
  onLaunch: function () {
    //调用wx用户登陆
    wx.login({
        success: res => {
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          this.checkUser(res.code, null);
        }
    })

    var config = wx.getStorageSync('config');
    var that = this;
    //获取并保存用户的经纬度信息
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        let location={
          latitude: res.latitude,
          longitude: res.longitude
        };
        wx.setStorage({
          key: 'location',
          data: location,
        })
      }
    })
  },

  //判断用户是否已经注册
  checkUser:function(code,openid){
    wx.request({
      url: this.url + 'ajax_check_user_registed.php',
      data: {
        openId: openid,
        code: code
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        //保存返回的openId和unionId
        let config = {
          openId: res.data.result.openId,
          unionId: res.data.result.unionId,
          registed: res.data.result.registed
        };
        wx.showToast({
          title: "unionId=" + res.data.result.unionId,
          icon: 'success',
          duration: 2000
        });
        wx.setStorage({
          key: 'config',
          data: config,
        });
      },
      fail: function (res) {
        wx.showToast({
          title: "失败",
          icon: 'success',
          duration: 2000
        });
      },
      complete: function (res) {
      },
    })
  },
  globalData: {
    userInfo: null
  },
})