严智鑫
2024-08-19 de46cb80b7c4d1fb74a4b72d8fc4406e7e3144e7
UI-Project/src/views/Identify/identify.vue
@@ -2,23 +2,18 @@
  <div style="display: flex;">
<el-input v-model="inputValue" style="margin-left: 15px;margin-top: 10px;width: 240px" placeholder="请输入工程号" @blur="handleBlur"/>
<el-pagination
      v-model:current-page="currentPage3"
      v-model:page-size="pageSize3"
      v-model:current-page="currentPage"
      :page-size="pageSize"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="prev, pager, next, jumper"
      :total="1000"
      @size-change="handleSizeChange"
      :total="totalPages"
      @current-change="handleCurrentChange"
    />
    </div>
    <el-card style="flex: 1;margin-left: 10px;margin-top: 10px;margin-right: 10px;" v-loading="loading">
      <el-scrollbar height="550px" >
  <div id="app" style="margin-top: 20px;">
    <!-- <div
      :style="{ width: `${olWidth}px`, height: `${olHeight}px`,position: 'relative',}"
    >  -->
    <el-card style="flex: 1;margin-left: 10px;margin-top: 10px;margin-right: 10px;height: 600px;" v-loading="loading">
      <el-scrollbar height="550px" width="1200px" style="background-color: #e9e9eb;">
  <div style="position: relative;">
    <div  
      v-for="(rect, index) in adjustedRects"  
      :key="rect.glass_id"  
@@ -32,13 +27,13 @@
     <!-- 箭头 -->  
     <!-- <div id="arrow"></div>  
     <div id="line"></div>   -->
     <!-- <div  class="centered-text" >NG24030401B01</div> -->
     <div  class="centered-text">
    <div>{{ rect.process_id }}</div>
    <div style="margin-top: 50px;margin-left: -85px;">{{ rect.widtha }}*{{ rect.heighta }}</div>
    <div>{{ rect.glass_id }}</div>
    <div>{{ rect.widtha }}*{{ rect.heighta }}</div>
  </div>
  </div> 
  <!-- 点击弹出 -->
   </div>
  </el-scrollbar>
  <el-dialog v-model="blind" top="30vh" width="15%" style="text-align: center;">
        <el-button type="warning" plain :icon="Delete" @click="handleDamage(currentGlassId)"  style="width: 140px;margin-left: 10px;">
          {{ $t('order.dilapidation') }}
@@ -47,15 +42,12 @@
          <el-icon class="el-icon--right"><Upload /></el-icon>
          {{ $t('order.Takeaway') }}</el-button>
  </el-dialog> 
   </div>
  <!-- </div>   -->
  </el-scrollbar>
  </el-card>
</template>  
<script setup lang="ts">  
import { Delete, Upload } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, onMounted, onBeforeUnmount,onUnmounted,watchEffect } from 'vue';
import { ref, onMounted, onBeforeUnmount,onUnmounted, computed  } from 'vue';
import request from "@/utils/request"
import { WebSocketHost ,host} from '@/utils/constants'
import { initializeWebSocket, closeWebSocket } from '@/utils/WebSocketService';
@@ -65,11 +57,18 @@
const blind = ref(false)
const olWidth = ref(); 
const olHeight = ref();
const process_id = ref(); // 用于存储process_id的响应式引用
const glass_id = ref();
const inputValue = ref('');
const currentGlassId = ref(null); // 存储当前点击矩形的 glass_id  
const adjustedRects = ref([]);
const raw = ref([]);
let webSocket: WebSocket | null = null;
const totalPages = ref(0);
const pageSize = ref(1);
const currentPage = ref(1);
const disabled = false;
const size = 'small';
const rawData = ref([]);
// const handleBind = (row) => {
//   blind.value = true; // 打开绑定架子对话框
// };
@@ -78,42 +77,6 @@
  currentGlassId.value = glassId;  
  blind.value = true;  
}  
// onMounted(async () => {
//   try {
//     const response = await request.post('/cacheGlass/taskCache/currentCutTerritory'); // 替换为你的API端点
//     if (response.code === 200) {
//       // const process_id = response.data[0].process_id
//       const rawRects = response.data; // 设置矩形数据
//       console.log(response.data);
//       const { olWidth: newolWidth, olHeight: newolHeight, process_id: newprocess_id ,glass_id:newglass_id } = response.data; // 获取尺寸
//       olWidth.value = newolWidth; // 设置容器宽度
//       olHeight.value = newolHeight; // 设置容器高度
//       process_id.value = newprocess_id;
//       glass_id.value = newglass_id;
//       adjustedRects.value = rawRects.map(rect => ({
//         ...rect, // 复制原始对象的其他属性
//         x_axis: (rect.x_axis*100) * 0.003, // 将x值除以3
//         y_axis: (rect.y_axis*100) * 0.003,
//         width: (rect.width*100) * 0.002 ,
//         widtha: rect.width ,
//         heighta: rect.height ,
//         height:( rect.height*100) * 0.002 ,
//         glass_state: rect.glass_state
//       }));
//       console.log(adjustedRects.value);
//       //   console.log( (rect.width*100) / 300 );
//     } else {
//       // console.error('Failed to fetch rectangles from API.');
//       console.error('Failed to fetch rects from API.');
//     }
//   } catch (error) {
//     // console.error('Error fetching rectangles :', error);
//     console.error('Error fetching rects :', error);
//   }
// });
// 破损
const handleDamage = async () => {
  try  {
@@ -121,16 +84,16 @@
    //   console.log(url);
    //   const response = await request.post(url)
  const response = await request.post('/cacheGlass/taskCache/identControls', {
    identId: currentGlassId.value,
    controlsId: 201,
    glassId: currentGlassId.value,
    state: 8,
    line: 1001,
    machine: '识别',
    workingProcedure: '识别',
    })
    if (response.code == 200) {
      ElMessage.success(response.message);
      // window.location.reload() 
      blind.value = false;
      updateRectStatus(currentGlassId.value, 201);
      updateRectStatus(currentGlassId.value, 8);
    } else {
      // 请求失败,显示错误消息
      ElMessage.error(response.msg);
@@ -148,17 +111,17 @@
    //   console.log(url);
    //   const response = await request.post(url)
  const response = await request.post('/cacheGlass/taskCache/identControls', {
    identId: currentGlassId.value,
    controlsId: 200,
    glassId: currentGlassId.value,
    state: 9,
    line: 1001,
    machine: '识别',
    workingProcedure: '识别',
    })
    if (response.code == 200) {
      // 绑定成功,处理逻辑
      ElMessage.success(response.message);
      // window.location.reload() 
      blind.value = false;
      updateRectStatus(currentGlassId.value, 200);
      updateRectStatus(currentGlassId.value, 9);
    } else {
      // 请求失败,显示错误消息
      ElMessage.error(response.msg);
@@ -170,41 +133,53 @@
  }
}
const handleBlur = async () => {  
  if (inputValue.value !== '') {
    try {
  if (inputValue.value) {
      var url="/cacheGlass/taskCache/cutTerritory?current="+inputValue.value;
      const response = await request.post(url)
      if (response.data && response.data.currentCutTerritory) {
        // 假设返回的数据需要调整坐标和尺寸
        adjustedRects.value = response.data.currentCutTerritory.map(rect => ({
    if (response.code === 200) {
      console.log(response.data);
      const rawRects = response.data[0];
      rawData.value = response.data;
      totalPages.value = rawData.value.length;
      const scaleFactor =  1621.78/6000;
      adjustedRects.value = rawRects.map(rect => ({
          ...rect,  
          x_axis: 1360 - (rect.x_axis + rect.width) * 0.37,
          y_axis: (rect.y_axis * 100) * 0.003,
          width: (rect.width * 100) * 0.002,
          height: (rect.height * 100) * 0.002,
        x_axis: (6000 -(rect.x_axis + rect.width)) * scaleFactor,
        y_axis: rect.y_axis * 0.17,
        width: rect.width * scaleFactor ,
          widtha: rect.width ,
          heighta: rect.height ,
        height: rect.height * 0.165 ,
          glass_state: rect.glass_state  
        }));
      // 如果WebSocket已连接,则关闭
      if (webSocket) {
        webSocket.close();
        webSocket = null;
      }  
    } catch (error) {
      console.error('请求数据失败:', error);
       currentPage.value = 1;
    }  
  } else{
    connectWebSocket();
  }  
};  
// const handleBlur = async () => {
//   if (inputValue.value !=''){
//     try {
//       var url="/cacheGlass/taskCache/cutTerritory?current="+inputValue.value;
//       const response = await request.post(url)
//       console.log(response.data.length);
//     if (response.code == 200) {
//       adjustedRects.value = response.data
//       ElMessage.success(response.message);
//     } else {
//       ElMessage.error(response.msg);
//     }
// }
const handleCurrentChange = (val: number) => {
  currentPage.value = val;
  const page = currentPage.value - 1
      const scaleFactor =  1621.78/6000;
      adjustedRects.value = rawData.value[page]?.map(rect => ({
      // adjustedRects.value = rawRects.map(rect => ({
        ...rect,
        x_axis: (6000 -(rect.x_axis + rect.width)) * scaleFactor,
        y_axis: rect.y_axis * 0.17,
        width: rect.width * scaleFactor ,
        widtha: rect.width ,
        heighta: rect.height ,
        height: rect.height * 0.165 ,
        glass_state: rect.glass_state
      }));
};
function getRectColor(state: number): string {  
  switch (state) {  
    case 0:  
@@ -215,12 +190,12 @@
      return '#b3e19d';  
    case 120:  
      return '#f89898';  
    case 200:
      return 'lightblue';
    case 201:
    case 8:
      return '#911005';
    case 9:
      return '#f3d19e';  
    default:
      return '#911005'; // 默认颜色
    // default:
    //   return '#911005'; // 默认颜色
  }  
// 更新矩形状态  
@@ -231,38 +206,45 @@
    }  
  });  
}
let socket = null;
const connectWebSocket = () => {
  if (!webSocket) {
const socketUrl = `ws://${WebSocketHost}:${host}/api/cacheGlass/api/talk/cacheGlass`;
const handleMessage = (data) => {
  // adjustedRects.value = data.currentCutTerritory[0]
    webSocket = new WebSocket(socketUrl);
    webSocket.onmessage = (event) => {
      const data = JSON.parse(event.data);
      const scaleFactor =  1621.78/6000;
  adjustedRects.value = data.currentCutTerritory[0].map(rect => ({  
        ...rect,
        x_axis: 1360 -(rect.x_axis + rect.width) * 0.37,
        // x_axis: (rect.x_axis*100) * 0.003,
        y_axis: (rect.y_axis*100) * 0.003,
        width: (rect.width*100) * 0.002 ,
        x_axis: (6000 -(rect.x_axis + rect.width)) * scaleFactor,
        y_axis: rect.y_axis * 0.17,
        width: rect.width * scaleFactor ,
        widtha: rect.width ,
        heighta: rect.height ,
        height:( rect.height*100) * 0.002 ,
        height: rect.height * 0.165 ,
        glass_state: rect.glass_state
      }));  
};
    webSocket.onerror = (error) => {
      console.error('WebSocket Error:', error);
    };
    webSocket.onclose = () => {
      console.log('WebSocket Connection Closed');
      // 可以选择重新连接
    };
  }
};
onMounted(() => {
  socket = initializeWebSocket(socketUrl, handleMessage);
  // 初始时,如果输入框为空,则连接WebSocket
  if (!inputValue.value) {
    connectWebSocket();
  }
});
  onUnmounted(() => {
    if (socket) {
    closeWebSocket(socket);
  if (webSocket) {
    webSocket.close();
    }
    });
  watchEffect(() => {
  if (inputValue.value === '') {
    handleMessage();
  }
});
onBeforeUnmount(() => {
  console.log("关闭了")
  closeWebSocket();
});
</script>  
  
@@ -273,10 +255,11 @@
.centered-text {
  /* 设置文字居中样式 */  
  display: flex;
  /* display: flex; */
  justify-content: center;  
  align-items: center;  
  height: 100%; /* 确保div占据整个矩形的高度 */  
  font-size: small;
#rect {  
  position: relative; /* 确保箭头可以相对于矩形定位 */