//index.js
//获取应用实例
let method = require("../../utils/reuqest.js")
const app = getApp()

Page({
  data: {
    id:'1',
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    sessionKey:'',
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  getSesionkey(){
    let that = this;
    wx.login({
      success: function (res) { 
        method.getRequest('/login/getSessionKey/'+res.code,data=>{
          that.setData({
            sessionKey :data.data
          })
          if(typeof(data.data!==String) ){
            wx.setStorageSync('Authorization', data.data.authorization)
            wx.setStorageSync('wxImg', data.data.loginUser.wxName)
            wx.setStorageSync('wxName', data.data.loginUser.wxHeadImg)
            wx.switchTab({
              url: '/pages/home/home',
            })
          }       
        })   
      }
    })
  },
  //获取用户信息
  // getUserInfo(e) {
  //   this.sessionKey()
  //   let that = this;
  //   wx.getSetting({
  //     success: function (res) {
  //       if (res.authSetting['scope.userInfo']) {
  //         wx.navigateBack({
  //           delta: 0,
  //         })
  //       }else{
  //         wx.getUserInfo({
  //           success(res) {

  //           }
  //         })
  //       }
  //     }
  //   })
  // },
  onLoad: function () {
  //  this.getSesionkey();
    // this.getUserInfo()
  },
  onShow:function(){
  //  let authorization=wx.getStorageSync('authorization');
  //  if(authorization){
  //   wx.switchTab({
  //     url: '/pages/home/home',
  //   })
  //  }
  },
  bindGetUserInfo: function (e) {
    this.getSesionkey()
    let that = this;
    setTimeout(()=>{
      let postData = {
        sessionKey: that.data.sessionKey,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        type: "1"
      }
      method.postRequest('/login', postData, data => {
        if (data.statusCode == 0) {
          wx.showToast({
            title: '授权成功',
          })
          wx.switchTab({
            url: '/pages/home/home',
          })
        }
      },2000)
    })
    // let postData = {
    //   sessionKey: that.data.sessionKey,
    //   encryptedData: e.detail.encryptedData,
    //   iv: e.detail.iv,
    //   type: "1"
    // }
    // method.postRequest('/login', postData, data => {
    //   if (data.statusCode == 0) {
    //     wx.showToast({
    //       title: '授权成功',
    //     })
    //     wx.switchTab({
    //       url: '/pages/home/home',
    //     })
    //   }
    // })
  }

})