Home WeChat Applet Mini Program Development How to get user's mobile phone number through WeChat mini program

How to get user's mobile phone number through WeChat mini program

Jun 23, 2018 am 11:17 AM
WeChat applet Phone number user

This article mainly introduces in detail how the WeChat applet obtains the user's mobile phone number. It has certain reference value. Interested friends can refer to it.

I am currently working on a WeChat applet. , you need to obtain the user's mobile phone number. The specific steps are as follows:

Flow chart:


1. First, the client calls wx.login and callback The data contains jscode, which is used to obtain openid (user unique identification) and sessionkey (session key).

2. After getting the jscode, send it to the server. The server uses it to interact with the WeChat server to obtain the openid and sessionkey. The specific acquisition method is as follows:

(1) You need to write a HttpUrlConnection tool class:

public class MyHttpUrlConnection { 
 private final int mTimeout = 10000; // 超时时间 
 /** 
 * get访问 
 */ 
 public String[] requestJson(String url) { 
 return request(url); 
 } 
 private String[] request(String connurl) { 
 String[] resultStr = new String[]{"", ""}; 
 StringBuilder resultData = new StringBuilder(""); 
 HttpURLConnection conn = null; 
 try { 
  URL url = new URL(connurl); 
  conn = (HttpURLConnection) url.openConnection(); 
  conn.setRequestMethod("GET"); 
  conn.setUseCaches(false); 
  conn.setConnectTimeout(mTimeout); 
  conn.connect(); 
  int resultCode = conn.getResponseCode(); 
  InputStreamReader in; 
  if (resultCode == 200) { 
  in = new InputStreamReader(conn.getInputStream()); 
  BufferedReader buffer = new BufferedReader(in); 
  String inputLine; 
  while ((inputLine = buffer.readLine()) != null) { 
   resultData.append(inputLine); 
   resultData.append("\n"); 
  } 
  buffer.close(); 
  in.close(); 
  } 
  resultStr[0] = resultData.toString(); 
  resultStr[1] = resultCode + ""; 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } finally { 
  if (conn != null) { 
  conn.disconnect(); 
  } 
 } 
 return resultStr; 
 } 
}
Copy after login

(2) Then use this tool class to communicate with WeChat The server establishes a connection and obtains the desired data:

 String url = "https://api.weixin.qq.com/sns/jscode2session?appid=""&secret=""&js_code=" 
   + jsCode + "&grant_type=authorization_code"; 
 String res[] = connection.requestJson(url); 
 System.out.println(res[0]); 
 JSONObject object = JSON.parseObject(res[0]); 
 String openId = object.getString("openid"); 
 String session_key = object.getString("session_key");
Copy after login

The appid and secret can be queried in your own developer account, and js_code is sent by the client. Come over, so that the session key can be obtained in the returned data.

3. After server A gets the sessionkey, it generates a random number we call 3rdsession, with 3rdSessionId as the key and sessionkey openid as the value and cached in redis or memcached; because the WeChat team does not recommend directly storing the sessionkey on the network Upload transmission, the developer generates a unique key and associates it with the session key. Its functions are: (1) Return the 3rdSessionId to the client to maintain the mini program login state.

(2). Find the user sessionkey and openid through 3rdSessionId.

4. The client gets the 3rdSessionId and caches it in storage.
5. User sensitive data encryptedData can be obtained through wx.getUserIinfo.
6. The client sends encryptedData, 3rdSessionId and offset together to server A
7. Server A obtains session_key from the cache based on 3rdSessionId
8. Use AES to decrypt encryptedData on server A to achieve user sensitivity Data decryption.

There are three parameters needed to decrypt data, namely:

1, encryptedData (ciphertext)
2, iv (vector)
3, aesKey (Key) That is, sessionkey

needs to Base64 decode the above three variables when decrypting:

byte[] encrypData = UtilEngine.decode(encData); 
byte[] ivData = UtilEngine.decode(iv); 
byte[] sessionKey = UtilEngine.decode(session_key);
Copy after login

Then Use the AES decryption method to decrypt:

public static byte[] decrypt(byte[] key, byte[] iv, byte[] encData) 
 throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, 
 InvalidKeyException, BadPaddingException, IllegalBlockSizeException { 
 AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv); 
 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
 SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); 
 cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); 
 return cipher.doFinal(encData); 
}
Copy after login

In this way, the user's mobile phone number can be obtained in the returned data.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the code for WeChat applet to upload pictures to the server

The WeChat applet returns a multi-level page Implementation method

WeChat applet development friend list letter list jumps to the corresponding position

The above is the detailed content of How to get user's mobile phone number through WeChat mini program. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Xiaohongshu account to find users? Can I find my mobile phone number? How to use Xiaohongshu account to find users? Can I find my mobile phone number? Mar 22, 2024 am 08:40 AM

With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the "Discover" button in the lower right corner, and then select the "Notes" option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the "Follow" button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select "Personal Information"

How to set up if WeChat does not allow adding by mobile phone number How to set up if WeChat does not allow adding by mobile phone number Mar 12, 2024 pm 05:04 PM

How to set up WeChat to disallow adding via mobile phone number? In WeChat APP, you can disallow adding WeChat via mobile phone number, but most users don’t know how to set it up to not allow WeChat to be added via mobile phone number. Next is WeChat brought by the editor to users It is not allowed to add setting method tutorials through mobile phone numbers. Interested users can come and take a look! Tutorial on how to use WeChat. How to set up WeChat when adding a mobile phone number is not allowed. 1. First open the WeChat APP, enter the main page, click [Me] in the lower right corner to enter, and select the [Settings] function; 2. Then enter the setting function page and find [Friend Permissions] ] Service function; 3. Then on the friend permissions interface, slide the button behind [Verification is required when adding me as a friend]; 4. Finally, on the page shown below, slide [Mobile Phone Number]

How to change the mobile phone number of Xiaohongshu account? Will there be any impact if I change my mobile phone number? How to change the mobile phone number of Xiaohongshu account? Will there be any impact if I change my mobile phone number? Mar 22, 2024 am 08:11 AM

In the digital age, Xiaohongshu has become a platform for many people to share their lives and discover good things. Over time, we may need to change the mobile phone number on the account. So, how to change Xiaohongshu’s mobile phone number? 1. How to change the mobile phone number of Xiaohongshu account? 1. Log in to your Xiaohongshu account and enter the “My” page. 2. Click "..." in the upper right corner and select "Settings". 3. On the settings page, find the "Account and Security" option and click to enter. 4. On the Account and Security page, find the "Mobile Phone Number" option and click to enter. 5. At this time, the page will display the current mobile phone number, click "Change Mobile Number". 6. Enter the new mobile phone number, receive the verification code, and fill it out. 7. Set a new password to ensure account security. 8. After completing the above steps,

Log in to Ubuntu as superuser Log in to Ubuntu as superuser Mar 20, 2024 am 10:55 AM

In Ubuntu systems, the root user is usually disabled. To activate the root user, you can use the passwd command to set a password and then use the su- command to log in as root. The root user is a user with unrestricted system administrative rights. He has permissions to access and modify files, user management, software installation and removal, and system configuration changes. There are obvious differences between the root user and ordinary users. The root user has the highest authority and broader control rights in the system. The root user can execute important system commands and edit system files, which ordinary users cannot do. In this guide, I'll explore the Ubuntu root user, how to log in as root, and how it differs from a normal user. Notice

Xianyu WeChat mini program officially launched Xianyu WeChat mini program officially launched Feb 10, 2024 pm 10:39 PM

Xianyu's official WeChat mini program has quietly been launched. In the mini program, you can post private messages to communicate with buyers/sellers, view personal information and orders, search for items, etc. If you are curious about what the Xianyu WeChat mini program is called, take a look now. What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3. If you want to use it, you must activate WeChat payment before you can purchase it;

Tutorial: How to delete a normal user account in Ubuntu system? Tutorial: How to delete a normal user account in Ubuntu system? Jan 02, 2024 pm 12:34 PM

Many users have been added to the Ubuntu system. I want to delete the users that are no longer in use. How to delete them? Let’s take a look at the detailed tutorial below. 1. Open the terminal command line and use the userdel command to delete the specified user. Be sure to add the sudo permission command, as shown in the figure below. 2. When deleting, be sure to be in the administrator directory. Ordinary users do not have this permission. , as shown in the figure below 3. After the delete command is executed, how to judge whether it has been truly deleted? Next we use the cat command to open the passwd file, as shown in the figure below 4. We see that the deleted user information is no longer in the passwd file, which proves that the user has been deleted, as shown in the figure below 5. Then we enter the home file

What is sudo and why is it important? What is sudo and why is it important? Feb 21, 2024 pm 07:01 PM

sudo (superuser execution) is a key command in Linux and Unix systems that allows ordinary users to run specific commands with root privileges. The function of sudo is mainly reflected in the following aspects: Providing permission control: sudo achieves strict control over system resources and sensitive operations by authorizing users to temporarily obtain superuser permissions. Ordinary users can only obtain temporary privileges through sudo when needed, and do not need to log in as superuser all the time. Improved security: By using sudo, you can avoid using the root account during routine operations. Using the root account for all operations may lead to unexpected system damage, as any mistaken or careless operation will have full permissions. and

How to register two B-site numbers with one mobile phone number? How does Station B unbind its mobile phone? How to register two B-site numbers with one mobile phone number? How does Station B unbind its mobile phone? Mar 21, 2024 pm 10:10 PM

Bilibili (Bilibili), as a video sharing website very popular among Chinese young people, has attracted a large number of users. Some users hope to have two Bilibili accounts so that they can be managed and used separately. So, how to register two B-site numbers with one mobile phone number? This article will focus on this issue and how to unbind the mobile phone. 1. How to register two B-site numbers with one mobile phone number? 1. Register a new account: First, open the Bilibili App on your mobile phone or log in to the official website, click the "Register" button, and select the registration method. You can use your mobile phone number, email or third-party account (such as WeChat, QQ, etc.) to register. 2. When registering an account, please fill in the necessary information according to the system prompts, including mobile phone number, verification code, and set password. Be sure to use different accounts

See all articles