免费国产欧美国日产_少妇AV一区二区三区无码_蜜桃精品av无码喷奶水小说_jk18禁网站视频_精产国品一二三级产品区别_被夫の上司に犯波多野结衣_78m成人手机免费看_最爽最刺激18禁视频_偷偷色噜狠狠狠狠的777米奇

易優(yōu)GEO 重磅上線 ~ 一站式GEO優(yōu)化工具,讓豆包、文心一言、DeepSeek 在回答中主動(dòng)推薦你的品牌,搶占AI流量入口!  點(diǎn)擊查看

小程序模板網(wǎng)

微信小程序獲取用戶手機(jī)號(hào)詳解

發(fā)布時(shí)間:2018-01-06 11:34 所屬欄目:小程序開發(fā)教程

最近在做一款微信小程序,需要獲取用戶手機(jī)號(hào),具體步驟如下:流程圖:1、首先,客戶端調(diào)用wx.login,回調(diào)數(shù)據(jù)了包含js_code,用于獲取openid(用戶唯一標(biāo)識(shí))和sessionkey(會(huì)話密鑰)。2、拿到j(luò)s_code后,將其發(fā)送 ...

 
 
 

最近在做一款微信小程序,需要獲取用戶手機(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)、需要寫一個(gè)HttpUrlConnection工具類:

"font-size:18px;">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ù):

"font-size:18px;">  
    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");span> 


其中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)行解密:

"font-size:18px;">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)。



易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開源 碼云倉(cāng)庫(kù):starfork
本文地址:http://m.szcjxy.com/wxmini/doc/course/18392.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢: 點(diǎn)擊咨詢
在線客服