评论

关于用户未关注公众号也可以获取unionID方法

解密encryptedData获取unionID方法关键片段

js
getUserInfo: function (e) {
var encryptedData = e.detail.encryptedData;
var iv = e.detail.iv;
//上面两个参数是加密内容
//用户按了允许授权按钮后需要处理的逻辑方法体
if (e.detail.userInfo) {
wx.setStorageSync(‘hasUserInfo’, true);
wx.setStorageSync(‘avatarUrl’, e.detail.userInfo.avatarUrl);
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true,
avatarUrl: wx.getStorageSync(‘avatarUrl’)
})
// 登录
wx.login({
success: res => {
console.log(res)
wx.request({
url: wx.getStorageSync(‘serverurl’) + “/advisorySmallUser/getuserInfo”,
data: {
code: res.code,
encryptedData: encryptedData,
iv: iv
},
method: “POST”,
header: {
‘content-type’: ‘application/x-www-form-urlencoded’ // 默认值
},
success: function (res) {
console.log(res)
if (res.data.status == ‘0’ ){
wx.showToast({
title: “登录失败,未获取到用户关键信息,请退出重新进入”,
icon: “none”,
duration: 8000
})
}else{
wx.request({
url: wx.getStorageSync(‘serverurl’) + “/advisorySmallUser/getCheckUser”,
data: {
unionid: wx.getStorageSync(‘unionId’),
avatarUrl: wx.getStorageSync(‘avatarUrl’)
},
method: “POST”,
header: {
‘content-type’: ‘application/x-www-form-urlencoded’ // 默认值
},
success: function (res) {
if (res.data.status == 1) {
if (res.data.data) {
//data 不为空
//存在,跳转到欢迎页
wx.reLaunch({
url: “…/indexc/indexc”
})
} else {
wx.setStorageSync(‘hasUserInfo’, true);
//不存在,则跳转到注册页
// wx.navigateTo({
// url: “…/customer/customer”
// })
}
}
}
})
}
}
});
}
})
} else {
console.log(“用户按了拒绝按钮”)
//用户按了拒绝按钮
wx.showModal({
title: ‘警告’,
content: ‘您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!’,
showCancel: false,
confirmText: ‘返回授权’,
success: function (res) {
if (res.confirm) {
wx.setStorageSync(‘hasUserInfo’, false);
console.log(‘用户点击了“返回授权”’)
}
}
})
}
}

后台Java部分
@RequestMapping(value = “/getuserInfo”, method = RequestMethod.POST)
@ResponseBody
public JSONObject getUserInfo(String code, String encryptedData, String iv) {
JSONObject jsondata = new JSONObject();
if (StringUtils.isEmpty(code)) {
jsondata.put(ConstantUtils.STATUS, ConstantUtils.ZERO);
jsondata.put(ConstantUtils.MSG, “code不能为空”);
log.error(“code不能为空”);
return jsondata;
}
String appid = “appid”;
String appsecret = “appsecret”;
jsondata = MgWeChat.getOpenId(appid, appsecret, code);
if (jsondata.get(“openid”) != null) {
if (!jsondata.containsKey(“unionid”)) {
// 被加密的数据
byte[] dataByte = Base64.decode(encryptedData);
// 加密秘钥
byte[] keyByte = Base64.decode(jsondata.get(“session_key”).toString());
// 偏移量
byte[] ivByte = Base64.decode(iv);
try {
// 如果密钥不足16位,那么就补足. 这个if 中的内容很重要
int base = 16;
if (keyByte.length % base != 0) {
int groups = keyByte.length / base + (keyByte.length % base != 0 ? 1 : 0);
byte[] temp = new byte[groups * base];
Arrays.fill(temp, (byte) 0);
System.arraycopy(keyByte, 0, temp, 0, keyByte.length);
keyByte = temp;
}
// 初始化
Security.addProvider(new BouncyCastleProvider());
Cipher cipher = Cipher.getInstance(“AES/CBC/PKCS7Padding”, “BC”);
SecretKeySpec spec = new SecretKeySpec(keyByte, “AES”);
AlgorithmParameters parameters = AlgorithmParameters.getInstance(“AES”);
parameters.init(new IvParameterSpec(ivByte));
cipher.init(Cipher.DECRYPT_MODE, spec, parameters);// 初始化
byte[] resultByte = cipher.doFinal(dataByte);
if (null != resultByte && resultByte.length > 0) {
String result = new String(resultByte, “UTF-8”);
System.out.println(result);
JSONObject jSONObject = JSONObject.parseObject(result);
String unionId = jSONObject.get(“unionId”).toString();
jsondata.put(“unionid”, unionId);
}
return jsondata;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidParameterSpecException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
} else {
jsondata.put(“unionid”, jsondata.get(“unionid”));
jsondata.put(“openid”, jsondata.get(“openid”));
jsondata.put(“session_key”, jsondata.get(“session_key”));
jsondata.put(ConstantUtils.STATUS, ConstantUtils.ONE);
jsondata.put(ConstantUtils.MSG, ConstantUtils.SUCCESS);
}
} else {
System.out.println(“未获取到用户信息”);
jsondata.put(“unionid”, null);
jsondata.put(“openid”, null);
jsondata.put(“session_key”, null);
jsondata.put(ConstantUtils.STATUS, ConstantUtils.ZERO);
jsondata.put(ConstantUtils.MSG, ConstantUtils.SUCCESS);
}
return jsondata;
}

最后一次编辑于  10-18  
点赞 0
收藏
评论