Applet using the cloud function to get the phone number

Applets way to obtain the phone number

 First, to get the encrypted data, and then submitted to the server decrypting

Second, the acquired  cloudID, the function of cloud, advantages: direct access to the raw data, save server resources, function as an example below cloud

1. applet xml

<button class="gettel" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber"> 快捷获取</button>

2. Click the button to call the function cloud, cloud passed to the function cloudID

    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
}

 

Published 85 original articles · won praise 45 · views 950 000 +

Guess you like

Origin blog.csdn.net/flysnownet/article/details/103559606
Recommended