From 06a2ea704ebe6b6e782b94201666a81762d046dc Mon Sep 17 00:00:00 2001 From: HoshinoSuzumi Date: Fri, 28 Jun 2024 21:31:48 +0800 Subject: [PATCH] wip --- .../aigc/course-generate/CGTaskCard.vue | 101 +++++++++++++++--- .../aigc/course-generate/CourseGenerate.vue | 46 ++++++-- composables/useDownload.ts | 54 +++++++--- package.json | 1 + typings/types.d.ts | 2 +- yarn.lock | 5 + 6 files changed, 175 insertions(+), 34 deletions(-) diff --git a/components/aigc/course-generate/CGTaskCard.vue b/components/aigc/course-generate/CGTaskCard.vue index 5512bea..bbbdf5c 100644 --- a/components/aigc/course-generate/CGTaskCard.vue +++ b/components/aigc/course-generate/CGTaskCard.vue @@ -3,6 +3,8 @@ import type { PropType } from 'vue' import dayjs from 'dayjs' import { useDownload } from '~/composables/useDownload' +const toast = useToast() + const props = defineProps({ course: { type: Object as PropType, @@ -21,9 +23,61 @@ const stateDisplay = computed(() => { } return { color: 'blue', - text: '进行中', + text: !!props.course.progress ? `${ props.course.progress }%` : '队列中', } }) +const isFailed = computed(() => props.course.progress === -1) +const isDownloadable = computed(() => !isFailed.value && props.course.progress === 100) + +const downloadProgress = ref(0) + +const startDownload = async ( + url: string, + filename: string, +) => { + downloadProgress.value = 0 + + const { + download, + progressEmitter, + } = useDownload(url, filename) + + progressEmitter.on('done', () => { + downloadProgress.value = 100 + toast.add({ + title: '下载完成', + description: '资源下载已完成', + color: 'green', + icon: 'i-tabler-check', + }) + }) + + progressEmitter.on('progress', (progress) => { + downloadProgress.value = progress + }) + + progressEmitter.on('error', err => { + downloadProgress.value = 0 + toast.add({ + title: '下载失败', + description: err.message || '下载失败,未知错误', + color: 'red', + icon: 'i-tabler-alert-triangle', + }) + }) + + download() +} + +const copyTaskId = () => { + navigator.clipboard.writeText(props.course.task_id) + toast.add({ + title: '复制成功', + description: '已复制任务 ID', + color: 'green', + icon: 'i-tabler-check', + }) +}