rev:载具本地打印

This commit is contained in:
2025-12-04 16:12:12 +08:00
parent 098a5f2d54
commit 9f5b25e687
2 changed files with 109 additions and 21 deletions

View File

@@ -1,47 +1,106 @@
import { MessageBox } from 'element-ui'
// ====页面动态加载C-Lodop云打印必须的文件CLodopfuncs.js====
// 注意:此代码会在客户端电脑上执行,连接客户端本地的打印服务
// 对于远程部署场景,客户端电脑需要安装 C-Lodop 服务,打印会连接到客户端本地的打印服务
var head = document.head || document.getElementsByTagName('head')[0] || document.documentElement
var oscript = document.createElement('script')
// 获取当前页面的协议和主机(用于支持远程访问)
var protocol = window.location.protocol
var hostname = window.location.hostname
// 判断是否为本地访问,如果是远程访问则使用客户端本地的 localhost
// 对于远程部署场景,客户端电脑需要安装 C-Lodop 服务,打印会连接到客户端本地的打印服务
var lodopHost = (hostname === 'localhost' || hostname === '127.0.0.1') ? hostname : 'localhost'
// 让本机的浏览器打印(更优先一点)
oscript = document.createElement('script')
oscript.src = 'http://localhost:8000/CLodopfuncs.js?priority=2'
oscript.src = `http://${lodopHost}:8000/CLodopfuncs.js?priority=2`
head.insertBefore(oscript, head.firstChild)
// 加载双端口(8000和18000避免其中某个端口被占用
oscript = document.createElement('script')
oscript.src = 'http://localhost:18000/CLodopfuncs.js?priority=1'
oscript.src = `http://${lodopHost}:18000/CLodopfuncs.js?priority=1`
head.insertBefore(oscript, head.firstChild)
// 下载loadLodop
function loadLodop() {
window.open('./CLodop_Setup_for_Win32NT.exe')
// 尝试从多个可能的路径下载
const lodopPaths = [
'./CLodop_Setup_for_Win32NT.exe',
'/static/CLodop_Setup_for_Win32NT.exe',
'http://www.lodop.net/download/CLodop_Setup_for_Win32NT.exe'
]
// 优先使用本地路径
const link = document.createElement('a')
link.href = lodopPaths[0]
link.download = 'CLodop_Setup_for_Win32NT.exe'
link.click()
// 如果本地路径失败,打开下载页面
setTimeout(() => {
window.open('http://www.lodop.net/download.html', '_blank')
}, 1000)
}
// ====获取LODOP对象的主过程====
function getLodop() {
var LODOP
try {
LODOP = getCLodop()
if (!LODOP && document.readyState !== 'complete') {
MessageBox.alert('C-Lodop打印控件还没准备好请稍后再试')
return
// 检查是否已加载 CLodop
if (typeof getCLodop === 'undefined') {
MessageBox({
title: '打印控件未就绪',
type: 'warning',
message: '打印控件正在加载中,请稍候再试。如果持续无法使用,请确保已安装 C-Lodop 打印服务并已启动。',
duration: 3000
})
return null
}
LODOP = getCLodop()
if (!LODOP) {
if (document.readyState !== 'complete') {
MessageBox.alert('C-Lodop打印控件还没准备好请稍后再试')
return null
}
// 如果页面已加载完成但仍无法获取 LODOP说明未安装或服务未启动
MessageBox({
title: '打印服务未就绪',
type: 'error',
showCancelButton: true,
message: '无法连接到打印服务。请确保:\n1. 已安装 C-Lodop 打印控件\n2. C-Lodop 服务正在运行\n3. 打印机已正确连接\n\n点击确定下载并安装打印控件',
callback: res => {
if (res === 'confirm') {
loadLodop()
}
}
})
return null
}
//设置打印版权
LODOP.SET_LICENSES('浙江省烟草专卖局(公司)', 'C0C4A46A3A0D1F526D426018D9F11921', '', '')
LODOP.SET_SHOW_MODE('SETUP_ENABLESS', '10000000000000')
return LODOP
} catch (err) {
console.error('LODOP 初始化错误:', err)
MessageBox({
title: '温馨提示',
type: 'warning',
title: '打印控件错误',
type: 'error',
showCancelButton: true,
message: '您还未安装打印控件,点击确定下载打印控件,安装成功后刷新页面即可进行打印',
message: '打印控件初始化失败。请确保:\n1. 已安装 C-Lodop 打印控件\n2. C-Lodop 服务正在运行\n3. 打印机已正确连接\n\n点击确定下载并安装打印控件',
callback: res => {
if (res === 'confirm') {
loadLodop()
}
}
})
return null
}
}

View File

@@ -289,18 +289,47 @@ export default {
this.crud.notify('请选择一条记录', CRUD.NOTIFICATION_TYPE.INFO)
return
}
for (let i = 0; i < _selectData.length; i++) {
const code = _selectData[i].storagevehicle_code
const LODOP = getLodop()
LODOP.SET_SHOW_MODE('HIDE_DISBUTTIN_SETUP', 1)// 隐藏那些无效按钮
// 打印纸张大小设置https://www.it610.com/article/2094844.html
LODOP.SET_PRINT_PAGESIZE(1, '50mm', '30mm', '')
// LODOP.ADD_PRINT_RECT('0mm', '0mm', '48mm', '28mm', 0, 1)
LODOP.ADD_PRINT_BARCODE('4.3mm', '8.2mm', '40mm', '20mm', '128Auto', code)
// LODOP.PREVIEW()// 预览
LODOP.PRINT()// 打印
this.crud.notify('打印成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
// 获取 LODOP 对象
const LODOP = getLodop()
if (!LODOP) {
// getLodop 内部已经处理了错误提示
return
}
try {
LODOP.PRINT_INIT('载具标签打印')
LODOP.SET_SHOW_MODE('HIDE_DISBUTTIN_SETUP', 1)
// 批量打印选中的记录
for (let i = 0; i < _selectData.length; i++) {
const code = _selectData[i].storagevehicle_code
const name = _selectData[i].storagevehicle_name || code
// 打印纸张大小60 * 80
// 参数说明SET_PRINT_PAGESIZE(方向, 宽度, 高度, 单位)
// 方向0-纵向1-横向
LODOP.SET_PRINT_PAGESIZE(0, '60mm', '80mm', '')
// 添加条码
// 条码位置:水平居中 (60-50)/2 = 5mm垂直居中 (80-30)/2 = 25mm
// 条码大小50mm宽 * 30mm高可根据实际需要调整
LODOP.ADD_PRINT_BARCODE('25mm', '5mm', '30mm', '50mm', '128Auto', code)
// 如果需要打印多份,使用 NEWPAGE 分隔
if (i < _selectData.length - 1) {
LODOP.NEWPAGE()
}
}
// 执行打印(会在客户端电脑上执行)
LODOP.PRINT()
this.crud.notify(`成功打印 ${_selectData.length} 条记录`, CRUD.NOTIFICATION_TYPE.SUCCESS)
this.crud.toQuery()
} catch (error) {
console.error('打印错误:', error)
this.crud.notify('打印失败' + (error.message || '未知错误'), CRUD.NOTIFICATION_TYPE.ERROR)
}
}
}