let app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    IMG_NAME: app.IMG_NAME,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    
  },

  /**
   * 授权登录
   */
  authorLogin: function (e) {
    let _this = this;
    if (e.detail.errMsg !== 'getUserInfo:ok') {
      return false;
    }
    wx.showLoading({
      title: "正在登录",
      mask: true
    });
    // 执行微信登录
    wx.login({
      success: function (res) {
        //发送请求
        wx.request({
          header: {
            'content-type': 'application/json'
          },
          method: 'POST',
          url: app.DOMAIN_NAME + '/social_user_login/login',
          data: {
            code: res.code,
            rawData: e.detail.rawData,
            encryptedData: e.detail.encryptedData,
            ivStr: e.detail.iv,
            signature: e.detail.signature
          },
          success(res) {
            console.log(res);
            if (res.data.code != 0) {
              wx.showModal({
                confirmText: '好的',
                content: res.data.errmsg || '服务器开小差去了，请重试',
                showCancel: false
              });
            } else if (res.data.code == 0) {
              wx.setStorageSync('userId', res.data.userId)
              wx.setStorageSync('token', res.data.token)
              wx.hideLoading()
              wx.reLaunch({
                url: '/pages/index/index',
              })
            }
          },
          fail(ret) {}
        })
      }
    });
  },

  /**
   * 授权成功 跳转回原页面
   */
  navigateBack: function () {
    wx.navigateBack();
    // let currentPage = wx.getStorageSync('currentPage');
    // wx.redirectTo({
    //   url: '/' + currentPage.route + '?' + App.urlEncode(currentPage.options)
    // });
  },

})