analyze.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. <template>
  2. <div class="analysis-container">
  3. <el-row :gutter="20">
  4. <!-- 左侧主操作区 -->
  5. <el-col :span="16">
  6. <el-card class="upload-section">
  7. <div class="input-method-select" v-if="inputMethod != 'done'">
  8. <el-radio-group v-model="inputMethod">
  9. <el-radio-button value="upload">文件上传</el-radio-button>
  10. <el-radio-button value="input">在线输入</el-radio-button>
  11. </el-radio-group>
  12. </div>
  13. <div v-if="inputMethod === 'upload'" class="upload-area">
  14. <!-- 节点文件上传 -->
  15. <el-upload class="file-upload" :on-change="handleNodeFileChange" :auto-upload="false"
  16. :show-file-list="false">
  17. <el-button type="primary" style="width: 120px; margin-right: 20px" plain>
  18. <el-icon>
  19. <Upload />
  20. </el-icon>
  21. 选择节点文件
  22. </el-button>
  23. <div class="file-info">
  24. {{ nodeFile ? nodeFile.name : '未选择文件' }}
  25. <span v-if="nodeFile">({{ formatSize(nodeFile.size) }})</span>
  26. </div>
  27. </el-upload>
  28. <!-- 边文件上传 -->
  29. <el-upload class="file-upload" :on-change="handleEdgeFileChange" :auto-upload="false"
  30. :show-file-list="false">
  31. <el-button type="primary" style="width: 120px; margin-right: 20px" plain>
  32. <el-icon>
  33. <Upload />
  34. </el-icon>
  35. 选择边文件
  36. </el-button>
  37. <div class="file-info">
  38. {{ edgeFile ? edgeFile.name : '未选择文件' }}
  39. <span v-if="edgeFile">({{ formatSize(edgeFile.size) }})</span>
  40. </div>
  41. </el-upload>
  42. <!-- 上传按钮和进度 -->
  43. <el-button type="success" :disabled="!canUpload" @click="handleUpload" class="upload-button">
  44. 开始上传验证
  45. </el-button>
  46. </div>
  47. <!-- 在线输入节点与边 -->
  48. <div v-if="inputMethod === 'input'" class="upload-area">
  49. <div class="online-input">
  50. <el-row :gutter="20">
  51. <!-- 节点输入列 -->
  52. <el-col :span="12">
  53. <div class="input-section">
  54. <h4>输入节点</h4>
  55. <div v-for="(node, index) in nodes" :key="index" class="input-row">
  56. <el-row :gutter="10" align="middle">
  57. <el-col :span="4">
  58. <div class="node-id">ID:{{ node.id }}</div>
  59. </el-col>
  60. <el-col :span="6">
  61. <el-select v-model="node.type" placeholder="选择类型" @change="handleNodeTypeChange(index)">
  62. <el-option label="S" value="S" />
  63. <el-option label="D" value="D" />
  64. <el-option label="I" value="I" />
  65. </el-select>
  66. </el-col>
  67. <el-col :span="10">
  68. <el-input v-model="node.name" placeholder="输入节点名称" />
  69. </el-col>
  70. <el-col :span="4">
  71. <el-button v-if="index < nodes.length - 1" @click="deleteNode(index)" type="danger" plain
  72. size="small">
  73. 删除
  74. </el-button>
  75. </el-col>
  76. </el-row>
  77. </div>
  78. </div>
  79. </el-col>
  80. <!-- 边输入列 -->
  81. <el-col :span="12">
  82. <div class="input-section">
  83. <h4>输入边</h4>
  84. <div v-for="(edge, index) in edges" :key="index" class="input-row">
  85. <el-row :gutter="10" align="middle">
  86. <el-col :span="4">
  87. <div class="node-id">ID:{{ index + 1 }}</div>
  88. </el-col>
  89. <!-- 起始节点 -->
  90. <el-col :span="8">
  91. <el-select v-model="edge.from" placeholder="起始节点" :disabled="nodeOptions.length === 0"
  92. @change="handleEdgeChange(index)">
  93. <el-option v-for="node in nodeOptions" :key="node.id"
  94. :label="`ID:${node.id}-${node.name || '未命名'}`" :value="node.id" />
  95. </el-select>
  96. </el-col>
  97. <!-- 终止节点 -->
  98. <el-col :span="8">
  99. <el-select v-model="edge.to" placeholder="终止节点" :disabled="nodeOptions.length === 0"
  100. @change="handleEdgeChange(index)">
  101. <el-option v-for="node in nodeOptions" :key="node.id"
  102. :label="`ID:${node.id}-${node.name || '未命名'}`" :value="node.id" />
  103. </el-select>
  104. </el-col>
  105. <!-- 删除按钮 -->
  106. <el-col :span="4">
  107. <el-button v-if="index < edges.length - 1" @click="deleteEdge(index)" type="danger" plain
  108. size="small">
  109. 删除
  110. </el-button>
  111. </el-col>
  112. </el-row>
  113. </div>
  114. </div>
  115. </el-col>
  116. </el-row>
  117. </div>
  118. <el-button type="primary" @click="handleValidation" class="validate-button">
  119. 开始输入验证
  120. </el-button>
  121. </div>
  122. <div v-else >
  123. <router-view></router-view>
  124. </div>
  125. </el-card>
  126. </el-col>
  127. <!-- 右侧说明 -->
  128. <el-col v-if="inputMethod === 'input' || inputMethod === 'upload'" :span="8">
  129. <el-card class="instruction-section">
  130. <h3>文件格式说明</h3>
  131. <div class="instruction-content">
  132. <div class="file-format">
  133. <el-tag type="success" class="format-tag">节点文件格式</el-tag>
  134. <el-divider />
  135. <div class="format-example">
  136. <P>文件应使用CSV格式</P>
  137. <p>每一行按照:“节点编号,节点类型,节点名称” 顺序放置数据</p>
  138. <p class="example-text">示例:</p>
  139. <pre>
  140. 1,S,侦察节点
  141. 2,D,决策节点
  142. 3,I,打击节点</pre>
  143. </div>
  144. </div>
  145. <div class="file-format">
  146. <el-tag type="warning" class="format-tag">边文件格式</el-tag>
  147. <el-divider />
  148. <div class="format-example">
  149. <P>文件应使用CSV格式</P>
  150. <p>每一行按照:“起始节点,终止节点” 顺序放置数据</p>
  151. <p class="example-text">示例:</p>
  152. <pre>
  153. 1,2
  154. 2,3
  155. 1,3</pre>
  156. </div>
  157. </div>
  158. </div>
  159. </el-card>
  160. </el-col>
  161. </el-row>
  162. </div>
  163. </template>
  164. <script setup>
  165. import { ref, computed, onMounted, inject, watch } from 'vue'
  166. import { useRouter, useRoute } from 'vue-router'
  167. import { ElMessage, ElMessageBox } from 'element-plus'
  168. import { Upload, ArrowDown, Plus } from '@element-plus/icons-vue'
  169. import { getData, postData, deleteData, postFile } from '@/api/axios.js'
  170. // Store数据
  171. const useAnalyzeInfo = inject('analyzeInfo')
  172. // 响应式数据
  173. const inputMethod = ref('upload')
  174. // 上传的节点和边文件
  175. const nodeFile = ref(null)
  176. const edgeFile = ref(null)
  177. // 在线输入的节点和边
  178. const nodes = ref([{ id: 1, type: '', name: '' }])
  179. const edges = ref([{ from: '', to: '' }])
  180. const router = useRouter()
  181. // 计算属性
  182. // 是否允许上传文件
  183. const canUpload = computed(() => {
  184. return nodeFile.value && edgeFile.value
  185. })
  186. // 在线输入边时节点的可选项
  187. const nodeOptions = computed(() => {
  188. return nodes.value
  189. .filter(node => node.type !== '')
  190. .map(node => ({
  191. id: node.id,
  192. name: node.name || '未命名',
  193. type: node.type
  194. }))
  195. })
  196. // 方法
  197. // 在线输入自动添加节点
  198. const handleNodeTypeChange = (index) => {
  199. // 当最后一个节点选择类型后自动添加新行
  200. if (index === nodes.value.length - 1) {
  201. nodes.value.push({ id: nodes.value.length + 1, type: '', name: '' })
  202. }
  203. }
  204. // 删除节点
  205. const deleteNode = (index) => {
  206. // 保存原始ID列表
  207. const originalIds = nodes.value.map(n => n.id)
  208. const deletedId = originalIds[index]
  209. // 删除关联边
  210. edges.value = edges.value.filter(edge =>
  211. edge.from !== deletedId && edge.to !== deletedId
  212. )
  213. // 删除节点并创建新数组
  214. const newNodes = [...nodes.value]
  215. newNodes.splice(index, 1)
  216. // 创建ID映射表(旧ID -> 新ID)
  217. const idMap = new Map()
  218. newNodes.forEach((node, i) => {
  219. const newId = i + 1
  220. idMap.set(node.id, newId) // 记录原始ID到新ID的映射
  221. node.id = newId // 更新节点ID
  222. })
  223. // 更新节点数组
  224. nodes.value = newNodes
  225. // 更新边数据中的ID引用
  226. edges.value = edges.value.map(edge => ({
  227. from: idMap.get(edge.from),
  228. to: idMap.get(edge.to)
  229. }))
  230. // 确保至少保留一个空行
  231. if (nodes.value.length === 0) {
  232. nodes.value.push({ id: 1, type: '', name: '' })
  233. }
  234. }
  235. // 获取有效节点ID列表
  236. const validNodeIds = computed(() => {
  237. return nodes.value
  238. .filter(node => node.type !== '')
  239. .map(node => node.id)
  240. })
  241. // 在线输入自动添加边
  242. const handleEdgeChange = (index) => {
  243. const currentEdge = edges.value[index]
  244. // 当最后一个边填写完整后自动添加新行
  245. if (index === edges.value.length - 1 && currentEdge.from && currentEdge.to) {
  246. edges.value.push({ from: '', to: '' })
  247. }
  248. }
  249. // 删除边
  250. const deleteEdge = (index) => {
  251. edges.value.splice(index, 1)
  252. }
  253. // 验证在线输入
  254. const handleValidation = async () => {
  255. const errors = []
  256. const edgeSet = new Set()
  257. // 检查边数据
  258. // 因为始终有空行,所有没有输入的时候长度为1
  259. if (edges.value.length == 1) {
  260. errors.push(`没有输入边`)
  261. }
  262. if (nodes.value.length == 1) {
  263. errors.push(`没有输入节点`)
  264. }
  265. edges.value.slice(0, -1).forEach((edge, index) => {
  266. // 检查节点是否存在
  267. if (!validNodeIds.value.includes(edge.from)) {
  268. errors.push(`边 ${index + 1}: 起始节点 ${edge.from} 不存在`)
  269. }
  270. if (!validNodeIds.value.includes(edge.to)) {
  271. errors.push(`边 ${index + 1}: 终止节点 ${edge.to} 不存在`)
  272. }
  273. // 自环边检查
  274. if (edge.from === edge.to) {
  275. errors.push(`边 ${index + 1}: 不允许连接自身(自环边)`)
  276. }
  277. // 归一化两个方向的边
  278. const [min, max] = [edge.from, edge.to].sort()
  279. // 默认检查无向边
  280. const checkDirection = false
  281. const edgeKey = checkDirection
  282. ? `${edge.from},${edge.to}`
  283. : `${min},${max}`
  284. // 检查重复边
  285. if (edgeSet.has(edgeKey)) {
  286. errors.push(`发现重复边: ${edge.from} → ${edge.to}`)
  287. } else {
  288. edgeSet.add(edgeKey)
  289. }
  290. })
  291. // 处理验证结果
  292. if (errors.length > 0) {
  293. ElMessage.error({
  294. message: `发现 ${errors.length} 个错误:<br>${errors.join('<br>')}`,
  295. duration: 5000,
  296. dangerouslyUseHTMLString: true, // 启用 HTML 解析
  297. customClass: 'error-message' // 可选:添加自定义样式类
  298. })
  299. return
  300. }
  301. // 构造上传数据
  302. const uploadData = {
  303. nodes: nodes.value
  304. .filter(node => node.type !== '')
  305. .map(node => ({
  306. id: node.id,
  307. type: node.type,
  308. name: node.name
  309. })),
  310. edges: edges.value
  311. .filter(edge => edge.from && edge.to)
  312. .map(edge => ({
  313. source: edge.from,
  314. target: edge.to
  315. }))
  316. }
  317. // 执行上传
  318. try {
  319. const response = await postData('/inputFile/', uploadData)
  320. ElMessage.success('输入数据验证通过,上传成功')
  321. preparePlan(response)
  322. } catch (error) {
  323. ElMessage.error('输入数据上传失败: ' + error.message)
  324. }
  325. }
  326. // 上传文件处理
  327. const handleNodeFileChange = (file, fileList) => {
  328. if (fileList.length > 1) {
  329. fileList.splice(0, 1);
  330. }
  331. nodeFile.value = fileList[0].raw
  332. }
  333. const handleEdgeFileChange = (file, fileList) => {
  334. if (fileList.length > 1) {
  335. fileList.splice(0, 1);
  336. }
  337. edgeFile.value = fileList[0].raw
  338. }
  339. const formatSize = (bytes) => {
  340. if (bytes === 0) return '0 B'
  341. const k = 1024
  342. const sizes = ['B', 'KB', 'MB', 'GB']
  343. const i = Math.floor(Math.log(bytes) / Math.log(k))
  344. return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
  345. }
  346. const handleUpload = async () => {
  347. try {
  348. // 上传文件
  349. console.log(nodeFile)
  350. const formData = new FormData();
  351. formData.append('nodeFileName', nodeFile.value.name)
  352. formData.append('edgeFileName', edgeFile.value.name)
  353. formData.append('nodes', nodeFile.value)
  354. formData.append('edges', edgeFile.value)
  355. formData.append('type', 'csv')
  356. formData.append('usage', 'input')
  357. const response = await postFile('/uploadfile/', formData);
  358. if (response.status == 'success') {
  359. ElMessage.success('文件上传成功')
  360. nodeFile.value = null;
  361. edgeFile.value = null;
  362. preparePlan(response)
  363. } else {
  364. ElMessage.error("上传文件出错")
  365. console.log(response)
  366. }
  367. } catch (error) {
  368. console.log(error)
  369. ElMessage.error('文件验证失败: ' + error.response.data.message)
  370. nodeFile.value = null;
  371. edgeFile.value = null;
  372. }
  373. }
  374. // 上传文件和输入得到的响应结果相同,使用同一函数跳转到规划页面
  375. const preparePlan = (response) => {
  376. // 保存上传的文件信息
  377. response.data.forEach(file => {
  378. if (file.content === 'node') {
  379. useAnalyzeInfo.analyzeInfo.value.nodeFile.id = file.id
  380. useAnalyzeInfo.analyzeInfo.value.nodeFile.amount = file.ndoes
  381. useAnalyzeInfo.analyzeInfo.value.nodeFile.sNodes = file.sNodes
  382. useAnalyzeInfo.analyzeInfo.value.nodeFile.dNodes = file.dNodes
  383. useAnalyzeInfo.analyzeInfo.value.nodeFile.iNodes = file.iNodes
  384. }
  385. if (file.content === 'edge') {
  386. useAnalyzeInfo.analyzeInfo.value.edgeFile.id = file.id
  387. useAnalyzeInfo.analyzeInfo.value.edgeFile.amount = file.edges
  388. }
  389. // 获取创建的分析任务ID
  390. if (file.content === 'mission') {
  391. useAnalyzeInfo.analyzeInfo.value.mission.id = file.id
  392. useAnalyzeInfo.analyzeInfo.value.mission.name = file.name
  393. useAnalyzeInfo.analyzeInfo.value.mission.status = file.status
  394. }
  395. })
  396. // 将获取到的数据写入页面缓存,防止刷新丢失
  397. sessionStorage.setItem('analyze-info', JSON.stringify(useAnalyzeInfo.analyzeInfo.value))
  398. // 跳转到规划页面
  399. inputMethod.value = "done";
  400. console.log(useAnalyzeInfo.analyzeInfo.value)
  401. router.push(`/dashboard/analyze/plan`)
  402. updateUploadHistory()
  403. }
  404. const route = useRoute();
  405. // 监听路由变化
  406. watch(
  407. () => route.path,
  408. (newPath) => {
  409. // 路由变为plan时,修改显示内容
  410. if (newPath === '/dashboard/analyze/plan' || newPath === '/dashboard/analyze/plan/calculate') {
  411. inputMethod.value = "done"
  412. }
  413. if (newPath === '/dashboard/analyze'){
  414. inputMethod.value = "upload"
  415. }
  416. },
  417. { immediate: true }
  418. );
  419. </script>
  420. <style lang="scss" scoped>
  421. .analysis-container {
  422. padding: 20px;
  423. height: calc(100vh - 60px);
  424. overflow-y: auto;
  425. .upload-section {
  426. margin-bottom: 20px;
  427. .input-method-select {
  428. margin-bottom: 20px;
  429. }
  430. .upload-area {
  431. display: flex;
  432. flex-direction: column;
  433. gap: 15px;
  434. .file-upload {
  435. display: flex;
  436. align-items: center;
  437. gap: 10px;
  438. padding: 15px;
  439. border: 1px dashed var(--el-border-color);
  440. border-radius: 8px;
  441. .file-info {
  442. color: var(--el-text-color-secondary);
  443. font-size: 0.9em;
  444. }
  445. }
  446. .upload-button {
  447. margin-top: 15px;
  448. width: 200px;
  449. align-self: center;
  450. }
  451. .progress-bar {
  452. margin-top: 10px;
  453. }
  454. }
  455. }
  456. .instruction-section {
  457. height: 100%;
  458. h3 {
  459. margin-bottom: 15px;
  460. }
  461. .instruction-content {
  462. .file-format {
  463. margin-bottom: 25px;
  464. .format-tag {
  465. margin-bottom: 10px;
  466. }
  467. .format-example {
  468. background: var(--el-fill-color-light);
  469. padding: 10px;
  470. border-radius: 6px;
  471. pre {
  472. margin: 0;
  473. font-family: monospace;
  474. }
  475. .example-text {
  476. color: var(--el-text-color-secondary);
  477. margin: 8px 0;
  478. }
  479. }
  480. }
  481. }
  482. }
  483. }
  484. .online-input {
  485. padding: 15px;
  486. .input-section {
  487. border: 1px solid var(--el-border-color);
  488. border-radius: 8px;
  489. padding: 15px;
  490. margin-bottom: 20px;
  491. h4 {
  492. margin-bottom: 15px;
  493. color: var(--el-text-color-primary);
  494. }
  495. .input-row {
  496. margin-bottom: 12px;
  497. .node-id {
  498. padding: 8px 12px;
  499. background: var(--el-fill-color-light);
  500. border-radius: 4px;
  501. text-align: center;
  502. }
  503. }
  504. }
  505. }
  506. .el-message.error-message {
  507. white-space: pre-line;
  508. line-height: 1.6;
  509. padding: 15px 20px;
  510. }
  511. </style>