html
<button class='bottom' type='primary' open-type="getPhoneNumber" withCredentials="true" lang="zh_CN"
@click="getUserInfo" @getphonenumber="getPhoneNumber">授权微信</button>
js部分
//登录并获取token
getUserInfo() {
// 展示加载框
uni.getUserProfile({
lang: 'zh_CN',
desc: '登录',
success: infoRes => {
this.userInfo = infoRes.userInfo; //用户信息,微信头像,昵称等等
console.log(this.userInfo)
uni.login({
provider: 'weixin',
success: res => {
let code = res.code
uni.request({
method: "POST",
url: `${this.$baseUrl}/business/user/login`,
header: {
Accept: 'application/json',
'XX-Device-Type': 'wxapp',
},
data: {
nickname: this.userInfo.nickName,
avatar: this.userInfo.avatarUrl,
code,
},
success: v => {
console.log(res)
}
})
}
});
},
fail(res) {
uni.showToast({
title: '登录授权失败',
icon: 'none',
})
}
});
},
//获取电话号码
getPhoneNumber(e) {
let token = uni.getStorageSync('access_token')
uni.request({
method: "POST",
url: 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' + token,
data: {
code: e.detail.code
},
success: res => {
console.log(res.data.phone_info.phoneNumber)
}
})
},