From d5f598da574a56cea86dba7f7ccaea151639cf73 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Mon, 21 Sep 2026 03:00:46 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E5=B7=A5=E4=BD=9C=E5=8F=B0):=20=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=94=AF=E6=8C=81=E5=A4=9A=E9=80=89=EF=BC=8C=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=89=B9=E9=87=8F=E6=89=93=E5=8D=B0=E5=B7=A5=E4=BD=9C?= =?UTF-8?q?=E5=8F=B0=E4=BA=8C=E7=BB=B4=E7=A0=81=EF=BC=88A4=20=E4=B8=89?= =?UTF-8?q?=E5=88=97=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../workbench2/config/template-config.ts | 11 +- src/views/system/setting/workbench2/index.vue | 145 +++++++++++++++++- 2 files changed, 154 insertions(+), 2 deletions(-) diff --git a/src/views/system/setting/workbench2/config/template-config.ts b/src/views/system/setting/workbench2/config/template-config.ts index ffbd2d89..6393275f 100644 --- a/src/views/system/setting/workbench2/config/template-config.ts +++ b/src/views/system/setting/workbench2/config/template-config.ts @@ -549,6 +549,15 @@ export const TOOLBAR_BUTTONS: Record = { size: 'small', action: 'openCreateDialog' }, + printQrcode: { + key: 'printQrcode', + label: '打印二维码', + category: 'function', + type: 'warning', + size: 'small', + disabledKey: 'noSelection', + action: 'printWorkbenchQrcode' + }, toggleDetail: { key: 'toggleDetail', label: '显示详情', @@ -580,7 +589,7 @@ export const tabPages: TabPageRawConfig[] = [ tableColumnKeys: ['id', 'name', 'warehouse_id', 'type', 'status', 'device_ids', 'login_code', 'remark', 'created_at', 'updated_at', 'operation'], upperTableDefaults: { title: '工作台列表', - showSelection: false, + showSelection: true, showIndex: true, emptyText: '暂无工作台数据,可调整左侧筛选条件或点击新增' }, diff --git a/src/views/system/setting/workbench2/index.vue b/src/views/system/setting/workbench2/index.vue index 7f961c62..ba533b38 100644 --- a/src/views/system/setting/workbench2/index.vue +++ b/src/views/system/setting/workbench2/index.vue @@ -349,6 +349,7 @@ @@ -504,6 +505,7 @@ import { ref, computed, nextTick, onMounted, reactive } from 'vue' import { ElMessage } from 'element-plus' import { Search } from '@element-plus/icons-vue' import type { FormInstance, FormRules } from 'element-plus' +import QRCode from 'qrcode' import { useSettingsStore } from '@/stores/modules/settings' import { useVerticalSplitPane } from '@/composables/useVerticalSplitPane' import ScanBox from '@/components/ScanBox/index.vue' @@ -691,7 +693,8 @@ const toolbarActionMap: Record void> = { toggleDetailVisible: () => { detailVisible.value = !detailVisible.value }, openCreateDialog: () => { openCreate() }, handleExport: () => { console.log('[Workbench2] 导出(不含明细)') }, - handleExportItem: () => { console.log('[Workbench2] 导出(含明细)') } + handleExportItem: () => { console.log('[Workbench2] 导出(含明细)') }, + printWorkbenchQrcode: () => { void printWorkbenchQrcodes(selectedRows.value) } } function handleToolbarButtonClick(action: string) { @@ -887,6 +890,146 @@ async function copyRowLoginCode() { await copyTextToClipboard(String(loginCodeQrData.value?.login_code || '')) } +/** 打印当前弹窗中单个工作台的登录码二维码 */ +async function printRowLoginCodeQrcode() { + const data = loginCodeQrData.value + if (!data) return + await printWorkbenchQrcodes([data]) +} + +// ============================================================ +// 打印工作台二维码(列表多选批量 / 单行) +// ============================================================ +type WorkbenchQrcodeItem = { id?: number; name: string; login_code: string } + +// ── A4 纵向打印排版参数(单位 mm)──────────────────────────── +// A4 = 210×297;页边距 8 → 版心 194×281; +// 每行 3 个、间距 4 → 单个标签宽 (194 - 4×2) / 3 = 62mm,高 64mm → 每页 4 行 12 个 +const PRINT_QR_COLUMNS = 3 +const PRINT_QR_PAGE_MARGIN_MM = 8 +const PRINT_QR_GAP_MM = 4 +const PRINT_QR_LABEL_HEIGHT_MM = 64 +const PRINT_QR_SIZE_MM = 44 + +function escapeHtml(text: string): string { + const map: Record = { + '&': '&', + '<': '<', + '>': '>', + '"': '"', + "'": ''' + } + return String(text).replace(/[&<>"']/g, (ch) => map[ch] ?? ch) +} + +/** 从行数据中提取可打印的二维码项(无登录码的会被跳过) */ +function normalizeQrcodeItems(rows: Record[]): WorkbenchQrcodeItem[] { + return (Array.isArray(rows) ? rows : []) + .map((row) => ({ + id: row?.id, + name: String(row?.name || `工作台-${row?.id ?? ''}`).trim(), + login_code: String(row?.login_code ?? '').trim() + })) + .filter((item) => !!item.login_code) +} + +/** 等待打印页中的二维码图片就绪(数据 URL 通常即时,超时兜底) */ +function waitForImages(win: Window) { + const images = Array.from(win.document.images || []) + return Promise.all( + images.map( + (img) => + new Promise((resolve) => { + if (img.complete) { + resolve() + return + } + img.addEventListener('load', () => resolve(), { once: true }) + img.addEventListener('error', () => resolve(), { once: true }) + setTimeout(resolve, 1500) + }) + ) + ) +} + +/** 打印工作台登录码二维码:生成打印页并调起系统打印 */ +async function printWorkbenchQrcodes(rows: Record[]) { + const source = Array.isArray(rows) ? rows : [] + const items = normalizeQrcodeItems(source) + + if (items.length === 0) { + ElMessage.warning('所选工作台没有可用的登录码,无法生成二维码') + return + } + if (items.length < source.length) { + ElMessage.info(`已跳过 ${source.length - items.length} 个未生成登录码的工作台`) + } + + const win = window.open('', '_blank', 'width=900,height=700') + if (!win) { + ElMessage.warning('请允许浏览器弹出窗口以打印二维码') + return + } + + let labels = '' + try { + labels = ( + await Promise.all( + items.map(async (item) => { + const dataUrl = await QRCode.toDataURL(item.login_code, { + width: 600, + margin: 1, + errorCorrectionLevel: 'M' + }) + return `
+ +
${escapeHtml(item.name)}
+
` + }) + ) + ).join('') + } catch { + win.close() + ElMessage.error('二维码生成失败,请重试') + return + } + + win.document.open() + win.document.write(`工作台登录码二维码 +
${labels}
`) + win.document.close() + + await waitForImages(win) + win.focus() + win.print() +} + function resetFormData() { formData.id = undefined formData.name = ''