微信小程序如何获取用户openId、手机号?
∇ 获取用户openId
调用wx.login,在成功回调中,拿自己小程序的appId、secret请求接口 url:
‘https://api.weixin.qq.com/sns/jscode2session?appid=’ + appId + ‘&secret=’ + secret + ‘&js_code=’ +code + ‘&grant_type=authorization_code’,请求成功即可拿到openid和session-key
wx.request({
url: ‘https://api.weixin.qq.com/sns/jscode2session?appid=’ + appId + ‘&secret=’ + secret + ‘&js_code=’ +
code + ‘&grant_type=authorization_code’,
header: {
‘content-type’: ‘application/json’
},
success: function(res) {
// console.log(‘bindGetUserInfo res为’);
// console.log(res);
that.data.openid = res.data.openid; //返回openid
that.data.session_key = res.data.session_key;
// console.log(‘openid为’ + that.data.openid);
// console.log(‘session_key为’ + that.data.session_key);
}
})
∇ 获取用户手机号码
小程序的appId必须要在微信公众平台完成微信认证(里面需要上传一些企业的一些证件和信息)才能有获取用户手机号的权限
<button open-type=”getPhoneNumber” bindgetphonenumber=”getPhoneNumber” >获取加密手机号</button>
//获取手机号码
getPhoneNumber(e) {
var that = this;
// console.log(e.detail.errMsg)
// console.log(e.detail.iv)
// console.log(‘getPhoneNumber为’ + e.detail.encryptedData);
// console.log(e.detail.encryptedData)
that.data.userPhoneNumber = e.detail.encryptedData;
that.data.userIv = e.detail.iv;
wx.showToast({
title: ‘手机号获取成功’,
icon: ‘success’,
duration: 1500
})
},
最后拿用户的获取到的e.detail.iv、和e.detail.encryptedData(加密的手机号)和前面拿到过的session-key传给后台,交给后台解密即可
<来客推(www.laiketui.com)是国内知名商城系统及商城网站建设提供商,为企业级商家提供零售商城、B2B2C多用户商城系统、社区团购、微信分销系统、小程序商城、商城系统等多端商城网站建设解决方案>
申明:本网站部分文章和图片来源网络编辑,如有侵权及时沟通删除,来客电商原创文章,转载请注明来源。