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