2 个回答

你检查下你调用时用户登录状态,如果刷新了没有使用加密时的sessionKey会导致调用失败,你是否判定了第三方的调用会回调参数,默认的获取手机号是不用验证码的
参考:
获取手机号

wxml

<button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授权登录</button>
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机 </button>

js

Page({

  /**
   * 页面的初始数据
   */
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权,可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              //获取用户敏感数据密文和偏移向量
              if (res.userInfo) {
                //数据库存储用户首次基本信息
                wx.request({
                  url: 'https:/xxxx',
                  data: {
                    userid: wx.getStorageSync('userid'),
                    avatarUrl: res.userInfo.avatarUrl,
                    city: res.userInfo.city,
                    country: res.userInfo.country,
                    gender: res.userInfo.gender,
                    nickName: res.userInfo.nickName,
                    province: res.userInfo.province
                  },
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  method: 'POST',
                  success: function (res) {
                    //console.log(res);
                    wx.showToast({
                      title: '登录成功',
                    })
                  }
                })
              }
            }
          })
        }
      }
    })
  },

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

  getPhoneNumber: function (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
  },
}

效果图

图片描述
图片描述

我顺序点击登录与收获手机

如果你的手机号在任何小程序没有授权过(任何小程序)它第一次会来短信,只要授权过就不会来短信了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题