优化称重代码

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