How to Obtain Mobile Number by Mini Program
1. Get the encrypted data and submit it to the server for decryption,
2. Get cloudID, use cloud function, advantage: get raw data directly, save server resources
1.wxml
<button class="gettel" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"> 获取手机号</button>
2. Click the button to call the cloud function and pass the cloudID to the cloud function
getPhoneNumber(e) {
var that = this;
wx.cloud.callFunction({
name: 'getMobile',
data: {
weRunData: wx.cloud.CloudID(e.detail.cloudID),
}
}).then(res => {
that.setData({
mobile: res.result,
})
}).catch(err => {
console.error(err);
});
},
3. Cloud function getMobile
// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
// 云函数入口函数
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
var moblie = event.weRunData.data.phoneNumber;
return moblie
}