const app = getApp()
var arr = [];
Page({
  data: {
    //测试.
    versions: false,
    token: '',
    img: app.globalData.imgUrl,
    title: app.globalData.title,
    logoUrl: '',
    bind_mobile: 0,
    session_key:''
  },
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: app.globalData.title
    })
    this.setData({
      logoUrl: app.globalData.logoUrl
    })
    wx.removeStorageSync('token');
    wx.login({
      success: function (res) {
        wx.getSystemInfo({
          success(res) {
            console.log(arr)
            arr = [{
                label: '品牌',
                type: 'brand',
                value: res.brand
              },
              {
                label: '型号',
                type: 'model',
                value: res.model
              },
              {
                label: '系统平台',
                type: 'platform',
                value: res.platform
              },
              {
                label: '系统版本',
                type: 'system',
                value: res.system
              },
              {
                label: '微信版本',
                type: 'version',
                value: res.version
              },
              {
                label: '小程序版本',
                type: 'SDKVersion',
                value: res.SDKVersion
              },
              {
                label: '定位状态',
                type: 'locationEnabled',
                value: res.locationEnabled ? '开' : '关'
              },
              {
                label: '定位授权',
                type: 'locationAuthorized',
                value: res.locationAuthorized ? '已授权' : '未授权'
              }
            ]
          }
        })
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        console.log(res.code)
        var data = {
          'code': res.code,
          'appid': app.globalData.appid,
          'phone_detail': JSON.stringify(arr),
          'invite_user_id': app.globalData.id,
          'language':'zh'
        };
        app.request("/auth/weapp-login", data, "POST").then(res => {
          console.log(res)
          wx.setStorageSync('token', res.data.token);
          wx.setStorageSync('session_key', res.data.session_key);
          wx.setStorageSync('token_time', res.data.exp);
          wx.setStorageSync('user_ID', res.data.user.id);
          that.setData({
            token: res.data.token,
            session_key:res.data.session_key
          })
          app.request("/user/status", '', "GET").then(res => {
            console.log(res)
            if (res.statusCode == 200) {
              that.setData({
                bind_mobile: res.data.is_bind_mobile
              })
            }
          })
        })
      }
    })
  },
  onGotUserInfo: function (e) {
    console.log(e)
      if (e.detail.errMsg == "getUserInfo:fail auth deny") {
        wx.showToast({
          title: '授权失败请重试',
          icon: 'none'
        })
      } else {
        wx.showLoading({
          title: '登陆中请稍等...',
          mask: true
        })
        if (e.detail.rawData) {
          var data = e.detail.userInfo
          wx.setStorageSync('userInfo', data)
          if (wx.getStorageSync('home').id != undefined) {
            data.area_id = wx.getStorageSync('home').id
          }
          console.log(data)
          app.request("/auth/weapp-userinfo-sync", data, "POST").then(res => {
            if (res.statusCode == 200) {
              wx.showToast({
                title: '授权成功',
                icon: 'none'
              })
              wx.setStorageSync('userInfo', res.data)
              // wx.navigateBack()
              if (app.globalData.compatible) {
                console.log(app.globalData.compatible)
                wx.redirectTo({
                  url: '/pages/compatible/index/index',
                })
              } else {
                wx.redirectTo({
                  url: '/pages/index/index',
                })
              }
            } else {
              wx.showToast({
                title: '登陆失败请重试',
                icon: 'none'
              })
              wx.redirectTo({
                url: '/pages/index/index',
              })
            }
          })
        } else {
          wx.showToast({
            title: '授权失败',
            icon: 'none'
          })
        }
      }
  },
  getPhoneNumber: function (e) {
    var that = this;
    console.log(e)
      if (e.detail.errMsg == "getPhoneNumber:fail user deny" || e.detail.iv==undefined) {
        wx.showToast({
          title: '授权失败请重试',
          icon: 'none'
        })
      } else {
        let data = {
          phone_detail: JSON.stringify(arr),
          type: 1,
          session_key: that.data.session_key,
          iv: e.detail.iv,
          encryptedData: e.detail.encryptedData
        }
        app.request('/auth/mobileLogin', data, 'POST', app.globalData.req).then(res => {
          if (res.statusCode == 200) {
            console.log(res)
            wx.setStorageSync('userInfo', res.data.user)
            wx.setStorageSync('token', res.data.token);
            if (app.globalData.compatible) {
              wx.reLaunch({
                url: '/pages/compatible/index/index',
              })
            } else {
              wx.reLaunch({
                url: '/pages/index/index',
              })
            }
          }
        })
      }
  },
  agreement: function () {
    wx.navigateTo({
      url: '/pages/personal/privacy/privacy',
    })
  },
  mobile_login() {
    wx.navigateTo({
      url: '/pages/mobile_login/mobile_login',
    })
  },
  onReady: function () {

  },
  onShow: function () {
    wx.hideHomeButton()
  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})