优化称重连接

This commit is contained in:
zc
2026-04-07 16:37:51 +08:00
parent 45bd3bc8f5
commit a54a56865a
2 changed files with 134 additions and 69 deletions

View File

@@ -11,6 +11,7 @@
<div class="step-item" :class="{ active: activeStep >= 2, completed: activeStep > 2 }">
<div class="step-circle">2</div>
<div class="step-title">称重登记</div>
<div class="step-title">30分钟有效</div>
</div>
<div class="step-line"></div>
<div class="step-item" :class="{ active: activeStep >= 3, completed: activeStep > 3 }">
@@ -270,7 +271,7 @@ import {
getVmCloseTask,
getVmSaveImageTask,
validateWeighing,
vmSend
vmSend, weighAHStart, weighAHStop
} from '@/apis/weightManage/weightManage'
import {type WorkOrderResp, addWorkOrder} from '@/apis/workOrder/workOrder'
import {getCaptureImage, getCheckStatus, getEnterWeighPage, getLeaveWeighPage } from "@/apis/weightManage/ys";
@@ -394,6 +395,9 @@ const stopStatusCheck = () => {
}
}
// 称重登记页面超时定时器
let weighingTimeoutTimer: number | null = null
// 组件挂载时
onMounted(() => {
// 启动图片自动刷新,每秒更新一次
@@ -430,6 +434,11 @@ onMounted(() => {
getVmSaveImageTask().catch(error => {
console.error('启动vm定时任务保存图片失败:', error)
})
// 清除称重登记页面的超时定时器
if (weighingTimeoutTimer) {
clearTimeout(weighingTimeoutTimer)
weighingTimeoutTimer = null
}
} else if (newVal === 2) {
// 进入称重登记页面断开灯光并关闭vm定时任务
disconnect().catch(error => {
@@ -441,6 +450,23 @@ onMounted(() => {
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 {
// 离开称重页面断开灯光并关闭vm定时任务
leaveWeighPage()
@@ -450,6 +476,11 @@ onMounted(() => {
disconnect().catch(error => {
console.error('断开灯光失败:', error)
})
// 清除称重登记页面的超时定时器
if (weighingTimeoutTimer) {
clearTimeout(weighingTimeoutTimer)
weighingTimeoutTimer = null
}
}
})
})
@@ -463,6 +494,11 @@ onBeforeUnmount(() => {
}
// 停止状态检查
stopStatusCheck()
// 清除称重登记页面的超时定时器
if (weighingTimeoutTimer) {
clearTimeout(weighingTimeoutTimer)
weighingTimeoutTimer = null
}
// 离开称重页面
if (activeStep.value === 2) {
leaveWeighPage()
@@ -613,81 +649,97 @@ const fetchMaterialData = async (code: string) => {
// 处理下一步
const handleNext = async () => {
if (activeStep.value < 3) {
if (activeStep.value === 2) {
// 当在称重登记页面点击完成时,显示确认弹框
Modal.confirm({
title: '确认完成',
content: '确定要完成称重登记吗?',
onOk: async () => {
try {
// 准备工作订单数据
const workOrderData = {
materialId: formData.id,
workOrderInfos: weighingList.value,
}
// 调用后端接口
addWorkOrder(workOrderData).then((res) => {
if (res.code === '0') {
Notification.success({
title: '操作成功',
content: `工单创建成功!`,
})
workOrderResp.value.id = res.data?.id || ''
workOrderResp.value.matchResult = 'success'
workOrderResp.value.title = res.data?.title || ''
workOrderResp.value.orderNo = res.data?.orderNo || ''
workOrderResp.value.totalWeight = res.data?.totalWeight || ''
workOrderResp.value.totalCalculatedWeight = res.data?.totalCalculatedWeight || ''
workOrderResp.value.totalCount = res.data?.totalCount || ''
// 关闭WebSocket连接
closeWebSocket()
// 跳转到完成页面
activeStep.value++
return true
}
})
} catch (error) {
console.error('创建工作订单失败:', error)
Message.error('创建工作订单失败')
console.log('当前步骤:', activeStep.value)
// 只处理步骤1和步骤2
if (activeStep.value >= 3) return;
// 步骤2显示确认弹框
if (activeStep.value === 2) {
Modal.confirm({
title: '确认完成',
content: '确定要完成称重登记吗?',
onOk: async () => {
try {
// 准备工作订单数据
const workOrderData = {
materialId: formData.id,
workOrderInfos: weighingList.value,
}
},
})
} else if (activeStep.value === 1 && compareMatchResult.value !== 'success') {
// 当在扫码核验页面点击开始比对时,调用后端接口获取比对结果
try {
// 调用后端接口
addWorkOrder(workOrderData).then((res) => {
if (res.code === '0') {
Notification.success({
title: '操作成功',
content: `工单创建成功!`,
})
workOrderResp.value.id = res.data?.id || ''
workOrderResp.value.matchResult = 'success'
workOrderResp.value.title = res.data?.title || ''
workOrderResp.value.orderNo = res.data?.orderNo || ''
workOrderResp.value.totalWeight = res.data?.totalWeight || ''
workOrderResp.value.totalCalculatedWeight = res.data?.totalCalculatedWeight || ''
workOrderResp.value.totalCount = res.data?.totalCount || ''
const materialCode = formData.inputMaterialCode?.trim()
if (!materialCode) {
Message.error('请先扫描物料编码')
}
// 关闭WebSocket连接
closeWebSocket()
//停用电子称
weighAHStop()
if (!formData.materialProcess || formData.materialProcess === '') {
Message.error('未找到物料流程,无法对比')
return
// 跳转到完成页面
activeStep.value++
return true
}
})
} catch (error) {
console.error('创建工作订单失败:', error)
Message.error('创建工作订单失败')
}
},
})
return;
}
// 调用后端接口获取比对结果 // todo
const res = await vmSend(formData.materialProcess);
if (res.data && res.data == materialCode) {
compareMatchResult.value === 'success';
Message.success('比对成功')
} else {
// 比对失败,提示错误
Message.error('比对失败')
}
} catch (error) {
console.error('比对失败:', error)
Message.error('比对失败,请重试')
// 步骤1且未成功比对执行比对操作
if (activeStep.value === 1 && compareMatchResult.value !== 'success') {
try {
const materialCode = formData.inputMaterialCode?.trim()
if (!materialCode) {
Message.error('请先扫描物料编码')
return;
}
} else {
activeStep.value++
// 进入称重页面时建立WebSocket连接
if (activeStep.value === 2) {
establishWebSocket()
if (!formData.materialProcess || formData.materialProcess === '') {
Message.error('未找到物料流程,无法对比')
return;
}
// 调用后端接口获取比对结果 // todo
const res = await vmSend(formData.materialProcess);
if (res.data && res.data == materialCode) {
compareMatchResult.value = 'success'
Message.success('比对成功')
} else {
// 比对失败,提示错误
Message.error('比对失败')
}
// compareMatchResult.value = 'success'
} catch (error) {
console.error('比对失败:', error)
Message.error('比对失败,请重试')
}
return; // 提前返回,避免执行后续代码
}
// 其他情况步骤1且已成功比对或步骤0直接进入下一步
activeStep.value++
// 进入称重页面时建立WebSocket连接
if (activeStep.value === 2) {
weighAHStart().catch(error => {
console.error('启动电子称连接线程失败:', error)
})
establishWebSocket()
}
}
@@ -916,6 +968,9 @@ const playAudio = (filename: string) => {
// 组件卸载时关闭WebSocket连接
onUnmounted(() => {
weighAHStop().catch(error => {
console.error('终止电子称连接线程失败:', error)
})
closeWebSocket()
})
</script>