This commit is contained in:
2024-06-28 21:31:48 +08:00
parent 48aced8b6e
commit 06a2ea704e
6 changed files with 175 additions and 34 deletions

View File

@@ -3,6 +3,8 @@ import type { PropType } from 'vue'
import dayjs from 'dayjs' import dayjs from 'dayjs'
import { useDownload } from '~/composables/useDownload' import { useDownload } from '~/composables/useDownload'
const toast = useToast()
const props = defineProps({ const props = defineProps({
course: { course: {
type: Object as PropType<resp.gen.CourseGenItem>, type: Object as PropType<resp.gen.CourseGenItem>,
@@ -21,9 +23,61 @@ const stateDisplay = computed(() => {
} }
return { return {
color: 'blue', 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',
})
}
</script> </script>
<template> <template>
@@ -32,27 +86,33 @@ const stateDisplay = computed(() => {
> >
<div class="relative w-full aspect-video"> <div class="relative w-full aspect-video">
<NuxtImg <NuxtImg
class="w-full aspect-video object-cover pointer-events-none absolute inset-0"
v-if="!!course.video_cover" v-if="!!course.video_cover"
:src="course.video_cover" :src="course.video_cover"
alt="image" alt="image"
class="w-full h-full object-cover pointer-events-none absolute inset-0" loading="lazy"
/> />
<div <div
v-else v-else
class="absolute inset-0 bg-gradient-to-br from-purple-400 to-primary-400 flex justify-center items-center" class="absolute inset-0 bg-gradient-to-br from-purple-400 to-primary-400 flex justify-center items-center"
> >
<Icon class="text-white text-[64px] animate-pulse" name="i-tabler-photo-video"/> <Icon
v-if="isFailed"
class="text-white text-[64px] opacity-50"
name="i-tabler-alert-triangle"
/>
<Icon v-else class="text-white text-[64px] animate-pulse" name="i-tabler-photo-video"/>
</div> </div>
<div class="absolute inset-2 flex justify-end items-start"> <div class="absolute inset-2 flex justify-end items-start">
<UTooltip :prevent="course.progress > -1" :text="course.message || ''"> <UTooltip :prevent="course.progress > -1" :text="course.message || ''">
<UBadge <UBadge
:color="stateDisplay.color" :color="stateDisplay.color"
:variant="stateDisplay.color === 'red' ? 'solid' : 'subtle'" :variant="isFailed ? 'solid' : 'subtle'"
class="shadow" class="shadow"
size="sm" size="sm"
> >
<Icon <Icon
v-if="stateDisplay.color === 'red'" v-if="isFailed"
class="text-base mr-0.5" class="text-base mr-0.5"
name="i-tabler-alert-triangle" name="i-tabler-alert-triangle"
/> />
@@ -63,8 +123,10 @@ const stateDisplay = computed(() => {
</div> </div>
<div class="px-2 pt-1 pb-2 flex justify-between"> <div class="px-2 pt-1 pb-2 flex justify-between">
<div class="flex-1 overflow-hidden pt-1"> <div class="flex-1 overflow-hidden pt-1">
<h1 class="text-sm font-medium overflow-hidden text-ellipsis text-nowrap" <h1
title="课程名字课程名字课程名字课程名字课程名字课程名字课程名字课程名字"> :title="course.title"
class="text-sm font-medium overflow-hidden text-ellipsis text-nowrap"
>
<Icon class="-mt-0.5 -ml-0.5 text-base" name="i-tabler-book-2"/> <Icon class="-mt-0.5 -ml-0.5 text-base" name="i-tabler-book-2"/>
<span class="pl-0.5">{{ course.title }}</span> <span class="pl-0.5">{{ course.title }}</span>
</h1> </h1>
@@ -74,6 +136,7 @@ const stateDisplay = computed(() => {
class="hover:text-primary font-medium" class="hover:text-primary font-medium"
tabindex="-1" tabindex="-1"
:title="course.task_id" :title="course.task_id"
@click="copyTaskId"
> >
复制ID 复制ID
</button> </button>
@@ -83,13 +146,11 @@ const stateDisplay = computed(() => {
<UButtonGroup> <UButtonGroup>
<UButton <UButton
color="white" color="white"
label="下载" :disabled="!isDownloadable"
:label="downloadProgress > 0 && downloadProgress < 100 ? `${downloadProgress.toFixed(0)}%` : '下载'"
leading-icon="i-tabler-download" leading-icon="i-tabler-download"
size="xs" size="xs"
@click="() => { @click="startDownload(course.video_url, `眩生花微课_${ props.course.title }_${ props.course.task_id }.mp4`)"
const {download} = useDownload(course.video_url, `眩生花微课_${course.title}_${course.task_id}.mp4`)
download()
}"
/> />
<UDropdown <UDropdown
:items="[ :items="[
@@ -97,19 +158,31 @@ const stateDisplay = computed(() => {
label: '预览课程', label: '预览课程',
icon: 'i-tabler-play', icon: 'i-tabler-play',
shortcuts: ['空格'], shortcuts: ['空格'],
disabled: !isDownloadable,
}, {
label: '查看字幕',
icon: 'i-solar-subtitles-linear',
shortcuts: ['Alt', 'D'],
disabled: !isDownloadable,
}, { }, {
label: '下载字幕', label: '下载字幕',
icon: 'i-tabler-file-download', icon: 'i-tabler-file-download',
shortcuts: ['Alt', 'S'], shortcuts: ['Alt', 'S'],
disabled: !isDownloadable,
}], [{ }], [{
label: '删除记录', label: '删除记录',
icon: 'i-tabler-trash-x', icon: 'i-tabler-trash-x',
shortcuts: ['Delete'], shortcuts: ['Delete'],
}], }],
]" ]"
:popper="{ placement: 'bottom-start' }" :popper="{ placement: 'bottom-end' }"
> >
<UButton color="white" size="xs" trailing-icon="i-tabler-dots"/> <UButton
:disabled="course.progress > 1 && course.progress < 100"
color="white"
size="xs"
trailing-icon="i-tabler-dots"
/>
</UDropdown> </UDropdown>
</UButtonGroup> </UButtonGroup>
</div> </div>

View File

@@ -9,6 +9,7 @@ const loginState = useLoginState()
const toast = useToast() const toast = useToast()
const isCreateCourseModalOpen = ref(false) const isCreateCourseModalOpen = ref(false)
const creationPending = ref(false)
const { const {
data: courseList, data: courseList,
@@ -29,6 +30,7 @@ const {
}, },
) )
const onCreateCourseClick = () => { const onCreateCourseClick = () => {
isCreateCourseModalOpen.value = true isCreateCourseModalOpen.value = true
} }
@@ -60,24 +62,55 @@ const onCreateCourseSubmit = async (event: FormSubmitEvent<CreateCourseSchema>)
}) })
return return
} }
creationPending.value = true
// upload PPTX file // upload PPTX file
useFileGo(selected_file.value[0]).then(url => { useFileGo(selected_file.value[0]).then(url => {
useFetchWrapped<req.gen.CourseGenCreate & AuthedRequest, resp.gen.CourseGenCreate>('App.Digital_Convert.Create', { useFetchWrapped<req.gen.CourseGenCreate & AuthedRequest, BaseResponse<resp.gen.CourseGenCreate>>('App.Digital_Convert.Create', {
token: loginState.token!, token: loginState.token!,
user_id: loginState.user.id, user_id: loginState.user.id,
task_title: event.data.task_title, task_title: event.data.task_title,
gen_server: event.data.gen_server as 'main' | 'standby1', gen_server: event.data.gen_server as 'main' | 'standby1',
speed: event.data.speed, speed: 2 - event.data.speed,
ppt_url: url, ppt_url: url,
digital_human_id: '40696', digital_human_id: 40696,
custom_video: '[]', custom_video: '[]',
opening_url: '', opening_url: '',
ending_url: '', ending_url: '',
}).then(res => { }).then(res => {
console.log(res) if (res.data.record_status === 1) {
toast.add({
title: '创建成功',
description: '微课视频已开始生成',
color: 'green',
icon: 'i-tabler-check',
})
refreshCourseList()
isCreateCourseModalOpen.value = false
} else {
toast.add({
title: '创建失败',
description: '未知错误',
color: 'red',
icon: 'i-tabler-alert-triangle',
})
}
creationPending.value = false
}).catch(e => {
creationPending.value = false
toast.add({
title: '创建失败',
description: e.message || '未知错误',
color: 'red',
icon: 'i-tabler-alert-triangle',
})
}) })
}) })
} }
onMounted(() => {
const i = setInterval(refreshCourseList, 1000 * 5)
onBeforeUnmount(() => clearInterval(i))
})
</script> </script>
<template> <template>
@@ -94,10 +127,10 @@ const onCreateCourseSubmit = async (event: FormSubmitEvent<CreateCourseSchema>)
/> />
</div> </div>
<div class="p-4"> <div class="p-4">
<div class="relative grid grid-cols-1 sm:grid-cols-2 2xl:grid-cols-4 gap-4"> <div class="relative grid grid-cols-1 sm:grid-cols-2 xl:grid-cols-3 2xl:grid-cols-4 gap-4">
<CGTaskCard v-for="(course, index) in courseList" :key="index" :course="course"/> <CGTaskCard v-for="(course, index) in courseList" :key="index" :course="course"/>
</div> </div>
<pre class="overflow-hidden">{{ JSON.stringify(courseList, null, 2) }}</pre> <!-- <pre class="overflow-hidden">{{ JSON.stringify(courseList, null, 2) }}</pre>-->
</div> </div>
<UModal <UModal
@@ -177,6 +210,7 @@ const onCreateCourseSubmit = async (event: FormSubmitEvent<CreateCourseSchema>)
label="提交" label="提交"
type="submit" type="submit"
variant="solid" variant="solid"
:loading="creationPending"
/> />
</div> </div>
</UForm> </UForm>

View File

@@ -1,16 +1,44 @@
export const useDownload = (url: string, filename: string) => { import { EventEmitter } from 'events'
export const useDownload = (url: string, filename: string): {
download: () => void
progressEmitter: EventEmitter
} => {
const progressEmitter = new EventEmitter()
const download = () => { const download = () => {
fetch(url) const xhr = new XMLHttpRequest()
.then((response) => response.blob()) xhr.open('GET', url, true)
.then((blob) => { xhr.responseType = 'blob'
const url = window.URL.createObjectURL(new Blob([blob])) xhr.onprogress = (event) => {
const link = document.createElement('a') if (event.lengthComputable) {
link.href = url const percentComplete = (event.loaded / event.total) * 100
link.setAttribute('download', filename) progressEmitter.emit('progress', percentComplete)
document.body.appendChild(link) }
link.click() }
link.parentNode?.removeChild(link) xhr.onload = function () {
}) if (this.status === 200) {
const blob = new Blob([this.response], { type: 'application/octet-stream' })
const url = window.URL.createObjectURL(blob)
const link = document.createElement('a')
link.href = url
link.setAttribute('download', filename)
document.body.appendChild(link)
link.click()
link.parentNode?.removeChild(link)
progressEmitter.emit('done')
} else {
progressEmitter.emit('error', new Error('资源已过期或不存在'))
}
}
xhr.onerror = function () {
progressEmitter.emit('error', new Error('网络错误,下载失败'))
}
xhr.send()
}
return {
download,
progressEmitter,
} }
return { download }
} }

View File

@@ -18,6 +18,7 @@
"@nuxt/ui": "^2.14.1", "@nuxt/ui": "^2.14.1",
"@uniiem/object-trim": "^0.2.0", "@uniiem/object-trim": "^0.2.0",
"@uniiem/uuid": "^0.2.1", "@uniiem/uuid": "^0.2.1",
"events": "^3.3.0",
"highlight.js": "^11.9.0", "highlight.js": "^11.9.0",
"idb-keyval": "^6.2.1", "idb-keyval": "^6.2.1",
"markdown-it": "^14.1.0", "markdown-it": "^14.1.0",

2
typings/types.d.ts vendored
View File

@@ -71,7 +71,7 @@ namespace req {
* @param digital_human_id 数字人物 ID (string || number 存疑) * @param digital_human_id 数字人物 ID (string || number 存疑)
*/ */
interface CourseGenCreate { interface CourseGenCreate {
digital_human_id: string digital_human_id: number
ppt_url: string ppt_url: string
opening_url: string opening_url: string
ending_url: string ending_url: string

View File

@@ -4118,6 +4118,11 @@ etag@^1.8.1, etag@~1.8.1:
resolved "https://registry.npmmirror.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887" resolved "https://registry.npmmirror.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
integrity sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg== integrity sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==
events@^3.3.0:
version "3.3.0"
resolved "https://registry.yarnpkg.com/events/-/events-3.3.0.tgz#31a95ad0a924e2d2c419a813aeb2c4e878ea7400"
integrity sha512-mQw+2fkQbALzQ7V0MY0IqdnXNOeTtP4r0lN9z7AAawCXgqea7bDii20AYrIBrFd/Hx0M2Ocz6S111CaFkUcb0Q==
execa@^5.1.1: execa@^5.1.1:
version "5.1.1" version "5.1.1"
resolved "https://registry.npmmirror.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd" resolved "https://registry.npmmirror.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd"