Home  >  Article  >  WeChat Applet  >  How to obtain mobile phone number information in the mini program

How to obtain mobile phone number information in the mini program

angryTom
angryTomOriginal
2020-03-26 11:16:527188browse

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

How to obtain mobile phone number information in the mini program

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 == &#39;getPhoneNumber:ok&#39;) {
      wx.checkSession({
        success:function(){
          that.deciyption(sessionID,encryptedDataStr,iv);
        },
        fail:function(){
          wx.login({
            success: res => {
              console.log(res,&#39;sessionkey过期&#39;)
              wx.request(&#39;url&#39;,{code:res.code},function(res){
                var userinfo=res.data.data;
                wx.setStorageSync(&#39;userinfo&#39;,userinfo);
                that.setData({
                  userinfo:userinfo
                });
                that.deciyption(userinfo.Session_key,encryptedDataStr,iv);
              })
            }
          })
        }
      })
    }
  },
 deciyption(sessionID,encryptedDataStr,iv){
    wx.request(&#39;url&#39;, {
      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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn