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

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)
        }
      }
    })
  }
})

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

相关推荐
2501_915921432 小时前
详细解析,iOS 应用上架 App Store 的完整流程与指南
android·ios·小程序·https·uni-app·iphone·webview
微三云 - 廖会灵 (私域系统开发)3 小时前
小程序商城系统安全运维剖析:漏洞、风控与长效迭代方案
运维·小程序·系统安全
T01156183 小时前
独立开发者商单实战|商超门店小程序(自提 + 同城配送):小程序全端页面 & 业务流程总图汇总
微信小程序·小程序·个人开发·前端实战
T01156183 小时前
独立开发者商单实战|商超门店小程序(自提 + 同城配送)用户端 & 后台联动踩坑上篇
小程序
梦想的颜色5 小时前
UniApp 四端对齐实战:iOS App / Android App / 小程序 / H5 兼容坑点与完整解决方案|AI 辅助多端落地
android·ios·小程序·uni‑app·uniapp 四端适配·多端兼容·ai 生成 uni‑app
黄华SJ520it20 小时前
二二复制定点裂变双轨商城系统开发:原理、架构与实战指南
运维·小程序·架构·系统开发
小码哥0681 天前
2026陪诊小程序与APP开发技术分析
大数据·人工智能·小程序
00后程序员张1 天前
使用Instruments工具深入分析iOS应用性能与启动时间优化
android·macos·ios·小程序·uni-app·cocoa·iphone
程序喵大人1 天前
【C++进阶】STL容器与迭代器 - 10 把容器、迭代器和数据流串成一个小程序
开发语言·c++·容器·小程序·迭代器·stl
2501_916007472 天前
Python实现HTTPS爬虫的完整指南:使用requests、BeautifulSoup、Selenium和Scrapy
爬虫·python·ios·小程序·https·uni-app·iphone