diff --git a/src/locales/en.ts b/src/locales/en.ts index 49e6628f..a2081d53 100644 --- a/src/locales/en.ts +++ b/src/locales/en.ts @@ -3332,7 +3332,7 @@ export default { hostName: 'Host Name', systemPrinterName: 'Printer Name', defaultStatus: 'Is Default', - isEnable: 'Is Enabled', + isEnabled: 'Is Enabled', remark: 'remark', createTime: 'Create Time', updateTime: 'Update Time', diff --git a/src/locales/zh-CN.ts b/src/locales/zh-CN.ts index 1793396d..11d0b5a5 100644 --- a/src/locales/zh-CN.ts +++ b/src/locales/zh-CN.ts @@ -2818,7 +2818,7 @@ export default { hostName: '主机名', systemPrinterName: '打印机名称', defaultStatus: '是否默认', - isEnable: '是否启用', + isEnabled: '是否启用', remark: '备注', createTime: '创建时间', updateTime: '更新时间', diff --git a/src/utils/dict.ts b/src/utils/dict.ts index 6d70292c..662a7f69 100644 --- a/src/utils/dict.ts +++ b/src/utils/dict.ts @@ -297,5 +297,9 @@ export enum DICT_TYPE { IOT_DEVICE_ATTRIBUTE_UNIT = "iot_device_attribute_unit", IOT_ALARM_REGISTRATION = "alarm_registration", - PRIMARY_FLAG = "primary_flag" + PRIMARY_FLAG = "primary_flag", + + IS_DEFAULT = 'is_default', // 是否默认 + IS_ENABLED = 'is_enabled', // 是否启用 + } diff --git a/src/views/mes/printconfig/ConfigForm.vue b/src/views/mes/printconfig/ConfigForm.vue index 67ee35ed..56c6171a 100644 --- a/src/views/mes/printconfig/ConfigForm.vue +++ b/src/views/mes/printconfig/ConfigForm.vue @@ -30,8 +30,8 @@ - - + + @@ -66,8 +66,8 @@ const formData = ref({ id: undefined, hostName: undefined, systemPrinterName: undefined, - isDefault: undefined, - isEnabled: undefined, + isDefault: false, + isEnabled: true, remark: undefined, createdAt: undefined, updatedAt: undefined, @@ -156,9 +156,11 @@ const printData = ref({ address: '北京市朝阳区测试街道123号' }); -const handleClientPrint = () => { +const handleClientPrint = async () => { + // 校验表单 + await formRef.value.validate() if (!hiprintTemplate) { - return + buildTestPrintTemplate() } syncClientState() if (!clientConnected.value) { @@ -169,7 +171,7 @@ const handleClientPrint = () => { clientPrinting.value = true try { hiprintTemplate.print2(printData.value, { - printer: selectedPrinter.value, + printer: formData.value.systemPrinterName, title: dialogTitle.value }) } catch (error: any) { @@ -177,6 +179,49 @@ const handleClientPrint = () => { message.error(error?.message || '本地客户端打印失败') } } + +const buildTestPrintTemplate = () => { + if (hiprintTemplate) { + return + } + const printElements = [ + { + options: { left: 60, top: 10, height: 10, width: 200, title: '订单号', field: 'orderNo', testData: 'TEST-20231125-001', fontSize: 22, textAlign: 'center' }, + printElementType: { title: '文本', type: 'text' } + }, + { + options: { left: 60, top: 68, height: 10, width: 200, title: '客户名', field: 'customerName', testData: '测试客户', fontSize: 22, textAlign: 'center' }, + printElementType: { title: '文本', type: 'text' } + } + ] + const templateJson = { + panels: [{ + index: 0, + height: 100, + width: 220, + paperHeader: 5, + paperFooter: 5, + printElements + }] + } + hiprintTemplate = new hiprint.PrintTemplate({ template: templateJson }) + printEventBound = false + bindPrintEvents() +} +const bindPrintEvents = () => { + if (!hiprintTemplate || printEventBound) { + return + } + hiprintTemplate.on('printSuccess', () => { + clientPrinting.value = false + message.success('本地客户端打印已发送') + }) + hiprintTemplate.on('printError', (data: any) => { + clientPrinting.value = false + message.error(data?.msg || data?.message || '本地客户端打印失败') + }) + printEventBound = true +} const applyClientHost = () => { const socket = getHiwebSocket() let host = normalizeClientHost() @@ -254,8 +299,8 @@ const resetForm = () => { id: undefined, hostName: undefined, systemPrinterName: undefined, - isDefault: undefined, - isEnabled: undefined, + isDefault: false, + isEnabled: true, remark: undefined, createdAt: undefined, updatedAt: undefined, diff --git a/src/views/mes/printconfig/index.vue b/src/views/mes/printconfig/index.vue index 6d75b28a..568ef147 100644 --- a/src/views/mes/printconfig/index.vue +++ b/src/views/mes/printconfig/index.vue @@ -75,20 +75,28 @@ - - + + + + + + @@ -110,6 +118,15 @@ > {{ t('action.del') }} + + + {{ t('TemplateManagement.PrintConfig.testPrint') }} + @@ -132,7 +149,7 @@ import { dateFormatter } from '@/utils/formatTime' import download from '@/utils/download' import { ConfigApi, ConfigVO } from '@/api/mes/printconfig' import ConfigForm from './ConfigForm.vue' - +import { getIntDictOptions, DICT_TYPE } from '@/utils/dict' /** 打印机配置 列表 */ defineOptions({ name: 'PrinterConfig' }) @@ -166,6 +183,7 @@ const queryParams = reactive({ const queryFormRef = ref() // 搜索的表单 const exportLoading = ref(false) // 导出的加载中 const clientHostStorageKey = 'hiprint-client-host' +const hostNameStorageKey = 'hiprint-connected-hostname' const defaultClientHost = 'http://127.0.0.1:17521' let hiprintInited = false @@ -334,7 +352,12 @@ const resolveCurrentHostName = async () => { } settled = true window.clearTimeout(timer) - resolve(hostName || getSocketHostName()) + // 缓存主机名到 localStorage,下次刷新立即显示 + const result = hostName || getSocketHostName() + if (result) { + localStorage.setItem(hostNameStorageKey, result) + } + resolve(result) } const timer = window.setTimeout(() => finish(), 3000) @@ -383,15 +406,20 @@ const resolveCurrentHostName = async () => { } const initCurrentHostName = async () => { + // 先从缓存读取,立即显示 + const cachedHostName = localStorage.getItem(hostNameStorageKey) + if (cachedHostName) { + currentHostName.value = cachedHostName + queryParams.hostName = cachedHostName + } + // 再通过 WebSocket 刷新获取最新主机名 const hostName = await resolveCurrentHostName() - if (hostName) { + if (hostName && hostName !== cachedHostName) { currentHostName.value = hostName queryParams.hostName = hostName } - } -/** 查询列表 */ const getList = async () => { loading.value = true try { @@ -422,11 +450,67 @@ const resetQuery = () => { /** 添加/修改操作 */ const formRef = ref() const openForm = (type: string, id?: number) => { - let host = normalizeClientHost(); + let host = currentHostName.value; formRef.value.open(type,host, id) } +/** 测试打印按钮操作 */ +const handleTestPrint = async (row: any) => { + const printerName = row.systemPrinterName + if (!printerName) { + message.warning('该配置未选择打印机,无法测试打印') + return + } + ensureHiprintInit() + const testData = { + orderNo: 'TEST-' + Date.now(), + customerName: '测试客户', + amount: 199.99, + address: '北京市朝阳区测试街道123号' + } + const printElements = [ + { options: { left: 10, top: 10, height: 10, width: 200, title: '订单号', field: 'orderNo', testData: testData.orderNo, fontSize: 14, textAlign: 'center' }, printElementType: { title: '文本', type: 'text' } }, + { options: { left: 10, top: 28, height: 10, width: 200, title: '客户名', field: 'customerName', testData: testData.customerName, fontSize: 14, textAlign: 'center' }, printElementType: { title: '文本', type: 'text' } }, + { options: { left: 10, top: 46, height: 14, width: 200, title: '金额', field: 'amount', testData: '199.99', fontSize: 18, textAlign: 'center', fontWeight: 'bold' }, printElementType: { title: '文本', type: 'text' } }, + { options: { left: 10, top: 68, height: 10, width: 200, title: '地址', field: 'address', testData: testData.address, fontSize: 12, textAlign: 'center' }, printElementType: { title: '文本', type: 'text' } } + ] + const templateJson = { panels: [{ index: 0, height: 100, width: 220, paperHeader: 5, paperFooter: 5, printElements }] } + applyClientHost() + const socket = getHiwebSocket() + if (!socket) { + message.warning('打印客户端未初始化') + return + } + if (socket.opened) { + doTestPrint(socket, printerName, testData, templateJson) + return + } + socket.stop() + socket.start((status: boolean) => { + if (!status) { + message.warning('未连接到本地打印客户端,请先确认 electron-hiprint 已启动') + return + } + doTestPrint(socket, printerName, testData, templateJson) + }) +} /** 删除按钮操作 */ + + +const doTestPrint = (socket: any, printerName: string, testData: any, templateJson: any) => { + const printTemplate = new hiprint.PrintTemplate({ template: templateJson }) + printTemplate.on('printSuccess', () => { + message.success('测试打印已发送到: ' + printerName) + }) + printTemplate.on('printError', (data: any) => { + message.error(data?.msg || data?.message || '测试打印失败') + }) + try { + printTemplate.print2(testData, { printer: printerName, title: '测试打印' }) + } catch (error: any) { + message.error(error?.message || '测试打印失败') + } +} const handleDelete = async (id: number) => { try { // 删除的二次确认