var Token = require('../../utils/util.js');

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 用户信息相关
    userInfo: null,
    hasUserInfo: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'),
    
    // 登录状态相关
    phone: wx.getStorageSync('uid'),
    uidFlag: wx.getStorageSync('uidFlag'),
    code: '',
    
    // 界面布局相关
    statusBarHeight: 0,
    height: 0,
  },

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

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

  // 初始化状态栏高度
  initStatusBarHeight() {
    const statusBarHeight = wx.getSystemInfoSync().statusBarHeight
    this.setData({
      statusBarHeight,
      height: 46 + statusBarHeight
    })
  },

  // 登录相关方法
  getLogin() {
    wx.login({
      success: res => {
        app.globalData.code = res.code
        this.setData({ code: res.code })
        
        this.getUserInfo()
      }
    })
  },

  getUserInfo() {
    wx.getUserInfo({
      success: (data) => {
        const { encryptedData, iv } = data
        app.globalData.encryptedData = encryptedData
        app.globalData.iv = iv
      }
    })
  },

  // 获取手机号并登录
  getPhoneNumber() {
    wx.request({
      url: app.globalData.url + 'app/Ghdwechat/wechatLogin',
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        code: app.globalData.code,
        encryptedData: app.globalData.encryptedData,
        iv: app.globalData.iv
      },
      success: this.handleLoginResponse.bind(this)
    })
  },

  handleLoginResponse(res) {
    if (res.data.erro === 0) {
      this.saveUserData(res.data)
      this.navigateToHome()
    }
    
    wx.showToast({
      title: res.data.msg,
      icon: 'none'
    })
  },

  saveUserData(data) {
    const { uid } = data
    wx.setStorageSync('phone', uid)
    wx.setStorageSync('uid', uid)
    wx.setStorageSync('token', uid)
    wx.setStorageSync('uidFlag', true)
    
    this.setData({
      uidFlag: true,
      phone: uid
    })
    
    app.globalData.uid = uid
  },

  navigateToHome() {
    wx.switchTab({
      url: '/pages/home/home'
    })
  },

  // 页面导航方法
  shoucang() {
    wx.navigateTo({
      url: '/pages/myshoucang/myshoucang_index?type=1'
    })
  },

  pintuan() {
    wx.navigateTo({
      url: '/pages/order/order'
    })
  },

  qianbao() {
    wx.navigateTo({
      url: '/pages/wodeqianbao/qianbao'
    })
  },

  guanyu() {
    wx.navigateTo({
      url: '/pages/guanyu/guanyu'
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },
})