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

  /**
   * 页面的初始数据
   */
  data: {
    imgs:[],
  },
  // 登陆
  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) => {
      if (res.data.code == 20000) {
        wx.setStorageSync('token', res.data.data.token)
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    }).catch((errMsg) => { })
  },

  //获取启动页图片
  getImg(e) {
    let that = this;
    let url = '/wxapp/public/loginImg';
    let param = {}
    app.post(url, param).then((res) => {
      if (res.data.code == 20000) {
        that.setData({
          imgs:res.data.data.img
        })
      }
    }).catch((errMsg) => { })
  },

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

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

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})