uniapp中微信小程序获取用户信息与手机号
快速获取手机号
·
<!-- #ifdef MP-WEIXIN -->
<view>
<button open-type="getUserInfo" @getuserinfo="wxGetUserInfo">登录</button>
</view>
<view>
<button open-type="getPhoneNumber" @getphonenumber="wxGetPhone">获取手机号</button>
</view>
<!-- #endif -->
//获取手机号
wxGetPhone(e) {
console.log("phone", e);
if (!e.detail.code) {
uni.showToast({
title: '获取手机号失败',
icon: 'none'
})
return;
}
that.decryptPhone(e.detail.code);
},
//解密手机
decryptPhone(obj) {
var that = this;
//传给后台解密,获得手机号
this.$api.getPhone({code:obj})
.then(ss => {
if (ss.code == 200) {
console.log(ss)
} else {
uni.showToast({
title: '获取手机号失败,请重试',
icon: 'none'
})
}
})
.catch(ss => {
uni.showToast({
title: '获取手机号失败,请重试',
icon: 'none'
})
})
},
// 获取用户信息
wxGetUserInfo(e) {
uni.showModal({
title: '温馨提示',
content: '亲,授权微信登录后才能正常使用小程序功能',
success(res) {
console.log(0)
console.log(res)
//如果用户点击了确定按钮
if (res.confirm) {
uni.getUserProfile({
desc: '获取你的昵称、头像、地区及性别',
success: res => {
console.log(res);
console.log(1);
},
fail: res => {
console.log(2);
console.log(res)
//拒绝授权
uni.showToast({
title: '您拒绝了请求,不能正常使用小程序',
icon: 'error',
duration: 2000
});
return;
}
});
} else if (res.cancel) {
//如果用户点击了取消按钮
console.log(3);
uni.showToast({
title: '您拒绝了请求,不能正常使用小程序',
icon: 'error',
duration: 2000
});
return;
}
}
});
},
更多推荐
所有评论(0)