// shoppingGuid/page/pages/welcomGuider/welcomGuider.js
const wxService = require('../../../../utils/wxService')
const app = getApp();
wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    code : null,
    jsCodeSession : {},
    btnIsDisabled : true,
    show : false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const currentEnv = wx.getStorageSync('_qyWeChat');
    console.log(currentEnv)
    if(!currentEnv){
      wx.showToast({
        title: '请在企业微信中使用导购!',
        icon : 'none',
        duration : 2000
      });
      this.setData({
        show : true,
      });
      return ;
    }

    wx.showLoading({
      title: '检查登录状态..',
    });

    // this.qyLogin();
    // return ;

    //先判断是是不是已经登录了
    let currentUserInfo = wx.getStorageSync('guidBaseInfo');
    let userInfo = wx.getStorageSync('guidInfo');
    if (currentUserInfo){
      wx.qy.checkSession({
        success : res => {
          wx.redirectTo({
            url: '/shoppingGuid/page/pages/home/home',
          });
        },
        fail : res => { //失效了
          console.log('check session fail , starting login ----')
          this.qyLogin();
        }
      });
    }
    else{
      this.qyLogin();
    }
  },
  //企业微信登录
  qyLogin(){
    const currentEnv = wx.getStorageSync('_qyWeChat');
    if (currentEnv) {
      wx.qy.login({
        success: res => {
          this.setData({
            code: res.code
          });
          this.jscode2session(res.code);
        },
        fail: res => {
          console.log('fail -----', res)
        }
      })
    }
    else {
      //不是在企业微信内打开的 提示信息
      wx.showToast({
        title: '请在企业微信内打开!',
        icon: 'none'
      });
    }
  },

  //获取session_key
  jscode2session(code){
    wxService.post(`/member/qiyeweixin/jscode2session?brandId=${app.globalData.brandId}&code=${code}`).then(r => {
      this.setData({
        jsCodeSession : r.data.data,
        btnIsDisabled : false,
      });
    })
  },

  //获取企业微信信息
  onTapGetEnterpriseUserInfo(){
    const currentEnv = wx.getStorageSync('_qyWeChat');
    if(currentEnv){
      wx.qy.getEnterpriseUserInfo({
        success: res => {
          this.getGuidUserInfo(res);
        },
        fail: err => {
          console.log('获取企业微信信息失败，失败信息是----', err)
        }
      })
    }
    else{
      //不是在企业微信内打开的 提示信息
      wx.showToast({
        title: '请在企业微信内打开!',
        icon: 'none'
      });
    }
  },

  //授权获取用户信息之后
  getGuidUserInfo(userInfo){
    wx.showLoading({
      title: '正在登录..',
    });
    //判断是在企业微信中登录
    let param = {
      brandId: app.globalData.brandId,
      deviceid: this.data.jsCodeSession.deviceid,
      userid: this.data.jsCodeSession.userid,
      corpid: this.data.jsCodeSession.corpid,
    };
    wxService.post('/member/qiyeweixin/minaLogin', param).then(r => {
      wx.setStorageSync('guidInfo', userInfo);
      wx.setStorageSync('token', r.data.data.token);
      console.log('login token -----', r.data.data.token)
      let qiyeLoginUserInfo = r.data.data;
      wx.setStorageSync('guidBaseInfo', qiyeLoginUserInfo);
      wx.hideLoading();
      wx.redirectTo({
        url: '/shoppingGuid/page/pages/home/home',
      });
    })

  },

  //进入商城
  onTapToMall(e){
    wx.redirectTo({
      url: '/pages/userCenter/userCenter',
    })
  },

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

  },

})