TaskCenterPanel.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. <template>
  2. <!-- 任务中心浮动面板 -->
  3. <transition name="task-panel">
  4. <div v-if="open" class="task-panel" :class="{ expanded: showDetail }">
  5. <el-card class="task-panel-card" shadow="always">
  6. <div class="task-panel-header">
  7. <div class="title">
  8. <span class="title-icon">📋</span>
  9. 任务中心
  10. </div>
  11. <el-button text :icon="Close" @click="handleClose" />
  12. </div>
  13. <el-tabs v-model="activeTab" class="task-tabs" @tab-change="handleTabChange">
  14. <el-tab-pane label="全部" name="all" />
  15. <el-tab-pane name="processing">
  16. <template #label>
  17. <span class="tab-label">
  18. <span class="tab-text">运行中</span>
  19. <span class="tab-count" v-if="statusTotals.processing > 0">{{ statusTotals.processing }}</span>
  20. </span>
  21. </template>
  22. </el-tab-pane>
  23. <el-tab-pane label="已完成" name="completed" />
  24. <el-tab-pane name="failed">
  25. <template #label>
  26. <span class="tab-label">
  27. <span class="tab-text">失败</span>
  28. <span class="tab-count error" v-if="statusTotals.failed > 0">{{ statusTotals.failed }}</span>
  29. </span>
  30. </template>
  31. </el-tab-pane>
  32. <el-tab-pane name="pending">
  33. <template #label>
  34. <span class="tab-label">
  35. <span class="tab-text">等待中</span>
  36. <span class="tab-count" v-if="statusTotals.pending > 0">{{ statusTotals.pending }}</span>
  37. </span>
  38. </template>
  39. </el-tab-pane>
  40. </el-tabs>
  41. <div class="task-body">
  42. <!-- 左侧任务列表 -->
  43. <div class="task-list" v-loading="listLoading">
  44. <div
  45. v-for="item in list"
  46. :key="item.id"
  47. class="task-card"
  48. :class="[{ active: selectedId === item.id }, statusClass(item.status)]"
  49. @click="handleSelect(item.id)"
  50. >
  51. <div class="task-title-row">
  52. <div class="task-title">{{ item.name || `任务 ${item.id?.slice(0, 8)}` }}</div>
  53. <el-tag size="small" :type="tagType(item.status)">{{ statusText(item.status) }}</el-tag>
  54. </div>
  55. <div class="task-sub" v-if="item.documentId">
  56. <span>文档: {{ item.documentId?.slice(0, 8) }}...</span>
  57. </div>
  58. <div class="task-meta">
  59. <div class="task-meta-left">
  60. <div class="task-time">{{ formatRelativeTime(item.createdAt || item.startedAt) }}</div>
  61. <template v-if="item.status === 'processing' && item.startedAt">
  62. <span class="task-meta-separator">·</span>
  63. <div class="task-duration-inline">
  64. <span class="task-duration-value">{{ formatElapsedTime(item.startedAt) }}</span>
  65. </div>
  66. </template>
  67. </div>
  68. <div class="task-meta-right">
  69. <span class="task-step" v-if="item.currentStep">{{ stepText(item.currentStep) }}</span>
  70. </div>
  71. </div>
  72. <!-- 进度条区域 -->
  73. <div class="task-progress-section" v-if="selectedId !== item.id">
  74. <template v-if="item.status === 'processing' || item.status === 'pending'">
  75. <div class="task-progress-row" v-if="item.progress != null">
  76. <el-progress :percentage="item.progress" :stroke-width="6" />
  77. <span class="task-progress-percent">{{ item.progress }}%</span>
  78. </div>
  79. </template>
  80. <template v-else-if="item.status === 'failed'">
  81. <div class="task-progress-row" v-if="item.progress != null">
  82. <el-progress :percentage="item.progress" :stroke-width="6" status="exception" />
  83. <span class="task-progress-percent task-progress-percent--error">{{ item.progress }}%</span>
  84. </div>
  85. </template>
  86. </div>
  87. <!-- 操作按钮 -->
  88. <div class="task-actions">
  89. <el-button
  90. text
  91. type="danger"
  92. size="small"
  93. :icon="Delete"
  94. @click.stop="handleDelete(item)"
  95. :disabled="item.status === 'processing'"
  96. >
  97. 删除
  98. </el-button>
  99. </div>
  100. </div>
  101. <el-empty v-if="!listLoading && (!list || list.length === 0)" description="暂无任务" />
  102. </div>
  103. <!-- 右侧任务详情 -->
  104. <div class="task-detail" :class="{ show: showDetail }" v-loading="detailLoading">
  105. <template v-if="detail">
  106. <div class="detail-header">
  107. <h3>{{ detail.name || `任务详情` }}</h3>
  108. <el-tag :type="tagType(detail.status)">{{ statusText(detail.status) }}</el-tag>
  109. </div>
  110. <div class="detail-meta">
  111. <span>开始时间:{{ formatDateTime(detail.startedAt) || '-' }}</span>
  112. <span v-if="detail.completedAt">| 完成时间:{{ formatDateTime(detail.completedAt) }}</span>
  113. </div>
  114. <div class="detail-progress">
  115. <el-progress :percentage="detail.progress ?? 0" :status="detail.status === 'failed' ? 'exception' : undefined" />
  116. <div class="progress-info">
  117. <div class="current-stage" v-if="detail.currentStep">
  118. <span class="current-stage-label">当前阶段:</span>
  119. <span class="current-stage-value">{{ stepText(detail.currentStep) }}</span>
  120. </div>
  121. </div>
  122. </div>
  123. <!-- 失败信息 -->
  124. <template v-if="detail.status === 'failed' && detail.errorMessage">
  125. <div class="detail-section">错误信息</div>
  126. <div class="task-error-info">
  127. <div class="error-remark">{{ detail.errorMessage }}</div>
  128. </div>
  129. </template>
  130. <!-- 执行阶段 -->
  131. <div class="detail-section">执行阶段</div>
  132. <div v-if="detail.stages && detail.stages.length" class="stages">
  133. <div v-for="s in detail.stages" :key="s.stageName" class="stage-row">
  134. <div class="stage-name">
  135. <span class="stage-icon" :class="s.status">
  136. <el-icon v-if="s.status === 'completed'"><CircleCheckFilled /></el-icon>
  137. <el-icon v-else-if="s.status === 'in_progress'"><Loading /></el-icon>
  138. <el-icon v-else-if="s.status === 'failed'"><CircleCloseFilled /></el-icon>
  139. <el-icon v-else><Clock /></el-icon>
  140. </span>
  141. {{ s.displayName }}
  142. </div>
  143. <div class="stage-meta">
  144. <span :class="['stage-status', s.status]">{{ stageStatusText(s.status) }}</span>
  145. <span v-if="s.resultSummary" class="stage-result">{{ s.resultSummary }}</span>
  146. </div>
  147. </div>
  148. </div>
  149. <el-empty v-else description="暂无阶段数据" :image-size="60" />
  150. </template>
  151. <template v-else>
  152. <el-empty description="请选择任务查看详情" :image-size="80" />
  153. </template>
  154. </div>
  155. </div>
  156. </el-card>
  157. </div>
  158. </transition>
  159. </template>
  160. <script setup>
  161. import { computed, watch, onMounted } from 'vue'
  162. import { Close, Delete, CircleCheckFilled, CircleCloseFilled, Loading, Clock } from '@element-plus/icons-vue'
  163. import { useTaskCenterStore } from '@/stores/taskCenter'
  164. import { ElMessage, ElMessageBox } from 'element-plus'
  165. const store = useTaskCenterStore()
  166. const open = computed(() => store.open)
  167. const list = computed(() => store.list)
  168. const listLoading = computed(() => store.listLoading)
  169. const detailLoading = computed(() => store.detailLoading)
  170. const selectedId = computed(() => store.selectedId)
  171. const detail = computed(() => store.detail)
  172. const showDetail = computed(() => !!store.selectedId)
  173. const statusTotals = computed(() => store.statusTotals || { processing: 0, failed: 0, pending: 0 })
  174. const activeTab = computed({
  175. get: () => store.activeTab,
  176. set: (v) => store.setActiveTab(v)
  177. })
  178. function handleClose() {
  179. store.close()
  180. }
  181. function handleTabChange() {
  182. store.selectedId = null
  183. store.detail = null
  184. store.fetchList()
  185. }
  186. function handleSelect(id) {
  187. if (store.selectedId === id) {
  188. store.selectedId = null
  189. store.detail = null
  190. return
  191. }
  192. store.selectTask(id)
  193. }
  194. watch(
  195. () => store.open,
  196. (val) => {
  197. if (val) {
  198. store.selectedId = null
  199. store.detail = null
  200. store.fetchRunningCount()
  201. store.fetchStatusTotals()
  202. store.fetchList()
  203. store.startListPolling()
  204. } else {
  205. store.stopListPolling()
  206. }
  207. }
  208. )
  209. onMounted(() => {
  210. store.fetchList({ pageNum: 1, pageSize: 20 })
  211. })
  212. // 工具函数
  213. function tagType(status) {
  214. if (status === 'processing') return 'primary'
  215. if (status === 'completed') return 'success'
  216. if (status === 'failed') return 'danger'
  217. return 'info'
  218. }
  219. function statusClass(status) {
  220. if (status === 'processing') return 'status-processing'
  221. if (status === 'completed') return 'status-completed'
  222. if (status === 'failed') return 'status-failed'
  223. return 'status-pending'
  224. }
  225. function statusText(status) {
  226. if (status === 'processing') return '运行中'
  227. if (status === 'completed') return '已完成'
  228. if (status === 'failed') return '失败'
  229. return '等待中'
  230. }
  231. function stageStatusText(status) {
  232. if (status === 'in_progress') return '进行中'
  233. if (status === 'completed') return '已完成'
  234. if (status === 'failed') return '失败'
  235. return '等待中'
  236. }
  237. function stepText(step) {
  238. const stepMap = {
  239. 'parsing': '文本解析',
  240. 'pdf_extraction': 'PDF提取',
  241. 'word_extraction': 'Word提取',
  242. 'excel_extraction': 'Excel提取',
  243. 'ocr': 'OCR识别',
  244. 'saving': '保存文本',
  245. 'layout_analysis': '版面分析',
  246. 'recording': '记录存储',
  247. 'completed': '已完成',
  248. 'failed': '失败'
  249. }
  250. return stepMap[step] || step
  251. }
  252. function formatRelativeTime(v) {
  253. if (!v) return ''
  254. const d = parseDateTime(v)
  255. if (!d) return ''
  256. const now = new Date()
  257. const diffMs = now.getTime() - d.getTime()
  258. const diffSec = Math.floor(diffMs / 1000)
  259. if (diffSec < 60 && diffSec >= 0) return `${diffSec}秒前`
  260. const diffMin = Math.floor(diffSec / 60)
  261. if (diffMin < 60 && diffMin >= 0) return `${diffMin}分钟前`
  262. const diffHour = Math.floor(diffMin / 60)
  263. if (diffHour < 24 && diffHour >= 0) return `${diffHour}小时前`
  264. return formatDateTime(v)
  265. }
  266. function formatElapsedTime(startTime) {
  267. const start = parseDateTime(startTime)
  268. if (!start) return '-'
  269. const now = new Date()
  270. const diffMs = Math.max(0, now.getTime() - start.getTime())
  271. return formatMs(diffMs)
  272. }
  273. function formatDateTime(v) {
  274. if (!v) return ''
  275. if (typeof v === 'string') return v
  276. return v.toLocaleString('zh-CN')
  277. }
  278. function parseDateTime(v) {
  279. if (!v) return null
  280. if (v instanceof Date) return v
  281. if (typeof v !== 'string') return null
  282. const s = v.replace(' ', 'T')
  283. const d = new Date(s)
  284. if (isNaN(d.getTime())) return null
  285. return d
  286. }
  287. function formatMs(ms) {
  288. if (ms == null) return '-'
  289. const s = Math.floor(ms / 1000)
  290. if (s < 60) return `${s}s`
  291. const m = Math.floor(s / 60)
  292. const rs = s % 60
  293. if (m < 60) return `${m}m${rs}s`
  294. const h = Math.floor(m / 60)
  295. const rm = m % 60
  296. return `${h}h${rm}m`
  297. }
  298. async function handleDelete(item) {
  299. try {
  300. await ElMessageBox.confirm(
  301. `确认删除任务"${item.name || item.id}"吗?`,
  302. '删除确认',
  303. {
  304. confirmButtonText: '确定',
  305. cancelButtonText: '取消',
  306. type: 'warning'
  307. }
  308. )
  309. await store.deleteTask(item.id)
  310. ElMessage.success('删除成功')
  311. } catch (error) {
  312. if (error !== 'cancel') {
  313. ElMessage.error(error?.message || '删除失败')
  314. }
  315. }
  316. }
  317. </script>
  318. <style scoped>
  319. .task-panel {
  320. position: fixed;
  321. right: 16px;
  322. bottom: 16px;
  323. width: 480px;
  324. max-width: calc(100vw - 32px);
  325. z-index: 4000;
  326. transition: width 0.2s ease;
  327. }
  328. .task-panel.expanded {
  329. width: 900px;
  330. }
  331. .task-panel-card {
  332. border-radius: 12px;
  333. border: 1px solid rgba(0, 0, 0, 0.06);
  334. box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
  335. }
  336. .task-panel-header {
  337. display: flex;
  338. align-items: center;
  339. justify-content: space-between;
  340. margin-bottom: 12px;
  341. }
  342. .task-panel-header .title {
  343. font-weight: 700;
  344. font-size: 16px;
  345. display: flex;
  346. align-items: center;
  347. gap: 8px;
  348. }
  349. .task-panel-header .title-icon {
  350. font-size: 20px;
  351. }
  352. .task-tabs :deep(.el-tabs__item) {
  353. font-weight: 600;
  354. }
  355. .tab-label {
  356. display: inline-flex;
  357. align-items: center;
  358. gap: 6px;
  359. }
  360. .tab-count {
  361. display: inline-flex;
  362. align-items: center;
  363. justify-content: center;
  364. min-width: 18px;
  365. height: 18px;
  366. padding: 0 6px;
  367. border-radius: 999px;
  368. font-size: 12px;
  369. color: #fff;
  370. background: var(--el-color-primary);
  371. }
  372. .tab-count.error {
  373. background: var(--el-color-danger);
  374. }
  375. .task-body {
  376. display: flex;
  377. gap: 0;
  378. height: 480px;
  379. }
  380. .task-panel.expanded .task-body {
  381. gap: 16px;
  382. }
  383. .task-list {
  384. width: 100%;
  385. overflow: auto;
  386. padding-right: 6px;
  387. transition: width 0.2s ease;
  388. }
  389. .task-panel.expanded .task-list {
  390. width: 360px;
  391. border-right: 1px solid #ebeef5;
  392. padding-right: 16px;
  393. }
  394. .task-card {
  395. padding: 12px;
  396. border: 1px solid rgba(0, 0, 0, 0.06);
  397. border-radius: 10px;
  398. margin-bottom: 10px;
  399. cursor: pointer;
  400. transition: all 0.15s ease;
  401. background: #fff;
  402. }
  403. .task-card:hover {
  404. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  405. transform: translateY(-1px);
  406. }
  407. .task-card.status-processing {
  408. background-color: rgba(64, 158, 255, 0.05);
  409. border-color: rgba(64, 158, 255, 0.2);
  410. }
  411. .task-card.status-completed {
  412. background-color: rgba(103, 194, 58, 0.05);
  413. }
  414. .task-card.status-failed {
  415. background-color: rgba(245, 108, 108, 0.05);
  416. border-color: rgba(245, 108, 108, 0.2);
  417. }
  418. .task-card.status-pending {
  419. background-color: rgba(144, 147, 153, 0.05);
  420. }
  421. .task-card.active {
  422. border-color: var(--el-color-primary);
  423. box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.15);
  424. }
  425. .task-title-row {
  426. display: flex;
  427. align-items: center;
  428. justify-content: space-between;
  429. gap: 10px;
  430. margin-bottom: 4px;
  431. }
  432. .task-title {
  433. font-weight: 600;
  434. font-size: 14px;
  435. overflow: hidden;
  436. text-overflow: ellipsis;
  437. white-space: nowrap;
  438. }
  439. .task-sub {
  440. font-size: 12px;
  441. color: #909399;
  442. margin-bottom: 8px;
  443. }
  444. .task-meta {
  445. display: flex;
  446. align-items: center;
  447. justify-content: space-between;
  448. margin-bottom: 8px;
  449. }
  450. .task-meta-left {
  451. display: flex;
  452. align-items: center;
  453. gap: 6px;
  454. font-size: 12px;
  455. color: #909399;
  456. }
  457. .task-meta-separator {
  458. color: #c0c4cc;
  459. }
  460. .task-duration-inline {
  461. display: flex;
  462. align-items: center;
  463. }
  464. .task-duration-value {
  465. font-weight: 600;
  466. color: var(--el-color-primary);
  467. }
  468. .task-meta-right {
  469. display: flex;
  470. align-items: center;
  471. }
  472. .task-step {
  473. font-size: 12px;
  474. color: var(--el-color-primary);
  475. font-weight: 500;
  476. }
  477. .task-progress-section {
  478. margin-top: 8px;
  479. }
  480. .task-progress-row {
  481. display: flex;
  482. align-items: center;
  483. gap: 8px;
  484. }
  485. .task-progress-row :deep(.el-progress) {
  486. flex: 1;
  487. }
  488. .task-progress-row :deep(.el-progress__text) {
  489. display: none;
  490. }
  491. .task-progress-percent {
  492. font-size: 12px;
  493. color: #606266;
  494. font-weight: 600;
  495. min-width: 36px;
  496. text-align: right;
  497. }
  498. .task-progress-percent--error {
  499. color: var(--el-color-danger);
  500. }
  501. .task-actions {
  502. display: flex;
  503. align-items: center;
  504. justify-content: flex-end;
  505. margin-top: 8px;
  506. padding-top: 8px;
  507. border-top: 1px solid rgba(0, 0, 0, 0.06);
  508. }
  509. .task-detail {
  510. overflow: auto;
  511. width: 0;
  512. opacity: 0;
  513. pointer-events: none;
  514. transition: all 0.2s ease;
  515. }
  516. .task-detail.show {
  517. width: calc(100% - 376px);
  518. opacity: 1;
  519. pointer-events: auto;
  520. padding-left: 16px;
  521. }
  522. .detail-header {
  523. display: flex;
  524. align-items: center;
  525. justify-content: space-between;
  526. margin-bottom: 12px;
  527. }
  528. .detail-header h3 {
  529. margin: 0;
  530. font-size: 16px;
  531. }
  532. .detail-meta {
  533. font-size: 12px;
  534. color: #909399;
  535. margin-bottom: 12px;
  536. }
  537. .detail-progress {
  538. padding: 12px;
  539. border-radius: 8px;
  540. border: 1px solid rgba(0, 0, 0, 0.06);
  541. background: #fafafa;
  542. margin-bottom: 16px;
  543. }
  544. .progress-info {
  545. margin-top: 8px;
  546. }
  547. .current-stage {
  548. font-size: 13px;
  549. display: flex;
  550. align-items: center;
  551. gap: 4px;
  552. }
  553. .current-stage-label {
  554. color: #909399;
  555. }
  556. .current-stage-value {
  557. color: var(--el-color-primary);
  558. font-weight: 600;
  559. }
  560. .detail-section {
  561. margin-top: 16px;
  562. font-weight: 600;
  563. margin-bottom: 8px;
  564. font-size: 14px;
  565. }
  566. .task-error-info {
  567. padding: 12px;
  568. border-radius: 8px;
  569. border: 1px solid rgba(245, 108, 108, 0.3);
  570. background: rgba(245, 108, 108, 0.05);
  571. }
  572. .error-remark {
  573. color: var(--el-color-danger);
  574. font-size: 13px;
  575. line-height: 1.6;
  576. word-break: break-word;
  577. }
  578. .stages {
  579. border: 1px solid #ebeef5;
  580. border-radius: 8px;
  581. overflow: hidden;
  582. }
  583. .stage-row {
  584. padding: 10px 12px;
  585. border-bottom: 1px solid #ebeef5;
  586. display: flex;
  587. align-items: center;
  588. justify-content: space-between;
  589. }
  590. .stage-row:last-child {
  591. border-bottom: none;
  592. }
  593. .stage-name {
  594. display: flex;
  595. align-items: center;
  596. gap: 8px;
  597. font-size: 13px;
  598. }
  599. .stage-icon {
  600. display: flex;
  601. align-items: center;
  602. justify-content: center;
  603. width: 20px;
  604. height: 20px;
  605. }
  606. .stage-icon.completed {
  607. color: var(--el-color-success);
  608. }
  609. .stage-icon.in_progress {
  610. color: var(--el-color-primary);
  611. }
  612. .stage-icon.failed {
  613. color: var(--el-color-danger);
  614. }
  615. .stage-icon.pending {
  616. color: #c0c4cc;
  617. }
  618. .stage-meta {
  619. display: flex;
  620. align-items: center;
  621. gap: 8px;
  622. font-size: 12px;
  623. }
  624. .stage-status {
  625. padding: 2px 8px;
  626. border-radius: 4px;
  627. background: #f5f7fa;
  628. }
  629. .stage-status.completed {
  630. color: var(--el-color-success);
  631. background: rgba(103, 194, 58, 0.1);
  632. }
  633. .stage-status.in_progress {
  634. color: var(--el-color-primary);
  635. background: rgba(64, 158, 255, 0.1);
  636. }
  637. .stage-status.failed {
  638. color: var(--el-color-danger);
  639. background: rgba(245, 108, 108, 0.1);
  640. }
  641. .stage-result {
  642. color: #909399;
  643. }
  644. /* 动画 */
  645. .task-panel-enter-active,
  646. .task-panel-leave-active {
  647. transition: all 0.2s ease;
  648. }
  649. .task-panel-enter-from,
  650. .task-panel-leave-to {
  651. opacity: 0;
  652. transform: translateY(10px);
  653. }
  654. @media (max-width: 768px) {
  655. .task-panel {
  656. width: calc(100vw - 32px);
  657. left: 16px;
  658. right: 16px;
  659. }
  660. .task-body {
  661. flex-direction: column;
  662. height: 60vh;
  663. }
  664. .task-panel.expanded .task-list {
  665. width: 100%;
  666. border-right: none;
  667. border-bottom: 1px solid #ebeef5;
  668. padding-bottom: 12px;
  669. max-height: 200px;
  670. }
  671. .task-detail.show {
  672. width: 100%;
  673. padding-left: 0;
  674. padding-top: 12px;
  675. }
  676. }
  677. </style>