uni-app获取微信手机号
时间: 2023-12-10 15:02:19 浏览: 167
在uni-app中获取微信手机号的步骤如下:
1. 在uni.login()方法中获取微信提供的code。
2. 通过微信提供的获取微信手机号的方法getphonenumber拿到encryptedData iv两个字段的内容。
3. 通过微信提供的解密方法,可以拿到最终的手机号。
以下是示例代码:
```javascript
// 获取微信手机号
uni.login({
provider: 'weixin',
success: res => {
uni.getUserInfo({
provider: 'weixin',
success: userInfoRes => {
uni.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data: {
appid: 'your appid',
secret: 'your secret',
js_code: res.code,
grant_type: 'authorization_code'
},
success: sessionRes => {
uni.request({
url: 'https://api.weixin.qq.com/wxa/getphonenumber',
method: 'POST',
header: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + sessionRes.data.session_key
},
data: {
encryptedData: userInfoRes.encryptedData,
iv: userInfoRes.iv
},
success: phoneRes => {
console.log(phoneRes.data.phoneNumber)
}
})
}
})
}
})
}
})
```
阅读全文
相关推荐

















