小程序获取手机号码信息 ,接口服务器 使用laravel + Easywechat
参考资料
注意事项:只能使用button 按钮
wxml
<button class="cu-tag margin-top text-white justify-start " open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">{{userInfo.mobile ? userInfo.mobile : '手机号码未提供,请点击授权~'}}</button>
js
getPhoneNumber: function(e) {
let that = this, token = wx.getStorageSync('token');
if (e.detail.errMsg =='getPhoneNumber:ok'){
app.util.request({
url: 'mobile',
data: { token: token, encryptedData: e.detail.encryptedData, iv: e.detail.iv },
success: function (res) {
that.baseData();//重新获取用户基本信息
}
})
}else{
wx.showToast({
title: '绑定手机号失败',
})
}
},
后台部分代码
public function mobile(Request $request, User $user){
....
$wechat = Factory::miniProgram(config('wechat'));
$res = $wechat->encryptor->decryptData($session_key, $request->iv, $request->encryptedData);
....
}