Series Article Directory
Article Directory
foreword
Obtaining the user's mobile phone number is one of the common requirements when developing a WeChat applet. This tutorial will give you a detailed introduction to how to implement the function of obtaining the user's mobile phone number in the front-end and back-end, and how to protect user privacy and security.
1. The front end realizes the acquisition of the user's mobile phone number
In the WeChat applet, the user's login credential code can be obtained by calling wx.login(), and then the code is sent to the backend server, and the backend obtains the user's mobile phone number through the interface provided by WeChat. Specific steps are as follows:
1. The front end obtains the user login credential code
In the logic layer of the front-end page, call the wx.login() method to obtain the user's login credential code and send it to the back-end server.
// 前端页面的逻辑层
wx.login({
success: (res) => {
if (res.code) {
// 获取到用户登录凭证 code
const code = res.code;
// 将 code 发送给后端服务器
wx.request({
url: 'https://your-backend-server.com/getPhoneNumber',
data: {
code: code },
method: 'POST',
success: (res)