UI-Project/src/views/ManualReporting/manualReporting.vue
@@ -16,7 +16,7 @@
const blind = ref(false)
const errorInfo = ref(true)
const loadData = ref([]);
const taskinggoruby = ref([]);
const findMachine = ref([]);
const sendRecords = ref([]);
const downLineTask = ref([]);
@@ -31,20 +31,65 @@
console.log(machineId);
var errorScanId;
var errorOpen=false;
//使用WebSocket方式展示数据
let socket = null;
let socketDownLineTask = null;
const socketUrl = `ws://${WebSocketHost}:${host}/api/deviceInteraction/api/talk/manualReporting`;
const socketDownLineTaskUrl = `ws://${WebSocketHost}:${host}/api/deviceInteraction/api/talk/downLineTask`;
// 定义消息处理函数,更新 receivedData 变量
const handleMessage = (data) => {
  // 更新 tableData 的数据
  //taskinggoruby.value = data.taskinggoruby[0];
  //loadData.value =getDataSort(data.taskingList[0]);
  const dataJson = data[""+machineId][0];
  loadData.value = dataJson.taskingList[0];
  findMachine.value = dataJson.machine[0];
// 分组后的统计数据
const taskingGroupBy = ref([]);
// 分组并统计的方法
const groupAndCount = () => {
  // 创建一个临时对象用于分组计数
  const groupMap = {};
  // 遍历原始数据
  loadData.value.forEach(item => {
    // 生成唯一的分组键(组合三个字段)
    const groupKey = `${item.scanId || ''}-${item.batchNumber || ''}-${item.taskType || ''}`;
    // 检查该分组是否已存在
    if (groupMap[groupKey]) {
      // 已存在则数量+1
      groupMap[groupKey].reportCount++;
    } else {
      // 不存在则创建新分组
      groupMap[groupKey] = {
        scanId: item.scanId,
        batchNumber: item.batchNumber,
        taskType: item.taskType,
        taskQuantity: item.taskQuantity,
        reportCount:1
  
      };
    }
  });
  console.log(taskingGroupBy.value);
  // 将对象转换为数组
  taskingGroupBy.value = Object.values(groupMap);
};
// 新增:定时任务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;
        groupAndCount();
      }
    });
    // 设备数据
    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){
@@ -57,31 +102,51 @@
    ElMessageBox.close();
    errorOpen=false;
  }
};
// 定义消息处理函数,更新 receivedData 变量
const handleMessage2 = (data) => {
  downLineTask.value = data.downLineTask[0];
  // console.log(data);
};
onUnmounted(() => {
  if (socket) {
    closeWebSocket(socket);
  }
  if (socketDownLineTask) {
    closeWebSocket(socketDownLineTask);
  }
});
onBeforeUnmount(() => {
  console.log("关闭了")
  closeWebSocket();
    // 线下数据
  } catch (err) {
    console.error('定时请求后端接口失败:', err);
  }
};
/**
 * 启动定时器
 * @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,并传递消息处理函数
  //使用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) => {
@@ -362,7 +427,7 @@
    <div id="main-body" style="min-height:240px;">
      <!-- 表格内容 -->
      <el-table :data="taskinggoruby" stripe
      <el-table :data="taskingGroupBy" stripe
        :header-cell-style="{ background: '#F2F3F5 ', color: '#1D2129', textAlign: 'center' }"
        :cell-style="{ textAlign: 'center' }">
        <!-- <el-table-column type="selection" min-width="30" /> -->
@@ -383,13 +448,6 @@
        <el-table-column prop="scanId" :label="$t('glassInfo.scanId')" />
        <el-table-column prop="taskQuantity" :label="$t('glassInfo.taskQuantity')" />
        <el-table-column prop="reportCount" :label="$t('glassInfo.reportCount')" />
        <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="damagedTask(scope.row)">{{ $t('functionState.lose')
              }}</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>