严智鑫
2025-09-04 5bd56f3ebf96aa9a71e4a452cd2402df59d17384
UI-Project/src/views/Marking/marking.vue
@@ -2,8 +2,12 @@
<script setup>
import request from "@/utils/request";
import { ElMessage, ElMessageBox } from "element-plus";
import { reactive, ref, onMounted } from 'vue'
import { reactive, ref, onMounted, onBeforeUnmount,onUnmounted } from 'vue'
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 requestData = reactive({
@@ -12,45 +16,99 @@
});
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;
const pageParamsJson=JSON.parse(pageParams);
const machineId = pageParamsJson.machineId;//当前页面的设备ID
console.log(machineId);
//const machineId=12;//当前页面的设备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;
      }
    });
    // 线下数据
    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);
  }
};
/**
 * 启动定时器
 * @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 () => {
  load();
  startTimer(500); // 500ms 间隔(可根据业务调整,如1000ms)
  // //使用WebSocket方式展示数据
  // socket = initializeWebSocket(socketUrl, handleMessage);// 初始化 WebSocket,并传递消息处理函数
  // //使用WebSocket方式展示数据
  // socketDownLineTask = initializeWebSocket(socketDownLineTaskUrl, handleMessage2);// 初始化 WebSocket,并传递消息处理函数
});
//获取数据
const load = async() => {
  //获取设备任务数据
  try {
    const response = await request.post('/deviceInteraction/tasking/findMachineTask',
    {
      "id": 11
    }); // 替换为你的API端点
    if (response.code === 200) {
      loadData.value.findTaskingData= response.data;
    } else {
      ElMessage.warning(res.msg)
    }
  } catch (error) {
    // console.error('Error fetching rects :', error);
  }
  //获取设备状态
  try {
    const response = await request.post('/deviceInteraction/machine/findMachine',
    {
      "id": 11
    }); // 替换为你的API端点
    if (response.code === 200) {
      findMachine.value= response.data;
    } else {
      ElMessage.warning(res.msg)
    }
  } catch (error) {
    // console.error('Error fetching rects :', error);
  }
}
//修改工作状态 【失败/正在工作/完工】
const workStatus = async(row,state) => {
  let url;
  if(state=="失败"){
  if(state=="重发"){
    url="/deviceInteraction/tasking/loseMachineTask";
  }else if(state=="正在工作"){
    url="/deviceInteraction/tasking/startMachineTask";
@@ -60,11 +118,11 @@
    return;
  }
  ElMessageBox.confirm(
        t('marking.tips'),
        t('functionState.tips'),
        t('delivery.prompt'),  
        {
          confirmButtonText: t('marking.sure'),
          cancelButtonText: t('marking.cancel'),
          confirmButtonText: t('functionState.sure'),
          cancelButtonText: t('functionState.cancel'),
          type: 'warning',
        }
      )
@@ -72,11 +130,10 @@
          //开始修改
          request.post(url,
            {
              "id": 11
              "id": machineId
            }).then((res) => { // 替换为你的API端点  
              if (res.code === 200) {
                ElMessage.success(res.message);
                load();
              } else {
                ElMessage.warning(res.message)
              }
@@ -85,7 +142,7 @@
        .catch(() => {
          ElMessage({
            type: 'info',
            message: t('marking.cancel'),
            message: t('functionState.cancel'),
          })
        })
@@ -94,11 +151,11 @@
//开工/暂停
const machineStatus = async(state) => {
  ElMessageBox.confirm(
        t('marking.tips'),
        t('functionState.tips'),
        t('delivery.prompt'),  
        {
          confirmButtonText: t('marking.sure'),
          cancelButtonText: t('marking.cancel'),
          confirmButtonText: t('functionState.sure'),
          cancelButtonText: t('functionState.cancel'),
          type: 'warning',
        }
      )
@@ -106,12 +163,11 @@
          //下线接口
          request.post("/deviceInteraction/machine/updateMachineState",
            {
              "id": 11,
              "id": machineId,
              "state": state
            }).then((res) => { // 替换为你的API端点  
              if (res.code === 200) {
                ElMessage.success(res.message);
                this.load();
              } else {
                ElMessage.warning(res.message)
              }
@@ -120,18 +176,18 @@
        .catch(() => {
          ElMessage({
            type: 'info',
            message: t('marking.cancel'),
            message: t('functionState.cancel'),
          })
        })
}
//破损
const damagedTask = async(row) => {
  ElMessageBox.confirm(
        t('marking.tips'),
        t('functionState.tips'),
        t('delivery.prompt'),  
        {
          confirmButtonText: t('marking.sure'),
          cancelButtonText: t('marking.cancel'),
          confirmButtonText: t('functionState.sure'),
          cancelButtonText: t('functionState.cancel'),
          type: 'warning',
        }
      )
@@ -143,7 +199,6 @@
            }).then((res) => { // 替换为你的API端点  
              if (res.code === 200) {
                ElMessage.success(res.message);
                this.load();
              } else {
                ElMessage.warning(res.message)
              }
@@ -152,18 +207,18 @@
        .catch(() => {
          ElMessage({
            type: 'info',
            message: t('marking.cancel'),
            message: t('functionState.cancel'),
          })
        })
}
//下线(拿走)
const glassDownLine = async(row) => {
  ElMessageBox.confirm(
        t('marking.tips'),
        t('functionState.tips'),
        t('delivery.prompt'),  
        {
          confirmButtonText: t('marking.sure'),
          cancelButtonText: t('marking.cancel'),
          confirmButtonText: t('functionState.sure'),
          cancelButtonText: t('functionState.cancel'),
          type: 'warning',
        }
      )
@@ -175,7 +230,6 @@
            }).then((res) => { // 替换为你的API端点  
              if (res.code === 200) {
                ElMessage.success(res.message);
                this.load();
              } else {
                ElMessage.warning(res.message)
              }
@@ -184,7 +238,7 @@
        .catch(() => {
          ElMessage({
            type: 'info',
            message: t('marking.cancel'),
            message: t('functionState.cancel'),
          })
        })
}
@@ -195,50 +249,66 @@
</script>
<template>
  <div ref="content">
  <div ref="content" style="padding:0 20px;">
    <div id="div-title" style="font-size: 20px; font-weight: bold; margin:10px 0 10px 0;padding-left: 20px;">
      自动打标机
      {{$t('machine.marking')}}
    </div>
    <hr />
    <br>
    <div id="search" style="padding-left: 20px;">
    <div id="search" >
      <!-- 功能 -->
      <el-button type="primary" id="ButtonMachineStatus" @click="machineStatus((findMachine['state']=='暂停'?'开工':'暂停'))">{{findMachine['state']=='开工'?'开工':'暂停'}}</el-button>
      <el-button type="primary" id="ButtonTopLine" @click="topLine">上线</el-button>
      <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="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> -->
    </div>
    <div id="main-body" style="min-height:240px;">
    <div id="main-body">
      <!-- 表格内容 -->
      <el-table :data="loadData.findTaskingData" stripe
      <el-table :data="loadData" stripe style="height:260px"
        :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="序号" min-width="30" />
        <el-table-column prop="batchNumber" label="批次号" />
        <el-table-column prop="taskType" label="任务类型" />
        <el-table-column prop="glassId" label="玻璃编号" />
        <el-table-column prop="length" label="长" />
        <el-table-column prop="width" label="宽" />
        <el-table-column prop="thickness" label="厚" />
        <el-table-column prop="workState" label="状态" />
        <el-table-column type="index" :label="$t('glassInfo.number')" min-width="30" />
        <el-table-column prop="isMarking" :label="$t('glassInfo.isMarking')"/>
        <el-table-column prop="batchNumber" :label="$t('glassInfo.batchNumber')"/>
        <el-table-column prop="taskType" :label="$t('glassInfo.taskType')"/>
        <el-table-column prop="glassId" :label="$t('glassInfo.glassId')"/>
        <el-table-column prop="scanId" :label="$t('glassInfo.scanId')" />
        <el-table-column prop="length" :label="$t('glassInfo.length')"/>
        <el-table-column prop="width" :label="$t('glassInfo.width')"/>
        <el-table-column prop="thickness" :label="$t('glassInfo.thickness')"/>
        <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, '失败')">重发</el-button>
            <el-button size="mini" link type="primary" plain @click="workStatus(scope.row, '完工')">完工</el-button>
            <el-button size="mini" link type="primary" plain @click="damagedTask(scope.row)">破损</el-button>
            <el-button size="mini" link type="primary" plain @click="glassDownLine(scope.row)">下线</el-button>
            <el-button size="mini" link type="primary" plain @click="workStatus(scope.row, '重发')" >{{$t('functionState.anew')}}</el-button>
            <el-button size="mini" link type="primary" plain @click="workStatus(scope.row, '完工')">{{$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>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div id="main-body"
      style="width: 100%; height: 460px;background-image: url(../../src/assets/自动打标机.png) ;background-size: 100% 100%;">
      <!-- 画图内容 -->
      <div style="width: 100px; height: 100px; background-color: red; position: relative; top: 171px; left: 218px">
    <div id="main-body" style="width: 100%; height: 460px;min-width: 1200px;">
      <img style="width: 70%;min-width: 900px; height: 460px;float: left;" src="../../assets/自动打标机.png">
      <!-- <el-image style="width: 70%;min-width: 900px; height: 460px;float: left;" src="../../src/assets/自动打标机.png"></el-image> -->
      <!-- <div style="width: 70%;min-width: 900px; height: 460px;background-image: url('../../src/assets/自动打标机.png') ;background-size: 100% 100%;float: left;">
      </div> -->
      <div style="width: 30%;min-width: 300px; height: 460px;float: left;padding: 20px;;">
          <!-- <div style="text-align: center;height: 30px;line-height: 30px;font-size: 20px;padding: 20px;">数据交互记录</div> -->
          <el-table :data="sendRecords" stripe :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>
      <div style="clear:both"></div>
      <!-- 画图内容
      <div style="width: 100px; height: 100px; background-color: red; position: relative; top: 171px; left: 218px">
      </div>-->
    </div>
  </div>
</template>