优化称重代码

This commit is contained in:
zc
2026-04-07 17:40:19 +08:00
parent a54a56865a
commit 5d5996b915

View File

@@ -244,7 +244,7 @@
<!-- 操作按钮 -->
<div class="action-buttons">
<a-button
v-if="activeStep > 1 && activeStep < 3"
v-if="activeStep === 2"
class="previous-button"
@click="handlePrevious"
>
@@ -398,6 +398,78 @@ const stopStatusCheck = () => {
// 称重登记页面超时定时器
let weighingTimeoutTimer: number | null = null
// 初始化步骤1扫码验证页面的操作
const initStep1 = () => {
// 进入扫码验证页面连接灯光并启动vm定时任务保存图片
connect().catch(error => {
console.error('连接灯光失败:', error)
})
getVmSaveImageTask().catch(error => {
console.error('启动vm定时任务保存图片失败:', error)
})
// 清除称重登记页面的超时定时器
if (weighingTimeoutTimer) {
clearTimeout(weighingTimeoutTimer)
weighingTimeoutTimer = null
}
}
// 进入步骤2称重登记页面的操作
const enterStep2 = () => {
// 进入称重登记页面断开灯光并关闭vm定时任务
disconnect().catch(error => {
console.error('断开灯光失败:', error)
})
getVmCloseTask().catch(error => {
console.error('关闭vm定时任务失败:', error)
})
nextTick(() => {
enterWeighPage()
})
// 启动30分钟超时定时器
if (weighingTimeoutTimer) {
clearTimeout(weighingTimeoutTimer)
}
weighingTimeoutTimer = window.setTimeout(() => {
// 超时提示
Modal.confirm({
title: '页面超时',
content: '称重登记页面已超过30分钟请重新加载页面',
onOk: () => {
// 停用电子称
weighAHStop()
// 重新加载页面
window.location.reload()
}
})
}, 30 * 60 * 1000) // 30分钟
// 启动电子称连接线程并建立WebSocket连接
weighAHStart().catch(error => {
Message.error('与电子称的连接建立失败')
return
})
establishWebSocket()
}
// 离开称重页面的操作
const leaveWeighingPage = () => {
// 离开称重页面断开灯光并关闭vm定时任务
leaveWeighPage()
// 清除称重登记页面的超时定时器
if (weighingTimeoutTimer) {
clearTimeout(weighingTimeoutTimer)
weighingTimeoutTimer = null
}
// 停用电子称并关闭WebSocket连接
weighAHStop().catch(error => {
console.error('停用电子称失败:', error)
})
closeWebSocket()
}
// 组件挂载时
onMounted(() => {
// 启动图片自动刷新,每秒更新一次
@@ -416,71 +488,17 @@ onMounted(() => {
// 初始进入扫码验证页面,连接灯光
if (activeStep.value === 1) {
connect().catch(error => {
console.error('连接灯光失败:', error)
})
getVmSaveImageTask().catch(error => {
console.error('启动vm定时任务保存图片失败:', error)
})
initStep1()
}
// 监听步骤变化
watch(activeStep, (newVal) => {
if (newVal === 1) {
// 进入扫码验证页面连接灯光并启动vm定时任务保存图片
connect().catch(error => {
console.error('连接灯光失败:', error)
})
getVmSaveImageTask().catch(error => {
console.error('启动vm定时任务保存图片失败:', error)
})
// 清除称重登记页面的超时定时器
if (weighingTimeoutTimer) {
clearTimeout(weighingTimeoutTimer)
weighingTimeoutTimer = null
}
initStep1()
} else if (newVal === 2) {
// 进入称重登记页面断开灯光并关闭vm定时任务
disconnect().catch(error => {
console.error('断开灯光失败:', error)
})
getVmCloseTask().catch(error => {
console.error('关闭vm定时任务失败:', error)
})
nextTick(() => {
enterWeighPage()
})
// 启动30分钟超时定时器
if (weighingTimeoutTimer) {
clearTimeout(weighingTimeoutTimer)
}
weighingTimeoutTimer = window.setTimeout(() => {
// 超时提示
Modal.confirm({
title: '页面超时',
content: '称重登记页面已超过30分钟请重新加载页面',
onOk: () => {
// 停用电子称
weighAHStop()
// 重新加载页面
window.location.reload()
}
})
}, 30 * 60 * 1000) // 30分钟
enterStep2()
} else {
// 离开称重页面断开灯光并关闭vm定时任务
leaveWeighPage()
getVmCloseTask().catch(error => {
console.error('关闭vm定时任务失败:', error)
})
disconnect().catch(error => {
console.error('断开灯光失败:', error)
})
// 清除称重登记页面的超时定时器
if (weighingTimeoutTimer) {
clearTimeout(weighingTimeoutTimer)
weighingTimeoutTimer = null
}
leaveWeighingPage()
}
})
})
@@ -682,11 +700,6 @@ const handleNext = async () => {
workOrderResp.value.totalCalculatedWeight = res.data?.totalCalculatedWeight || ''
workOrderResp.value.totalCount = res.data?.totalCount || ''
// 关闭WebSocket连接
closeWebSocket()
//停用电子称
weighAHStop()
// 跳转到完成页面
activeStep.value++
return true
@@ -717,7 +730,7 @@ const handleNext = async () => {
// 调用后端接口获取比对结果 // todo
const res = await vmSend(formData.materialProcess);
if (res.data && res.data == materialCode) {
if (res.data && res.data === materialCode) {
compareMatchResult.value = 'success'
Message.success('比对成功')
} else {
@@ -734,23 +747,15 @@ const handleNext = async () => {
// 其他情况步骤1且已成功比对或步骤0直接进入下一步
activeStep.value++
// 进入称重页面时建立WebSocket连接
if (activeStep.value === 2) {
weighAHStart().catch(error => {
console.error('启动电子称连接线程失败:', error)
})
establishWebSocket()
}
// 进入称重页面的操作已在watch监听器中处理
}
// 处理上一步
const handlePrevious = () => {
if (activeStep.value > 1) {
activeStep.value--
// 离开称重页面时关闭WebSocket连接
if (activeStep.value !== 2) {
closeWebSocket()
}
weighAHStop()
closeWebSocket()
}
}