前言
小程序有一個獲取用戶很便捷的api,就是通過getPhoneNumber獲取用戶的已經(jīng)綁定微信的手機號碼。有一點要大家注意,現(xiàn)在微信和注重用戶體驗,有些方法都是需要用戶主動去觸發(fā)才能調(diào)用的,比如getPhoneNumber。
實現(xiàn)思路:
1、通過wx.login獲取code,從而獲取到用戶的openID和sessionKey
2、通過getPhoneNumber獲取encryptedData,iv
3、通過參數(shù)【encryptedData】 、【iv】 、【sessionKey】 請求后臺解密獲取用戶手機號
直接上干貨:
1、用戶點擊獲取用戶手機號碼按鈕
1
2
3
|
< button class = 'pop_btn' plain = "true" open-type = 'getPhoneNumber' bindgetphonenumber = "getPhoneNumber" >獲取用戶手機號</ button > |
2、彈出授權圖片:
3、通過解密獲取手機號碼
直接上代碼:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
wxlogin: function () { //獲取用戶的openID和sessionKey var that = this ; wx.login({ //獲取code 使用wx.login得到的登陸憑證,用于換取openid success: (res) = >{ wx.request({ method: "GET" , url: 'https://xxxwx/wxlogin.do' , data: { code: res.code, appId: "appIdSbcx" , appKey: "appKeySbcx" }, header: { 'content-type' : 'application/json' // 默認值 }, success: (res) = >{ console.log(res); that.setData({ sessionKey: res.data.session_key }); } }); } }); } getPhoneNumber: function (e) { //點擊獲取手機號碼按鈕 var that = this ; wx.checkSession({ success: function () { console.log(e.detail.errMsg) console.log(e.detail.iv) console.log(e.detail.encryptedData) var ency = e.detail.encryptedData; var iv = e.detail.iv; var sessionk = that.data.sessionKey; if (e.detail.errMsg == 'getPhoneNumber:fail user deny' ) { that.setData({ modalstatus: true }); } else { //同意授權 wx.request({ method: "GET" , url: 'https://xxx/wx/deciphering.do' , data: { encrypdata: ency, ivdata: iv, sessionkey: sessionk }, header: { 'content-type' : 'application/json' // 默認值 }, success: (res) = >{ console.log( "解密成功~~~~~~~將解密的號碼保存到本地~~~~~~~~" ); console.log(res); var phone = res.data.phoneNumber; console.log(phone); }, fail: function (res) { console.log( "解密失敗~~~~~~~~~~~~~" ); console.log(res); } }); } }, fail: function () { console.log( "session_key 已經(jīng)失效,需要重新執(zhí)行登錄流程" ); that.wxlogin(); //重新登錄 } }); } |
后臺代碼:
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
|
/** * 解密并且獲取用戶手機號碼 * @param encrypdata * @param ivdata * @param sessionkey * @param request * @return * @throws Exception */ @RequestMapping(value = "deciphering" , method = RequestMethod.GET) public @ResponseBody String deciphering(String encrypdata, String ivdata, String sessionkey, HttpServletRequest request) { byte[] encrypData = Base64.decode(encrypdata); byte[] ivData = Base64.decode(ivdata); byte[] sessionKey = Base64.decode(sessionkey); String str= "" ; try { str = decrypt(sessionKey,ivData,encrypData); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(str); return str; } public static String decrypt(byte[] key, byte[] iv, byte[] encData) throws Exception { 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 new String(cipher.doFinal(encData), "UTF-8" ); } |
總結
到此這篇關于微信小程序用戶授權獲取手機號的文章就介紹到這了,更多相關小程序授權獲取手機號內(nèi)容請搜索服務器之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/chen_pan_pan/article/details/80606658