這篇文章主要為大家詳細(xì)介紹了微信小程序如何獲取用戶手機(jī)號(hào),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
最近在做一款微信小程序,需要獲取用戶手機(jī)號(hào),具體步驟如下:
流程圖:
1、首先,客戶端調(diào)用wx.login,回調(diào)數(shù)據(jù)了包含jscode,用于獲取openid(用戶唯一標(biāo)識(shí))和sessionkey(會(huì)話密鑰)。
2、拿到j(luò)scode后,將其發(fā)送給服務(wù)端,服務(wù)端拿它與微信服務(wù)端做交互獲取openid和sessionkey。具體獲取方法如下:
(1)需要寫(xiě)一個(gè)HttpUrlConnection工具類:
public class MyHttpUrlConnection { private final int mTimeout = 10000; // 超時(shí)時(shí)間 /** * get訪問(wèn) */ 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)然后通過(guò)這個(gè)工具類與微信服務(wù)器建立連接,獲取想要的數(shù)據(jù):
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都是自己開(kāi)發(fā)者賬號(hào)里可以查詢到的,js_code是客戶端發(fā)過(guò)來(lái)的,這樣在返回的數(shù)據(jù)中就可以獲取sessionkey。
3、服務(wù)器A拿到sessionkey后,生成一個(gè)隨機(jī)數(shù)我們叫3rdsession,以3rdSessionId為key,以sessionkey + openid為value緩存到redis或memcached中;因?yàn)槲⑿艌F(tuán)隊(duì)不建議直接將sessionkey在網(wǎng)絡(luò)上傳輸,由開(kāi)發(fā)者自行生成唯一鍵與sessionkey關(guān)聯(lián)。其作用是: (1)、將3rdSessionId返回給客戶端,維護(hù)小程序登錄態(tài)。
(2)、通過(guò)3rdSessionId找到用戶sessionkey和openid。
4、客戶端拿到3rdSessionId后緩存到storage,
5、通過(guò)wx.getUserIinfo可以獲取到用戶敏感數(shù)據(jù)encryptedData 。
6、客戶端將encryptedData、3rdSessionId和偏移量一起發(fā)送到服務(wù)器A
7、服務(wù)器A根據(jù)3rdSessionId從緩存中獲取session_key
8、在服務(wù)器A使用AES解密encryptedData,從而實(shí)現(xiàn)用戶敏感數(shù)據(jù)解密。
解密數(shù)據(jù)需要用到的參數(shù)有三個(gè),分別是:
1、encryptedData(密文)
2、iv(向量)
3、aesKey(密鑰)也就是sessionkey
在解密的時(shí)候要將上述三個(gè)變量做Base64解碼:
byte[] encrypData = UtilEngine.decode(encData); byte[] ivData = UtilEngine.decode(iv); byte[] sessionKey = UtilEngine.decode(session_key);
然后使用AES解密方法進(jìn)行解密:
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); }
這樣在返回的數(shù)據(jù)中就可以拿到用戶的手機(jī)號(hào)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,更多相關(guān)內(nèi)容請(qǐng)關(guān)注PHP中文網(wǎng)!
相關(guān)推薦:
以上就是如何通過(guò)微信小程序獲取用戶手機(jī)號(hào)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注有卡有網(wǎng)。