[已删除]
[已删除]
  • 发布:2024-09-30 08:51
  • 更新:2024-09-30 08:51
  • 阅读:249

uniapp在一件获取手机号总是报错Error: Illegal Buffer,找不到问题在哪

分类:uni-app

nodejs版本解密手机号报错?查阅不少资料已经按照不少答案改了一下午还是不对。前几天在微信交流社区提问也没有一个正解
重新修改了很多次代码,但是还是解密的时候报错。
以下是我的进入页面的onLoad,这是使用了login获取到了code在到云函数中获取sessionKey。

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云函数中报错:Error: Illegal Buffer
以下是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云开发上,这块一直报错不知道为啥)

2024-09-30 08:51 负责人:无 分享
已邀请:

要回复问题请先登录注册