analyze.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 label="upload">文件上传</el-radio-button>
  10. <el-radio-button label="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. <div v-else class="upload-area">
  50. <router-view></router-view>
  51. </div>
  52. </el-card>
  53. <!-- 历史文件列表 -->
  54. <el-card class="history-section">
  55. <h3>历史上传记录</h3>
  56. <el-table :data="fileHistory" style="width: 100%">
  57. <el-table-column label="" width="40">
  58. <template #default="scope">
  59. <el-button v-if="scope.row.content == 'node'" type="primary"
  60. style="width: 20px; height:20px; padding: 2px;">N</el-button>
  61. <el-button v-if="scope.row.content == 'edge'" type="warning"
  62. style="width: 20px; height:20px; padding: 2px;">E</el-button>
  63. </template>
  64. </el-table-column>
  65. <el-table-column prop="fileName" label="文件名" width="180" />
  66. <el-table-column prop="uploadTime" label="上传时间" width="180" />
  67. <el-table-column prop="fileSize" label="文件大小">
  68. </el-table-column>
  69. <el-table-column label="分析记录">
  70. <template #default="{ row }">
  71. <el-dropdown>
  72. <span class="analysis-records">
  73. 查看记录<el-icon><arrow-down /></el-icon>
  74. </span>
  75. <template #dropdown>
  76. <el-dropdown-menu>
  77. <el-dropdown-item v-for="(record, index) in row.records" :key="index">
  78. {{ record.time }} - {{ record.type }}
  79. </el-dropdown-item>
  80. </el-dropdown-menu>
  81. </template>
  82. </el-dropdown>
  83. </template>
  84. </el-table-column>
  85. <el-table-column label="操作" width="120">
  86. <template #default="{ row }">
  87. <el-button type="danger" size="small" @click="handleDeleteFile(row)" plain>
  88. 删除
  89. </el-button>
  90. </template>
  91. </el-table-column>
  92. </el-table>
  93. </el-card>
  94. </el-col>
  95. <!-- 右侧说明 -->
  96. <el-col :span="8">
  97. <el-card class="instruction-section">
  98. <h3>文件格式说明</h3>
  99. <div class="instruction-content">
  100. <div class="file-format">
  101. <el-tag type="success" class="format-tag">节点文件格式</el-tag>
  102. <el-divider />
  103. <div class="format-example">
  104. <p>节点编号 节点类型 节点描述 节点名称</p>
  105. <p class="example-text">示例:</p>
  106. <pre>001 User "普通用户" 张三
  107. 002 Product "电子产品" 手机
  108. 003 Category "商品类目" 数码</pre>
  109. </div>
  110. </div>
  111. <div class="file-format">
  112. <el-tag type="warning" class="format-tag">边文件格式</el-tag>
  113. <el-divider />
  114. <div class="format-example">
  115. <p>起始节点 终止节点</p>
  116. <p class="example-text">示例:</p>
  117. <pre>001 002
  118. 002 003
  119. 003 001</pre>
  120. </div>
  121. </div>
  122. </div>
  123. </el-card>
  124. </el-col>
  125. </el-row>
  126. </div>
  127. </template>
  128. <script setup>
  129. import { ref, computed, onMounted, inject } from 'vue'
  130. import { useRouter } from 'vue-router'
  131. import { ElMessage, ElMessageBox } from 'element-plus'
  132. import { Upload, ArrowDown } from '@element-plus/icons-vue'
  133. import { getData, postData, deleteData, postFile } from '@/api/axios.js'
  134. // Store数据
  135. const useAnalyzeInfo = inject('analyzeInfo')
  136. // 响应式数据
  137. const inputMethod = ref('upload')
  138. const nodeFile = ref(null)
  139. const edgeFile = ref(null)
  140. const uploadProgress = ref(0)
  141. const uploadStatus = ref('')
  142. const fileHistory = ref([])
  143. const router = useRouter()
  144. // 计算属性
  145. const canUpload = computed(() => {
  146. return nodeFile.value && edgeFile.value
  147. })
  148. // 方法
  149. const handleNodeFileChange = (file, fileList) => {
  150. if (fileList.length > 1) {
  151. fileList.splice(0, 1);
  152. }
  153. nodeFile.value = fileList[0].raw
  154. }
  155. const handleEdgeFileChange = (file, fileList) => {
  156. if (fileList.length > 1) {
  157. fileList.splice(0, 1);
  158. }
  159. edgeFile.value = fileList[0].raw
  160. }
  161. const formatSize = (bytes) => {
  162. if (bytes === 0) return '0 B'
  163. const k = 1024
  164. const sizes = ['B', 'KB', 'MB', 'GB']
  165. const i = Math.floor(Math.log(bytes) / Math.log(k))
  166. return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
  167. }
  168. const handleUpload = async () => {
  169. try {
  170. uploadStatus.value = ''
  171. uploadProgress.value = 0
  172. // 上传文件
  173. console.log(nodeFile)
  174. const formData = new FormData();
  175. formData.append('nodeFileName', nodeFile.value.name)
  176. formData.append('edgeFileName', edgeFile.value.name)
  177. formData.append('nodes', nodeFile.value)
  178. formData.append('edges', edgeFile.value)
  179. formData.append('type', 'csv')
  180. formData.append('usage', 'input')
  181. const response = await postFile('/uploadfile/', formData);
  182. if (response.status == 'success') {
  183. ElMessage.success('文件上传成功')
  184. uploadStatus.value = 'success'
  185. nodeFile.value = null;
  186. edgeFile.value = null;
  187. // 保存上传的文件信息
  188. response.data.forEach(file => {
  189. if (file.content === 'node') {
  190. useAnalyzeInfo.analyzeInfo.value.nodeFile.id = file.id
  191. useAnalyzeInfo.analyzeInfo.value.nodeFile.name = file.name
  192. useAnalyzeInfo.analyzeInfo.value.nodeFile.amount = file.ndoes
  193. useAnalyzeInfo.analyzeInfo.value.nodeFile.sNodes = file.sNodes
  194. useAnalyzeInfo.analyzeInfo.value.nodeFile.dNodes = file.dNodes
  195. useAnalyzeInfo.analyzeInfo.value.nodeFile.iNodes = file.iNodes
  196. }
  197. if (file.content === 'edge') {
  198. useAnalyzeInfo.analyzeInfo.value.edgeFile.id = file.id
  199. useAnalyzeInfo.analyzeInfo.value.edgeFile.name = file.name
  200. useAnalyzeInfo.analyzeInfo.value.edgeFile.amount = file.edges
  201. }
  202. // 获取创建的分析任务ID
  203. if (file.content === 'mission') {
  204. useAnalyzeInfo.analyzeInfo.value.mission.id = file.id
  205. useAnalyzeInfo.analyzeInfo.value.mission.name = file.name
  206. }
  207. })
  208. // 跳转到规划页面
  209. inputMethod.value = "done";
  210. console.log(useAnalyzeInfo.analyzeInfo.value)
  211. router.push(`/dashboard/analyze/plan`)
  212. updateUploadHistory()
  213. } else {
  214. ElMessage.error("上传文件出错")
  215. console.log(response)
  216. }
  217. } catch (error) {
  218. console.log(error)
  219. ElMessage.error('文件验证失败: ' + error.response.data.message)
  220. uploadStatus.value = 'exception'
  221. nodeFile.value = null;
  222. edgeFile.value = null;
  223. }
  224. }
  225. const handleDeleteFile = (file) => {
  226. ElMessageBox.confirm(
  227. `确定要删除文件 ${file.fileName} 吗?此操作不可恢复。`,
  228. '警告',
  229. {
  230. confirmButtonText: '确定',
  231. cancelButtonText: '取消',
  232. type: 'warning'
  233. }
  234. ).then(() => {
  235. deleteData('/uploadfile/', { id: file.id }).then(response => {
  236. if (response.status == 'success') {
  237. ElMessage.success('文件已删除')
  238. updateUploadHistory()
  239. } else {
  240. ElMessage.error('文件删除失败' + response.message)
  241. }
  242. })
  243. .catch(error => {
  244. ElMessage.error('文件删除失败');
  245. console.log(error)
  246. })
  247. }).catch(() => { })
  248. }
  249. const updateUploadHistory = () => {
  250. getData('/uploadfile/')
  251. .then(response => {
  252. fileHistory.value = []
  253. response.data.reverse().forEach(item => {
  254. fileHistory.value.push({
  255. id: item.id,
  256. content: item.content,
  257. fileName: item.name,
  258. uploadTime: item.uploadTime.split('.')[0].replace('T', ' '),
  259. fileSize: item.size,
  260. records: []
  261. })
  262. })
  263. // history.forEach(element => {
  264. // console.log(element.uploadTime.replace('T', ' ').aplit('.')[0])
  265. // });
  266. console.log(fileHistory.value)
  267. })
  268. .catch(error => {
  269. ElMessage.error('获取上传历史失败')
  270. console.log(error)
  271. })
  272. }
  273. onMounted(() => {
  274. updateUploadHistory();
  275. })
  276. </script>
  277. <style lang="scss" scoped>
  278. .analysis-container {
  279. padding: 20px;
  280. height: calc(100vh - 60px);
  281. overflow-y: auto;
  282. .upload-section {
  283. margin-bottom: 20px;
  284. .input-method-select {
  285. margin-bottom: 20px;
  286. }
  287. .upload-area {
  288. display: flex;
  289. flex-direction: column;
  290. gap: 15px;
  291. .file-upload {
  292. display: flex;
  293. align-items: center;
  294. gap: 10px;
  295. padding: 15px;
  296. border: 1px dashed var(--el-border-color);
  297. border-radius: 8px;
  298. .file-info {
  299. color: var(--el-text-color-secondary);
  300. font-size: 0.9em;
  301. }
  302. }
  303. .upload-button {
  304. margin-top: 15px;
  305. width: 200px;
  306. align-self: center;
  307. }
  308. .progress-bar {
  309. margin-top: 10px;
  310. }
  311. }
  312. }
  313. .history-section {
  314. h3 {
  315. margin-bottom: 15px;
  316. color: var(--el-text-color-primary);
  317. }
  318. .analysis-records {
  319. cursor: pointer;
  320. color: var(--el-color-primary);
  321. display: flex;
  322. align-items: center;
  323. gap: 5px;
  324. }
  325. }
  326. .instruction-section {
  327. height: 100%;
  328. h3 {
  329. margin-bottom: 15px;
  330. }
  331. .instruction-content {
  332. .file-format {
  333. margin-bottom: 25px;
  334. .format-tag {
  335. margin-bottom: 10px;
  336. }
  337. .format-example {
  338. background: var(--el-fill-color-light);
  339. padding: 10px;
  340. border-radius: 6px;
  341. pre {
  342. margin: 0;
  343. font-family: monospace;
  344. }
  345. .example-text {
  346. color: var(--el-text-color-secondary);
  347. margin: 8px 0;
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }
  354. </style>