text: const WXAPI = require('../../wxapi/main')
  var app = getApp();
  Page({
  
      /**
       * 页面的初始数据
       */
      data: {
  
      },
  
      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
  
      },
  
      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function () {
  
      },
  
      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
  
      },
  
      /**
       * 生命周期函数--监听页面隐藏
       */
      onHide: function () {
  
      },
  
      /**
       * 生命周期函数--监听页面卸载
       */
      onUnload: function () {
  
      },
  
      /**
       * 页面相关事件处理函数--监听用户下拉动作
       */
      onPullDownRefresh: function () {
  
      },
  
      /**
       * 页面上拉触底事件的处理函数
       */
      onReachBottom: function () {
  
      },
  
      /**
       * 用户点击右上角分享
       */
      onShareAppMessage: function () {
  
      },
      bindGetUserInfo: function (e) {
          if (!e.detail.userInfo) {
              return;
          }
          if (app.globalData.isConnected) {
              wx.setStorageSync('userInfo', e.detail.userInfo)
              this.login();
          } else {
              wx.showToast({
                  title: '当前无网络',
                  icon: 'none',
              })
          }
      },
      login: function () {
          const that = this;
          const token = wx.getStorageSync('token');
          if (token) {
              WXAPI.checkToken(token).then(function (res) {
                  if (res.code != 0) {
                      wx.removeStorageSync('token')
                      that.login();
                  } else {
                      // 回到原来的地方
                      app.navigateToLogin = false;
                      wx.navigateBack();
                  }
              })
              return;
          }
          wx.login({
              success: function (res) {
                  WXAPI.login(res.code).then(function (res) {
                      if (res.code == 10000) {
                          // 去注册 zgf
                          // that.registerUser();
                          return;
                      }
                      if (res.code != 0) {
                          // 登录错误
                          wx.hideLoading();
                          wx.showModal({
                              title: '',
                              content: '无法登录，请重试',
                              showCancel: false
                          })
                          return;
                      }
                      wx.getUserInfo({
                          withCredentials: false,
                          success: data => {
                              console.log("从前端获取当前用户信息，然后再去更新后台用户数据 : " + JSON.stringify(data.userInfo));
                              //  {
                              //     "nickName":"风声",
                              //     "gender":1,
                              //     "language":"zh_CN",
                              //     "city":"Nanjing",
                              //     "province":"Jiangsu",
                              //     "country":"China",
                              //     "avatarUrl":"https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTK8fyGFaEeep5zVTBpibZNL9CicvicuSTiaI25CwZy8AeKPmpmdkRCOxuWtHcibS4Xp2yj6rYXXTKvubjg/132"
                              //     }
  
                              var userInfo = data.userInfo;
                              userInfo["uid"] = res.data.uid;
                              WXAPI.updateUserInfo(userInfo).then(function (data) {
                                  if (data.code != 0) {
                                      wx.showModal({
                                          title: '',
                                          content: data.msg,
                                          showCancel: false
                                      })
                                  }
                              });
                          },
                          fail: () => {},
                          complete: () => {}
                      });
                      // 这里的token，后端返回的是当前用户对应的openid,唯一标识当前用户
                      wx.setStorageSync('token', res.data.token);
                      // 当前用户对应后端存储的自增长id
                      wx.setStorageSync('uid', res.data.uid);
                      // 回到原来的地方放
                      app.navigateToLogin = false;
                      wx.navigateBack();
                  }).catch(error => {
                      console.log("登录请求失败", error);
                  });
              }
          })
      },
      registerUser: function () {
          let that = this;
          wx.login({
              success: function (res) {
                  let code = res.code; // 微信登录接口返回的 code 参数，下面注册接口需要用到
                  wx.getUserInfo({
                      success: function (res) {
                          let iv = res.iv;
                          let encryptedData = res.encryptedData;
                          let referrer = '' // 推荐人
                          let referrer_storge = wx.getStorageSync('referrer');
                          if (referrer_storge) {
                              referrer = referrer_storge;
                          }
                          // 下面开始调用注册接口
                          console.log("encryptedData = " + encryptedData + ", iv = " + iv + ", referrer = " + referrer);
                          WXAPI.register({
                              code: code,
                              encryptedData: encryptedData,
                              iv: iv,
                              referrer: referrer
                          }).then(function (res) {
                              wx.hideLoading();
                              that.login();
                          })
                      }
                  })
              }
          })
      }
  })