优化称重代码

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,35 +398,8 @@ const stopStatusCheck = () => {
// 称重登记页面超时定时器 // 称重登记页面超时定时器
let weighingTimeoutTimer: number | null = null let weighingTimeoutTimer: number | null = null
// 组件挂载时 // 初始化步骤1扫码验证页面的操作
onMounted(() => { const initStep1 = () => {
// 启动图片自动刷新,每秒更新一次
imageRefreshTimer = setInterval(() => {
// 添加时间戳参数,避免浏览器缓存
imgData.imgUrl = `${imgData.baseUrl}?t=${Date.now()}`
weighingImgData.imgUrl = `${weighingImgData.baseUrl}?t=${Date.now()}`
}, 1500)
// 页面加载后自动聚焦到物料编码输入框
nextTick(() => {
if (materialCodeInput.value) {
materialCodeInput.value.focus()
}
})
// 初始进入扫码验证页面,连接灯光
if (activeStep.value === 1) {
connect().catch(error => {
console.error('连接灯光失败:', error)
})
getVmSaveImageTask().catch(error => {
console.error('启动vm定时任务保存图片失败:', error)
})
}
// 监听步骤变化
watch(activeStep, (newVal) => {
if (newVal === 1) {
// 进入扫码验证页面连接灯光并启动vm定时任务保存图片 // 进入扫码验证页面连接灯光并启动vm定时任务保存图片
connect().catch(error => { connect().catch(error => {
console.error('连接灯光失败:', error) console.error('连接灯光失败:', error)
@@ -439,7 +412,10 @@ onMounted(() => {
clearTimeout(weighingTimeoutTimer) clearTimeout(weighingTimeoutTimer)
weighingTimeoutTimer = null weighingTimeoutTimer = null
} }
} else if (newVal === 2) { }
// 进入步骤2称重登记页面的操作
const enterStep2 = () => {
// 进入称重登记页面断开灯光并关闭vm定时任务 // 进入称重登记页面断开灯光并关闭vm定时任务
disconnect().catch(error => { disconnect().catch(error => {
console.error('断开灯光失败:', error) console.error('断开灯光失败:', error)
@@ -467,20 +443,62 @@ onMounted(() => {
} }
}) })
}, 30 * 60 * 1000) // 30分钟 }, 30 * 60 * 1000) // 30分钟
} else {
// 启动电子称连接线程并建立WebSocket连接
weighAHStart().catch(error => {
Message.error('与电子称的连接建立失败')
return
})
establishWebSocket()
}
// 离开称重页面的操作
const leaveWeighingPage = () => {
// 离开称重页面断开灯光并关闭vm定时任务 // 离开称重页面断开灯光并关闭vm定时任务
leaveWeighPage() leaveWeighPage()
getVmCloseTask().catch(error => {
console.error('关闭vm定时任务失败:', error)
})
disconnect().catch(error => {
console.error('断开灯光失败:', error)
})
// 清除称重登记页面的超时定时器 // 清除称重登记页面的超时定时器
if (weighingTimeoutTimer) { if (weighingTimeoutTimer) {
clearTimeout(weighingTimeoutTimer) clearTimeout(weighingTimeoutTimer)
weighingTimeoutTimer = null weighingTimeoutTimer = null
} }
// 停用电子称并关闭WebSocket连接
weighAHStop().catch(error => {
console.error('停用电子称失败:', error)
})
closeWebSocket()
}
// 组件挂载时
onMounted(() => {
// 启动图片自动刷新,每秒更新一次
imageRefreshTimer = setInterval(() => {
// 添加时间戳参数,避免浏览器缓存
imgData.imgUrl = `${imgData.baseUrl}?t=${Date.now()}`
weighingImgData.imgUrl = `${weighingImgData.baseUrl}?t=${Date.now()}`
}, 1500)
// 页面加载后自动聚焦到物料编码输入框
nextTick(() => {
if (materialCodeInput.value) {
materialCodeInput.value.focus()
}
})
// 初始进入扫码验证页面,连接灯光
if (activeStep.value === 1) {
initStep1()
}
// 监听步骤变化
watch(activeStep, (newVal) => {
if (newVal === 1) {
initStep1()
} else if (newVal === 2) {
enterStep2()
} else {
leaveWeighingPage()
} }
}) })
}) })
@@ -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,24 +747,16 @@ 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()
} }
}
} }
// 打印 // 打印