Files
xsh-assistant-next/app/components/aigc/generation/CGTaskCard.client.vue

508 lines
14 KiB
Vue

<script setup lang="ts">
import type { PropType } from 'vue'
import dayjs from 'dayjs'
import { useDownload } from '~/composables/useDownload'
import gsap from 'gsap'
const toast = useToast()
const loginState = useLoginState()
const srtEditor = ref()
const props = defineProps({
course: {
type: Object as PropType<resp.gen.CourseGenItem>,
required: true,
},
})
const emit = defineEmits(['delete'])
defineShortcuts({
p: {
handler: () => {
if (isDropdownOpen.value && isDownloadable.value) {
isPreviewModalOpen.value = true
}
},
},
meta_d: {
handler: () => {
if (isDropdownOpen.value && isDownloadable.value) {
srtEditor.value.open()
isDropdownOpen.value = false
}
},
},
meta_s: {
handler: async () => {
if (isDropdownOpen.value && isDownloadable.value) {
await startDownload(
await fetchCourseSubtitleUrl(props.course),
`眩生花微课_${props.course.title}_${props.course.task_id}.srt`
)
}
},
},
delete: {
handler: () => {
if (isDropdownOpen.value) {
emit('delete', props.course.task_id)
}
},
},
})
const isDropdownOpen = ref(false)
const isPreviewModalOpen = ref(false)
const stateDisplay = computed(() => {
if (props.course.progress === -1)
return {
color: 'error' as const,
text: '失败',
}
if (props.course.progress === 100)
return {
color: 'success' as const,
text: '完成',
}
return {
color: 'info' as const,
text: props.course.progress
? `${tweenedGenerateProgress.value.toFixed(0)}%`
: '队列中',
}
})
const isFailed = computed(() => props.course.progress === -1)
const isDownloadable = computed(
() => !isFailed.value && props.course.progress === 100
)
const generateProgress = computed(() => {
return props.course.progress || 0
})
const tweenedGenerateProgress = ref(0)
watch(
generateProgress,
(newValue) => {
gsap.to(tweenedGenerateProgress, {
duration: 5,
value: newValue,
})
},
{
immediate: true,
}
)
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: 'success',
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: 'error',
icon: 'i-tabler-alert-triangle',
})
})
download()
}
const copyTaskId = (extraMessage?: string) => {
navigator.clipboard.writeText(
props.course.task_id + (extraMessage ? ` ${extraMessage}` : '')
)
toast.add({
title: '复制成功',
description: '已复制任务 ID',
color: 'success',
icon: 'i-tabler-check',
})
}
const isCombinationModalOpen = ref(false)
const combinationState = ref<0 | 1 | undefined>(0)
const onCombination = async () => {
isCombinationModalOpen.value = true
combinationState.value = undefined
const srtResponse = await (
await fetch(await fetchCourseSubtitleUrl(props.course))
).blob()
if (!srtResponse) {
toast.add({
title: '获取字幕失败',
description: '无法获取字幕文件,请稍后重试',
color: 'error',
icon: 'i-tabler-alert-triangle',
})
return
}
const srtBlob = new Blob([srtResponse], { type: 'text/plain' })
const srtUrl = URL.createObjectURL(srtBlob)
useVideoSubtitleEmbedding(props.course.video_url, srtUrl)
.then((src) => {
startDownload(
src,
`眩生花微课_${props.course.title}_${props.course.task_id}_combinated.mp4`
)
combinationState.value = 1
})
.catch((err) => {
toast.add({
title: '嵌入字幕失败',
description: err.message || '未知错误',
color: 'error',
icon: 'i-tabler-alert-triangle',
})
combinationState.value = 0
})
.finally(() => {
setTimeout(() => {
combinationState.value = 0
isCombinationModalOpen.value = false
}, 3000)
})
}
const onRetryClick = (course: resp.gen.CourseGenItem) => {
useFetchWrapped<
req.gen.CourseGenCreate & AuthedRequest,
BaseResponse<resp.gen.CourseGenCreate>
>('App.Digital_Convert.Create', {
token: loginState.token!,
user_id: loginState.user.id,
task_title: course.title,
gen_server: 'main',
speed: 2 - course.speed,
ppt_url: course.ppt_url,
digital_human_id: course.digital_human_id,
custom_video: '[]',
opening_url: course.opening_url || '',
ending_url: course.opening_url || '',
}).then(
(res) => {
if (res.data.record_status === 1) {
toast.add({
title: '重试已提交',
description: '已加入生成队列',
color: 'success',
icon: 'i-tabler-check',
})
// delete
emit('delete', course.task_id)
} else {
toast.add({
title: '提交重试失败',
description: res.msg || '未知错误',
color: 'error',
icon: 'i-tabler-alert-triangle',
})
}
},
(err) => {
toast.add({
title: '提交重试失败',
description: err.message || '未知错误',
color: 'error',
icon: 'i-tabler-alert-triangle',
})
}
)
}
</script>
<template>
<div
class="hover:shadow-xs w-full overflow-hidden rounded-xl border border-neutral-200 transition dark:border-neutral-700"
>
<div class="group relative aspect-video w-full">
<NuxtImg
class="pointer-events-none absolute inset-0 aspect-video w-full object-cover"
v-if="!!course.video_cover"
:src="course.video_cover"
alt="image"
loading="lazy"
/>
<div
v-else
class="bg-linear-to-br to-primary-400 pattern absolute inset-0 flex items-center justify-center from-purple-400"
>
<Icon
v-if="isFailed"
class="text-[64px] text-white opacity-50"
name="i-tabler-alert-triangle"
/>
<Icon
v-else
class="animate-pulse text-[64px] text-white"
name="i-tabler-photo-video"
/>
</div>
<div class="absolute inset-2 flex items-start justify-end">
<UTooltip
:prevent="course.progress > -1"
:text="course.message || ''"
>
<UBadge
:color="stateDisplay.color"
variant="solid"
class="shadow-xs"
size="sm"
>
<Icon
v-if="isFailed"
class="mr-0.5 text-base"
name="i-tabler-alert-triangle"
/>
{{ stateDisplay.text }}
</UBadge>
</UTooltip>
</div>
<div
v-if="isDownloadable"
class="absolute inset-0 flex items-center justify-center bg-black/10 opacity-0 backdrop-blur-md duration-300 group-hover:opacity-100"
>
<div
class="flex aspect-square w-14 cursor-pointer items-center justify-center rounded-full bg-gray-300/50 backdrop-blur-md"
@click="isPreviewModalOpen = true"
>
<Icon
name="i-tabler-play"
class="text-3xl text-white"
/>
</div>
</div>
</div>
<div class="flex justify-between px-2 pb-2 pt-1">
<div class="flex-1 overflow-hidden pt-1">
<h1
:title="course.title"
class="inline-flex items-center overflow-hidden text-ellipsis text-nowrap text-sm font-medium"
>
<Icon
class="text-base"
name="i-tabler-book-2"
/>
<span class="pl-0.5">{{ course.title }}</span>
</h1>
<p class="space-x-2 pt-0.5 text-xs text-neutral-400">
<span>
{{ dayjs(course.create_time * 1000).format('YYYY-MM-DD HH:mm:ss') }}
</span>
<button
v-if="course.task_id"
class="hover:text-primary font-medium"
tabindex="-1"
:title="course.task_id"
@click="
copyTaskId(
isFailed ? `\n\n${course.message}\n${course.ppt_url}` : ''
)
"
>
{{ isFailed ? '复制错误报告' : '复制 ID' }}
</button>
</p>
</div>
<div class="flex items-end gap-1">
<UFieldGroup>
<UButton
color="neutral"
variant="outline"
:disabled="!isFailed && !isDownloadable"
:label="isFailed ? '重试' : isDownloadable ? '下载' : '生成中'"
:leading-icon="isFailed ? 'i-tabler-refresh' : 'i-tabler-download'"
size="sm"
@click="
() => {
if (isFailed) {
onRetryClick(course)
} else {
onCombination()
}
}
"
/>
<!-- retry -->
<UDropdownMenu
v-model:open="isDropdownOpen"
:content="{
align: 'end',
}"
:items="[
[
{
label: '下载原视频',
icon: 'i-tabler-file-plus',
disabled: !isDownloadable,
onClick: () =>
startDownload(
course.video_url,
`眩生花微课_${props.course.title}_${props.course.task_id}.mp4`
),
},
{
label: '预览课程',
icon: 'i-tabler-play',
shortcuts: ['P'],
disabled: !isDownloadable,
onClick: () => (isPreviewModalOpen = true),
},
{
label: '编辑字幕',
icon: 'i-solar-subtitles-linear',
shortcuts: ['meta', 'D'],
disabled: !isDownloadable,
onClick: () => {
srtEditor.open()
isDropdownOpen = false
},
},
{
label: '下载字幕',
icon: 'i-tabler-file-download',
shortcuts: ['meta', 'S'],
disabled: !isDownloadable,
onClick: async () => {
await startDownload(
await fetchCourseSubtitleUrl(course),
`眩生花微课_${props.course.title}_${props.course.task_id}.srt`
)
},
},
],
[
{
label: '删除记录',
icon: 'i-tabler-trash-x',
shortcuts: ['Delete'],
onClick: () => emit('delete', course.task_id),
},
],
]"
>
<UButton
:disabled="course.progress > 1 && course.progress < 100"
color="neutral"
variant="outline"
size="sm"
trailing-icon="i-tabler-dots"
/>
</UDropdownMenu>
</UFieldGroup>
</div>
</div>
<UModal v-model:open="isPreviewModalOpen">
<template #content>
<UCard>
<template #header>
<div class="flex items-center justify-between">
<div
class="overflow-hidden text-base font-semibold leading-6 text-gray-900 dark:text-white"
>
<p>微课视频预览</p>
<p
class="w-full overflow-hidden text-ellipsis text-nowrap text-xs text-blue-500"
>
{{ course.title }}
</p>
</div>
<UButton
class="-my-1"
color="neutral"
icon="i-tabler-x"
variant="ghost"
@click="isPreviewModalOpen = false"
/>
</div>
</template>
<video
class="rounded-xs shadow-xs w-full"
controls
autoplay
:src="course.video_url"
/>
</UCard>
</template>
</UModal>
<UModal v-model:open="isCombinationModalOpen">
<template #content>
<UCard>
<template #header>
<div class="flex items-center justify-between">
<div
class="overflow-hidden text-base font-semibold leading-6 text-gray-900 dark:text-white"
>
<p>嵌入视频字幕</p>
<p
class="w-full overflow-hidden text-ellipsis text-nowrap text-xs text-blue-500"
>
{{ course.title }}
</p>
</div>
<UButton
class="-my-1"
color="neutral"
icon="i-tabler-x"
variant="ghost"
@click="isCombinationModalOpen = false"
/>
</div>
</template>
<UProgress
animation="carousel"
:value="combinationState"
:max="['嵌入字幕中', '合并完成,开始下载']"
>
<template #step-0="{ step }">
<span class="inline-flex items-center gap-1 text-emerald-500">
<UIcon name="tabler:text-caption" />
{{ step }}
</span>
</template>
<template #step-1="{ step }">
<span class="text-primary-500 inline-flex items-center gap-1">
<UIcon name="tabler:paperclip" />
{{ step }}
</span>
</template>
</UProgress>
</UCard>
</template>
</UModal>
<AigcGenerationSRTEditor
ref="srtEditor"
:course="course"
/>
</div>
</template>
<style scoped></style>