From 1ba44d4b0a9b4cc151c96a57a3747217be903554 Mon Sep 17 00:00:00 2001 From: duxingchen Date: Thu, 6 Aug 2026 10:52:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=93=E5=8D=B0=E6=89=80=E8=A7=81=E5=8D=B3?= =?UTF-8?q?=E6=89=80=E5=BE=97:=20iframe=E5=85=8B=E9=9A=86DOM+=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E6=A0=B7=E5=BC=8F+=E9=9D=99=E9=BB=98=E6=89=93?= =?UTF-8?q?=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/pages/admin/CreateProductDialog.tsx | 75 ++++++++----------- 1 file changed, 30 insertions(+), 45 deletions(-) diff --git a/frontend/src/pages/admin/CreateProductDialog.tsx b/frontend/src/pages/admin/CreateProductDialog.tsx index e128e10..ebda308 100644 --- a/frontend/src/pages/admin/CreateProductDialog.tsx +++ b/frontend/src/pages/admin/CreateProductDialog.tsx @@ -289,57 +289,42 @@ export default function CreateProductDialog({ open, onClose, onCreated }: Props) // ============================================================ function printCurrentQRCode() { - if (!createdSn) return; - const qrUrl = `/api/v1/products/qrcode/${createdSn}`; - const productName = selected?.material_name ?? ""; - const spec = selected?.spec_model ?? ""; - const orderNo2 = orderNo || "—"; - const printWindow = window.open("", "_blank", "width=420,height=480"); - if (!printWindow) return; - printWindow.document.write(` + const printArea = document.getElementById("label-print-area"); + if (!printArea) return; + const printContent = printArea.innerHTML; + + const styles = Array.from(document.querySelectorAll("style, link[rel=\"stylesheet\"]")) + .map(el => el.outerHTML) + .join(""); + + const iframe = document.createElement("iframe"); + iframe.style.position = "absolute"; + iframe.style.width = "0"; + iframe.style.height = "0"; + iframe.style.border = "none"; + document.body.appendChild(iframe); + + const doc = iframe.contentWindow!.document; + doc.write(` + - - 打印标签 - ${createdSn} + 打印标签${styles} - - - +
${printContent}
`); - printWindow.document.close(); + doc.close(); + + iframe.contentWindow!.onload = () => { + iframe.contentWindow!.focus(); + iframe.contentWindow!.print(); + setTimeout(() => { document.body.removeChild(iframe); }, 1000); + }; } // ============================================================ @@ -358,7 +343,7 @@ export default function CreateProductDialog({ open, onClose, onCreated }: Props) {createdSn ? ( /* ---- 成功页 ---- */
-
+
QR