UI-Project/src/views/FlipSlice/flipSlice.vue
@@ -26,81 +26,124 @@
const pageParams=userInfo.user.menus[0].pages[2].params;
const pageParamsJson=JSON.parse(pageParams);
const machineId = pageParamsJson.machineId;//当前页面的设备ID
console.log(machineId);
//const machineId = 26;//当前页面的设备ID
var errorScanId;
var errorOpen=false;
//使用WebSocket方式展示数据
let socket = null;
let socketDownLineTask = null;
const socketUrl = `ws://${WebSocketHost}:${host}/api/deviceInteraction/api/talk/flipSlice`;
const socketDownLineTaskUrl = `ws://${WebSocketHost}:${host}/api/deviceInteraction/api/talk/downLineTask`;
// 定义消息处理函数,更新 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].reverse();
  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;
    // 设备数据
    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);
  }
};
// 定义消息处理函数,更新 receivedData 变量
const handleMessage2 = (data) => {
  downLineTask.value = data.downLineTask[0];
  // console.log(data);
/**
 * 启动定时器
 * @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(() => {
  if (socket) {
    closeWebSocket(socket);
  }
  if (socketDownLineTask) {
    closeWebSocket(socketDownLineTask);
  }
});
onBeforeUnmount(() => {
  console.log("关闭了")
  closeWebSocket();
  clearTimer();
});
onMounted(async () => {
  //使用WebSocket方式展示数据
  socket = initializeWebSocket(socketUrl, handleMessage);// 初始化 WebSocket,并传递消息处理函数
  //使用WebSocket方式展示数据
  socketDownLineTask = initializeWebSocket(socketDownLineTaskUrl, handleMessage2);// 初始化 WebSocket,并传递消息处理函数
  startTimer(500); // 500ms 间隔(可根据业务调整,如1000ms)
  // //使用WebSocket方式展示数据
  // socket = initializeWebSocket(socketUrl, handleMessage);// 初始化 WebSocket,并传递消息处理函数
  // //使用WebSocket方式展示数据
  // socketDownLineTask = initializeWebSocket(socketDownLineTaskUrl, handleMessage2);// 初始化 WebSocket,并传递消息处理函数
});
//报警提示
const errorInfoFuntion = async (info) => {
  let infoScancode=info;
  ElMessageBox.confirm(
    infoScancode,
    t('delivery.prompt'),
    {
      confirmButtonText: t('functionState.sureStart'),
      cancelButtonText: t('functionState.cancel'),
      type: 'warning',
    }
      infoScancode,
      t('delivery.prompt'),
      {
        confirmButtonText: t('functionState.sureStart'),
        cancelButtonText: t('functionState.cancel'),
        type: 'warning',
      }
  )
    .then(() => {
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: t('functionState.cancel'),
      .then(() => {
      })
    })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: t('functionState.cancel'),
        })
      })
}
//修改工作状态 【失败/正在工作/完工】
const workStatus = async (row, state) => {
@@ -115,199 +158,199 @@
    return;
  }
  ElMessageBox.confirm(
    t('functionState.tips'),
    t('delivery.prompt'),
    {
      confirmButtonText: t('functionState.sure'),
      cancelButtonText: t('functionState.cancel'),
      type: 'warning',
    }
      t('functionState.tips'),
      t('delivery.prompt'),
      {
        confirmButtonText: t('functionState.sure'),
        cancelButtonText: t('functionState.cancel'),
        type: 'warning',
      }
  )
    .then(() => {
      //开始修改
      request.post(url,
        {
          "id": machineId
        }).then((res) => { // 替换为你的API端点
      .then(() => {
        //开始修改
        request.post(url,
            {
              "id": machineId
            }).then((res) => { // 替换为你的API端点
          if (res.code === 200) {
            ElMessage.success(res.message);
          } else {
            ElMessage.warning(res.message)
          }
        })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: t('functionState.cancel'),
      })
    })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: t('functionState.cancel'),
        })
      })
}
//开工/暂停
const machineStatus = async (state) => {
  ElMessageBox.confirm(
    t('functionState.tips'),
    t('delivery.prompt'),
    {
      confirmButtonText: t('functionState.sure'),
      cancelButtonText: t('functionState.cancel'),
      type: 'warning',
    }
      t('functionState.tips'),
      t('delivery.prompt'),
      {
        confirmButtonText: t('functionState.sure'),
        cancelButtonText: t('functionState.cancel'),
        type: 'warning',
      }
  )
    .then(() => {
      //下线接口
      request.post("/deviceInteraction/machine/updateMachineState",
        {
          "id": machineId,
          "state": state
        }).then((res) => { // 替换为你的API端点
      .then(() => {
        //下线接口
        request.post("/deviceInteraction/machine/updateMachineState",
            {
              "id": machineId,
              "state": state
            }).then((res) => { // 替换为你的API端点
          if (res.code === 200) {
            ElMessage.success(res.message);
          } else {
            ElMessage.warning(res.message)
          }
        })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: t('functionState.cancel'),
      })
    })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: t('functionState.cancel'),
        })
      })
}
//定制/标准
const machineMode = async (mode) => {
  ElMessageBox.confirm(
    t('functionState.tips'),
    t('delivery.prompt'),
    {
      confirmButtonText: t('functionState.sure'),
      cancelButtonText: t('functionState.cancel'),
      type: 'warning',
    }
      t('functionState.tips'),
      t('delivery.prompt'),
      {
        confirmButtonText: t('functionState.sure'),
        cancelButtonText: t('functionState.cancel'),
        type: 'warning',
      }
  )
    .then(() => {
      //下线接口
      request.post("/deviceInteraction/machine/toggleModeMachine",
        {
          "id": machineId,
          "mode": mode
        }).then((res) => { // 替换为你的API端点
      .then(() => {
        //下线接口
        request.post("/deviceInteraction/machine/toggleModeMachine",
            {
              "id": machineId,
              "mode": mode
            }).then((res) => { // 替换为你的API端点
          if (res.code === 200) {
            ElMessage.success(res.message);
          } else {
            ElMessage.warning(res.message)
          }
        })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: t('functionState.cancel'),
      })
    })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: t('functionState.cancel'),
        })
      })
}
//破损
const damagedTask = async (row) => {
  ElMessageBox.confirm(
    t('functionState.tips'),
    t('delivery.prompt'),
    {
      confirmButtonText: t('functionState.sure'),
      cancelButtonText: t('functionState.cancel'),
      type: 'warning',
    }
      t('functionState.tips'),
      t('delivery.prompt'),
      {
        confirmButtonText: t('functionState.sure'),
        cancelButtonText: t('functionState.cancel'),
        type: 'warning',
      }
  )
    .then(() => {
      //下线接口
      request.post("/deviceInteraction/tasking/damagedTask",
        {
          "glassId": row.glassId
        }).then((res) => { // 替换为你的API端点
      .then(() => {
        //下线接口
        request.post("/deviceInteraction/tasking/damagedTask",
            {
              "glassId": row.glassId
            }).then((res) => { // 替换为你的API端点
          if (res.code === 200) {
            ElMessage.success(res.message);
          } else {
            ElMessage.warning(res.message)
          }
        })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: t('functionState.cancel'),
      })
    })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: t('functionState.cancel'),
        })
      })
}
//下线(拿走)
const glassDownLine = async (row) => {
  ElMessageBox.confirm(
    t('functionState.tips'),
    t('delivery.prompt'),
    {
      confirmButtonText: t('functionState.sure'),
      cancelButtonText: t('functionState.cancel'),
      type: 'warning',
    }
      t('functionState.tips'),
      t('delivery.prompt'),
      {
        confirmButtonText: t('functionState.sure'),
        cancelButtonText: t('functionState.cancel'),
        type: 'warning',
      }
  )
    .then(() => {
      //下线接口
      request.post("/deviceInteraction/tasking/glassDownLine",
        {
          "glassId": row.glassId,
        }).then((res) => { // 替换为你的API端点
      .then(() => {
        //下线接口
        request.post("/deviceInteraction/tasking/glassDownLine",
            {
              "glassId": row.glassId,
            }).then((res) => { // 替换为你的API端点
          if (res.code === 200) {
            ElMessage.success(res.message);
          } else {
            ElMessage.warning(res.message)
          }
        })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: t('functionState.cancel'),
      })
    })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: t('functionState.cancel'),
        })
      })
}
const topLineShow = async () => {
  blind.value = true;
  blind.value = true;
}
//上线
const topLine = async (row) => {
  ElMessageBox.confirm(
    t('functionState.tips'),
    t('delivery.prompt'),
    {
      confirmButtonText: t('functionState.sure'),
      cancelButtonText: t('functionState.cancel'),
      type: 'warning',
    }
      t('functionState.tips'),
      t('delivery.prompt'),
      {
        confirmButtonText: t('functionState.sure'),
        cancelButtonText: t('functionState.cancel'),
        type: 'warning',
      }
  )
    .then(() => {
      //上线接口
      request.post("/deviceInteraction/tasking/glassTopLine",
        {
          "glassId": row.glassId,
          "lineConfigurationId": machineId
        }).then((res) => { // 替换为你的API端点
      .then(() => {
        //上线接口
        request.post("/deviceInteraction/tasking/glassTopLine",
            {
              "glassId": row.glassId,
              "lineConfigurationId": machineId
            }).then((res) => { // 替换为你的API端点
          if (res.code === 200) {
            ElMessage.success(res.message);
          } else {n
            ElMessage.warning(res.message)
          }
        })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: t('functionState.cancel'),
      })
    })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: t('functionState.cancel'),
        })
      })
}
const handleDialogClose = () => {
const handleDialogClose = () => {
}
</script>
<template>
@@ -316,7 +359,7 @@
      {{ $t('machine.flipSlice') }}
    </div>
    <el-dialog v-model="blind" top="30vh" style="text-align: center;" @close="handleDialogClose">
        <!-- 线下玻璃 -->
      <!-- 线下玻璃 -->
      <el-table :data="downLineTask" stripe :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center' }" :cell-style="{ textAlign: 'center' }">
        <!-- <el-table-column type="selection" min-width="30" /> -->
        <el-table-column type="index" :label="$t('glassInfo.number')" min-width="30" />
@@ -330,7 +373,7 @@
        <el-table-column fixed="right" :label="$t('productStock.operate')" align="center" width="70">
          <template #default="scope">
            <el-button size="mini" link type="primary" plain @click="topLine(scope.row)">{{
              $t('functionState.topLine') }}</el-button>
                $t('functionState.topLine') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
@@ -340,13 +383,13 @@
    <div id="search">
      <!-- 功能 -->
      <el-button :type="(findMachine['state'] == '暂停' ? 'danger' : 'success')" id="ButtonMachineStatus"
        @click="machineStatus((findMachine['state'] == '暂停' ? '开工' : '暂停'))">{{ findMachine['state'] == '开工' ?
                 @click="machineStatus((findMachine['state'] == '暂停' ? '开工' : '暂停'))">{{ findMachine['state'] == '开工' ?
          $t('functionState.start') : $t('functionState.stop') }}</el-button>
      <el-button :type="(findMachine['mode'] == '定制' ? 'danger' : 'success')" id="ButtonMachineMode"
        @click="machineMode((findMachine['mode'] == 0 ? 1 : 0))">{{ findMachine['mode'] == 0 ?
         $t('functionState.modeD') : $t('functionState.modeB') }}</el-button>
                 @click="machineMode((findMachine['mode'] == 0 ? 1 : 0))">{{ findMachine['mode'] == 0 ?
          $t('functionState.modeD') : $t('functionState.modeB') }}</el-button>
      <el-button type="primary" id="ButtonTopLine" @click="topLineShow">{{ $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>
@@ -355,8 +398,8 @@
    <div id="main-body">
      <!-- 表格内容 -->
      <el-table :data="loadData" stripe style="height:260px"
        :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }">
                :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center' }"
                :cell-style="{ textAlign: 'center' }">
        <!-- <el-table-column type="selection" min-width="30" /> -->
        <el-table-column type="index" :label="$t('glassInfo.number')" min-width="30" />
        <el-table-column prop="batchNumber" :label="$t('glassInfo.batchNumber')" />
@@ -371,13 +414,13 @@
        <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>
                $t('functionState.anew') }}</el-button>
            <el-button size="mini" link type="primary" plain @click="workStatus(scope.row, '完工')">{{
              $t('functionState.finish') }}</el-button>
                $t('functionState.finish') }}</el-button>
            <el-button size="mini" link type="primary" plain @click="damagedTask(scope.row)">{{ $t('functionState.lose')
              }}</el-button>
            <el-button size="mini" link type="primary" plain @click="glassDownLine(scope.row)">{{
              $t('functionState.downLine') }}</el-button>
                $t('functionState.downLine') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
@@ -391,8 +434,8 @@
      </div> -->
      <div style="width: 30%;min-width: 300px; height: 460px;float: left;padding: 20px;;">
        <el-table :data="sendRecords" stripe
          :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center', height: '30px' }"
          :cell-style="{ textAlign: 'center' }">
                  :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center', height: '30px' }"
                  :cell-style="{ textAlign: 'center' }">
          <el-table-column prop="sendContent" :label="$t('glassInfo.sendContent')" />
        </el-table>
      </div>
@@ -467,6 +510,6 @@
  height:10px;
  border-radius: 50%;
  display: block;
  margin-left: 10px;
  margin-left: 10px;
}
</style>