// pages/backScreenlogin/backScreenlogin.js
let app = getApp();
Page({

  /**
	 * 页面的初始数据
	 */
  data: {

  },

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

  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {

  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {

  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {
    wxfaceapp.postMsg({
      targetAppid: app.data.accept_message_id,
      content: JSON.stringify({
        bolshow: false,
        moneyBol: true
      }),
      success(res) {

      },
      fail(res) {
        // wx.showToast({
        // 	title: res.reply,
        // 	icon: 'none',
        // 	duration: 2000
        // })
      }
    });
  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

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

  },
  getUserProfileTap(){
    let that = this;
    wx.getUserProfile({
      desc:'获取用户信息',
      success:function(_info){
        console.log("info:", _info);
        let info = {
          'nickName' : _info.userInfo.nickName,
          'avatarUrl' : _info.userInfo.avatarUrl,
          'gender' : _info.userInfo.gender
        };
        that.loginbtn(info);
      }
    });
  },
  loginbtn(info) {
    wx.showLoading({
      title: '登录中',
    });
    setTimeout(function() {
      wx.hideLoading();
    }, 3000);
    var that = this;
    // that.setLoginState();
    wxfaceapp.isLoginOnFaceApp({
      success() {
        //成功，说明此时青蛙App具备登录态，可以进行小程序内登录
        that.setLoginState(info);
      },
      fail(res) {
        wx.showToast({
          title: res.reply,
          icon: 'none',
          duration: 2000
        });
        console.log(res,'登录失败');
        //失败，说明此时青蛙App不具备登录态，需要进行刷脸登录获取登录态，然后才可以进行小程序内登录
      }
    });
  },
  // 登录
  setLoginState: function(_info) {
    var that = this;
    // var _session;
    // var _wx_token;
    wx.login({
      success: function(_json) {
        console.log('login登录成功，获取用户info');
        if (_json.code) {
          // 请求登录接口
          that.requestServerData(app.getFrogproNetAddresss('member.login.index'), "get", {
            code: _json.code,
            info: _info
          }).then(function(res) {
            var _data = res.data;
            if (_data.result == 1) {
              // 存储key
              if (_data.data.user_info) {
                wx.setStorageSync("nickname", _data.data.user_info.nickname);
              }
              if(_data.msg.session){
                wx.setStorageSync('session', _data.msg.session);
              }
              if(_data.msg.wx_token){
                wx.setStorageSync('wx_token', _data.msg.wx_token);
              }
              if(_data.data.store){
                wx.setStorageSync('storeMessage', _data.data.store);
              }
              // try {
              //   wx.setStorageSync('session', _data.msg.session);
              // } catch (e) {}
              // try {
              //   wx.setStorageSync('wx_token', _data.msg.wx_token);
              // } catch (e) {}
              // try {
              //   wx.setStorageSync('storeMessage', _data.data.store);
              // } catch (e) {}
              let Json = {
                wx_token: _data.msg.wx_token,
                session: _data.msg.session,
                store_id: _data.data.store.store_id,
                dispatch_type_id: 2,
                goods_id: _data.data.store.goods_id,
                host: app.data.host,
                uniacid: app.data.uniacid
              };
              console.log('kkkk:::',Json);
              wxfaceapp.postMsg({
                targetAppid: app.data.accept_message_id,
                content: JSON.stringify(Json),
                success(res) {

                },
                fail(res) {
                  wx.showToast({
                    title: res.reply,
                    icon: 'none',
                    duration: 2000
                  });
                  console.log(res,'发送失败');
                }
              });
              wx.hideLoading();
              wx.redirectTo({
                url: '/packageC/frogpro/storeManagement/storeManagement'
              });
              // 设置全局变量session；
            } else {
              wx.showToast({
                title: _data.msg,
                icon: 'none',
                duration: 2000,
                mask: true
              });
              console.log('错误信息:::' + res.errMsg);
            }
            // 异步请求到的数据data
          }).catch(function(res) {
            console.log('错误信息:::' + res.errMsg);
          });
        } else {
          console.log('错误信息:::' + res.errMsg);
        }
      },
      fail: function(res) {
        console.log('登录失败，请求被拒绝！获取用户登录态失败！' + res);
      }
    });
  },
  requestServerData(url, method, data) {
    return new Promise(function(resolve, reject) {
      wx.request({
        url: url,
        data: data,
        header: {
          'Content-Type': 'application/json',
          'Accept': 'application/json'
        },
        method: method,
        success: function(res) {
          resolve(res);
        },
        fail: function(res) {
          reject(res);
        }
      });
    });
  },
});
