1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<template>
    <div >
      <RectRenderer 
    :layoutData="layoutData" 
    :gw="1400"
    :gh="800"
    :state=0
    style="width: 1500px; height: 800px; position: relative;"
    v-if="dataLoaded"
  />
  
  
    </div>
    <button @click="submitLayouts" style="position: fixed; bottom: 20px; right: 20px; padding: 10px; background: #4CAF50; color: white; border: none; border-radius: 5px; cursor: pointer;">
      保存OPT
    </button>
  </template>
 
<script setup>import { ref, onMounted } from 'vue';
import RectRenderer from './page/RectRenderer.vue';
import request from "@/utils/request";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
 
const { t } = useI18n();
 
const savedProjectNo = localStorage.getItem('projectNo');
const processId = savedProjectNo;
const layoutData = ref(null);
const dataLoaded = ref(false);
 
const selectLayout = () => {
  request.post(`/glassOptimize/selectOptimizeResult/${processId}`)
      .then((res) => {
        if (res.code === 200 && res.data && res.data.data && res.data.data.length > 0) {
          try {
            console.log("原始数据:", res.data.data[0]); // 调试信息
 
            // 解析保存的布局数据
            const parsedData = JSON.parse(res.data.data[0].Layouts);
            console.log("解析后的数据:", parsedData); // 调试信息
 
            // 确保数据结构正确
            if (parsedData && parsedData.layouts) {
              layoutData.value = parsedData;
              dataLoaded.value = true;
            } else {
              ElMessage.warning("数据格式不正确");
            }
          } catch (error) {
            console.error("数据解析失败:", error);
            ElMessage.error("数据解析失败: " + error.message);
          }
        } else {
          ElMessage.warning("未找到优化数据");
        }
      })
      .catch((error) => {
        console.error("请求失败:", error);
        ElMessage.error(t('basicData.msg.requestFailed'));
      });
};
 
onMounted(() => {
  selectLayout();
});
 
const submitLayouts = async () => {
  try {
    // 确保有数据可以提交
    if (!layoutData.value) {
      ElMessage.warning('没有可保存的数据');
      return;
    }
 
    const response = await request.post('/glassOptimize/generateOpt', {
      Layouts: JSON.stringify(layoutData.value) // 序列化整个layoutData对象
    }, {
      headers: {
        'Content-Type': 'application/json'
      },
      responseType: 'blob'
    });
 
    // 处理下载
    const downloadUrl = window.URL.createObjectURL(response);
    const a = document.createElement('a');
    a.href = downloadUrl;
    a.download = 'output.opt';
    document.body.appendChild(a);
    a.click();
    document.body.removeChild(a);
    window.URL.revokeObjectURL(downloadUrl);
    ElMessage.success('OPT文件下载成功,请选择文件路径');
  } catch (error) {
    console.error('下载失败:', error);
    ElMessage.error('下载失败,请稍后再试');
  }
};
</script>