TaskProgressDialog.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <script setup lang="ts">
  2. import { computed } from 'vue';
  3. import { ElDialog, ElProgress, ElTag, ElButton, ElEmpty, ElScrollbar } from 'element-plus';
  4. import {
  5. Loading,
  6. Check,
  7. Close,
  8. Delete,
  9. ChatDotRound,
  10. VideoCamera,
  11. User,
  12. Upload,
  13. ChatLineSquare,
  14. } from '@element-plus/icons-vue';
  15. import { useTaskQueueStore } from '@/stores/taskQueue';
  16. import { PLATFORMS } from '@media-manager/shared';
  17. import type { Task, TaskType, TaskStatus } from '@media-manager/shared';
  18. import dayjs from 'dayjs';
  19. const taskStore = useTaskQueueStore();
  20. // 图标映射
  21. const iconMap: Record<TaskType, typeof ChatDotRound> = {
  22. sync_comments: ChatDotRound,
  23. sync_works: VideoCamera,
  24. sync_account: User,
  25. publish_video: Upload,
  26. batch_reply: ChatLineSquare,
  27. delete_work: Delete,
  28. };
  29. // 状态配置
  30. const statusConfig: Record<TaskStatus, { text: string; type: 'info' | 'warning' | 'success' | 'danger' }> = {
  31. pending: { text: '等待中', type: 'info' },
  32. running: { text: '执行中', type: 'warning' },
  33. completed: { text: '已完成', type: 'success' },
  34. failed: { text: '失败', type: 'danger' },
  35. cancelled: { text: '已取消', type: 'info' },
  36. };
  37. // 按时间倒序排序(最新的在前)
  38. const sortByTimeDesc = (a: Task, b: Task) => {
  39. const timeA = new Date(a.createdAt).getTime();
  40. const timeB = new Date(b.createdAt).getTime();
  41. return timeB - timeA;
  42. };
  43. // 按状态分组任务,每组内按时间倒序排列
  44. const groupedTasks = computed(() => {
  45. const running = taskStore.tasks
  46. .filter(t => t.status === 'running')
  47. .sort(sortByTimeDesc);
  48. const pending = taskStore.tasks
  49. .filter(t => t.status === 'pending')
  50. .sort(sortByTimeDesc);
  51. const completed = taskStore.tasks
  52. .filter(t => t.status === 'completed' || t.status === 'failed' || t.status === 'cancelled')
  53. .sort(sortByTimeDesc);
  54. return { running, pending, completed };
  55. });
  56. function getIcon(type: TaskType) {
  57. return iconMap[type] || ChatDotRound;
  58. }
  59. function getStatusConfig(status: TaskStatus) {
  60. return statusConfig[status] || statusConfig.pending;
  61. }
  62. function getPlatformName(platform?: string): string {
  63. if (!platform) return '';
  64. return PLATFORMS[platform as keyof typeof PLATFORMS]?.name || platform;
  65. }
  66. function formatTime(time?: string) {
  67. if (!time) return '-';
  68. return dayjs(time).format('HH:mm:ss');
  69. }
  70. function handleCancel(task: Task) {
  71. taskStore.cancelTask(task.id);
  72. }
  73. function handleClose() {
  74. taskStore.closeDialog();
  75. }
  76. function handleClearCompleted() {
  77. taskStore.clearCompletedTasks();
  78. }
  79. </script>
  80. <template>
  81. <ElDialog
  82. :model-value="taskStore.isDialogVisible"
  83. title="任务队列"
  84. width="560px"
  85. :close-on-click-modal="true"
  86. :close-on-press-escape="true"
  87. @close="handleClose"
  88. >
  89. <div class="task-dialog-content">
  90. <!-- 空状态 -->
  91. <ElEmpty
  92. v-if="taskStore.tasks.length === 0"
  93. description="暂无任务"
  94. :image-size="80"
  95. />
  96. <ElScrollbar v-else max-height="400px">
  97. <!-- 执行中的任务 -->
  98. <div v-if="groupedTasks.running.length > 0" class="task-group">
  99. <div class="task-group-title">
  100. <el-icon class="spin-icon"><Loading /></el-icon>
  101. 执行中 ({{ groupedTasks.running.length }})
  102. </div>
  103. <div
  104. v-for="task in groupedTasks.running"
  105. :key="task.id"
  106. class="task-item running"
  107. >
  108. <div class="task-header">
  109. <div class="task-info">
  110. <el-icon class="task-icon"><component :is="getIcon(task.type)" /></el-icon>
  111. <span class="task-title">{{ task.title }}</span>
  112. <el-tag v-if="task.platform" size="small" type="info" class="platform-tag">
  113. {{ getPlatformName(task.platform) }}
  114. </el-tag>
  115. <el-tag v-else-if="task.accountName" size="small" type="info" class="platform-tag">
  116. {{ task.accountName }}
  117. </el-tag>
  118. </div>
  119. <ElTag size="small" :type="getStatusConfig(task.status).type">
  120. {{ getStatusConfig(task.status).text }}
  121. </ElTag>
  122. </div>
  123. <div class="task-progress">
  124. <ElProgress
  125. :percentage="task.progress || 0"
  126. :stroke-width="8"
  127. :show-text="true"
  128. />
  129. </div>
  130. <div v-if="task.currentStep" class="task-step">
  131. {{ task.currentStep }}
  132. </div>
  133. </div>
  134. </div>
  135. <!-- 等待中的任务 -->
  136. <div v-if="groupedTasks.pending.length > 0" class="task-group">
  137. <div class="task-group-title">
  138. 等待中 ({{ groupedTasks.pending.length }})
  139. </div>
  140. <div
  141. v-for="task in groupedTasks.pending"
  142. :key="task.id"
  143. class="task-item pending"
  144. >
  145. <div class="task-header">
  146. <div class="task-info">
  147. <el-icon class="task-icon"><component :is="getIcon(task.type)" /></el-icon>
  148. <span class="task-title">{{ task.title }}</span>
  149. <el-tag v-if="task.platform" size="small" type="info" class="platform-tag">
  150. {{ getPlatformName(task.platform) }}
  151. </el-tag>
  152. <el-tag v-else-if="task.accountName" size="small" type="info" class="platform-tag">
  153. {{ task.accountName }}
  154. </el-tag>
  155. </div>
  156. <div class="task-actions">
  157. <ElTag size="small" type="info">等待中</ElTag>
  158. <ElButton
  159. size="small"
  160. type="danger"
  161. text
  162. @click="handleCancel(task)"
  163. >
  164. 取消
  165. </ElButton>
  166. </div>
  167. </div>
  168. <div class="task-meta">
  169. 创建于 {{ formatTime(task.createdAt) }}
  170. </div>
  171. </div>
  172. </div>
  173. <!-- 已完成的任务 -->
  174. <div v-if="groupedTasks.completed.length > 0" class="task-group">
  175. <div class="task-group-title">
  176. 已完成 ({{ groupedTasks.completed.length }})
  177. <ElButton
  178. size="small"
  179. text
  180. type="primary"
  181. @click="handleClearCompleted"
  182. >
  183. 清空
  184. </ElButton>
  185. </div>
  186. <div
  187. v-for="task in groupedTasks.completed"
  188. :key="task.id"
  189. class="task-item completed"
  190. :class="{ 'is-failed': task.status === 'failed' }"
  191. >
  192. <div class="task-header">
  193. <div class="task-info">
  194. <el-icon class="task-icon"><component :is="getIcon(task.type)" /></el-icon>
  195. <span class="task-title">{{ task.title }}</span>
  196. <el-tag v-if="task.platform" size="small" type="info" class="platform-tag">
  197. {{ getPlatformName(task.platform) }}
  198. </el-tag>
  199. <el-tag v-else-if="task.accountName" size="small" type="info" class="platform-tag">
  200. {{ task.accountName }}
  201. </el-tag>
  202. </div>
  203. <ElTag size="small" :type="getStatusConfig(task.status).type">
  204. {{ getStatusConfig(task.status).text }}
  205. </ElTag>
  206. </div>
  207. <div class="task-result">
  208. <template v-if="task.status === 'completed'">
  209. <el-icon class="result-icon success"><Check /></el-icon>
  210. {{ task.result?.message || '完成' }}
  211. </template>
  212. <template v-else-if="task.status === 'failed'">
  213. <el-icon class="result-icon failed"><Close /></el-icon>
  214. {{ task.error || '失败' }}
  215. </template>
  216. <template v-else>
  217. <el-icon class="result-icon"><Delete /></el-icon>
  218. 已取消
  219. </template>
  220. </div>
  221. <div class="task-meta">
  222. {{ formatTime(task.completedAt) }}
  223. </div>
  224. </div>
  225. </div>
  226. </ElScrollbar>
  227. </div>
  228. <template #footer>
  229. <div class="dialog-footer">
  230. <span class="footer-info">
  231. 共 {{ taskStore.tasks.length }} 个任务
  232. <template v-if="taskStore.activeTaskCount > 0">
  233. ,{{ taskStore.activeTaskCount }} 个进行中
  234. </template>
  235. </span>
  236. <ElButton @click="handleClose">关闭</ElButton>
  237. </div>
  238. </template>
  239. </ElDialog>
  240. </template>
  241. <style scoped lang="scss">
  242. @use '@/styles/variables.scss' as *;
  243. .task-dialog-content {
  244. min-height: 100px;
  245. }
  246. .task-group {
  247. margin-bottom: 20px;
  248. &:last-child {
  249. margin-bottom: 0;
  250. }
  251. }
  252. .task-group-title {
  253. display: flex;
  254. align-items: center;
  255. gap: 8px;
  256. font-size: 13px;
  257. font-weight: 600;
  258. color: $text-secondary;
  259. margin-bottom: 12px;
  260. padding-bottom: 8px;
  261. border-bottom: 1px solid $border-light;
  262. .spin-icon {
  263. width: 14px;
  264. height: 14px;
  265. color: $primary-color;
  266. animation: spin 1s linear infinite;
  267. }
  268. }
  269. .task-item {
  270. padding: 14px 16px;
  271. border-radius: $radius-lg;
  272. background: $bg-base;
  273. margin-bottom: 10px;
  274. border: 1px solid $border-light;
  275. transition: all 0.2s;
  276. &:last-child {
  277. margin-bottom: 0;
  278. }
  279. &.running {
  280. background: $primary-color-light;
  281. border-color: rgba($primary-color, 0.2);
  282. .task-icon {
  283. color: $primary-color;
  284. }
  285. }
  286. &.pending {
  287. background: $bg-base;
  288. .task-icon {
  289. color: $text-secondary;
  290. }
  291. }
  292. &.completed {
  293. background: #fff;
  294. &.is-failed {
  295. background: $danger-color-light;
  296. border-color: rgba($danger-color, 0.2);
  297. }
  298. }
  299. }
  300. .task-header {
  301. display: flex;
  302. justify-content: space-between;
  303. align-items: center;
  304. margin-bottom: 10px;
  305. }
  306. .task-info {
  307. display: flex;
  308. align-items: center;
  309. gap: 10px;
  310. }
  311. .task-icon {
  312. width: 20px;
  313. height: 20px;
  314. color: $primary-color;
  315. }
  316. .task-title {
  317. font-size: 14px;
  318. font-weight: 600;
  319. color: $text-primary;
  320. }
  321. .platform-tag {
  322. flex-shrink: 0;
  323. }
  324. .task-actions {
  325. display: flex;
  326. align-items: center;
  327. gap: 8px;
  328. }
  329. .task-progress {
  330. margin-bottom: 8px;
  331. :deep(.el-progress-bar__outer) {
  332. background: rgba(0, 0, 0, 0.06);
  333. border-radius: 4px;
  334. }
  335. :deep(.el-progress-bar__inner) {
  336. background: linear-gradient(90deg, $primary-color, #64b5f6);
  337. border-radius: 4px;
  338. }
  339. }
  340. .task-step {
  341. font-size: 12px;
  342. color: $text-secondary;
  343. }
  344. .task-result {
  345. display: flex;
  346. align-items: center;
  347. gap: 8px;
  348. font-size: 13px;
  349. color: $text-regular;
  350. .result-icon {
  351. width: 16px;
  352. height: 16px;
  353. &.success {
  354. color: $success-color;
  355. }
  356. &.failed {
  357. color: $danger-color;
  358. }
  359. }
  360. }
  361. .task-meta {
  362. font-size: 12px;
  363. color: $text-placeholder;
  364. margin-top: 6px;
  365. }
  366. .dialog-footer {
  367. display: flex;
  368. justify-content: space-between;
  369. align-items: center;
  370. }
  371. .footer-info {
  372. font-size: 13px;
  373. color: $text-secondary;
  374. }
  375. @keyframes spin {
  376. from { transform: rotate(0deg); }
  377. to { transform: rotate(360deg); }
  378. }
  379. </style>