// pages/start/start.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    openid:'',
    session_key:'',
    encrypted_data:'',
    iv:'',
  },

  // 登陆
  start(e) {
    this.setData({
      encrypted_data: e.detail.encryptedData,
      iv: e.detail.iv
    })
    let that = this;
    let url = 'wxapp/public/login';
    let param = {
      openid: this.data.openid,
      session_key: this.data.session_key,
      encrypted_data: this.data.encrypted_data,
      iv: this.data.iv,
    }
    app.post(url, param).then((res) => {
      console.log(res)
      if (res.data.code == 20000) {
        wx.setStorageSync('token', res.data.data.token)
        wx.switchTab({
          url: '/pages/yogaHome/yogaHome',
        })
      }else if(res.data.code==41000){
          wx.showToast({
            title: res.data.msg,
            icon:'none'
          })
      }

    }).catch((errMsg) => {})

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    wx.login({
      success: (res) => {
        console.log(res)
        let url = 'wxapp/public/getSessionKey';
        let params = {
          code: res.code,
        }
        app.post(url, params).then((res) => {
          console.log(res)
          if (res.data.code == 20000) {
            this.setData({
              openid: res.data.data.openid,
              session_key: res.data.data.session_key
            })
          }

        }).catch((errMsg) => {
          console.log(errMsg)
        })
      }
    });
  },

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

  },

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

  },

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

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

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

  },

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

  },

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

  }
})