1.已经有接口权限。
2.服务号已认证。
3.小程序和服务号是同一个主体。在小程序里面用云函数调用officialAccount.qrcode.create接口,创建公众号带参数的二维码。
4.报错信息如下:
--------
errMsg: "cloud.callFunction:ok"
requestID: "1429eb0a-6397-4fb4-aab4-5675df2ba247"
result:
error:
errCode: 48001
errMsg: "openapi.officialAccount.qrcode.create:fail api unauthorized rid: 69476dc2-2bd7acf0-3ab73db8"
---------
5.云函数权限和wx-sdk版本如下:
{
"permissions": {
"openapi": [
"wxacode.get",
"officialAccount.qrcode.create"
]
}
}
--------
"dependencies": {
"wx-server-sdk": "latest"
}
6.云函数调用代码如下:
// 生成二维码(使用云调用)
async function generateQRCodeByCloudCall(qrType, sceneStr, expireSeconds) {
try {
let result;
result = await cloud.openapi.officialAccount.qrcode.create({
expire_seconds: expireSeconds,
action_name: 'QR_STR_SCENE',
action_info: {
scene: { scene_str: sceneStr }
}
});
// 直接构建微信官方二维码URL
const qrUrl = `https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=${encodeURIComponent(result.ticket)}`
return {
success: true,
ticket: result.ticket,
expire_seconds: result.expire_seconds,
url: result.url, // 微信跳转URL
qrUrl: qrUrl, // 二维码图片URL
qrCode: result // 完整返回数据
}
} catch (error) {
console.error('生成二维码失败:', error)
return {
success: false,
error: error,
try_catch: "yes"
}
}
}
