// pages/login/login.js
import {
  getOpenidSessionKey
} from '../../utils/httpUtil';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: undefined,
    canIUseGetUserProfile: false
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    if (options.index) {
      this.setData({
        index: options.index
      })
    }
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getUserProfile: function (e) {
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (res) => {
        console.log('getUserProfile',res);
        getOpenidSessionKey((res) => {
          console.log('getUserProfilegetUserProfile',res)
          wx.showToast({
            title: '登录成功',
            icon: 'fail',
            duration: 1000,
            success: () => {
              const pages = getCurrentPages();
              const prevPage = pages[pages.length - 2];
              prevPage.setData({
                fromLoginIndex: this.data.index, // 有id就塞到第一位
              }, () => {
                wx.navigateBack({
                  delta: 1
                })
              })
            }
          })
        }, (error) => {
          wx.showToast({
            title: '登录失败',
            icon: 'fail',
            duration: 1000,
            success: () => {
              wx.navigateBack()
            }
          })
        });
      }
    })
  },
  impower: function (e) {
    console.log(e)
    var myEventDetail = {} // detail对象,提供给事件监听函数
    var myEventOption = {} // 触发事件的选项
    getOpenidSessionKey((res) => {
      console.log(res)
      wx.showToast({
        title: '登录成功',
        icon: 'fail',
        duration: 1000,
        success: () => {
          const pages = getCurrentPages();
          const prevPage = pages[pages.length - 2];
          prevPage.setData({
            fromLoginIndex: this.data.index, // 有id就塞到第一位
          }, () => {
            wx.navigateBack({
              delta: 1
            })
          })
        }
      })
    }, (error) => {
      wx.showToast({
        title: '登录失败',
        icon: 'fail',
        duration: 1000,
        success: () => {
          wx.navigateBack()
        }
      })
    });
  },
  touchMove: function () {
    return false
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})