const app = getApp();

Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },


  
  onLoad() {
    setTimeout(function () {
      wx.hideLoading()
    })

   
  },



  bindGetUserInfo(e) {
    var that = this;
    app.showLoading();



    wx.login({
      success: function (res) {
        console.log(res.code)
        if (res.code) {
          wx.getUserInfo({
            withCredentials: true,
            success: function (res_user) {

              console.log(res_user);
              console.log(res_user.userInfo.avatarUrl);
              console.log(res_user.userInfo.city);
              console.log(res_user.userInfo.country);
              console.log(res_user.userInfo.gender);
              console.log(res_user.userInfo.language);
              console.log(res_user.userInfo.nickName);
              console.log(res_user.userInfo.province);

      
           
              let p_url = 'public/wxlogin';
              let p_data = {
                encryptedData: res_user.encryptedData,
                iv: res_user.iv,
                code: res.code,

                headimgurl: res_user.userInfo.avatarUrl,
                city: res_user.userInfo.city,
                country: res_user.userInfo.country,
                sex: res_user.userInfo.gender,
                language: res_user.userInfo.language,
                nickname: res_user.userInfo.nickName,
                province: res_user.userInfo.province,
              };
              app.postRequest(p_url, p_data, function (res) {    
                console.log(res);
                if (res.code==201){
                  app.cancelLoading();
                  if (res.data.openid && res.data.wx_id){
                          wx.navigateTo({
                            url: '/pages/Landpage/Land/Land'
                          })
                    }
              
                }
                // console.log(res.data.openid);
                // that.setData({
                //   selectArray: 123
                // })

               // 同步传参
               try {
                 wx.setStorageSync('openid', res.data.openid)
                } catch (e) {

                }

                // 同步传参
                try {
                  wx.setStorageSync('wx_id', res.data.wx_id)
                } catch (e) {

                }

           
              })


            }, fail: function () {
              wx.showToast({
                title: '您拒绝了，无法登陆，请重新授权',
                icon: 'none',  // 不显示图标
                mask: true,   //  是否显示透明蒙层，防止触摸穿透	
                duration: 3000
              })
                // console.log('您拒绝了，无法登陆，请重新授权');


            }, complete: function (res) {


            }
          })
        }
      }
    })

  },
  





})  // Page