const util = require('../func/request')

//获取登录code
const getWxCode = () => {
  wx.login({
    success: res => {
      app.data.wxCode = res.code
    }
  })
}

//检测是否获取手机号
const isGetPhone = (e) => {
  if (e.detail.errMsg == 'getPhoneNumber:ok') {
    return true
  } else {
    return false
  }
}

//获取手机号
const getPhone = (e) => {
  
    if (!isGetPhone(e)) {
      return({
        state: false,
        msg: '我们需要获取您的手机号才能进行实名登录哦!'
      })
    };
    var code = app.data.wxCode;
    return util.request_rlxf({
      url: '/api/next/doGetPhone',
      data: {
        code: code,
        iv: e.detail.iv,
        encryptedData: e.detail.encryptedData,
        type:3
      },
      method: 'post',
      type: 'application/json'
    })
    .then((suc) => {
      getWxCode();
      return ({
        state: true,
        msg: suc
      })
    })
    .catch((err) => {
      getWxCode();
      return ({
        state: false,
        msg: err
      })
    })
}

module.exports = getPhone