From 077fac40c2e75c0c7cdf0f18105ee0616d8b0cab Mon Sep 17 00:00:00 2001 From: ZF sun <34314687@qq.com> Date: Sat, 8 Aug 2026 12:03:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(diy-recommend-code):=20=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=20setFontWeight=20=E6=97=A7=20canvas=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用户日志: TypeError: c.setFontWeight is not a function 原因: setFontWeight 是微信小程序新版 type=2d canvas 专有 API, 旧版不支持。运行时 ctx.setFontWeight 不存在,调用报错。 修复: 先用 typeof 检查,存在则调用;不存在则用 ctx.font = 'bold 36px sans-serif' 字符串设置字重(旧 API 支持)。 --- components-diy/diy-recommend-code.vue | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/components-diy/diy-recommend-code.vue b/components-diy/diy-recommend-code.vue index 7aac3a0..9ac0596 100644 --- a/components-diy/diy-recommend-code.vue +++ b/components-diy/diy-recommend-code.vue @@ -1322,7 +1322,12 @@ onShareClick() { ctx.setFontSize(36); ctx.setFillStyle(v.codeColor || '#DC143C'); ctx.setTextAlign('center'); - ctx.setFontWeight('bold'); + // 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;