analyze.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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="online">在线输入</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. <el-progress v-if="uploadProgress > 0" :percentage="uploadProgress" :status="uploadStatus"
  47. class="progress-bar" />
  48. </div>
  49. <!-- 在线输入节点与边 -->
  50. <div v-else class="upload-area">
  51. <div class="online-input">
  52. <el-row :gutter="20">
  53. <!-- 节点输入列 -->
  54. <el-col :span="12">
  55. <div class="input-section">
  56. <h4>输入节点</h4>
  57. <div v-for="(node, index) in nodes" :key="index" class="input-row">
  58. <el-row :gutter="10" align="middle">
  59. <el-col :span="4">
  60. <div class="node-id">ID:{{ node.id }}</div>
  61. </el-col>
  62. <el-col :span="6">
  63. <el-select v-model="node.type" placeholder="选择类型" @change="handleNodeTypeChange(index)">
  64. <el-option label="S" value="S" />
  65. <el-option label="D" value="D" />
  66. <el-option label="I" value="I" />
  67. </el-select>
  68. </el-col>
  69. <el-col :span="10">
  70. <el-input v-model="node.name" placeholder="输入节点名称" />
  71. </el-col>
  72. <el-col :span="4">
  73. <el-button v-if="index < nodes.length - 1" @click="deleteNode(index)" type="danger" plain
  74. size="small">
  75. 删除
  76. </el-button>
  77. </el-col>
  78. </el-row>
  79. </div>
  80. </div>
  81. </el-col>
  82. <!-- 边输入列 -->
  83. <el-col :span="12">
  84. <div class="input-section">
  85. <h4>输入边</h4>
  86. <div v-for="(edge, index) in edges" :key="index" class="input-row">
  87. <el-row :gutter="10" align="middle">
  88. <el-col :span="4">
  89. <div class="node-id">ID:{{ index + 1 }}</div>
  90. </el-col>
  91. <!-- 起始节点 -->
  92. <el-col :span="8">
  93. <el-select v-model="edge.from" placeholder="起始节点" :disabled="nodeOptions.length === 0"
  94. @change="handleEdgeChange(index)">
  95. <el-option v-for="node in nodeOptions" :key="node.id"
  96. :label="`ID:${node.id}-${node.name || '未命名'}`" :value="node.id" />
  97. </el-select>
  98. </el-col>
  99. <!-- 终止节点 -->
  100. <el-col :span="8">
  101. <el-select v-model="edge.to" placeholder="终止节点" :disabled="nodeOptions.length === 0"
  102. @change="handleEdgeChange(index)">
  103. <el-option v-for="node in nodeOptions" :key="node.id"
  104. :label="`ID:${node.id}-${node.name || '未命名'}`" :value="node.id" />
  105. </el-select>
  106. </el-col>
  107. <!-- 删除按钮 -->
  108. <el-col :span="4">
  109. <el-button v-if="index < edges.length - 1" @click="deleteEdge(index)" type="danger" plain
  110. size="small">
  111. 删除
  112. </el-button>
  113. </el-col>
  114. </el-row>
  115. </div>
  116. </div>
  117. </el-col>
  118. </el-row>
  119. </div>
  120. <el-button type="primary" @click="handleValidation" class="validate-button">
  121. 开始输入验证
  122. </el-button>
  123. </div>
  124. </el-card>
  125. <!-- 历史文件列表 -->
  126. <el-card class="history-section">
  127. <h3>历史上传记录</h3>
  128. <el-table :data="fileHistory" style="width: 100%">
  129. <el-table-column label="" width="40">
  130. <template #default="scope">
  131. <el-button v-if="scope.row.content == 'node'" type="primary"
  132. style="width: 20px; height:20px; padding: 2px;">N</el-button>
  133. <el-button v-if="scope.row.content == 'edge'" type="warning"
  134. style="width: 20px; height:20px; padding: 2px;">E</el-button>
  135. </template>
  136. </el-table-column>
  137. <el-table-column prop="fileName" label="文件名" width="180" />
  138. <el-table-column prop="uploadTime" label="上传时间" width="180" />
  139. <el-table-column prop="fileSize" label="文件大小">
  140. </el-table-column>
  141. <el-table-column label="分析记录">
  142. <template #default="{ row }">
  143. <el-dropdown>
  144. <span class="analysis-records">
  145. 查看记录<el-icon><arrow-down /></el-icon>
  146. </span>
  147. <template #dropdown>
  148. <el-dropdown-menu>
  149. <el-dropdown-item v-for="(record, index) in row.records" :key="index">
  150. {{ record.time }} - {{ record.type }}
  151. </el-dropdown-item>
  152. </el-dropdown-menu>
  153. </template>
  154. </el-dropdown>
  155. </template>
  156. </el-table-column>
  157. <el-table-column label="操作" width="120">
  158. <template #default="{ row }">
  159. <el-button type="danger" size="small" @click="handleDeleteFile(row)" plain>
  160. 删除
  161. </el-button>
  162. </template>
  163. </el-table-column>
  164. </el-table>
  165. </el-card>
  166. </el-col>
  167. <!-- 右侧说明 -->
  168. <el-col :span="8">
  169. <el-card class="instruction-section">
  170. <h3>文件格式说明</h3>
  171. <div class="instruction-content">
  172. <div class="file-format">
  173. <el-tag type="success" class="format-tag">节点文件格式</el-tag>
  174. <el-divider />
  175. <div class="format-example">
  176. <p>节点编号 节点类型 节点描述 节点名称</p>
  177. <p class="example-text">示例:</p>
  178. <pre>001 User "普通用户" 张三
  179. 002 Product "电子产品" 手机
  180. 003 Category "商品类目" 数码</pre>
  181. </div>
  182. </div>
  183. <div class="file-format">
  184. <el-tag type="warning" class="format-tag">边文件格式</el-tag>
  185. <el-divider />
  186. <div class="format-example">
  187. <p>起始节点 终止节点</p>
  188. <p class="example-text">示例:</p>
  189. <pre>001 002
  190. 002 003
  191. 003 001</pre>
  192. </div>
  193. </div>
  194. </div>
  195. </el-card>
  196. </el-col>
  197. </el-row>
  198. </div>
  199. </template>
  200. <script setup>
  201. import { ref, computed, onMounted, inject } from 'vue'
  202. import { useRouter } from 'vue-router'
  203. import { ElMessage, ElMessageBox } from 'element-plus'
  204. import { Upload, ArrowDown, Plus } from '@element-plus/icons-vue'
  205. import { getData, postData, deleteData, postFile } from '@/api/axios.js'
  206. // Store数据
  207. const useAnalyzeInfo = inject('analyzeInfo')
  208. // 响应式数据
  209. const inputMethod = ref('upload')
  210. // 上传的节点和边文件
  211. const nodeFile = ref(null)
  212. const edgeFile = ref(null)
  213. // 在线输入的节点和边
  214. const nodes = ref([{ id: 1, type: '', name: '' }])
  215. const edges = ref([{ from: '', to: '' }])
  216. const uploadProgress = ref(0)
  217. const uploadStatus = ref('')
  218. const fileHistory = ref([])
  219. const router = useRouter()
  220. // 计算属性
  221. // 是否允许上传文件
  222. const canUpload = computed(() => {
  223. return nodeFile.value && edgeFile.value
  224. })
  225. // 在线输入边时节点的可选项
  226. const nodeOptions = computed(() => {
  227. return nodes.value
  228. .filter(node => node.type !== '')
  229. .map(node => ({
  230. id: node.id,
  231. name: node.name || '未命名',
  232. type: node.type
  233. }))
  234. })
  235. // 方法
  236. // 在线输入自动添加节点
  237. const handleNodeTypeChange = (index) => {
  238. // 当最后一个节点选择类型后自动添加新行
  239. if (index === nodes.value.length - 1) {
  240. nodes.value.push({ id: nodes.value.length + 1, type: '', name: '' })
  241. }
  242. }
  243. // 删除节点
  244. const deleteNode = (index) => {
  245. // 保存原始ID列表
  246. const originalIds = nodes.value.map(n => n.id)
  247. const deletedId = originalIds[index]
  248. // 删除关联边
  249. edges.value = edges.value.filter(edge =>
  250. edge.from !== deletedId && edge.to !== deletedId
  251. )
  252. // 删除节点并创建新数组
  253. const newNodes = [...nodes.value]
  254. newNodes.splice(index, 1)
  255. // 创建ID映射表(旧ID -> 新ID)
  256. const idMap = new Map()
  257. newNodes.forEach((node, i) => {
  258. const newId = i + 1
  259. idMap.set(node.id, newId) // 记录原始ID到新ID的映射
  260. node.id = newId // 更新节点ID
  261. })
  262. // 更新节点数组
  263. nodes.value = newNodes
  264. // 更新边数据中的ID引用
  265. edges.value = edges.value.map(edge => ({
  266. from: idMap.get(edge.from),
  267. to: idMap.get(edge.to)
  268. }))
  269. // 确保至少保留一个空行
  270. if (nodes.value.length === 0) {
  271. nodes.value.push({ id: 1, type: '', name: '' })
  272. }
  273. }
  274. // 获取有效节点ID列表
  275. const validNodeIds = computed(() => {
  276. return nodes.value
  277. .filter(node => node.type !== '')
  278. .map(node => node.id)
  279. })
  280. // 在线输入自动添加边
  281. const handleEdgeChange = (index) => {
  282. const currentEdge = edges.value[index]
  283. // 当最后一个边填写完整后自动添加新行
  284. if (index === edges.value.length - 1 && currentEdge.from && currentEdge.to) {
  285. edges.value.push({ from: '', to: '' })
  286. }
  287. }
  288. // 删除边
  289. const deleteEdge = (index) => {
  290. edges.value.splice(index, 1)
  291. }
  292. // 验证在线输入
  293. const handleValidation = async () => {
  294. const errors = []
  295. const edgeSet = new Set()
  296. // 检查边数据
  297. // 因为始终有空行,所有没有输入的时候长度为1
  298. if(edges.value.length == 1){
  299. errors.push(`没有输入边`)
  300. }
  301. if(nodes.value.length == 1){
  302. errors.push(`没有输入节点`)
  303. }
  304. edges.value.slice(0, -1).forEach((edge, index) => {
  305. // 检查节点是否存在
  306. if (!validNodeIds.value.includes(edge.from)) {
  307. errors.push(`边 ${index + 1}: 起始节点 ${edge.from} 不存在`)
  308. }
  309. if (!validNodeIds.value.includes(edge.to)) {
  310. errors.push(`边 ${index + 1}: 终止节点 ${edge.to} 不存在`)
  311. }
  312. // 自环边检查
  313. if (edge.from === edge.to) {
  314. errors.push(`边 ${index + 1}: 不允许连接自身(自环边)`)
  315. }
  316. // 归一化两个方向的边
  317. const [min, max] = [edge.from, edge.to].sort()
  318. // 默认检查无向边
  319. const checkDirection = false
  320. const edgeKey = checkDirection
  321. ? `${edge.from},${edge.to}`
  322. : `${min},${max}`
  323. // 检查重复边
  324. if (edgeSet.has(edgeKey)) {
  325. errors.push(`发现重复边: ${edge.from} → ${edge.to}`)
  326. } else {
  327. edgeSet.add(edgeKey)
  328. }
  329. })
  330. // 处理验证结果
  331. if (errors.length > 0) {
  332. ElMessage.error({
  333. message: `发现 ${errors.length} 个错误:<br>${errors.join('<br>')}`,
  334. duration: 5000,
  335. dangerouslyUseHTMLString: true, // 启用 HTML 解析
  336. customClass: 'error-message' // 可选:添加自定义样式类
  337. })
  338. return
  339. }
  340. // 构造上传数据
  341. const uploadData = {
  342. nodes: nodes.value
  343. .filter(node => node.type !== '')
  344. .map(node => ({
  345. id: node.id,
  346. type: node.type,
  347. name: node.name
  348. })),
  349. edges: edges.value
  350. .filter(edge => edge.from && edge.to)
  351. .map(edge => ({
  352. source: edge.from,
  353. target: edge.to
  354. }))
  355. }
  356. // 执行上传
  357. try {
  358. response = await postData('/inputFile/', uploadData)
  359. ElMessage.success('输入数据验证通过,上传成功')
  360. } catch (error) {
  361. ElMessage.error('上传失败: ' + error.message)
  362. }
  363. }
  364. // 上传文件处理
  365. const handleNodeFileChange = (file, fileList) => {
  366. if (fileList.length > 1) {
  367. fileList.splice(0, 1);
  368. }
  369. nodeFile.value = fileList[0].raw
  370. }
  371. const handleEdgeFileChange = (file, fileList) => {
  372. if (fileList.length > 1) {
  373. fileList.splice(0, 1);
  374. }
  375. edgeFile.value = fileList[0].raw
  376. }
  377. const formatSize = (bytes) => {
  378. if (bytes === 0) return '0 B'
  379. const k = 1024
  380. const sizes = ['B', 'KB', 'MB', 'GB']
  381. const i = Math.floor(Math.log(bytes) / Math.log(k))
  382. return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
  383. }
  384. const handleUpload = async () => {
  385. try {
  386. uploadStatus.value = ''
  387. uploadProgress.value = 0
  388. // 上传文件
  389. console.log(nodeFile)
  390. const formData = new FormData();
  391. formData.append('nodeFileName', nodeFile.value.name)
  392. formData.append('edgeFileName', edgeFile.value.name)
  393. formData.append('nodes', nodeFile.value)
  394. formData.append('edges', edgeFile.value)
  395. formData.append('type', 'csv')
  396. formData.append('usage', 'input')
  397. const response = await postFile('/uploadfile/', formData);
  398. if (response.status == 'success') {
  399. ElMessage.success('文件上传成功')
  400. uploadStatus.value = 'success'
  401. nodeFile.value = null;
  402. edgeFile.value = null;
  403. // 保存上传的文件信息
  404. response.data.forEach(file => {
  405. if (file.content === 'node') {
  406. useAnalyzeInfo.analyzeInfo.value.nodeFile.id = file.id
  407. useAnalyzeInfo.analyzeInfo.value.nodeFile.name = file.name
  408. useAnalyzeInfo.analyzeInfo.value.nodeFile.amount = file.ndoes
  409. useAnalyzeInfo.analyzeInfo.value.nodeFile.sNodes = file.sNodes
  410. useAnalyzeInfo.analyzeInfo.value.nodeFile.dNodes = file.dNodes
  411. useAnalyzeInfo.analyzeInfo.value.nodeFile.iNodes = file.iNodes
  412. }
  413. if (file.content === 'edge') {
  414. useAnalyzeInfo.analyzeInfo.value.edgeFile.id = file.id
  415. useAnalyzeInfo.analyzeInfo.value.edgeFile.name = file.name
  416. useAnalyzeInfo.analyzeInfo.value.edgeFile.amount = file.edges
  417. }
  418. // 获取创建的分析任务ID
  419. if (file.content === 'mission') {
  420. useAnalyzeInfo.analyzeInfo.value.mission.id = file.id
  421. useAnalyzeInfo.analyzeInfo.value.mission.name = file.name
  422. }
  423. })
  424. // 跳转到规划页面
  425. inputMethod.value = "done";
  426. console.log(useAnalyzeInfo.analyzeInfo.value)
  427. router.push(`/dashboard/analyze/plan`)
  428. updateUploadHistory()
  429. } else {
  430. ElMessage.error("上传文件出错")
  431. console.log(response)
  432. }
  433. } catch (error) {
  434. console.log(error)
  435. ElMessage.error('文件验证失败: ' + error.response.data.message)
  436. uploadStatus.value = 'exception'
  437. nodeFile.value = null;
  438. edgeFile.value = null;
  439. }
  440. }
  441. const handleDeleteFile = (file) => {
  442. ElMessageBox.confirm(
  443. `确定要删除文件 ${file.fileName} 吗?此操作不可恢复。`,
  444. '警告',
  445. {
  446. confirmButtonText: '确定',
  447. cancelButtonText: '取消',
  448. type: 'warning'
  449. }
  450. ).then(() => {
  451. deleteData('/uploadfile/', { id: file.id }).then(response => {
  452. if (response.status == 'success') {
  453. ElMessage.success('文件已删除')
  454. updateUploadHistory()
  455. } else {
  456. ElMessage.error('文件删除失败' + response.message)
  457. }
  458. })
  459. .catch(error => {
  460. ElMessage.error('文件删除失败');
  461. console.log(error)
  462. })
  463. }).catch(() => { })
  464. }
  465. const updateUploadHistory = () => {
  466. getData('/uploadfile/')
  467. .then(response => {
  468. fileHistory.value = []
  469. response.data.reverse().forEach(item => {
  470. fileHistory.value.push({
  471. id: item.id,
  472. content: item.content,
  473. fileName: item.name,
  474. uploadTime: item.uploadTime.split('.')[0].replace('T', ' '),
  475. fileSize: item.size,
  476. records: []
  477. })
  478. })
  479. // history.forEach(element => {
  480. // console.log(element.uploadTime.replace('T', ' ').aplit('.')[0])
  481. // });
  482. console.log(fileHistory.value)
  483. })
  484. .catch(error => {
  485. ElMessage.error('获取上传历史失败')
  486. console.log(error)
  487. })
  488. }
  489. onMounted(() => {
  490. updateUploadHistory();
  491. })
  492. </script>
  493. <style lang="scss" scoped>
  494. .analysis-container {
  495. padding: 20px;
  496. height: calc(100vh - 60px);
  497. overflow-y: auto;
  498. .upload-section {
  499. margin-bottom: 20px;
  500. .input-method-select {
  501. margin-bottom: 20px;
  502. }
  503. .upload-area {
  504. display: flex;
  505. flex-direction: column;
  506. gap: 15px;
  507. .file-upload {
  508. display: flex;
  509. align-items: center;
  510. gap: 10px;
  511. padding: 15px;
  512. border: 1px dashed var(--el-border-color);
  513. border-radius: 8px;
  514. .file-info {
  515. color: var(--el-text-color-secondary);
  516. font-size: 0.9em;
  517. }
  518. }
  519. .upload-button {
  520. margin-top: 15px;
  521. width: 200px;
  522. align-self: center;
  523. }
  524. .progress-bar {
  525. margin-top: 10px;
  526. }
  527. }
  528. }
  529. .history-section {
  530. h3 {
  531. margin-bottom: 15px;
  532. color: var(--el-text-color-primary);
  533. }
  534. .analysis-records {
  535. cursor: pointer;
  536. color: var(--el-color-primary);
  537. display: flex;
  538. align-items: center;
  539. gap: 5px;
  540. }
  541. }
  542. .instruction-section {
  543. height: 100%;
  544. h3 {
  545. margin-bottom: 15px;
  546. }
  547. .instruction-content {
  548. .file-format {
  549. margin-bottom: 25px;
  550. .format-tag {
  551. margin-bottom: 10px;
  552. }
  553. .format-example {
  554. background: var(--el-fill-color-light);
  555. padding: 10px;
  556. border-radius: 6px;
  557. pre {
  558. margin: 0;
  559. font-family: monospace;
  560. }
  561. .example-text {
  562. color: var(--el-text-color-secondary);
  563. margin: 8px 0;
  564. }
  565. }
  566. }
  567. }
  568. }
  569. }
  570. .online-input {
  571. padding: 15px;
  572. .input-section {
  573. border: 1px solid var(--el-border-color);
  574. border-radius: 8px;
  575. padding: 15px;
  576. margin-bottom: 20px;
  577. h4 {
  578. margin-bottom: 15px;
  579. color: var(--el-text-color-primary);
  580. }
  581. .input-row {
  582. margin-bottom: 12px;
  583. .node-id {
  584. padding: 8px 12px;
  585. background: var(--el-fill-color-light);
  586. border-radius: 4px;
  587. text-align: center;
  588. }
  589. }
  590. }
  591. }
  592. .el-message.error-message {
  593. white-space: pre-line;
  594. line-height: 1.6;
  595. padding: 15px 20px;
  596. }
  597. </style>