1.方法一
export async function getPhoneNumber(data = {}) {
// 小程序的密钥 let SECRET = 'xxxxxxxxxxxxxxxxxxxxxx' return new Promise((resolve, reject) => { wepy.request({ url: `https://api.weixin.qq.com/cgi-bin/token`, data: { grant_type: 'client_credential', appid: 'xxxxxxxxxxxxxxxxxxxxx', secret: SECRET, }, }) .then((res) => { let ACCESS_TOKEN = res.data.access_token wepy .request({ method: 'post', url: `https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=${ACCESS_TOKEN}`, data: { code: data.code, }, }) .then((res) => { console.log(res.data) resolve(res.data.phone_info.phoneNumber) }) }) }) }
使用的是小程序的wepy框架,使用原生的小程序代码,将wepy.改为wx.即可
2.方法二
<button class="btn" open-type="getPhoneNumber" type="primary" bindgetphonenumber="onWeChatLogin">微信一键登录</button>
使用小程序Button里面的 open-type="getPhoneNumber" 来获取手机号的code,将其传给后端去解析,返回手机号
标签:code,手机号,res,wepy,获取,token,data From: https://www.cnblogs.com/panghu77/p/17490538.html