刚刚受一位大哥指导,重新修改了代码,但是还是解密的时候报错。以下是更新后的代码
在登录页面中的onLoad中:
onLoad() {
let _this = this
uni.login({
success(res) {
if (res.code) {
uniCloud.callFunction({
name: 'sys_getSessionKey',
data: {js_code: res.code},
success(res) {
this.session_key = res.result.session_key
}
})
}
}
})
},
在云函数sys_getSessionKey通过js_code、appId、appSecret已经获取到了sessionKey。(这里就不展示代码了,会提示泄露隐私)
然后在获取手机号的时候button上@getphonenumber="toGetPhone"
在toGetPhone方法中:
toGetPhone(e) {
let _this = this
if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
return uni.$u.toast('您已经取消授权')
} else {
uniCloud.callFunction({
name: 'gr_quickLogin',
data: {
code: e.detail.code,
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
sessionKey: _this.session_key
},
success(res) {
console.log(res)
}
})
}
},
在gr_quickLogin云函数中:
'use strict';
exports.main = async (event, context) => {
const WXBizDataCrypt = require('./WXBizDataCrypt.js')
var appId = "wx**************54"
var sessionKey = event.sessionKey
var encryptedData = event.encryptedData
var iv = event.iv
var pc = new WXBizDataCrypt(appId, sessionKey)
var data = pc.decryptData(encryptedData , iv)
console.log('解密后 data: ', data)
//返回数据给客户端
return data
};
其中WXBizDataCrypt.js使用的是官方提供的js
最后还是报错:
17:00:34.307 [本地调试]Error: Illegal Buffer
17:00:34.308 [本地调试] at global.__tempModuleExports.WXBizDataCrypt.decryptData (/Users/mengxiandong/Documents/HBuilderProjects/gr_ski/uniCloud-alipay/cloudfunctions/gr_quickLogin/WXBizDataCrypt.js:25:11)
17:00:34.308 [本地调试] at global.__tempModuleExports.exports.main (/Users/mengxiandong/Documents/HBuilderProjects/gr_ski/uniCloud-alipay/cloudfunctions/gr_quickLogin/index.js:9:16)
17:00:34.308 [本地调试] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
实在不知道为啥了,已经翻页了一下午的文档,看了各种案例。(前提是之前用java的后端没问题,最近想做迁移到uniapp云开发上,这块一直报错不知道为啥)