// pages/familyUser/familyUser.js 家校/家园用户注册
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    role: '',
    schoolNo:'',
    classNo: '',
    schoolOwner: '',
    classOwner: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      role: options.role,
      schoolNo: options.schoolNo,
      classNo: options.classNo,
      schoolOwner: options.schoolOwner,
      classOwner: options.classOwner
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  onReady: function () {
    // 生命周期函数--监听页面初次渲染完成
    //如果用户已经注册则跳转到首页
    var config = wx.getStorageSync('config');
    if (config.registed == true && config.unionId !== undefined) {
      //跳转到首页
      wx.switchTab({
        url: '../index/index',
      });
    }else{
      setTimeout(function () {
        config = wx.getStorageSync('config');
        if (config.registed == true && config.unionId !== undefined) {
          //跳转到首页
          wx.switchTab({
            url: '../index/index',
          });
        }
      }, 1000);
    }
  },
    

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //家校/家园用户注册
  regist(e){
    //获取本地存储的openId和unionId
    var config = wx.getStorageSync('config');
    var that = this;
    // 获取用户信息
    wx.getUserInfo({
      success: res => {
        // 可以将 res 发送给后台解码出 unionId
        app.globalData.userInfo = res.userInfo
        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        if (this.userInfoReadyCallback) {
          this.userInfoReadyCallback(res)
        }
        
        wx.request({
          url: app.url +'ajax_get_unionid.php',
          data:{
            openid:config.openId,
            iv:res.iv,
            encryptedData: res.encryptedData

          },
          success(resp){
            if(resp.data.status=='OK'){
              wx.request({
                url: app.url + 'ajax_user_register.php',
                data: {
                  unionId: resp.data.result.unionId,
                  openId: config.openId,
                  role: that.data.role,
                  name: app.globalData.userInfo.nickName,
                  headerImg: app.globalData.userInfo.avatarUrl,
                  schoolNo: that.data.schoolNo,
                  classNo: that.data.classNo,
                  schoolOwner: that.data.schoolOwner,
                  classOwner: that.data.classOwner
                },
                header: { 'content-type': 'application/json' },
                method: 'GET',
                dataType: 'json',
                success: function (res) {
                  if (res.data.status == 'OK' && res.data.result.recommend){
                    wx.redirectTo({
                      url: '/pages/memberChange/memberChange',
                    })
                  }else{
                    //跳转到首页
                    wx.switchTab({
                      url: '../index/index',
                    });
                  }
                },
                fail: function (res) {
                  wx.showToast({
                    title: res.data.result.message,
                    icon: 'success',
                    duration: 2000
                  });
                  //跳转到首页
                  wx.switchTab({
                    url: '../index/index',
                  });
                },
                complete: function (res) { },
              })
            }
          }
        })
      }
    })
  }
})