原生小程序一键获取手机号

1.效果图

2.代码index.wxml

html 复制代码
 <!-- 获取手机号 利用手机号快速填写的功能,将button组件 open-type 的值设置为 getPhoneNumber-->
  <button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">获取手机号</button>```
2.index.js

```html
Page({
  data: {
  },
  getPhoneNumber: function (e) {
    var that = this
    // 一定是getPhoneNumber里面的code
    const code = e.detail.code
    wx.login({//通过wx.login来获取code
      success: res => {
        if (res.code) {
          wx.getUserInfo({
            success: res1 => {
              wx.request({
                url: '后端接口',//获取到后端解析后的号码返回给前端
                method: 'POST',
                data: {
                  code: code,
                  encryptedData: res1.encryptedData,
                  iv: res1.iv
                },
                success: res => {
                  that.setData({
                    phone: res.data.phoneNumber
                  })
                }
              })
            }
          })
        } else {
          console.log('获取失败!' + res.errMsg)
        }
      }
    })
  }
})

注意:获取手机号的功能好像只允许经过认证的小程序使用,如果未认证只能使用测试号才可以

相关推荐
MoonMoonLee2 天前
小程序配置
小程序
2501_916007473 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
JQweryuiop3 天前
中小型游戏陪练工作室数字化管理实践:基于七彩屋电竞护航小程序订单、履约与绩效系统的落地复盘
大数据·游戏·小程序·架构
Ai-_Man3 天前
您您这可以把Mistral AI的多个会话比如说。左侧的多个会话一次性导出吗?不是单条会话里面的多次会对话。
人工智能·ai·小程序·电脑
2501_916008893 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
微擎应用市场3 天前
简单拼车小程序系统:实现出行与物流资源的高效精准匹配
小程序
2501_916008893 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
2601_963870203 天前
基于springboot的综合网上购物商场系统
微信小程序·小程序·课程设计
2601_963869953 天前
力序健身房管理系统
微信小程序·小程序
一 乐3 天前
二手交易平台|基于springboot + vue二手交易平台(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序