微信小程序用戶(hù)授權(quán)獲取手機(jī)號(hào)(getPhoneNumber)
前言
小程序有一個(gè)獲取用戶(hù)很便捷的api,就是通過(guò)getPhoneNumber獲取用戶(hù)的已經(jīng)綁定微信的手機(jī)號(hào)碼。有一點(diǎn)要大家注意,現(xiàn)在微信和注重用戶(hù)體驗(yàn),有些方法都是需要用戶(hù)主動(dòng)去觸發(fā)才能調(diào)用的,比如getPhoneNumber。
實(shí)現(xiàn)思路:
1、通過(guò)wx.login獲取code,從而獲取到用戶(hù)的openID和sessionKey
2、通過(guò)getPhoneNumber獲取encryptedData,iv
3、通過(guò)參數(shù)【encryptedData】 、【iv】 、【sessionKey】 請(qǐng)求后臺(tái)解密獲取用戶(hù)手機(jī)號(hào)
直接上干貨:
1、用戶(hù)點(diǎn)擊獲取用戶(hù)手機(jī)號(hào)碼按鈕
<button class='pop_btn' plain="true" open-type='getPhoneNumber' bindgetphonenumber="getPhoneNumber">獲取用戶(hù)手機(jī)號(hào)</button>
2、彈出授權(quán)圖片:

3、通過(guò)解密獲取手機(jī)號(hào)碼

直接上代碼:
wxlogin: function() { //獲取用戶(hù)的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' // 默認(rèn)值
},
success: (res) = >{
console.log(res);
that.setData({
sessionKey: res.data.session_key
});
}
});
}
});
}
getPhoneNumber: function(e) { //點(diǎn)擊獲取手機(jī)號(hào)碼按鈕
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 { //同意授權(quán)
wx.request({
method: "GET",
url: 'https://xxx/wx/deciphering.do',
data: {
encrypdata: ency,
ivdata: iv,
sessionkey: sessionk
},
header: {
'content-type': 'application/json' // 默認(rèn)值
},
success: (res) = >{
console.log("解密成功~~~~~~~將解密的號(hào)碼保存到本地~~~~~~~~");
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(); //重新登錄
}
});
}
后臺(tái)代碼:
/**
* 解密并且獲取用戶(hù)手機(jī)號(hào)碼
* @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");
}
總結(jié)
到此這篇關(guān)于微信小程序用戶(hù)授權(quán)獲取手機(jī)號(hào)的文章就介紹到這了,更多相關(guān)小程序授權(quán)獲取手機(jī)號(hào)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 微信小程序獲取手機(jī)號(hào)授權(quán)用戶(hù)登錄功能
- 微信小程序如何獲取用戶(hù)手機(jī)號(hào)
- 微信小程序獲取用戶(hù)綁定手機(jī)號(hào)方法示例
- 微信小程序如何通過(guò)用戶(hù)授權(quán)獲取手機(jī)號(hào)(getPhoneNumber)
- 微信小程序如何同時(shí)獲取用戶(hù)信息和用戶(hù)手機(jī)號(hào)
- 微信小程序獲取用戶(hù)手機(jī)號(hào)碼的詳細(xì)步驟
- 微信小程序中獲取用戶(hù)手機(jī)號(hào)授權(quán)登錄詳細(xì)步驟
- 微信小程序獲取用戶(hù)手機(jī)號(hào)碼詳細(xì)教程(前端+后端)
相關(guān)文章
layer關(guān)閉彈出窗口觸發(fā)表單提交問(wèn)題的處理方法
今天小編就為大家分享一篇layer關(guān)閉彈出窗口觸發(fā)表單提交問(wèn)題的處理方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09
微信公眾號(hào)h5使用微信支付及支付寶支付的步驟(前端)
微信H5支付是一種支付解決方案,主要適用于商戶(hù)在微信客戶(hù)端外的移動(dòng)端網(wǎng)頁(yè)上展示商品或服務(wù)的場(chǎng)景,這篇文章主要給大家介紹了關(guān)于微信公眾號(hào)h5使用微信支付及支付寶支付(前端)的相關(guān)資料,需要的朋友可以參考下2024-07-07
用javascript獲取textarea中的光標(biāo)位置
Javascript一向以他的靈活隨意而著稱(chēng),這也使得它的功能可以非常的強(qiáng)大,而由于沒(méi)有比較好的調(diào)試工具,又使得它使用起來(lái)困難重重,尤其使對(duì)于一些初學(xué)者,更是感覺(jué)到無(wú)從下手。今天探討的問(wèn)題是用javascript獲取textarea中光標(biāo)的位置。2008-05-05
javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)簡(jiǎn)易計(jì)算器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02

