fix(diy-recommend-code): 兼容 setFontWeight 旧 canvas API

用户日志: TypeError: c.setFontWeight is not a function
原因: setFontWeight 是微信小程序新版 type=2d canvas 专有 API,
旧版不支持。运行时 ctx.setFontWeight 不存在,调用报错。

修复: 先用 typeof 检查,存在则调用;不存在则用
ctx.font = 'bold 36px sans-serif' 字符串设置字重(旧 API 支持)。
This commit is contained in:
2026-08-08 12:03:46 +08:00
parent 10d45b1c3b
commit 077fac40c2

View File

@@ -1322,7 +1322,12 @@ onShareClick() {
ctx.setFontSize(36);
ctx.setFillStyle(v.codeColor || '#DC143C');
ctx.setTextAlign('center');
// setFontWeight 是新版 canvas 2d API,旧版不支持;用 font 字符串兜底
if (typeof ctx.setFontWeight === 'function') {
ctx.setFontWeight('bold');
} else {
try { ctx.font = 'bold 36px sans-serif'; } catch(e) {}
}
ctx.fillText(code, W / 2, blockY + blockH / 2 + 12);
// 6. 二维码(如果有 url)
const qr = this.qrcodeRenderUrl;