feat:打印消息缓存
This commit is contained in:
@@ -12,7 +12,7 @@ export default {
|
|||||||
clearInterval(printer.intervalID)
|
clearInterval(printer.intervalID)
|
||||||
printer.socketTask.close()
|
printer.socketTask.close()
|
||||||
}
|
}
|
||||||
}, 20000)
|
}, 10000)
|
||||||
},
|
},
|
||||||
//开始心跳
|
//开始心跳
|
||||||
heartbeatStart(printer) {
|
heartbeatStart(printer) {
|
||||||
@@ -44,7 +44,7 @@ export default {
|
|||||||
printer = {
|
printer = {
|
||||||
isOpen: false,
|
isOpen: false,
|
||||||
isOpening: false, //打开中
|
isOpening: false, //打开中
|
||||||
socketTask: false,
|
socketTask: null,
|
||||||
timeoutID: -1,
|
timeoutID: -1,
|
||||||
intervalID: -1,
|
intervalID: -1,
|
||||||
reConnectCount: 0, //重新连接次数
|
reConnectCount: 0, //重新连接次数
|
||||||
@@ -77,7 +77,6 @@ export default {
|
|||||||
complete: () => {}
|
complete: () => {}
|
||||||
})
|
})
|
||||||
if (!socketTask) return
|
if (!socketTask) return
|
||||||
// console.log(socketTask);
|
|
||||||
printer.socketTask = socketTask
|
printer.socketTask = socketTask
|
||||||
this.printMap.set(printServerIp, printer)
|
this.printMap.set(printServerIp, printer)
|
||||||
|
|
||||||
@@ -89,6 +88,18 @@ export default {
|
|||||||
uni.$emit('printStatus', {})
|
uni.$emit('printStatus', {})
|
||||||
console.log(socketTask)
|
console.log(socketTask)
|
||||||
console.log(printServerIp + ': WebSocket连接成功。。。')
|
console.log(printServerIp + ': WebSocket连接成功。。。')
|
||||||
|
|
||||||
|
// 发送 pending 消息
|
||||||
|
if (printer.pendingMessages?.length) {
|
||||||
|
printer.pendingMessages.forEach(msg => {
|
||||||
|
try {
|
||||||
|
printer.socketTask.send({ data: msg })
|
||||||
|
} catch (e) {
|
||||||
|
console.error('发送 pending 消息失败:', e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
printer.pendingMessages = []
|
||||||
|
}
|
||||||
this.heartbeatStart(printer)
|
this.heartbeatStart(printer)
|
||||||
this.checkConnect(printer) //检测连接是否正常
|
this.checkConnect(printer) //检测连接是否正常
|
||||||
})
|
})
|
||||||
@@ -104,7 +115,7 @@ export default {
|
|||||||
socketTask.onClose(res => {
|
socketTask.onClose(res => {
|
||||||
printer.isOpen = false
|
printer.isOpen = false
|
||||||
printer.isOpening = false
|
printer.isOpening = false
|
||||||
printer.socketTask = false
|
printer.socketTask = null
|
||||||
uni.$emit('printStatus', {})
|
uni.$emit('printStatus', {})
|
||||||
// console.log(printer.printServerIp + ": WebSocket连接关闭。。。");
|
// console.log(printer.printServerIp + ": WebSocket连接关闭。。。");
|
||||||
this.heartbeatClear(printer)
|
this.heartbeatClear(printer)
|
||||||
@@ -119,7 +130,7 @@ export default {
|
|||||||
socketTask.onError(res => {
|
socketTask.onError(res => {
|
||||||
printer.isOpen = false
|
printer.isOpen = false
|
||||||
printer.isOpening = false
|
printer.isOpening = false
|
||||||
printer.socketTask = false
|
printer.socketTask = null
|
||||||
uni.$emit('printStatus', {})
|
uni.$emit('printStatus', {})
|
||||||
// console.log(printServerIp + ": WebSocket连接失败。。。");
|
// console.log(printServerIp + ": WebSocket连接失败。。。");
|
||||||
// console.log(res);
|
// console.log(res);
|
||||||
@@ -175,20 +186,38 @@ export default {
|
|||||||
send(printServerIp, data) {
|
send(printServerIp, data) {
|
||||||
let printer = this.printMap.get(printServerIp)
|
let printer = this.printMap.get(printServerIp)
|
||||||
if (!printer) {
|
if (!printer) {
|
||||||
//不存在则打开
|
// 创建新 printer 对象(尚未连接)
|
||||||
|
printer = {
|
||||||
|
isOpen: false,
|
||||||
|
isOpening: false,
|
||||||
|
socketTask: null,
|
||||||
|
timeoutID: -1,
|
||||||
|
intervalID: -1,
|
||||||
|
reConnectCount: 0, //重新连接次数
|
||||||
|
printServerIp: printServerIp,
|
||||||
|
lastReceiveMessageTime: new Date().getTime(),
|
||||||
|
pendingMessages: [data]
|
||||||
|
}
|
||||||
|
this.printMap.set(printServerIp, printer)
|
||||||
this.open(printServerIp)
|
this.open(printServerIp)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if (printer?.socketTask) {
|
if (printer.isOpen && printer.socketTask?.readyState === 1) {
|
||||||
printer.socketTask.send({
|
printer.socketTask.send({
|
||||||
data: data
|
data: data
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
// 连接中 or 已断开 → 缓存
|
||||||
|
printer.pendingMessages = printer.pendingMessages || []
|
||||||
|
printer.pendingMessages.push(data)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
//获取打印模板并执行打印
|
//获取打印模板并执行打印
|
||||||
getPrintTemplateAndPrint(businessSubId) {
|
getPrintTemplateAndPrint(businessSubId, callback) {
|
||||||
getBusinessSubSample(businessSubId)
|
getBusinessSubSample(businessSubId)
|
||||||
.then(res => {
|
.then(async res => {
|
||||||
this.print(res)
|
await this.print(res)
|
||||||
|
callback && callback()
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
@@ -197,7 +226,7 @@ export default {
|
|||||||
//调用send方法执行打印
|
//调用send方法执行打印
|
||||||
print(businessSubData) {
|
print(businessSubData) {
|
||||||
const { printTemplate } = businessSubData
|
const { printTemplate } = businessSubData
|
||||||
if (!printTemplate) return uni.showToast({ title: '样品未配置打印模板' })
|
if (!printTemplate) return uni.showToast({ title: '样品未配置打印模板', icon: 'error' })
|
||||||
const me = this
|
const me = this
|
||||||
me.getReportData(printTemplate, function (res) {
|
me.getReportData(printTemplate, function (res) {
|
||||||
const reportData = {
|
const reportData = {
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ function handleReturnToStock() {
|
|||||||
})
|
})
|
||||||
if (res.isPrint == 1) {
|
if (res.isPrint == 1) {
|
||||||
// 执行打印
|
// 执行打印
|
||||||
nx.$print.print(res)
|
nx.$print.getPrintTemplateAndPrint(res.id)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,7 @@
|
|||||||
<up-button
|
<up-button
|
||||||
v-if="sampleData.id"
|
v-if="sampleData.id"
|
||||||
:disabled="sampleData.returnStatus !== 'completed'"
|
:disabled="sampleData.returnStatus !== 'completed'"
|
||||||
|
:loading="btnLoading"
|
||||||
type="primary"
|
type="primary"
|
||||||
style="width: 90%"
|
style="width: 90%"
|
||||||
text="打印归库标签"
|
text="打印归库标签"
|
||||||
@@ -110,8 +111,13 @@ async function getSampleDetail() {
|
|||||||
sampleData.value = list[0]
|
sampleData.value = list[0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let btnLoading = ref(false)
|
||||||
function handlePrint() {
|
function handlePrint() {
|
||||||
nx.$print.print(sampleData.value)
|
btnLoading.value = true
|
||||||
|
nx.$print.getPrintTemplateAndPrint(sampleData.value.id, () => {
|
||||||
|
uni.showToast({ title: '已打印', icon: 'success' })
|
||||||
|
btnLoading.value = false
|
||||||
|
})
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user