getphone.js 2.2 KB
// pages/getphone/getphone.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    session_key: '',
    code: '',
    phone:""
  },

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

  //获取sessionkey
  getsessionkey() {
    let that = this;
    wx.login({
      success: (res) => {
        console.log(res)
        that.setData({
          code: res.code
        })

        let url = 'user/getSessionKey',
          params = {
            code: that.data.code
          }
        app.post(url, params).then((res) => {
          console.log(res);
          that.setData({
            session_key: res.session_key
          })
        }).catch((err) => {

        })

      }
    });

  },

  //微信授权手机号
  getPhoneNumber(e) {
    console.log(e)



    let that = this;
    let url = 'user/getMobile',
      params = {
        sessionKey: that.data.session_key,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv
      }
    app.post(url, params,"post").then((res) => {
      console.log(res);
      that.setData({
        phone:res.tel
      })
      wx.navigateTo({
       
        url: '/pages/homepage/homepage',
      })
      that.bangdingshouji()

    }).catch((err) => {
      console.log(err)

    })
  },
  bangdingshouji() {
    let that = this;
    let url = 'user/phone',
      params = {
        phone:that.data.phone
      }
    app.post(url, params,"post").then((res) => {
      console.log(res);




    }).catch((err) => {

    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})