微信小程序获取用户手机号的方法与步骤

why 106 2024-08-27

这篇文章主要为大家详细介绍了微信小程序如何获取用户手机号,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

最近在做一款微信小程序,需要获取用户手机号,具体步骤如下:

流程图:

image.png

1、首先,客户端调用wx.login,回调数据了包含jscode,用于获取openid(用户唯一标识)和sessionkey(会话密钥)。

2、拿到jscode后,将其发送给服务端,服务端拿它与微信服务端做交互获取openid和sessionkey。具体获取方法如下:

(1)需要写一个HttpUrlConnection工具类:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

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;

 }

}


(2)然后通过这个工具类与微信服务器建立连接,获取想要的数据:


1

2

3

4

5

6

7

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");


其中appid和secret都是自己开发者账号里可以查询到的,js_code是客户端发过来的,这样在返回的数据中就可以获取sessionkey。

3、服务器A拿到sessionkey后,生成一个随机数我们叫3rdsession,以3rdSessionId为key,以sessionkey + openid为value缓存到redis或memcached中;因为微信团队不建议直接将sessionkey在网络上传输,由开发者自行生成唯一键与sessionkey关联。其作用是: (1)、将3rdSessionId返回给客户端,维护小程序登录态。

(2)、通过3rdSessionId找到用户sessionkey和openid。

4、客户端拿到3rdSessionId后缓存到storage,
5、通过wx.getUserIinfo可以获取到用户敏感数据encryptedData 。
6、客户端将encryptedData、3rdSessionId和偏移量一起发送到服务器A
7、服务器A根据3rdSessionId从缓存中获取session_key
8、在服务器A使用AES解密encryptedData,从而实现用户敏感数据解密。

解密数据需要用到的参数有三个,分别是:

1、encryptedData(密文)
2、iv(向量)
3、aesKey(密钥)也就是sessionkey

在解密的时候要将上述三个变量做Base64解码:


1

2

3

byte[] encrypData = UtilEngine.decode(encData);

byte[] ivData = UtilEngine.decode(iv);

byte[] sessionKey = UtilEngine.decode(session_key);


然后使用AES解密方法进行解密:


1

2

3

4

5

6

7

8

9

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);

}


这样在返回的数据中就可以拿到用户的手机号。

以上就是本文的全部内容,希望对大家的学习有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:微信小程序实现微信支付的具体步骤详解
下一篇:微信车载小程序,开启汽车智能化新时代
相关文章

 发表评论

暂时没有评论,来抢沙发吧~