最近在做一款微信小程序,需要獲取用戶手機(jī)號,具體步驟如下:

流程圖:

1、首先,客戶端調(diào)用wx.login,回調(diào)數(shù)據(jù)了包含js_code,用于獲取openid(用戶唯一標(biāo)識)和sessionkey(會話密鑰)。

2、拿到j(luò)s_code后,將其發(fā)送給服務(wù)端,服務(wù)端拿它與微信服務(wù)端做交互獲取openid和sessionkey。具體獲取方法如下:

(1)、需要寫一個HttpUrlConnection工具類:

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)、然后通過這個工具類與微信服務(wù)器建立連接,獲取想要的數(shù)據(jù):

	String url = "http://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ā)者賬號里可以查詢到的,js_code是客戶端發(fā)過來的,這樣在返回的數(shù)據(jù)中就可以獲取sessionkey。

3、 服務(wù)器A拿到session_key后,生成一個隨機(jī)數(shù)我們叫3rd_session,以3rdSessionId為key,以session_key + openid為value緩存到redis或memcached中;因?yàn)槲⑿艌F(tuán)隊(duì)不建議直接將session_key在網(wǎng)絡(luò)上傳輸,由開發(fā)者自行生成唯一鍵與session_key關(guān)聯(lián)。其作用是:

(1)、

將3rdSessionId返回給客戶端,維護(hù)小程序登錄態(tài)。

(2)、通過3rdSessionId找到用戶session_key和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ù)有三個,分別是:

1、 encryptedData(密文)

2、iv(向量)

3、aesKey(密鑰)也就是sessionkey

在解密的時候要將上述三個變量做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ī)號。