Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program user information to get mobile phone number


May 18, 2021 WeChat Mini Program Development Document


Table of contents


Get your phone number

To get the mobile phone number bound by WeChat users, you need to call the wx.login interface first.

Because the user is required to initiate an active trigger to initiate the get phone number interface, the feature is not called by the API and needs to be triggered with a click of the button component.

Note: The interface is currently open to non-individual developers and is open to certified small programs (not including overseas entities). Use with caution, and WeChat reserves the right to permanently reclaim the interface permissions of the small program if the user reports more or is found to be using it in an unnecessary scenario.

How to use it

The value of the button component open-type needs to be set to getPhoneNumber, and when the user clicks and agrees, the encrypted data returned by the WeChat server can be obtained through the bindgetphonenumber event callback, and then decrypted with session_key and app_id on the third-party service side to obtain the phone number.

Attention

Calling the wx.login login in the callback may refresh the login state. A t this point, the server uses code in exchange for sessionKey, which is not the sessionKey used for encryption, causing decryption to fail. Developers are advised to log in advance, or to use checkSession for login checking in callbacks to prevent logins from refreshing login status.

Code example

<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
Page({
  getPhoneNumber (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
  }
})

Returns a description of the parameters

Parameters Type Description The lowest version
encryptedData String Encrypted data for complete user information, including sensitive data, can be found in the encryption data decryption algorithm
iv String Initial vector of the encryption algorithm, see the encrypted data decision algorithm
cloudID string Sensitive data corresponding to the cloud ID, open cloud development of small programs will return, through the cloud call direct access to open data, see cloud call direct access to open data Base Library 2.8.0

The resulting open data is the following json structure:

{
    "phoneNumber": "13580006666",
    "purePhoneNumber": "13580006666",
    "countryCode": "86",
    "watermark":
    {
        "appid":"APPID",
        "timestamp": TIMESTAMP
    }
}
Parameters Type Description
phoneNumber String User-bound mobile phone number (foreign mobile phone number will have area code)
purePhoneNumber String A cell phone number that does not have an area code
countryCode String Code