2***@qq.com
2***@qq.com
  • 发布:2021-09-27 10:12
  • 更新:2021-11-26 16:14
  • 阅读:1308

#插件讨论# 【 uni-id - DCloud前端团队 】wxBizDataCrypt解密函数时不时解密失败

分类:uni-app
关联插件: uni-id

如下调用代码。但有时能解密成功,有时又提示解密失败,请问这个问题怎么破,感谢。
getphonenumber(e) {
let that = this;
uni.showLoading({
title: '登录中...',
mask: true
});
uni.login({
provider: 'weixin',
success(res) {
console.log("code:" + res.code)
console.log(e.detail.errMsg)
console.log("iv:" + e.detail.iv)
console.log("encryptedData:" + e.detail.encryptedData)
uniCloud.callFunction({
name: 'user-center',
data: {
action: 'wxBizDataCrypt',
params: {
iv: e.detail.iv,
code: res.code,
encryptedData: e.detail.encryptedData
}
},
success: (res) => {
console.log(JSON.stringify(res.result))
if (res.result.code == 0) {
let phoneNubmer = res.result.phoneNumber;
that.setVerifyCode(phoneNubmer, function() {
that.loginBySms(phoneNubmer);
});
} else {
uni.showModal({
content: "解密失败,请重新登录",
showCancel: false
})
}
uni.hideLoading();
}
});
},
fail(err) {
uni.hideLoading();
console.error('授权登录失败:' + JSON.stringify(err));
}
})
返回失败结果如下:
{
"data": {
"code": 80802,
"message": "解密失败:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt",
"msg": "解密失败:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt"
},
"header": {
"access-control-expose-headers": "Date,x-fc-request-id,x-fc-error-type,x-fc-code-checksum,x-fc-invocation-duration,x-fc-max-memory-usage,x-fc-log-result,x-fc-invocation-code-version",
"content-disposition": "attachment",
"content-length": "236",
"content-type": "application/json",
"date": "Mon, 27 Sep 2021 01:55:47 GMT",
"x-fc-code-checksum": "14931192961446868056",
"x-fc-invocation-duration": "278",
"x-fc-invocation-service-version": "LATEST",
"x-fc-max-memory-usage": "30.61",
"x-fc-request-id": "9b56a772-2673-424e-91aa-5bbb9750b182",
"x-serverless-request-id": "ac1409161632707746891192793",
"x-serverless-runtime-version": "1.2.1"
},
"success": true
}

返回成功结果如下:
{
"data": {
"code": 0,
"mobile": "18620128043"
},
"header": {
"access-control-expose-headers": "Date,x-fc-request-id,x-fc-error-type,x-fc-code-checksum,x-fc-invocation-duration,x-fc-max-memory-usage,x-fc-log-result,x-fc-invocation-code-version",
"content-disposition": "attachment",
"content-length": "59",
"content-type": "application/json",
"date": "Mon, 27 Sep 2021 02:02:43 GMT",
"x-fc-code-checksum": "14931192961446868056",
"x-fc-invocation-duration": "104",
"x-fc-invocation-service-version": "LATEST",
"x-fc-max-memory-usage": "68.87",
"x-fc-request-id": "7f4fb249-06d0-437b-97d7-7e9abe0624f8",
"x-serverless-cold-boot": "true",
"x-serverless-request-id": "ac141e1a1632708162061172394",
"x-serverless-runtime-version": "1.2.1"
},
"success": true
}

2021-09-27 10:12 负责人:无 分享
已邀请:
Zhanzr

Zhanzr

经测试,先调用uni.login获取code,接着调用uniID.code2SessionWeixin获取sessionKey,再接着调用uniID.wxBizDataCrypt进行解密可以正常解密

解密失败的原因,微信官方有解释:

在回调中调用 wx.login 登录,可能会刷新登录态。此时服务器使用 code 换取的 sessionKey 不是加密时使用的 sessionKey,导致解密失败。建议开发者提前进行 login;或者在回调中先使用 checkSession 进行登录态检查,避免 login 刷新登录态。

该问题目前已经被锁定, 无法添加新回复