267 lines
7.8 KiB
Vue
267 lines
7.8 KiB
Vue
<!-- Author: fruit.com.cn -->
|
||
<!-- 剧集转码面板:数据获取、轮询与子组件组合 -->
|
||
|
||
<script setup lang="ts">
|
||
import { computed, onBeforeUnmount, ref, watch } from 'vue'
|
||
import { ElMessage } from 'element-plus'
|
||
import {
|
||
getEpisodeTranscodeApi,
|
||
getMediaThumbnailsApi,
|
||
triggerMediaTranscodeApi
|
||
} from '@/api/admin/transcode'
|
||
import type { EpisodeTranscodeItem, MediaThumbnailResult } from '@/api/admin/types'
|
||
import { useI18n } from '@/hooks/web/useI18n'
|
||
import EpisodeTranscodeFrames from './EpisodeTranscodeFrames.vue'
|
||
import EpisodeTranscodeRenditions from './EpisodeTranscodeRenditions.vue'
|
||
import EpisodeTranscodeStatusBadge from './EpisodeTranscodeStatusBadge.vue'
|
||
import EpisodeTranscodeStatusPanel from './EpisodeTranscodeStatusPanel.vue'
|
||
import {
|
||
safeTranscodeProgress,
|
||
THUMBNAIL_PAGE_SIZE,
|
||
transcodeProgressStatus,
|
||
transcodeStatusKey,
|
||
transcodeStatusType
|
||
} from './episodeTranscodeUi'
|
||
|
||
const props = defineProps<{
|
||
episodeId: string | number
|
||
active: boolean
|
||
}>()
|
||
|
||
const POLL_INTERVAL_MS = 4000
|
||
|
||
const { t } = useI18n()
|
||
const expanded = ref<string[]>(['transcode'])
|
||
const detail = ref<EpisodeTranscodeItem | null>(null)
|
||
const thumbnails = ref<MediaThumbnailResult | null>(null)
|
||
const loading = ref(false)
|
||
const thumbnailLoading = ref(false)
|
||
const retriggering = ref(false)
|
||
const errorMessage = ref('')
|
||
const thumbnailError = ref('')
|
||
const pageIndex = ref(1)
|
||
const pageSize = ref(THUMBNAIL_PAGE_SIZE)
|
||
let pollTimer: ReturnType<typeof setTimeout> | null = null
|
||
let requestSeq = 0
|
||
let thumbnailSeq = 0
|
||
|
||
const mediaAssetId = computed(() => detail.value?.mediaAssetId || '')
|
||
const hasMedia = computed(() => /^[1-9]\d*$/.test(mediaAssetId.value))
|
||
const normalizedStatus = computed(() => String(detail.value?.status || '').toUpperCase())
|
||
const renditions = computed(() => detail.value?.renditions || [])
|
||
const isProcessing = computed(
|
||
() => normalizedStatus.value === 'QUEUED' || normalizedStatus.value === 'RUNNING'
|
||
)
|
||
const statusLabel = computed(() =>
|
||
t(`episodeTranscode.status.${transcodeStatusKey(detail.value?.status)}`)
|
||
)
|
||
const statusType = computed(() => transcodeStatusType(detail.value?.status))
|
||
const progressStatus = computed(() => transcodeProgressStatus(detail.value?.status))
|
||
const progress = computed(() => safeTranscodeProgress(detail.value?.progress))
|
||
const showOutputs = computed(() => !!detail.value && hasMedia.value && !errorMessage.value)
|
||
const statusView = computed(() => ({
|
||
loading: loading.value,
|
||
errorMessage: errorMessage.value,
|
||
hasMedia: hasMedia.value,
|
||
retriggering: retriggering.value,
|
||
isProcessing: isProcessing.value,
|
||
statusLabel: statusLabel.value,
|
||
statusType: statusType.value,
|
||
progressStatus: progressStatus.value,
|
||
progress: progress.value
|
||
}))
|
||
const framesView = computed(() => ({
|
||
data: thumbnails.value,
|
||
loading: thumbnailLoading.value,
|
||
errorMessage: thumbnailError.value,
|
||
pageIndex: pageIndex.value,
|
||
pageSize: pageSize.value
|
||
}))
|
||
|
||
watch(
|
||
[() => props.episodeId, () => props.active],
|
||
([episodeId, active], [previousEpisodeId]) => {
|
||
if (episodeId !== previousEpisodeId) resetState()
|
||
if (!active) {
|
||
stopPolling()
|
||
return
|
||
}
|
||
if (episodeId) void loadDetail()
|
||
},
|
||
{ immediate: true }
|
||
)
|
||
|
||
onBeforeUnmount(stopPolling)
|
||
|
||
function resetState() {
|
||
stopPolling()
|
||
requestSeq += 1
|
||
thumbnailSeq += 1
|
||
detail.value = null
|
||
thumbnails.value = null
|
||
errorMessage.value = ''
|
||
thumbnailError.value = ''
|
||
pageIndex.value = 1
|
||
pageSize.value = THUMBNAIL_PAGE_SIZE
|
||
}
|
||
|
||
function stopPolling() {
|
||
if (pollTimer) clearTimeout(pollTimer)
|
||
pollTimer = null
|
||
}
|
||
|
||
function schedulePolling() {
|
||
stopPolling()
|
||
if (!props.active || !isProcessing.value) return
|
||
pollTimer = setTimeout(() => {
|
||
pollTimer = null
|
||
void loadDetail(true)
|
||
}, POLL_INTERVAL_MS)
|
||
}
|
||
|
||
async function loadDetail(silent = false) {
|
||
if (!props.active || !props.episodeId) return
|
||
const seq = ++requestSeq
|
||
stopPolling()
|
||
if (!silent) loading.value = true
|
||
errorMessage.value = ''
|
||
try {
|
||
const response = await getEpisodeTranscodeApi(props.episodeId)
|
||
if (seq !== requestSeq) return
|
||
const result = response.data
|
||
detail.value = result
|
||
if (result.thumbnails) {
|
||
thumbnails.value = result.thumbnails
|
||
pageIndex.value = result.thumbnails.pageIndex || 1
|
||
pageSize.value = result.thumbnails.pageSize || THUMBNAIL_PAGE_SIZE
|
||
thumbnailError.value = ''
|
||
}
|
||
if (isProcessing.value) schedulePolling()
|
||
} catch (error) {
|
||
if (seq !== requestSeq) return
|
||
errorMessage.value = errorText(error, t('episodeTranscode.loadFailed'))
|
||
stopPolling()
|
||
} finally {
|
||
if (seq === requestSeq) loading.value = false
|
||
}
|
||
}
|
||
|
||
async function loadThumbnailPage() {
|
||
if (!hasMedia.value || thumbnailLoading.value) return
|
||
const seq = ++thumbnailSeq
|
||
thumbnailLoading.value = true
|
||
thumbnailError.value = ''
|
||
try {
|
||
const response = await getMediaThumbnailsApi(
|
||
mediaAssetId.value,
|
||
pageIndex.value,
|
||
pageSize.value
|
||
)
|
||
if (seq !== thumbnailSeq) return
|
||
const result = response.data
|
||
thumbnails.value = result
|
||
pageIndex.value = result.pageIndex || pageIndex.value
|
||
pageSize.value = result.pageSize || pageSize.value
|
||
} catch (error) {
|
||
if (seq !== thumbnailSeq) return
|
||
thumbnailError.value = errorText(error, t('episodeTranscode.loadFailed'))
|
||
} finally {
|
||
if (seq === thumbnailSeq) thumbnailLoading.value = false
|
||
}
|
||
}
|
||
|
||
async function retrigger() {
|
||
if (!hasMedia.value || isProcessing.value || retriggering.value) return
|
||
retriggering.value = true
|
||
errorMessage.value = ''
|
||
try {
|
||
await triggerMediaTranscodeApi(mediaAssetId.value)
|
||
if (detail.value) {
|
||
detail.value = { ...detail.value, status: 'QUEUED', progress: 0, errorMsg: '' }
|
||
}
|
||
ElMessage.success(t('episodeTranscode.triggerSuccess'))
|
||
await loadDetail(true)
|
||
schedulePolling()
|
||
} catch (error) {
|
||
errorMessage.value = errorText(error, t('episodeTranscode.triggerFailed'))
|
||
} finally {
|
||
retriggering.value = false
|
||
}
|
||
}
|
||
|
||
function changePage(value: number) {
|
||
pageIndex.value = value
|
||
void loadThumbnailPage()
|
||
}
|
||
|
||
function changePageSize(value: number) {
|
||
pageSize.value = value
|
||
pageIndex.value = 1
|
||
void loadThumbnailPage()
|
||
}
|
||
|
||
function errorText(error: unknown, fallback: string) {
|
||
return error instanceof Error && error.message ? error.message : fallback
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<section class="episode-editor-section episode-transcode-panel">
|
||
<el-collapse v-model="expanded">
|
||
<el-collapse-item name="transcode">
|
||
<template #title>
|
||
<div class="episode-transcode-panel__heading">
|
||
<span>{{ t('episodeTranscode.title') }}</span>
|
||
<EpisodeTranscodeStatusBadge
|
||
:visible="!!detail"
|
||
:status-label="statusLabel"
|
||
:status-type="statusType"
|
||
:progress="progress"
|
||
:progress-status="progressStatus"
|
||
:show-progress="!!detail && hasMedia"
|
||
/>
|
||
</div>
|
||
</template>
|
||
|
||
<EpisodeTranscodeStatusPanel
|
||
:detail="detail"
|
||
:view="statusView"
|
||
@retry="loadDetail()"
|
||
@retrigger="retrigger"
|
||
/>
|
||
<template v-if="showOutputs">
|
||
<EpisodeTranscodeRenditions :renditions="renditions" />
|
||
<EpisodeTranscodeFrames
|
||
:view="framesView"
|
||
@page-change="changePage"
|
||
@page-size-change="changePageSize"
|
||
@retry="loadThumbnailPage"
|
||
/>
|
||
</template>
|
||
</el-collapse-item>
|
||
</el-collapse>
|
||
</section>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.episode-transcode-panel :deep(.el-collapse) {
|
||
border: 0;
|
||
}
|
||
|
||
.episode-transcode-panel :deep(.el-collapse-item__header),
|
||
.episode-transcode-panel :deep(.el-collapse-item__wrap) {
|
||
background: transparent;
|
||
}
|
||
|
||
.episode-transcode-panel__heading {
|
||
display: flex;
|
||
width: 100%;
|
||
padding-right: 12px;
|
||
font-size: 15px;
|
||
font-weight: 650;
|
||
color: var(--el-text-color-primary);
|
||
align-items: center;
|
||
gap: 10px;
|
||
}
|
||
</style>
|