您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何在微信小程序中獲取用戶手機(jī)號(hào),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
流程圖:
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)需要寫一個(gè)HttpUrlConnection工具類:
public class MyHttpUrlConnection { private final int mTimeout = 10000; // 超時(shí)時(shí)間 /** * 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)然后通過這個(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都是自己開發(fā)者賬號(hào)里可以查詢到的,js_code是客戶端發(fā)過來的,這樣在返回的數(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ò)上傳輸,由開發(fā)者自行生成唯一鍵與sessionkey關(guān)聯(lián)。其作用是: (1)、將3rdSessionId返回給客戶端,維護(hù)小程序登錄態(tài)。
(2)、通過3rdSessionId找到用戶sessionkey和openid。
4、客戶端拿到3rdSessionId后緩存到storage,
5、通過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); }
關(guān)于如何在微信小程序中獲取用戶手機(jī)號(hào)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。