严智鑫
2025-09-04 5bd56f3ebf96aa9a71e4a452cd2402df59d17384
UI-Project/src/views/Marking/marking.vue
@@ -17,6 +17,7 @@
const loadData = ref([]);
const findMachine = ref([]);
const sendRecords = ref([]);
const downLineTask = ref([]);
const userInfo=JSON.parse(window.localStorage.getItem('userInfo'));
const pageParams=userInfo.user.menus[0].pages[3].params;
@@ -24,31 +25,85 @@
const machineId = pageParamsJson.machineId;//当前页面的设备ID
console.log(machineId);
//const machineId=12;//当前页面的设备ID
//使用WebSocket方式展示数据
let socket = null;
const socketUrl = `ws://${WebSocketHost}:${host}/api/deviceInteraction/api/talk/marking`;
// 定义消息处理函数,更新 receivedData 变量
const handleMessage = (data) => {
  // 更新 tableData 的数据
  let dataJson = data[""+machineId][0];
// 新增:定时任务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;
      }
    });
  loadData.value = dataJson.taskingList[0];
  findMachine.value = dataJson.machine[0];
  sendRecords.value = dataJson.sendRecords[0];
};
onUnmounted(() => {
  if (socket) {
    closeWebSocket(socket);
    // 设备数据
    request.post("/deviceInteraction/machine/findMachine",{
      "id": machineId
    }).then((res) => { // 替换为你的API端点
      if (res.code === 200) {
        // 假设后端返回的数据格式与 handleMessage 预期一致
        const responseData = res.data || {};
        findMachine.value =responseData;
      }
    });
    // 线下数据
    request.post("/deviceInteraction/tasking/selectGlassDownLine").then((res) => { // 替换为你的API端点
      if (res.code === 200) {
        // 假设后端返回的数据格式与 handleMessage 预期一致
        const responseData = res.data || {};
        downLineTask.value =responseData;
      }
    });
  } 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) => {
@@ -205,7 +260,7 @@
      <el-button :type="(findMachine['state']=='暂停'?'danger':'success')" id="ButtonMachineStatus" 
      @click="machineStatus((findMachine['state']=='暂停'?'开工':'暂停'))">
      {{findMachine['state']=='开工'?$t('functionState.start'):$t('functionState.stop')}}</el-button>
      <el-button type="primary" id="ButtonTopLine" @click="topLine">{{$t('functionState.topLine')}}</el-button>
<!--      <el-button type="primary" id="ButtonTopLine" @click="topLine">{{$t('functionState.topLine')}}</el-button>-->
      <!-- <el-button type="primary" id="searchButton" @click="downLine('下线')">下线</el-button>
      <el-button type="primary" id="searchButton" @click="workStatus('破损')">破损</el-button>
      <el-button type="primary" id="searchButton" @click="workStatus('完工')">完工</el-button> -->