feat(工作台): 列表支持多选,新增批量打印工作台二维码(A4 三列)
This commit is contained in:
parent
9a0ac370ec
commit
d5f598da57
@ -549,6 +549,15 @@ export const TOOLBAR_BUTTONS: Record<string, ToolbarButtonConfig> = {
|
||||
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: '暂无工作台数据,可调整左侧筛选条件或点击新增'
|
||||
},
|
||||
|
||||
@ -349,6 +349,7 @@
|
||||
</div>
|
||||
<template #footer>
|
||||
<el-button size="small" type="primary" @click="copyRowLoginCode">复制登录码</el-button>
|
||||
<el-button size="small" @click="printRowLoginCodeQrcode">打印二维码</el-button>
|
||||
<el-button size="small" @click="closeLoginCodeQr">关闭</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
@ -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<string, () => 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<string, string> = {
|
||||
'&': '&',
|
||||
'<': '<',
|
||||
'>': '>',
|
||||
'"': '"',
|
||||
"'": '''
|
||||
}
|
||||
return String(text).replace(/[&<>"']/g, (ch) => map[ch] ?? ch)
|
||||
}
|
||||
|
||||
/** 从行数据中提取可打印的二维码项(无登录码的会被跳过) */
|
||||
function normalizeQrcodeItems(rows: Record<string, any>[]): 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<void>((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<string, any>[]) {
|
||||
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 `<div class="label">
|
||||
<img class="label-qr" src="${dataUrl}" alt="">
|
||||
<div class="label-name">${escapeHtml(item.name)}</div>
|
||||
</div>`
|
||||
})
|
||||
)
|
||||
).join('')
|
||||
} catch {
|
||||
win.close()
|
||||
ElMessage.error('二维码生成失败,请重试')
|
||||
return
|
||||
}
|
||||
|
||||
win.document.open()
|
||||
win.document.write(`<!doctype html><html><head><meta charset="utf-8"><title>工作台登录码二维码</title>
|
||||
<style>
|
||||
/* A4 纵向:210×297mm,页边距 8mm → 版心 194mm,每行 3 个(间距 4mm) */
|
||||
@page { size: A4 portrait; margin: ${PRINT_QR_PAGE_MARGIN_MM}mm; }
|
||||
* { box-sizing: border-box; }
|
||||
html, body { padding: 0; margin: 0; }
|
||||
body { font-family: 'Microsoft YaHei', sans-serif; color: #303133; }
|
||||
.grid { display: flex; flex-wrap: wrap; gap: ${PRINT_QR_GAP_MM}mm; align-content: flex-start; }
|
||||
.label {
|
||||
/* 版心宽 - 2 个间距,三等分 */
|
||||
width: calc((100% - ${PRINT_QR_GAP_MM * (PRINT_QR_COLUMNS - 1)}mm) / ${PRINT_QR_COLUMNS});
|
||||
height: ${PRINT_QR_LABEL_HEIGHT_MM}mm;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 3mm 2mm;
|
||||
border: 1px dashed #c0c4cc;
|
||||
border-radius: 4px;
|
||||
text-align: center;
|
||||
overflow: hidden;
|
||||
break-inside: avoid;
|
||||
page-break-inside: avoid;
|
||||
}
|
||||
.label-name { font-size: 13px; font-weight: 600; line-height: 1.3; margin-top: 2mm; word-break: break-all; }
|
||||
.label-qr { display: block; width: ${PRINT_QR_SIZE_MM}mm; height: ${PRINT_QR_SIZE_MM}mm; flex: none; }
|
||||
@media print { .label { border-color: #dcdfe6; } }
|
||||
</style></head><body><div class="grid">${labels}</div></body></html>`)
|
||||
win.document.close()
|
||||
|
||||
await waitForImages(win)
|
||||
win.focus()
|
||||
win.print()
|
||||
}
|
||||
|
||||
function resetFormData() {
|
||||
formData.id = undefined
|
||||
formData.name = ''
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user