Home > Article > WeChat Applet > How to obtain mobile phone number information in the mini program
How to obtain mobile phone number information through the mini program
1. Before obtaining the mobile phone number, you must log in first.
Use wx.login to log in. If the login is successful, a code will be returned. The code will be passed to the background to obtain the login key session_key and other information. Store this information in data.
Recommended learning: Small program development
2. Use the button of type="getPhoneNumber" to obtain encryptedData, and iv pass these two information and session_key to In the background, the correct mobile phone number is returned through decryption.
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"> 手机号领取 </button>
The information obtained after clicking is
Before requesting the background interface, you must first use wx.checkSession to determine whether the login status has expired. If it expires, you need to obtain a new 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即可返回用户的手机号码 }) },
The above is the detailed content of How to obtain mobile phone number information in the mini program. For more information, please follow other related articles on the PHP Chinese website!