The phone number associated with the virtual account is not the real phone number, and for some cases, phone number verification is required.Weixin Mini Program, Prioritize the use of real account testing Solve the mobile phone verification code problem, refer to Use real account tested
If you really need virtual account support, you can add the logic of Mock mobile number to the code of Weixin Mini Program to achieve the function of verification through mobile number.
The specific process of virtual account Mock mobile number is as follows:
# Mock process
- Weixin Mini Program get mobile number information
code/encryptedData purePhoneNumber- The mobile phone number belongs to the virtual test number binding mobile phone number (12066600001, 12066600002, 12066600003, 12066600004), then replace it with the mobile phone number that needs a mock
# New Version Mock Sample
Starting with the base library 2.21.2, the interface for acquiring the mobile number has been upgraded with security. Here is the mock guide for the new version of the interface
- The ``gets the
code through themethod binding to [[TAG-1-AND]]`
# Through the business background Mock mobile phone number
- Business Back Office Call via service interface [phonenumber.getPhoneNumber HTTPS
] (https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/phonenumber/phonenumber.getPhoneNumber.html#HTTPS-%E8%B0%83%E7%94%A8) consume
codeget phone number informationphone_info phone_info.purePhoneNumberIn [12066600001, 12066600002...], replace it with the mobile number required by the business, such as13400000001
# Mock mobile number through cloud services
wx-server-sdkopenapi.phonenumber.getPhoneNumberInterface , get the mobile phone number informationphoneInfo throughcode ``phoneInfo.purePhoneNumberIn [12066600001, 12066600002...], replace it with the mobile number required by the business, such as13400000001- Code examples
// Cloud function getPhoneNumber
// getPhoneNumber/index.js
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
// Get phoneNumber cloud function entry function
exports.main = async (event, context) => {
const res = await cloud.openapi.phonenumber.getPhoneNumber({
code: event.code
})
if (["12066600001", "12066600002"].indexOf(res.phoneInfo.purePhoneNumber) > -1) {
const mockPhoneInfo = {
countryCode: "86",
phoneNumber: "13400000001",
purePhoneNumber: "13400000001",
watermark: res.phoneInfo.watermark
}
return mockPhoneInfo
}
return res.phoneInfo
}
// Weixin Mini Program
// getPhoneNumber.wxml
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
// getPhoneNumber.js
Page({
async getPhoneNumber(res) {
const result = await wx.cloud.callFunction({
name: 'getPhoneNumber',
data: {
code: res.detail.code
}
})
console.log("phone info is:", result.result)
}
})
# Old Version Mock Example
Compatibility scheme for older version of base library less than 2.21.2
- Encrypted mobile phone information is obtained via the
bindinggetPhoneNumbermethodencryptedData,iv,cloudID`
# Through the business background Mock mobile phone number
- Business back-office Decrypt cell phone number information via encrypted data decryption algorithm phone_info`
phone_info.purePhoneNumberIn [12066600001, 12066600002...], replace it with the mobile number required by the business, such as13400000001
# Mock mobile number through cloud services
wx-server-sdkgetOpenDataInterface , get mobile phone number informationphoneInfo throughcloudID ``phoneInfo.purePhoneNumberIn [12066600001, 12066600002...], replace it with the mobile number required by the business, such as13400000001- Code examples
// Cloud function getOpenData
// getOpenData/index.js
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
// Get openId cloud function entry function
exports.main = async (event, context) => {
const res = await cloud.getOpenData({
list: event.openData.list
})
for (let item of res.list) {
if (item.data && item.data.purePhoneNumber && ["12066600001", "12066600002"].indexOf(item.data.purePhoneNumber) > -1) {
// 获取手机号的请求且是测试号
const mockPhoneInfo = {
countryCode: "86",
phoneNumber: "13400000001",
purePhoneNumber: "13400000001",
watermark: item.data.watermark
}
item.data = mockPhoneInfo
}
}
return res.list
}
// Weixin Mini Program
// getPhoneNumber.wxml
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"></button>
// getPhoneNumber.js
Page({
async getPhoneNumber(res) {
const result = await wx.cloud.callFunction({
name: 'getOpenData',
data: {
openData: {
list: [res.detail.cloudID]
}
}
})
console.log("phone info is:", result.result[0].data)
}
})
For Mock virtual account location or request Request information, refer to Virtual account configuration Mock information