Files
dukang/apps/mini-user/src/lib/redeem-qr.ts
T
jacy cc0c0a6ef8 feat(fulfillment): 同城运费与路由修复,配送单展示商品用户地址
同城 MANUAL/ZZXFX 按小飞侠价规计费,路由查询回退仓配凭证;HQ 配送单补商品、用户和收货地址。门店核销回跳与小程序核销码一并带上。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-25 21:12:36 +08:00

43 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import './text-encoding-polyfill';
import QRCode from 'qrcode';
const QR_SIZE = 240;
const QR_OPTIONS = {
width: QR_SIZE * 2,
margin: 0,
color: { dark: '#1f1a17', light: '#ffffff' },
} as const;
/** 在 2d Canvas 上绘制二维码(小程序 / H5 通用;payload 为落地 URL 或纯 token */
export function drawRedeemQrOnCanvas(
ctx: CanvasRenderingContext2D,
payload: string,
sizePx = QR_SIZE,
) {
const qr = QRCode.create(payload, { errorCorrectionLevel: 'M' });
const count = qr.modules.size;
const cell = sizePx / count;
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, sizePx, sizePx);
ctx.fillStyle = '#1f1a17';
for (let row = 0; row < count; row++) {
for (let col = 0; col < count; col++) {
if (qr.modules.get(row, col)) {
ctx.fillRect(col * cell, row * cell, cell, cell);
}
}
}
}
/** H5Data URL;失败时回退到与 h5-user 相同的外部 QR 服务 */
export async function buildRedeemQrDataUrl(payload: string): Promise<string> {
try {
return await QRCode.toDataURL(payload, QR_OPTIONS);
} catch {
return `https://api.qrserver.com/v1/create-qr-code/?size=${QR_SIZE * 2}x${QR_SIZE * 2}&data=${encodeURIComponent(payload)}`;
}
}
export const REDEEM_QR_DISPLAY_SIZE = QR_SIZE;