taro/微信小程序 微信授权用户信息或者手机号
2020-05-28 本文已影响0人
逸笛
<Button openType="getPhoneNumber" onGetPhoneNumber={this.getPhoneNumber.bind(this)}>立即创建</Button>
getPhoneNumber(e) {
if (e.detail.encryptedData.length <= 0) {
Taro.showToast({
title: "获取微信手机号码失败,请重试",
icon: "none",
duration: 1500
});
return false;
}
api.request({
method: "POST",
url: "/auth/bindPhoneByWx",
data: {
access_token: user.getToken(),
encryptedData: e.detail.encryptedData,
iv: e.detail.iv
},
success: (response) => {
if (response.data.code === 1000) {
const result = response.data.result;
this.setState({ userWxPhone: result.phone });
Taro.setStorageSync("userWxPhone", result.phone);
} else {
Taro.showToast({
title: "授权失败,请重试",
icon: "none",
duration: 1500
});
}
},
error: function (e) { }
});
}