您好,欢迎访问一九零五行业门户网

小程序如何获取手机号信息

小程序如何获取手机号信息    
1、在获取手机号码之前,要先进行登陆。
使用wx.login进行登录,登录成功会返回一个code,将code传给后台,获取登录密钥session_key等信息。将这些信息存入data。
推荐学习:小程序开发
2、使用type=getphonenumber的button获取encrypteddata,iv将这两个信息和session_key传给后台,通过解密返回正确的手机号码。
<button open-type="getphonenumber" bindgetphonenumber="getphonenumber"> 手机号领取</button>
通过点击之后获取到的信息为
在 请求后台接口之前要先用wx.checksession判断登录状态是否过期,如果过期就需要获取新的session_key。
getphonenumber(e) { var msg = e.detail.errmsg, that = this; var that = this; var sessionid=that.data.userinfo.session_key, encrypteddatastr=e.detail.encrypteddata, iv= e.detail.iv; if (msg == 'getphonenumber:ok') { wx.checksession({ success:function(){ that.deciyption(sessionid,encrypteddatastr,iv); }, fail:function(){ wx.login({ success: res => { console.log(res,'sessionkey过期') wx.request('url',{code:res.code},function(res){ var userinfo=res.data.data; wx.setstoragesync('userinfo',userinfo); that.setdata({ userinfo:userinfo }); that.deciyption(userinfo.session_key,encrypteddatastr,iv); }) } }) } }) } }, deciyption(sessionid,encrypteddatastr,iv){ wx.request('url', { sessionid: sessionid, encrypteddatastr:encrypteddatastr, iv: iv }, function (res) { //这个res即可返回用户的手机号码 }) },
以上就是小程序如何获取手机号信息的详细内容。
其它类似信息

推荐信息