| | |
| | | import { useI18n } from 'vue-i18n' |
| | | import { WebSocketHost ,host} from '@/utils/constants' |
| | | import { initializeWebSocket, closeWebSocket } from '@/utils/WebSocketService'; |
| | | import {gridDateFormatter} from '@/stores/tool'; |
| | | |
| | | let language = ref(localStorage.getItem('lang') || 'zh') |
| | | const { t } = useI18n() |
| | |
| | | const loadData = ref([]); |
| | | const findMachine = ref([]); |
| | | const sendRecords = ref([]); |
| | | const machineId=14;//当前页面的设备ID |
| | | //使用WebSocket方式展示数据 |
| | | let socket = null; |
| | | const socketUrl = `ws://${WebSocketHost}:${host}/api/deviceInteraction/api/talk/silkScreenTask`; |
| | | // 定义消息处理函数,更新 receivedData 变量 |
| | | const handleMessage = (data) => { |
| | | // 更新 tableData 的数据 |
| | | loadData.value = data.taskingList[0]; |
| | | findMachine.value = data.machine[0]; |
| | | sendRecords.value = data.sendRecords[0]; |
| | | }; |
| | | onUnmounted(() => { |
| | | if (socket) { |
| | | closeWebSocket(socket); |
| | | |
| | | const userInfo=JSON.parse(window.localStorage.getItem('userInfo')); |
| | | const pageParams=userInfo.user.menus[0].pages[4].params; |
| | | const pageParamsJson=JSON.parse(pageParams); |
| | | const machineId = pageParamsJson.machineId;//当前页面的设备ID |
| | | console.log(machineId); |
| | | //const machineId=14;//当前页面的设备ID |
| | | // 新增:定时任务ID(用于清除定时器) |
| | | const timerId = ref(null); |
| | | /** |
| | | * 单次接口请求函数 |
| | | * 作用:调用后端接口,获取数据后交给 handleMessage 处理 |
| | | */ |
| | | const fetchMachineData = async () => { |
| | | try { |
| | | // 任务数据 |
| | | request.post("/deviceInteraction/tasking/findMachineTask",{ |
| | | "id": machineId |
| | | }).then((res) => { // 替换为你的API端点 |
| | | if (res.code === 200) { |
| | | // 假设后端返回的数据格式与 handleMessage 预期一致 |
| | | const responseData = res.data || {}; |
| | | loadData.value =responseData; |
| | | } |
| | | }); |
| | | |
| | | // 设备数据 |
| | | request.post("/deviceInteraction/machine/findMachine",{ |
| | | "id": machineId |
| | | }).then((res) => { // 替换为你的API端点 |
| | | if (res.code === 200) { |
| | | // 假设后端返回的数据格式与 handleMessage 预期一致 |
| | | const responseData = res.data || {}; |
| | | findMachine.value =responseData; |
| | | if (Object.keys(findMachine.value).length === 0){ |
| | | if(findMachine.value.remark.toString()!=errorScanId&&findMachine.value.remark.toString()!=""&&findMachine.value.remark.toString()!=undefined){ |
| | | //有数据就弹窗 |
| | | if(errorOpen){ |
| | | ElMessageBox.close(); |
| | | } |
| | | errorScanId=findMachine.value.remark.toString(); |
| | | errorInfoFuntion(findMachine.value.remark.toString()); |
| | | errorOpen=true; |
| | | }else if(findMachine.value.remark.toString()==""&&errorOpen){ |
| | | ElMessageBox.close(); |
| | | errorOpen=false; |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | |
| | | // 线下数据 |
| | | } catch (err) { |
| | | console.error('定时请求后端接口失败:', err); |
| | | } |
| | | }); |
| | | onBeforeUnmount(() => { |
| | | console.log("关闭了") |
| | | closeWebSocket(); |
| | | }; |
| | | |
| | | /** |
| | | * 启动定时器 |
| | | * @param {number} interval - 定时间隔(毫秒,默认500ms,与后端任务频率对齐) |
| | | */ |
| | | const startTimer = (interval = 500) => { |
| | | // 先清除已有定时器(避免重复创建) |
| | | if (timerId.value) clearInterval(timerId.value); |
| | | // 立即执行一次(避免首次等待间隔) |
| | | fetchMachineData(); |
| | | // 创建新定时器 |
| | | timerId.value = setInterval(() => { |
| | | fetchMachineData(); |
| | | }, interval); |
| | | }; |
| | | |
| | | /** |
| | | * 清除定时器(避免内存泄漏) |
| | | */ |
| | | const clearTimer = () => { |
| | | if (timerId.value) { |
| | | clearInterval(timerId.value); |
| | | timerId.value = null; // 重置ID |
| | | } |
| | | }; |
| | | |
| | | // 组件卸载时清除定时器(关键:避免组件销毁后定时器仍运行) |
| | | onUnmounted(() => { |
| | | clearTimer(); |
| | | }); |
| | | |
| | | onMounted(async () => { |
| | | //使用WebSocket方式展示数据 |
| | | socket = initializeWebSocket(socketUrl, handleMessage);// 初始化 WebSocket,并传递消息处理函数 |
| | | startTimer(500); // 500ms 间隔(可根据业务调整,如1000ms) |
| | | // //使用WebSocket方式展示数据 |
| | | // socket = initializeWebSocket(socketUrl, handleMessage);// 初始化 WebSocket,并传递消息处理函数 |
| | | // //使用WebSocket方式展示数据 |
| | | // socketDownLineTask = initializeWebSocket(socketDownLineTaskUrl, handleMessage2);// 初始化 WebSocket,并传递消息处理函数 |
| | | }); |
| | | //修改工作状态 【失败/正在工作/完工】 |
| | | const workStatus = async(row,state) => { |
| | |
| | | {{findMachine['state']=='开工'?$t('functionState.start'):$t('functionState.stop')}}</el-button> |
| | | <el-button type="primary" id="ButtonTopLine" @click="topLine">{{$t('functionState.topLine')}}</el-button> |
| | | </div> |
| | | <div id="main-body" style="height:240px; min-height:240px;"> |
| | | <div id="main-body" style=" min-height:240px;"> |
| | | <!-- 表格内容 --> |
| | | <el-table :data="loadData" stripe style="height:680px" |
| | | :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center' }" |
| | |
| | | <el-table-column prop="silkScreenX" :label="$t('glassInfo.silkScreenX')"/> |
| | | <el-table-column prop="silkScreenY" :label="$t('glassInfo.silkScreenY')"/> |
| | | <el-table-column prop="workState" :label="$t('glassInfo.workState')"/> |
| | | <el-table-column prop="operationRecordTime" :formatter="gridDateFormatter" :label="$t('glassInfo.operationRecordTime')" width="180"/> |
| | | <el-table-column fixed="right" :label="$t('productStock.operate')" align="center" width="270"> |
| | | <template #default="scope"> |
| | | <el-button size="mini" link type="primary" plain @click="workStatus(scope.row, '重发')" >{{$t('functionState.anew')}}</el-button> |