小程序通过用户授权获取手机号之getPhoneNumber

小程序有一个获取用户很便捷的api,就是通过getPhoneNumber获取用户的已经绑定微信的手机号码。有一点要大家注意,现在微信和注重用户体验,有些方法都是需要用户主动去触发才能调用的,比如getPhoneNumber。

实现思路:

1、通过wx.login获取code,从而获取到用户的openID和sessionKey

2、通过getPhoneNumber获取encryptedData,iv

3、通过参数【encryptedData】 、【iv】 、【sessionKey】  请求后台解密获取用户手机号

直接上干货:

1、用户点击获取用户手机号码按钮

<button class='pop_btn' plain="true"
open-type='getPhoneNumber' bindgetphonenumber="getPhoneNumber">获取用户手机号</button>

2、弹出授权图片:

小程序通过用户授权获取手机号之getPhoneNumber

3、通过解密获取手机号码

小程序通过用户授权获取手机号之getPhoneNumber

直接上代码:

wxlogin: function () {//获取用户的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' // 默认值
},
success: (res) => {
console.log(res);
that.setData({
sessionKey: res.data.session_key
});
}
});
}
});
}

getPhoneNumber: function (e) {//点击获取手机号码按钮

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 {//同意授权
wx.request({
method: "GET",
url: 'https://xxx/wx/deciphering.do',
data: {
encrypdata: ency,
ivdata: iv,
sessionkey: sessionk
},
header: {
'content-type': 'application/json' // 默认值
},
success: (res) => {
console.log("解密成功~~~~~~~将解密的号码保存到本地~~~~~~~~");
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 已经失效,需要重新执行登录流程");
that.wxlogin(); //重新登录
}
});

}

后台代码:

/**
* 解密并且获取用户手机号码
* @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");  
    }

如果我的文章帮助到了大家,减少大家的弯路,愿意打赏的请扫下面的二维码。哈哈~~~随意咯

小程序通过用户授权获取手机号之getPhoneNumber