Acquire Mobile Numbers

Calling thewx.login API is required before acquiring the user's mobile number linked to Weixin.

As the API for acquiring the mobile number can be initiated only by a user’s trigger, click the [button]((button) component to trigger it rather than directly calling the API.

Note: Currently, this API is applicable to non-individual developers, and the Mini Programs which have completed verification (not including overseas entities). It should be used with caution. If a large number of user reports are recorded, or the API is used when not necessary, Weixin has the right to permanently revoke access privileges to this API of the Mini Program.

Usage

Set the open-type value of the [button]((button) component to getPhoneNumber, click to agree, and then you can acquire the encrypted data returned by the Weixin server via bindgetphonenumber event callback. On the third party server, you candecrypt and acquire the mobile number by using session_key and app_id.

Note

During the callback, calling wx.login login may refresh the login status. In this case, the sessionKey that the server exchanges with the code is not the sessionKey that was used during encryption, causing decryption failure. We recommend developers to conduct login in advance, or start with login status check using checkSession during callback, avoiding login status refresh due to login.

Code Sample

<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)
  }
})

Response Parameters

Parameter Type Description Minimum Version
encryptedData String The complete encrypted user data, including the sensitive data. For details, see [Decryption Algorithm for Encrypted Data]((signature#decryption algorithm for encrypted data))
iv String The initial vector of the encryption algorithm. For details, see [Decryption Algorithm for Encrypted Data] ((signature#decryption algorithm for encrypted data))
cloudID String The Cloud ID corresponding to the sensitive data will be returned only after the Mini Program of the Cloud Base is enabled. The open data can be obtained directly via cloud call. For details, see the Directly Get Open Data via Cloud Call 2.8.0

There are two ways to acquire sensitive data, one is to use the [Decryption Algorithm for Encrypted Data] ((open-ability/signature#decryption algorithm for encrypted data)) to decrypt encryptedData in the developer's backend; and the other is to directly get open data nby cloudID via cloud call.

The acquired open data has the following json structure:

{
    "phoneNumber": "13580006666",
    "purePhoneNumber": "13580006666",
    "countryCode": "86",
    "watermark":
    {
        "appid":"APPID",
        "timestamp": TIMESTAMP
    }
}
Parameter Type Description
phoneNumber String The user's mobile number linked to Weixin (overseas numbers may have country codes)
purePhoneNumber String The mobile number without the country code
countryCode String The country code