const app = getApp()
var arr = [];
Page({
  data: {
    //测试.
    versions: false,
    token: '',
    img: app.globalData.imgUrl,
    title: app.globalData.title,
    logoUrl: '',
    bind_mobile: 0,
    session_key: '',
    isAgree:false,
  },
  onLoad: function (options) {
    var that = this;
    my.setNavigationBar({
      title: app.globalData.title
    })
    this.setData({
      logoUrl: app.globalData.logoUrl
    })
  },
    skipXieyi:function(){
    my.navigateTo({
      url: '/pages/agreement/agreement',
    })
  },
  onGotUserInfo: function (e) {
    console.log(e)
    if (e.detail.errMsg == "getUserInfo:fail auth deny") {
      my.showToast({
        content: '授权失败请重试',
        icon: 'none'
      })
    } else {
      my.showLoading({
        content: '登陆中请稍等...',
        mask: true
      })
      if (e.detail.rawData) {
        var data = e.detail.userInfo
        my.setStorageSync({ 'key': 'userInfo', data: data })
        if (my.getStorageSync({ 'key': 'home' }).data.id != undefined) {
          data.area_id = my.getStorageSync({ 'key': 'home' }).data.id
        }
        console.log(data)
        app.request("/auth/weapp-userinfo-sync", data, "POST").then(res => {
          if (res.status == 200) {
            my.showToast({
              content: '授权成功',
              icon: 'none'
            })
            my.setStorageSync({ 'key': 'userInfo', data: res.data })
            // my.navigateBack()
            if (app.globalData.compatible) {
              console.log(app.globalData.compatible)
              my.redirectTo({
                url: '/pages/compatible/index/index',
              })
            } else {
              my.redirectTo({
                url: '/pages/index/index',
              })
            }
          } else {
            my.showToast({
              content: '登陆失败请重试',
              icon: 'none'
            })
            my.redirectTo({
              url: '/pages/index/index',
            })
          }
        })
      } else {
        my.showToast({
          content: '授权失败',
          icon: 'none'
        })
      }
    }
  },
  onAuthError: function () {
    my.showToast({
      content: '授权失败请重试',
      type: 'none'
    });
  },

  getUserPhoneNumber: function (e) {
    console.log(e)
    my.getPhoneNumber({
      success: (res) => {
        console.log(res, "KKKKKKKKKKK")
        let encryptedData = JSON.parse(res.response).response;
        //userObject.phoneInfo = res.response;
        console.log(encryptedData);
        //手机号解密请求
        // phoneAuth(res.response);
        //  my.alert({
        //   title: encryptedData,
        // });4nbLCy+uBSvO8waRPGv3/EsHNqpa68oN8bPAyJqGjCT6UAVIaMuwChCdRsJkrsKRT3lGHdqyaET7YL2IptOLp7pZYW6w4wl9BHDLmx6V9kRindBZMBTYKzp7ov99o/E6rG3fZZCXSNA59aiKMojMA+JIzMRuo3GRBOcq0YEml6I=
        if (encryptedData.length == 172) return;
        let data = {
          phone_detail: JSON.stringify(arr),
          type: '3',
          session_key: encryptedData,
          auth_id: this.data.auth_id
        }
        app.request('/auth/mobileLogin', data, 'POST', app.globalData.req).then(res => {
          console.log(res, "PPPPppp")
          my.setStorageSync({ key: 'userInfo', data: res.data.user })
          my.setStorageSync({ key: 'token', data: res.data.token });
          console.log(my.getStorageSync({ key: 'token' }))
          // my.setStorageSync('token', res.data.token)
          // my.setStorageSync('token_time', res.data.exp)
          // my.setStorageSync('userInfo', res.data.user)
          my.reLaunch({
              url: '/pages/index/index',
          })
        })
      },
      fail: (res) => {
        console.log(res, "error");
      }
    })
  },
  //手机号登陆
  phoneLogin: function () {
    my.navigateTo({
      url: '/pages/phonelogin/phonelogin',
    })
  },
  //暂不登录
  noSign: function () {
    my.reLaunch({
      url: '/pages/index/index',
    })
  },
   //点击同意用户协议
   clickAgree(){
    this.setData({
      isAgree:!this.data.isAgree
    })
  },
  onReady: function () {

  },
  onShow: function () {
    my.hideBackHome()
  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  }
})