12 changed files with 1708 additions and 382 deletions

No files matched your search

+5
View File
@@ -1,4 +1,9 @@
#!/bin/sh #!/bin/sh
. "$(dirname "$0")/_/husky.sh" . "$(dirname "$0")/_/husky.sh"
if grep -qiE "(co-authored-by|generated with|assisted by).*(cursor|claude|codex|gpt|copilot|ai)" "$1"; then
echo "commit message contains AI attribution, remove it before committing" >&2
exit 1
fi
pnpm commitlint --edit "$1" pnpm commitlint --edit "$1"
+51
View File
@@ -527,5 +527,56 @@ export default {
inputPasswordDemo: { inputPasswordDemo: {
title: 'InputPassword', title: 'InputPassword',
inputPasswordDes: 'Secondary packaging of Input components based on ElementPlus' inputPasswordDes: 'Secondary packaging of Input components based on ElementPlus'
},
episodeBatch: {
title: 'Batch upload episodes',
entry: 'Batch upload',
selectFiles: 'Select videos',
pause: 'Pause',
resume: 'Resume',
retry: 'Retry',
retryAll: 'Retry all',
remove: 'Remove',
hint: 'Supports MP4 / MOV / MKV / WEBM, max 500MB each',
invalidType: 'Only MP4, MOV, MKV, WEBM are allowed',
oversize: 'File exceeds 500MB and was not queued',
overallProgress: 'Overall progress',
empty: 'No videos selected',
createEpisodes: 'Create episodes',
closeWhileUploading:
'Uploads are still in progress. Closing will stop dispatching new tasks. Continue?',
resumeTitle: 'Unfinished uploads found',
resumeConfirm:
'{count} unfinished upload(s) found. Resume? Re-select the same local files to match.',
resumeYes: 'Pick files to resume',
resumeNo: 'Discard',
resumeAbandoned: 'Unfinished uploads discarded',
resumeMatched: 'Matched {count} file(s) and resumed',
resumeUnmatched: '{count} session(s) could not match local files; pick again manually',
createTitle: 'Create episodes in batch',
createHint:
'Title uses the video file name; episode numbers are assigned automatically. Free vs paid follows the drama free-episode count; episodes beyond that are paid. You can edit them later in episode settings.',
colSource: 'Source file',
colNumber: 'Episode No.',
colTitle: 'Title',
colFree: 'Pricing',
labelFree: 'Free',
labelPaid: 'Paid',
colResult: 'Result',
missingVideo: 'Missing video URL',
createOk: 'Created',
createFailed: 'Create failed',
createAllSuccess: 'All episodes created',
createPartial: 'Some episodes failed; you can retry',
submitCreate: 'Create',
retryFailed: 'Retry failed',
status: {
waiting: 'Waiting',
uploading: 'Uploading',
merging: 'Merging',
done: 'Done',
failed: 'Failed',
paused: 'Paused'
}
} }
} }
+49
View File
@@ -521,5 +521,54 @@ export default {
inputPasswordDemo: { inputPasswordDemo: {
title: '密码输入框', title: '密码输入框',
inputPasswordDes: '基于 ElementPlus 的 Input 组件二次封装' inputPasswordDes: '基于 ElementPlus 的 Input 组件二次封装'
},
episodeBatch: {
title: '批量上传剧集',
entry: '批量上传',
selectFiles: '选择视频',
pause: '暂停',
resume: '继续',
retry: '重试',
retryAll: '全部重试',
remove: '移除',
hint: '支持 MP4 / MOV / MKV / WEBM,单文件不超过 500MB',
invalidType: '仅支持 MP4、MOV、MKV、WEBM',
oversize: '文件超过 500MB,未入队',
overallProgress: '总进度',
empty: '尚未选择视频',
createEpisodes: '批量创建分集',
closeWhileUploading: '仍有未完成上传,关闭后将暂停派发新任务,确定关闭吗?',
resumeTitle: '发现未完成上传',
resumeConfirm: '发现 {count} 个未完成上传,是否续传?请重新选择相同本地文件以匹配。',
resumeYes: '选择文件续传',
resumeNo: '放弃',
resumeAbandoned: '已放弃未完成上传',
resumeMatched: '已匹配 {count} 个文件并开始续传',
resumeUnmatched: '有 {count} 个会话无法匹配本地文件,请手动重选',
createTitle: '批量创建分集',
createHint:
'标题取视频文件名,集号自动顺延分配;免费集数由短剧管理中的「免费集数」配置决定,超出部分为收费集;创建后可在分集编辑中修改',
colSource: '源文件',
colNumber: '集号',
colTitle: '标题',
colFree: '收费类型',
labelFree: '免费',
labelPaid: '收费',
colResult: '结果',
missingVideo: '缺少视频地址',
createOk: '已创建',
createFailed: '创建失败',
createAllSuccess: '全部分集已创建',
createPartial: '部分分集创建失败,可重试',
submitCreate: '创建',
retryFailed: '重试失败项',
status: {
waiting: '等待中',
uploading: '上传中',
merging: '合并中',
done: '已完成',
failed: '失败',
paused: '已暂停'
}
} }
} }
@@ -1,95 +1,39 @@
<!-- Author: fruit.com.cn --> <!-- Author: fruit.com.cn -->
<script setup> <script setup>
import { computed, nextTick, onMounted, reactive, ref } from 'vue' import { computed, onMounted, ref } from 'vue'
import { useRoute } from 'vue-router' import { useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus' import { ElMessage, ElMessageBox } from 'element-plus'
import { AppIcons } from '@/constants/icons' import { AppIcons } from '@/constants/icons'
import { pageDramasApi } from '@/api/admin/drama' import { pageDramasApi } from '@/api/admin/drama'
import { import { deleteEpisodeApi, listEpisodesApi, sortEpisodesApi } from '@/api/admin/episode'
createEpisodeApi,
deleteEpisodeApi,
listEpisodesApi,
sortEpisodesApi,
updateEpisodeApi
} from '@/api/admin/episode'
import { useRbac } from '@/stores/rbac' import { useRbac } from '@/stores/rbac'
import { formatCount } from '@/utils/format' import { formatCount } from '@/utils/format'
import { statusClass } from '@/utils/status' import { statusClass } from '@/utils/status'
import { probeVideoDuration, uploadFile } from '@/utils/uploader' import { useI18n } from '@/hooks/web/useI18n'
import BatchUploadDialog from './upload/BatchUploadDialog.vue'
import EpisodeFormDialog from './upload/EpisodeFormDialog.vue'
import { listPendingSessions } from './upload/uploadSessions'
const route = useRoute() const route = useRoute()
const { can } = useRbac() const { can } = useRbac()
const { t } = useI18n()
const loading = ref(false) const loading = ref(false)
const dramas = ref([]) const dramas = ref([])
const episodes = ref([]) const episodes = ref([])
const selectedDramaId = ref(undefined) const selectedDramaId = ref(undefined)
const batchUploadVisible = ref(false)
const dialogVisible = ref(false)
const saving = ref(false)
const uploadingVideo = ref(false)
const videoPercent = ref(0)
const videoError = ref('')
const videoFileName = ref('')
const previewVisible = ref(false) const previewVisible = ref(false)
const previewEpisode = ref(null) const previewEpisode = ref(null)
const editingId = ref(null) const formDialogRef = ref()
const editingCommentCount = ref(0)
const formRef = ref()
const form = reactive({
number: 1,
title: '',
durationSec: 0,
videoUrl: '',
isFree: true,
unlockCoin: 0,
likeCount: 0,
status: 'draft'
})
const statusOptions = [
{ code: 'draft', text: '草稿' },
{ code: 'approved', text: '待发布' },
{ code: 'published', text: '已上线' },
{ code: 'offline', text: '已下线' }
]
const MAX_VIDEO_SIZE = 500 * 1024 * 1024
const VIDEO_EXTENSIONS = ['mp4', 'mov']
const formRules = {
number: [{ required: true, message: '请输入集号', trigger: 'change' }],
title: [{ required: true, message: '请输入剧集标题', trigger: 'blur' }],
videoUrl: [
{
validator: (_rule, value, callback) => {
if (form.status === 'published' && !value) {
callback(new Error('上线剧集必须上传视频'))
} else {
callback()
}
},
trigger: 'change'
}
]
}
const batchNextNumber = computed(() => nextNumber())
const selectedDrama = computed(() => dramas.value.find((item) => item.id === selectedDramaId.value)) const selectedDrama = computed(() => dramas.value.find((item) => item.id === selectedDramaId.value))
const videoDisplayName = computed( const existingEpisodeNumbers = computed(() =>
() => videoFileName.value || (form.videoUrl ? episodeBasename(form.videoUrl) : '尚未选择视频') episodes.value.map((item) => Number(item.number)).filter((n) => Number.isFinite(n))
) )
function episodeBasename(path) {
if (!path) return ''
const clean = path.split('?')[0].split('#')[0]
return decodeURIComponent(clean.split('/').pop() || clean)
}
async function resetValidation() {
await nextTick()
formRef.value?.clearValidate()
}
async function loadDramas() { async function loadDramas() {
const res = await pageDramasApi({ pageIndex: 1, pageSize: 200 }) const res = await pageDramasApi({ pageIndex: 1, pageSize: 200 })
dramas.value = res.data?.list || [] dramas.value = res.data?.list || []
@@ -120,133 +64,21 @@ function nextNumber() {
return episodes.value.reduce((max, item) => Math.max(max, item.number || 0), 0) + 1 return episodes.value.reduce((max, item) => Math.max(max, item.number || 0), 0) + 1
} }
function openBatchUpload() {
if (!selectedDramaId.value) return
batchUploadVisible.value = true
}
async function onBatchEpisodesCreated() {
await loadEpisodes()
}
function openCreate() { function openCreate() {
editingId.value = null formDialogRef.value?.openCreate()
Object.assign(form, {
number: nextNumber(),
title: '',
durationSec: 0,
videoUrl: '',
isFree: true,
unlockCoin: selectedDrama.value?.unlockCoin ?? 0,
likeCount: 0,
status: 'draft'
})
videoFileName.value = ''
videoError.value = ''
editingCommentCount.value = 0
dialogVisible.value = true
resetValidation()
} }
function openEdit(row) { function openEdit(row) {
editingId.value = row.id formDialogRef.value?.openEdit(row)
Object.assign(form, {
number: row.number,
title: row.title,
durationSec: row.durationSec || 0,
videoUrl: row.videoUrl || '',
isFree: !!row.isFree,
unlockCoin: row.unlockCoin || 0,
likeCount: row.likeCount ?? 0,
status: row.status
})
videoFileName.value = row.videoUrl ? episodeBasename(row.videoUrl) : ''
videoError.value = ''
editingCommentCount.value = row.commentCount ?? 0
dialogVisible.value = true
resetValidation()
}
async function saveEpisode() {
if (saving.value || uploadingVideo.value) return
try {
await formRef.value?.validate()
} catch {
return
}
const payload = {
dramaId: selectedDramaId.value,
number: form.number,
title: form.title.trim(),
durationSec: form.durationSec || 0,
videoUrl: form.videoUrl.trim(),
isFree: form.isFree,
unlockCoin: form.isFree ? 0 : form.unlockCoin || 0,
likeCount: form.likeCount,
status: form.status
}
saving.value = true
try {
if (editingId.value) {
await updateEpisodeApi(editingId.value, payload)
ElMessage.success('剧集已更新')
} else {
await createEpisodeApi(payload)
ElMessage.success('剧集已创建')
}
dialogVisible.value = false
await loadEpisodes()
} finally {
saving.value = false
}
}
function beforeVideoUpload(file) {
videoError.value = ''
const extension = String(file.name || '')
.split('.')
.pop()
?.toLowerCase()
if (!VIDEO_EXTENSIONS.includes(extension)) {
videoError.value = '视频仅支持 MP4 或 MOV 格式'
return false
}
if (file.size > MAX_VIDEO_SIZE) {
videoError.value = '视频大小不能超过 500MB'
return false
}
return true
}
/** 视频分片上传:先探测时长,再交给上传器,完成后回填地址与时长。 */
async function uploadVideo(options) {
uploadingVideo.value = true
videoPercent.value = 0
videoError.value = ''
try {
const duration = await probeVideoDuration(options.file)
const asset = await uploadFile(options.file, {
bizType: 'EPISODE',
durationSec: duration,
onProgress: (percent) => (videoPercent.value = percent)
})
form.videoUrl = asset.url
videoFileName.value = options.file.name
if (duration > 0) form.durationSec = duration
await resetValidation()
ElMessage.success('视频已上传')
} catch {
videoError.value = '视频上传失败,请检查网络后重试'
} finally {
uploadingVideo.value = false
}
}
function removeVideo() {
if (uploadingVideo.value) return
form.videoUrl = ''
videoFileName.value = ''
videoError.value = ''
resetValidation()
}
function beforeDialogClose(done) {
if (saving.value || uploadingVideo.value) {
ElMessage.warning(saving.value ? '正在保存,请稍候' : '视频正在上传,请稍候')
return
}
done()
} }
async function confirmDelete(row) { async function confirmDelete(row) {
@@ -304,11 +136,20 @@ async function move(index, offset) {
async function onDramaChange() { async function onDramaChange() {
await loadEpisodes() await loadEpisodes()
openBatchIfPendingSessions()
}
/** 进入页面或切换短剧时,若有 24h 内未完成上传则打开批量上传弹窗以提示续传。 */
function openBatchIfPendingSessions() {
if (!selectedDramaId.value || !can('episode:upload')) return
const pending = listPendingSessions(String(selectedDramaId.value))
if (pending.length) batchUploadVisible.value = true
} }
onMounted(async () => { onMounted(async () => {
await loadDramas() await loadDramas()
await loadEpisodes() await loadEpisodes()
openBatchIfPendingSessions()
}) })
</script> </script>
@@ -317,6 +158,9 @@ onMounted(async () => {
><div ><div
><div class="eyebrow">内容管理</div><h1>剧集管理</h1 ><div class="eyebrow">内容管理</div><h1>剧集管理</h1
><p>为短剧维护剧集信息、视频地址与播放顺序。</p></div ><p>为短剧维护剧集信息、视频地址与播放顺序。</p></div
><div class="page-heading-actions">
<el-button v-if="can('episode:upload')" :disabled="!selectedDramaId" @click="openBatchUpload"
><AppIcon :icon="AppIcons.upload" />{{ t('episodeBatch.entry') }}</el-button
><el-button ><el-button
v-if="can('episode:upload')" v-if="can('episode:upload')"
type="primary" type="primary"
@@ -325,6 +169,7 @@ onMounted(async () => {
@click="openCreate" @click="openCreate"
><AppIcon :icon="AppIcons.add" />新增剧集</el-button ><AppIcon :icon="AppIcons.add" />新增剧集</el-button
></div ></div
></div
> >
<section class="panel episode-panel"> <section class="panel episode-panel">
<div class="episode-context"> <div class="episode-context">
@@ -465,198 +310,12 @@ onMounted(async () => {
> >
</el-table> </el-table>
</section> </section>
<el-dialog <EpisodeFormDialog
v-model="dialogVisible" ref="formDialogRef"
:title="editingId ? '编辑剧集' : '新增剧集'" :drama-options="selectedDrama"
width="680px" :next-number="batchNextNumber"
class="episode-editor-dialog" @saved="loadEpisodes"
:close-on-click-modal="false"
:before-close="beforeDialogClose"
>
<el-form
ref="formRef"
:model="form"
:rules="formRules"
label-position="top"
class="episode-editor-form"
>
<section class="episode-editor-section">
<h3 class="episode-editor-section-title">基础信息</h3>
<div class="episode-editor-grid episode-editor-grid--two">
<el-form-item label="集号" prop="number">
<el-input-number
v-model="form.number"
class="episode-editor-number"
controls-position="right"
:min="1"
:max="999"
/> />
</el-form-item>
<el-form-item label="时长(秒)">
<el-input-number
v-model="form.durationSec"
class="episode-editor-number"
controls-position="right"
:min="0"
:max="9999"
/>
</el-form-item>
</div>
<el-form-item label="剧集标题" prop="title">
<el-input v-model="form.title" maxlength="100" placeholder="例如:命运的相遇" />
</el-form-item>
</section>
<section class="episode-editor-section">
<h3 class="episode-editor-section-title">视频文件</h3>
<el-form-item prop="videoUrl">
<div class="episode-video-editor">
<div class="episode-video-preview-card">
<video
v-if="form.videoUrl"
:src="form.videoUrl"
controls
preload="metadata"
class="episode-editor-video"
></video>
<el-upload
v-else
drag
class="episode-video-empty-upload"
:show-file-list="false"
accept=".mp4,.mov"
:before-upload="beforeVideoUpload"
:http-request="uploadVideo"
:disabled="uploadingVideo"
>
<div class="episode-video-empty-copy">
<AppIcon :icon="AppIcons.videoCameraFront" :size="30" />
<b>拖放或点击上传</b>
<span>MP4、MOV</span>
</div>
</el-upload>
<div v-if="uploadingVideo" class="episode-video-upload-mask">
<span>上传中</span>
<b>{{ videoPercent }}%</b>
</div>
</div>
<div class="episode-video-meta">
<div class="episode-video-status">
<el-tag :type="form.videoUrl ? 'success' : 'info'" effect="light">
{{ form.videoUrl ? '已上传' : '未上传' }}
</el-tag>
<span class="episode-video-filename" :title="videoDisplayName">
{{ videoDisplayName }}
</span>
</div>
<p class="episode-video-hint">支持 MP4、MOV,单个文件不超过 500MB。</p>
<el-alert
v-if="videoError"
:title="videoError"
type="error"
:closable="false"
show-icon
/>
<div class="episode-video-actions">
<el-upload
v-if="form.videoUrl"
:show-file-list="false"
accept=".mp4,.mov"
:before-upload="beforeVideoUpload"
:http-request="uploadVideo"
:disabled="uploadingVideo"
>
<el-button :loading="uploadingVideo" :disabled="uploadingVideo">
<AppIcon :icon="AppIcons.upload" />
更换视频
</el-button>
</el-upload>
<el-button
v-if="form.videoUrl"
type="danger"
:disabled="uploadingVideo"
@click="removeVideo"
>
移除
</el-button>
<a
v-if="form.videoUrl"
class="episode-video-link"
:href="form.videoUrl"
target="_blank"
rel="noopener noreferrer"
>
查看视频
</a>
</div>
</div>
</div>
</el-form-item>
</section>
<section class="episode-editor-section">
<h3 class="episode-editor-section-title">互动数据</h3>
<div class="episode-editor-grid episode-editor-grid--two">
<el-form-item label="本集点赞数">
<el-input-number
v-model="form.likeCount"
class="episode-editor-number"
controls-position="right"
:min="0"
:precision="0"
/>
</el-form-item>
<el-form-item label="本集评论数">
<div class="readonly-stat">{{ formatCount(editingCommentCount) }}</div>
<p class="readonly-stat-hint">按集实时统计,不可编辑</p>
</el-form-item>
</div>
</section>
<section class="episode-editor-section">
<h3 class="episode-editor-section-title">付费与状态</h3>
<div class="episode-editor-grid episode-editor-grid--three">
<el-form-item label="是否免费">
<el-switch v-model="form.isFree" />
</el-form-item>
<el-form-item v-show="!form.isFree" label="解锁金币">
<el-input-number
v-model="form.unlockCoin"
class="episode-editor-number"
controls-position="right"
:min="0"
:max="9999"
/>
</el-form-item>
<el-form-item label="状态">
<el-select v-model="form.status">
<el-option
v-for="item in statusOptions"
:key="item.code"
:label="item.text"
:value="item.code"
/>
</el-select>
</el-form-item>
</div>
</section>
</el-form>
<template #footer>
<el-button :disabled="saving || uploadingVideo" @click="dialogVisible = false">
取消
</el-button>
<el-button
type="primary"
:loading="saving"
:disabled="saving || uploadingVideo"
@click="saveEpisode"
>
保存
</el-button>
</template>
</el-dialog>
<el-dialog <el-dialog
v-model="previewVisible" v-model="previewVisible"
:title="`预览:第 ${previewEpisode?.number ?? ''} 集 ${previewEpisode?.title ?? ''}`" :title="`预览:第 ${previewEpisode?.number ?? ''} 集 ${previewEpisode?.title ?? ''}`"
@@ -672,4 +331,15 @@ onMounted(async () => {
></video> ></video>
<el-empty v-else description="该剧集还没有上传视频" /> <el-empty v-else description="该剧集还没有上传视频" />
</el-dialog> </el-dialog>
<BatchUploadDialog
v-if="selectedDramaId"
v-model="batchUploadVisible"
:drama-id="selectedDramaId"
:next-episode-number="batchNextNumber"
:default-unlock-coin="selectedDrama?.unlockCoin ?? 0"
:free-episodes="selectedDrama?.freeEpisodes ?? 0"
:existing-numbers="existingEpisodeNumbers"
@refreshed="loadEpisodes"
@created="onBatchEpisodesCreated"
/>
</template> </template>
@@ -0,0 +1,324 @@
<!-- Author: fruit.com.cn -->
<!-- 剧集批量上传弹窗:多选、队列、总进度、续传与批量建集入口 -->
<script setup lang="ts">
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { AppIcons } from '@/constants/icons'
import { useI18n } from '@/hooks/web/useI18n'
import CreateEpisodesDialog from './CreateEpisodesDialog.vue'
import UploadTaskRow from './UploadTaskRow.vue'
import { useUploadQueue } from './useUploadQueue'
import { MAX_VIDEO_SIZE, VIDEO_ACCEPT_ATTR, VIDEO_EXTENSIONS } from './videoLimit'
import type { UploadSessionRecord } from './uploadSessions'
const props = withDefaults(
defineProps<{
modelValue: boolean
dramaId: string | number
nextEpisodeNumber: number
defaultUnlockCoin: number
existingNumbers: number[]
/** 所属短剧免费集数;不传默认 0 */
freeEpisodes?: number
}>(),
{
freeEpisodes: 0
}
)
const emit = defineEmits<{
'update:modelValue': [value: boolean]
created: []
refreshed: []
}>()
const { t } = useI18n()
const visible = computed({
get: () => props.modelValue,
set: (value: boolean) => emit('update:modelValue', value)
})
const dramaKey = computed(() => String(props.dramaId || ''))
const fileInputRef = ref<HTMLInputElement | null>(null)
const resumeFileInputRef = ref<HTMLInputElement | null>(null)
const rejectedNames = ref<string[]>([])
const pendingSessions = ref<UploadSessionRecord[]>([])
const createVisible = ref(false)
const resumePromptShown = ref(false)
const queue = useUploadQueue({ getDramaId: () => dramaKey.value })
watch(
() => props.dramaId,
() => {
queue.clear()
resumePromptShown.value = false
}
)
const canCreateEpisodes = computed(() => queue.doneItems.value.length > 0)
function extensionOf(fileName: string): string {
const parts = String(fileName || '').split('.')
return (parts.pop() || '').toLowerCase()
}
function validateFile(file: File): string {
if (!VIDEO_EXTENSIONS.includes(extensionOf(file.name))) {
return t('episodeBatch.invalidType')
}
if (file.size > MAX_VIDEO_SIZE) {
return t('episodeBatch.oversize')
}
return ''
}
function onPickFiles(event: Event): void {
const input = event.target as HTMLInputElement
const files = Array.from(input.files || [])
input.value = ''
if (!files.length) return
const accepted: File[] = []
const rejected: string[] = []
for (const file of files) {
const reason = validateFile(file)
if (reason) {
rejected.push(`${file.name}: ${reason}`)
} else {
accepted.push(file)
}
}
rejectedNames.value = rejected
if (!accepted.length) return
queue.enqueue(accepted)
queue.start()
}
function openFilePicker(): void {
fileInputRef.value?.click()
}
function onBeforeUnload(event: BeforeUnloadEvent): void {
if (!queue.hasActiveUpload.value) return
event.preventDefault()
event.returnValue = ''
}
watch(
() => queue.hasActiveUpload.value,
(active) => {
if (active) {
window.addEventListener('beforeunload', onBeforeUnload)
} else {
window.removeEventListener('beforeunload', onBeforeUnload)
}
}
)
onBeforeUnmount(() => {
window.removeEventListener('beforeunload', onBeforeUnload)
})
async function promptResumeIfNeeded(): Promise<void> {
if (resumePromptShown.value || !dramaKey.value) return
const sessions = queue.getPendingSessions()
if (!sessions.length) return
resumePromptShown.value = true
pendingSessions.value = sessions
try {
await ElMessageBox.confirm(
t('episodeBatch.resumeConfirm', { count: sessions.length }),
t('episodeBatch.resumeTitle'),
{
confirmButtonText: t('episodeBatch.resumeYes'),
cancelButtonText: t('episodeBatch.resumeNo'),
type: 'info'
}
)
resumeFileInputRef.value?.click()
} catch {
queue.abandonSessions(sessions)
pendingSessions.value = []
ElMessage.info(t('episodeBatch.resumeAbandoned'))
}
}
async function onResumeFiles(event: Event): Promise<void> {
const input = event.target as HTMLInputElement
const files = Array.from(input.files || [])
input.value = ''
const sessions = pendingSessions.value
if (!files.length || !sessions.length) return
const { matched, unmatched } = await queue.resumeFromSessions(sessions, files)
pendingSessions.value = []
if (unmatched.length) {
ElMessage.warning(t('episodeBatch.resumeUnmatched', { count: unmatched.length }))
}
if (matched.length) {
ElMessage.success(t('episodeBatch.resumeMatched', { count: matched.length }))
queue.start()
}
}
function beforeClose(done: () => void): void {
if (!queue.hasActiveUpload.value) {
done()
return
}
ElMessageBox.confirm(t('episodeBatch.closeWhileUploading'), t('common.reminder'), {
type: 'warning',
confirmButtonText: t('common.ok'),
cancelButtonText: t('common.cancel')
})
.then(() => {
queue.pause()
done()
})
.catch(() => undefined)
}
watch(visible, (open) => {
if (open) {
void promptResumeIfNeeded()
}
})
function openCreateDialog(): void {
if (!canCreateEpisodes.value) return
createVisible.value = true
}
function onEpisodesCreated(): void {
createVisible.value = false
emit('created')
visible.value = false
queue.clear()
}
</script>
<template>
<el-dialog
v-model="visible"
:title="t('episodeBatch.title')"
width="720px"
class="batch-upload-dialog"
:close-on-click-modal="false"
:before-close="beforeClose"
>
<input
ref="fileInputRef"
type="file"
multiple
hidden
:accept="VIDEO_ACCEPT_ATTR"
@change="onPickFiles"
/>
<input
ref="resumeFileInputRef"
type="file"
multiple
hidden
:accept="VIDEO_ACCEPT_ATTR"
@change="onResumeFiles"
/>
<div class="batch-upload-toolbar">
<el-button type="primary" @click="openFilePicker">
<AppIcon :icon="AppIcons.upload" />
{{ t('episodeBatch.selectFiles') }}
</el-button>
<el-button :disabled="!queue.items.value.length" @click="queue.pause()">
{{ t('episodeBatch.pause') }}
</el-button>
<el-button :disabled="!queue.items.value.length" @click="queue.start()">
{{ t('episodeBatch.resume') }}
</el-button>
<el-button
:disabled="!queue.items.value.some((item) => item.status === 'failed')"
@click="queue.retryAll()"
>
{{ t('episodeBatch.retryAll') }}
</el-button>
</div>
<p class="batch-upload-hint">{{ t('episodeBatch.hint') }}</p>
<div v-if="rejectedNames.length" class="batch-upload-rejected">
<p v-for="name in rejectedNames" :key="name">{{ name }}</p>
</div>
<div v-if="queue.items.value.length" class="batch-upload-overall">
<span>{{ t('episodeBatch.overallProgress') }}</span>
<el-progress :percentage="queue.overallPercent.value" :stroke-width="12" />
</div>
<div class="batch-upload-list">
<UploadTaskRow
v-for="item in queue.items.value"
:key="item.id"
:item="item"
@retry="queue.retry"
@remove="queue.remove"
/>
<el-empty v-if="!queue.items.value.length" :description="t('episodeBatch.empty')" />
</div>
<template #footer>
<el-button @click="visible = false">{{ t('common.cancel') }}</el-button>
<el-button type="primary" :disabled="!canCreateEpisodes" @click="openCreateDialog">
{{ t('episodeBatch.createEpisodes') }}
</el-button>
</template>
</el-dialog>
<CreateEpisodesDialog
v-model="createVisible"
:drama-id="dramaId"
:done-items="queue.doneItems.value"
:next-episode-number="nextEpisodeNumber"
:default-unlock-coin="defaultUnlockCoin"
:free-episodes="freeEpisodes"
:existing-numbers="existingNumbers"
@settled="emit('refreshed')"
@created="onEpisodesCreated"
/>
</template>
<style scoped>
.batch-upload-toolbar {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 8px;
}
.batch-upload-hint {
margin: 0 0 12px;
color: var(--el-text-color-secondary);
font-size: 13px;
}
.batch-upload-rejected {
margin-bottom: 12px;
padding: 8px 12px;
border-radius: 6px;
background: color-mix(in srgb, var(--el-color-danger) 8%, transparent);
color: var(--el-color-danger);
font-size: 12px;
}
.batch-upload-rejected p {
margin: 0;
}
.batch-upload-overall {
margin-bottom: 12px;
}
.batch-upload-list {
max-height: 360px;
overflow: auto;
}
</style>
@@ -0,0 +1,263 @@
<!-- Author: fruit.com.cn -->
<!-- 批量创建分集确认清单:标题取文件名,集号自动顺延,失败可重试 -->
<script setup lang="ts">
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { createEpisodeApi } from '@/api/admin/episode'
import { useI18n } from '@/hooks/web/useI18n'
import type { UploadQueueItem } from './useUploadQueue'
const props = withDefaults(
defineProps<{
modelValue: boolean
dramaId: string | number
doneItems: UploadQueueItem[]
nextEpisodeNumber: number
defaultUnlockCoin: number
existingNumbers: number[]
/** 所属短剧免费集数;不传默认 0 */
freeEpisodes?: number
}>(),
{
freeEpisodes: 0
}
)
const emit = defineEmits<{
'update:modelValue': [value: boolean]
settled: []
created: []
}>()
const { t } = useI18n()
interface EpisodeDraft {
key: string
sourceName: string
number: number
title: string
isFree: boolean
videoUrl: string
durationSec: number
saved: boolean
error: string
}
const PUBLISHED_STATUS = 'published'
const rows = ref<EpisodeDraft[]>([])
const submitting = ref(false)
function baseName(fileName: string): string {
const dot = fileName.lastIndexOf('.')
if (dot <= 0) return fileName
return fileName.slice(0, dot)
}
function compareNatural(left: string, right: string): number {
return left.localeCompare(right, undefined, { numeric: true, sensitivity: 'base' })
}
/** 从起点起跳过已占用集号,返回下一个可用值。 */
function nextFreeNumber(start: number, used: Set<number>): number {
let candidate = start
while (used.has(candidate)) {
candidate += 1
}
return candidate
}
function buildRows(): void {
const sorted = [...props.doneItems].sort((left, right) =>
compareNatural(baseName(left.fileName), baseName(right.fileName))
)
const used = new Set(props.existingNumbers)
let cursor = props.nextEpisodeNumber
const freeLimit = props.freeEpisodes
rows.value = sorted.map((item) => {
const number = nextFreeNumber(cursor, used)
used.add(number)
cursor = number + 1
const isFree = number <= freeLimit
return {
key: item.id,
sourceName: item.fileName,
number,
title: baseName(item.fileName),
isFree,
videoUrl: item.url || '',
durationSec: item.durationSec || 0,
saved: false,
error: ''
}
})
}
watch(
() => props.modelValue,
(open) => {
if (open) buildRows()
}
)
function errorText(error: unknown): string {
if (error instanceof Error && error.message) return error.message
return t('episodeBatch.createFailed')
}
async function createOne(row: EpisodeDraft): Promise<void> {
if (!row.videoUrl) {
row.error = t('episodeBatch.missingVideo')
return
}
try {
await createEpisodeApi({
dramaId: props.dramaId as unknown as number,
number: row.number,
title: baseName(row.sourceName),
durationSec: row.durationSec,
videoUrl: row.videoUrl,
isFree: row.isFree,
unlockCoin: row.isFree ? 0 : props.defaultUnlockCoin,
status: PUBLISHED_STATUS
})
row.saved = true
row.error = ''
} catch (error) {
row.error = errorText(error)
}
}
async function submit(): Promise<void> {
if (submitting.value) return
submitting.value = true
try {
for (const row of rows.value) {
if (!row.saved) await createOne(row)
}
} finally {
submitting.value = false
}
emit('settled')
if (rows.value.every((row) => row.saved)) {
ElMessage.success(t('episodeBatch.createAllSuccess'))
emit('created')
return
}
ElMessage.warning(t('episodeBatch.createPartial'))
}
function closeDialog(): void {
emit('update:modelValue', false)
}
function hasFailed(): boolean {
return rows.value.some((row) => !row.saved && !!row.error)
}
</script>
<template>
<el-dialog
:model-value="modelValue"
:title="t('episodeBatch.createTitle')"
width="760px"
:close-on-click-modal="false"
@update:model-value="emit('update:modelValue', $event)"
>
<p class="create-episode-hint">{{ t('episodeBatch.createHint') }}</p>
<div class="create-episode-head">
<span>{{ t('episodeBatch.colSource') }}</span>
<span>{{ t('episodeBatch.colNumber') }}</span>
<span>{{ t('episodeBatch.colTitle') }}</span>
<span>{{ t('episodeBatch.colFree') }}</span>
<span>{{ t('episodeBatch.colResult') }}</span>
</div>
<div class="create-episode-list">
<div
v-for="row in rows"
:key="row.key"
class="create-episode-row"
:class="{ 'is-saved': row.saved }"
>
<b class="create-episode-source" :title="row.sourceName">{{ row.sourceName }}</b>
<span class="create-episode-number">{{ row.number }}</span>
<span class="create-episode-title" :title="row.title">{{ row.title }}</span>
<span class="create-episode-free" :class="row.isFree ? 'is-free' : 'is-paid'">
{{ row.isFree ? t('episodeBatch.labelFree') : t('episodeBatch.labelPaid') }}
</span>
<span class="create-episode-result">
{{ row.saved ? t('episodeBatch.createOk') : row.error }}
</span>
</div>
</div>
<template #footer>
<el-button @click="closeDialog">{{ t('common.cancel') }}</el-button>
<el-button type="primary" :loading="submitting" @click="submit">
{{ hasFailed() ? t('episodeBatch.retryFailed') : t('episodeBatch.submitCreate') }}
</el-button>
</template>
</el-dialog>
</template>
<style scoped>
.create-episode-hint {
margin: 0 0 12px;
color: var(--el-text-color-secondary);
font-size: 13px;
line-height: 1.5;
}
.create-episode-head,
.create-episode-row {
display: grid;
grid-template-columns: minmax(120px, 1.2fr) 72px minmax(140px, 1.4fr) 56px minmax(80px, 0.8fr);
gap: 8px;
align-items: center;
}
.create-episode-free {
font-size: 12px;
}
.create-episode-free.is-free {
color: var(--el-color-success);
}
.create-episode-free.is-paid {
color: var(--el-color-warning);
}
.create-episode-head {
margin-bottom: 8px;
color: var(--el-text-color-secondary);
font-size: 12px;
}
.create-episode-list {
display: flex;
flex-direction: column;
gap: 10px;
max-height: 420px;
overflow: auto;
}
.create-episode-source,
.create-episode-title {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.create-episode-number {
font-variant-numeric: tabular-nums;
}
.create-episode-result {
color: var(--el-text-color-secondary);
font-size: 12px;
}
.create-episode-row.is-saved .create-episode-result {
color: var(--el-color-success);
}
</style>
@@ -0,0 +1,280 @@
<!-- Author: fruit.com.cn -->
<!-- 剧集新建/编辑弹窗:表单校验、单集视频上传与保存 -->
<script setup>
import { nextTick, reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { createEpisodeApi, updateEpisodeApi } from '@/api/admin/episode'
import { formatCount } from '@/utils/format'
import EpisodeVideoUploader from './EpisodeVideoUploader.vue'
const props = defineProps({
dramaOptions: { type: Object, default: null },
nextNumber: { type: Number, default: 1 },
row: { type: Object, default: null }
})
const emit = defineEmits(['saved'])
const dialogVisible = ref(false)
const saving = ref(false)
const uploadingVideo = ref(false)
const videoFileName = ref('')
const editingId = ref(null)
const editingCommentCount = ref(0)
const formRef = ref()
const uploaderRef = ref()
const form = reactive({
number: 1,
title: '',
durationSec: 0,
videoUrl: '',
isFree: true,
unlockCoin: 0,
likeCount: 0,
status: 'draft'
})
const statusOptions = [
{ code: 'draft', text: '草稿' },
{ code: 'approved', text: '待发布' },
{ code: 'published', text: '已上线' },
{ code: 'offline', text: '已下线' }
]
const formRules = {
number: [{ required: true, message: '请输入集号', trigger: 'change' }],
title: [{ required: true, message: '请输入剧集标题', trigger: 'blur' }],
videoUrl: [
{
validator: (_rule, value, callback) => {
if (form.status === 'published' && !value) {
callback(new Error('上线剧集必须上传视频'))
} else {
callback()
}
},
trigger: 'change'
}
]
}
function episodeBasename(path) {
if (!path) return ''
const clean = path.split('?')[0].split('#')[0]
return decodeURIComponent(clean.split('/').pop() || clean)
}
async function resetValidation() {
await nextTick()
formRef.value?.clearValidate()
}
function openCreate() {
editingId.value = null
Object.assign(form, {
number: props.nextNumber,
title: '',
durationSec: 0,
videoUrl: '',
isFree: true,
unlockCoin: props.dramaOptions?.unlockCoin ?? 0,
likeCount: 0,
status: 'draft'
})
videoFileName.value = ''
uploaderRef.value?.clearError()
editingCommentCount.value = 0
dialogVisible.value = true
resetValidation()
}
function openEdit(row) {
const target = row || props.row
if (!target) return
editingId.value = target.id
Object.assign(form, {
number: target.number,
title: target.title,
durationSec: target.durationSec || 0,
videoUrl: target.videoUrl || '',
isFree: !!target.isFree,
unlockCoin: target.unlockCoin || 0,
likeCount: target.likeCount ?? 0,
status: target.status
})
videoFileName.value = target.videoUrl ? episodeBasename(target.videoUrl) : ''
uploaderRef.value?.clearError()
editingCommentCount.value = target.commentCount ?? 0
dialogVisible.value = true
resetValidation()
}
async function saveEpisode() {
if (saving.value || uploadingVideo.value) return
try {
await formRef.value?.validate()
} catch {
return
}
const dramaId = props.dramaOptions?.id
if (!dramaId) return
const payload = {
dramaId,
number: form.number,
title: form.title.trim(),
durationSec: form.durationSec || 0,
videoUrl: form.videoUrl.trim(),
isFree: form.isFree,
unlockCoin: form.isFree ? 0 : form.unlockCoin || 0,
likeCount: form.likeCount,
status: form.status
}
saving.value = true
try {
if (editingId.value) {
await updateEpisodeApi(editingId.value, payload)
ElMessage.success('剧集已更新')
} else {
await createEpisodeApi(payload)
ElMessage.success('剧集已创建')
}
dialogVisible.value = false
emit('saved')
} finally {
saving.value = false
}
}
function beforeDialogClose(done) {
if (saving.value || uploadingVideo.value) {
ElMessage.warning(saving.value ? '正在保存,请稍候' : '视频正在上传,请稍候')
return
}
done()
}
defineExpose({ openCreate, openEdit })
</script>
<template>
<el-dialog
v-model="dialogVisible"
:title="editingId ? '编辑剧集' : '新增剧集'"
width="680px"
class="episode-editor-dialog"
:close-on-click-modal="false"
:before-close="beforeDialogClose"
>
<el-form
ref="formRef"
:model="form"
:rules="formRules"
label-position="top"
class="episode-editor-form"
>
<section class="episode-editor-section">
<h3 class="episode-editor-section-title">基础信息</h3>
<div class="episode-editor-grid episode-editor-grid--two">
<el-form-item label="集号" prop="number">
<el-input-number
v-model="form.number"
class="episode-editor-number"
controls-position="right"
:min="1"
:max="999"
/>
</el-form-item>
<el-form-item label="时长(秒)">
<el-input-number
v-model="form.durationSec"
class="episode-editor-number"
controls-position="right"
:min="0"
:max="9999"
/>
</el-form-item>
</div>
<el-form-item label="剧集标题" prop="title">
<el-input v-model="form.title" maxlength="100" placeholder="例如:命运的相遇" />
</el-form-item>
</section>
<section class="episode-editor-section">
<h3 class="episode-editor-section-title">视频文件</h3>
<el-form-item prop="videoUrl">
<EpisodeVideoUploader
ref="uploaderRef"
v-model:video-url="form.videoUrl"
v-model:duration-sec="form.durationSec"
v-model:file-name="videoFileName"
v-model:uploading="uploadingVideo"
@change="resetValidation"
/>
</el-form-item>
</section>
<section class="episode-editor-section">
<h3 class="episode-editor-section-title">互动数据</h3>
<div class="episode-editor-grid episode-editor-grid--two">
<el-form-item label="本集点赞数">
<el-input-number
v-model="form.likeCount"
class="episode-editor-number"
controls-position="right"
:min="0"
:precision="0"
/>
</el-form-item>
<el-form-item label="本集评论数">
<div class="readonly-stat">{{ formatCount(editingCommentCount) }}</div>
<p class="readonly-stat-hint">按集实时统计,不可编辑</p>
</el-form-item>
</div>
</section>
<section class="episode-editor-section">
<h3 class="episode-editor-section-title">付费与状态</h3>
<div class="episode-editor-grid episode-editor-grid--three">
<el-form-item label="是否免费">
<el-switch v-model="form.isFree" />
</el-form-item>
<el-form-item v-show="!form.isFree" label="解锁金币">
<el-input-number
v-model="form.unlockCoin"
class="episode-editor-number"
controls-position="right"
:min="0"
:max="9999"
/>
</el-form-item>
<el-form-item label="状态">
<el-select v-model="form.status">
<el-option
v-for="item in statusOptions"
:key="item.code"
:label="item.text"
:value="item.code"
/>
</el-select>
</el-form-item>
</div>
</section>
</el-form>
<template #footer>
<el-button :disabled="saving || uploadingVideo" @click="dialogVisible = false">
取消
</el-button>
<el-button
type="primary"
:loading="saving"
:disabled="saving || uploadingVideo"
@click="saveEpisode"
>
保存
</el-button>
</template>
</el-dialog>
</template>
@@ -0,0 +1,172 @@
<!-- Author: fruit.com.cn -->
<!-- 单集视频上传:格式/大小校验、分片上传、预览与移除 -->
<script setup>
import { computed, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { AppIcons } from '@/constants/icons'
import { useI18n } from '@/hooks/web/useI18n'
import { probeVideoDuration, uploadFile } from '@/utils/uploader'
import { MAX_VIDEO_SIZE, VIDEO_ACCEPT_ATTR, VIDEO_EXTENSIONS } from './videoLimit'
const props = defineProps({
videoUrl: { type: String, default: '' },
durationSec: { type: Number, default: 0 },
fileName: { type: String, default: '' }
})
const emit = defineEmits([
'update:videoUrl',
'update:durationSec',
'update:fileName',
'update:uploading',
'change'
])
const { t } = useI18n()
const uploadingVideo = ref(false)
const videoPercent = ref(0)
const videoError = ref('')
const videoDisplayName = computed(
() => props.fileName || (props.videoUrl ? episodeBasename(props.videoUrl) : '尚未选择视频')
)
watch(uploadingVideo, (value) => emit('update:uploading', value))
function episodeBasename(path) {
if (!path) return ''
const clean = path.split('?')[0].split('#')[0]
return decodeURIComponent(clean.split('/').pop() || clean)
}
function clearError() {
videoError.value = ''
}
function beforeVideoUpload(file) {
videoError.value = ''
const extension = String(file.name || '')
.split('.')
.pop()
?.toLowerCase()
if (!VIDEO_EXTENSIONS.includes(extension)) {
videoError.value = t('episodeBatch.invalidType')
return false
}
if (file.size > MAX_VIDEO_SIZE) {
videoError.value = '视频大小不能超过 500MB'
return false
}
return true
}
/** 视频分片上传:先探测时长,再交给上传器,完成后回填地址与时长。 */
async function uploadVideo(options) {
uploadingVideo.value = true
videoPercent.value = 0
videoError.value = ''
try {
const duration = await probeVideoDuration(options.file)
const asset = await uploadFile(options.file, {
bizType: 'EPISODE',
durationSec: duration,
onProgress: (percent) => (videoPercent.value = percent)
})
emit('update:videoUrl', asset.url)
emit('update:fileName', options.file.name)
if (duration > 0) emit('update:durationSec', duration)
emit('change')
ElMessage.success('视频已上传')
} catch {
videoError.value = '视频上传失败,请检查网络后重试'
} finally {
uploadingVideo.value = false
}
}
function removeVideo() {
if (uploadingVideo.value) return
emit('update:videoUrl', '')
emit('update:fileName', '')
videoError.value = ''
emit('change')
}
defineExpose({ clearError, uploadingVideo })
</script>
<template>
<div class="episode-video-editor">
<div class="episode-video-preview-card">
<video
v-if="videoUrl"
:src="videoUrl"
controls
preload="metadata"
class="episode-editor-video"
></video>
<el-upload
v-else
drag
class="episode-video-empty-upload"
:show-file-list="false"
:accept="VIDEO_ACCEPT_ATTR"
:before-upload="beforeVideoUpload"
:http-request="uploadVideo"
:disabled="uploadingVideo"
>
<div class="episode-video-empty-copy">
<AppIcon :icon="AppIcons.videoCameraFront" :size="30" />
<b>拖放或点击上传</b>
<span>MP4、MOV、MKV、WEBM</span>
</div>
</el-upload>
<div v-if="uploadingVideo" class="episode-video-upload-mask">
<span>上传中</span>
<b>{{ videoPercent }}%</b>
</div>
</div>
<div class="episode-video-meta">
<div class="episode-video-status">
<el-tag :type="videoUrl ? 'success' : 'info'" effect="light">
{{ videoUrl ? '已上传' : '未上传' }}
</el-tag>
<span class="episode-video-filename" :title="videoDisplayName">
{{ videoDisplayName }}
</span>
</div>
<p class="episode-video-hint">{{ t('episodeBatch.hint') }}</p>
<el-alert v-if="videoError" :title="videoError" type="error" :closable="false" show-icon />
<div class="episode-video-actions">
<el-upload
v-if="videoUrl"
:show-file-list="false"
:accept="VIDEO_ACCEPT_ATTR"
:before-upload="beforeVideoUpload"
:http-request="uploadVideo"
:disabled="uploadingVideo"
>
<el-button :loading="uploadingVideo" :disabled="uploadingVideo">
<AppIcon :icon="AppIcons.upload" />
更换视频
</el-button>
</el-upload>
<el-button v-if="videoUrl" type="danger" :disabled="uploadingVideo" @click="removeVideo">
移除
</el-button>
<a
v-if="videoUrl"
class="episode-video-link"
:href="videoUrl"
target="_blank"
rel="noopener noreferrer"
>
查看视频
</a>
</div>
</div>
</div>
</template>
@@ -0,0 +1,121 @@
<!-- Author: fruit.com.cn -->
<!-- 批量上传队列单行:文件名、大小、进度、状态与操作 -->
<script setup lang="ts">
import { computed } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import type { UploadQueueItem } from './useUploadQueue'
const props = defineProps<{
item: UploadQueueItem
}>()
const emit = defineEmits<{
retry: [id: string]
remove: [id: string]
}>()
const { t } = useI18n()
const BYTES_PER_KB = 1024
const BYTES_PER_MB = 1024 * 1024
const sizeLabel = computed(() => {
const size = props.item.fileSize
if (size >= BYTES_PER_MB) {
return `${(size / BYTES_PER_MB).toFixed(1)} MB`
}
return `${Math.max(1, Math.round(size / BYTES_PER_KB))} KB`
})
const statusLabel = computed(() => t(`episodeBatch.status.${props.item.status}`))
const statusType = computed(() => {
switch (props.item.status) {
case 'done':
return 'success'
case 'failed':
return 'danger'
case 'paused':
return 'warning'
case 'merging':
case 'uploading':
return ''
default:
return 'info'
}
})
const canRetry = computed(() => props.item.status === 'failed')
const canRemove = computed(
() => props.item.status !== 'uploading' && props.item.status !== 'merging'
)
</script>
<template>
<div class="upload-task-row" :class="{ 'is-failed': item.status === 'failed' }">
<div class="upload-task-row__meta">
<b class="upload-task-row__name" :title="item.fileName">{{ item.fileName }}</b>
<small>{{ sizeLabel }}</small>
<el-tag :type="statusType" effect="light" size="small">{{ statusLabel }}</el-tag>
</div>
<el-progress
:percentage="item.percent"
:status="
item.status === 'failed' ? 'exception' : item.status === 'done' ? 'success' : undefined
"
:stroke-width="10"
/>
<p v-if="item.error" class="upload-task-row__error">{{ item.error }}</p>
<div class="upload-task-row__actions">
<el-button v-if="canRetry" size="small" type="primary" @click="emit('retry', item.id)">
{{ t('episodeBatch.retry') }}
</el-button>
<el-button v-if="canRemove" size="small" type="danger" text @click="emit('remove', item.id)">
{{ t('episodeBatch.remove') }}
</el-button>
</div>
</div>
</template>
<style scoped>
.upload-task-row {
padding: 12px 0;
border-bottom: 1px solid var(--el-border-color-lighter);
}
.upload-task-row__meta {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
}
.upload-task-row__name {
flex: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.upload-task-row__error {
margin: 6px 0 0;
color: var(--el-color-danger);
font-size: 12px;
}
.upload-task-row__actions {
margin-top: 8px;
display: flex;
gap: 8px;
justify-content: flex-end;
margin-left: auto;
}
.upload-task-row.is-failed {
background: color-mix(in srgb, var(--el-color-danger) 6%, transparent);
border-radius: 6px;
padding-left: 8px;
padding-right: 8px;
}
</style>
@@ -0,0 +1,72 @@
// Author: fruit.com.cn
/** 剧集批量上传断点会话:localStorage 读写与清理 */
export const UPLOAD_SESSION_KEY = 'reel_upload_sessions'
export const UPLOAD_SESSION_TTL_MS = 24 * 60 * 60 * 1000
export interface UploadSessionRecord {
uploadId: string
fileName: string
fileSize: number
lastModified: number
dramaId: string
bizType: string
createdAt: number
}
/** 读取全部会话,解析失败时返回空数组。 */
export function loadUploadSessions(): UploadSessionRecord[] {
try {
const raw = localStorage.getItem(UPLOAD_SESSION_KEY)
if (!raw) return []
const parsed = JSON.parse(raw)
return Array.isArray(parsed) ? (parsed as UploadSessionRecord[]) : []
} catch {
return []
}
}
/** 覆盖写入全部会话。 */
export function saveUploadSessions(sessions: UploadSessionRecord[]): void {
localStorage.setItem(UPLOAD_SESSION_KEY, JSON.stringify(sessions))
}
/** 按 dramaId 过滤且未超过 TTL 的未完成会话。 */
export function listPendingSessions(dramaId: string, now = Date.now()): UploadSessionRecord[] {
const dramaKey = String(dramaId)
return loadUploadSessions().filter(
(item) =>
String(item.dramaId) === dramaKey &&
now - Number(item.createdAt || 0) <= UPLOAD_SESSION_TTL_MS
)
}
/** 新增或按 uploadId 覆盖一条会话。 */
export function upsertUploadSession(record: UploadSessionRecord): void {
const next = loadUploadSessions().filter((item) => item.uploadId !== record.uploadId)
next.push(record)
saveUploadSessions(next)
}
/** 按 uploadId 移除会话。 */
export function removeUploadSession(uploadId: string): void {
if (!uploadId) return
saveUploadSessions(loadUploadSessions().filter((item) => item.uploadId !== uploadId))
}
/** 批量移除会话。 */
export function removeUploadSessions(uploadIds: string[]): void {
if (!uploadIds.length) return
const idSet = new Set(uploadIds)
saveUploadSessions(loadUploadSessions().filter((item) => !idSet.has(item.uploadId)))
}
/** 用文件名 + size + lastModified 匹配本地文件。 */
export function matchFileForSession(session: UploadSessionRecord, files: File[]): File | undefined {
return files.find(
(file) =>
file.name === session.fileName &&
file.size === session.fileSize &&
file.lastModified === session.lastModified
)
}
@@ -0,0 +1,310 @@
// Author: fruit.com.cn
/** 剧集批量上传队列:文件并发、进度加权、断点会话、失败隔离 */
import { computed, ref } from 'vue'
import { getUploadProgressApi } from '@/api/admin/media'
import { probeVideoDuration, resumeUpload, uploadFile } from '@/utils/uploader'
import {
listPendingSessions,
matchFileForSession,
removeUploadSession,
type UploadSessionRecord,
upsertUploadSession
} from './uploadSessions'
export type UploadItemStatus = 'waiting' | 'uploading' | 'merging' | 'done' | 'failed' | 'paused'
export interface UploadQueueItem {
id: string
file: File
fileName: string
fileSize: number
lastModified: number
percent: number
status: UploadItemStatus
error: string
uploadId: string
assetId?: number
url?: string
durationSec?: number
}
export interface UseUploadQueueOptions {
/** 当前短剧 id(调用时读取,避免切换剧后闭包过期) */
getDramaId: () => string
bizType?: string
}
const FILE_CONCURRENCY = 2
const CHUNK_CONCURRENCY = 3
const DEFAULT_BIZ_TYPE = 'EPISODE'
const MERGING_PERCENT = 95
const MAX_PERCENT = 100
let queueSeq = 0
function nextItemId(): string {
queueSeq += 1
return `uq-${Date.now()}-${queueSeq}`
}
function extractErrorMessage(error: unknown): string {
if (error instanceof Error && error.message) return error.message
return String(error || 'upload failed')
}
function isHttpNotFound(error: unknown): boolean {
const status = (error as { response?: { status?: number } })?.response?.status
return status === 404
}
/**
* 创建上传队列控制器。
* @param options dramaId 与可选 bizType
*/
export function useUploadQueue(options: UseUploadQueueOptions) {
const items = ref<UploadQueueItem[]>([])
const paused = ref(false)
const bizType = options.bizType || DEFAULT_BIZ_TYPE
let running = 0
const overallPercent = computed(() => {
const list = items.value
if (!list.length) return 0
const totalBytes = list.reduce((sum, item) => sum + item.fileSize, 0)
if (totalBytes <= 0) return 0
const weighted = list.reduce((sum, item) => sum + item.fileSize * item.percent, 0)
return Math.min(MAX_PERCENT, Math.round(weighted / totalBytes))
})
const hasActiveUpload = computed(() =>
items.value.some(
(item) =>
item.status === 'waiting' ||
item.status === 'uploading' ||
item.status === 'merging' ||
item.status === 'paused'
)
)
const doneItems = computed(() => items.value.filter((item) => item.status === 'done'))
function persistSession(item: UploadQueueItem, uploadId: string): void {
item.uploadId = uploadId
upsertUploadSession({
uploadId,
fileName: item.fileName,
fileSize: item.fileSize,
lastModified: item.lastModified,
dramaId: String(options.getDramaId()),
bizType,
createdAt: Date.now()
})
}
function clearSession(uploadId: string): void {
if (uploadId) removeUploadSession(uploadId)
}
async function runOne(item: UploadQueueItem, resumeId?: string): Promise<void> {
item.status = 'uploading'
item.error = ''
try {
const durationSec = await probeVideoDuration(item.file)
item.durationSec = durationSec
const uploadOptions = {
bizType,
concurrency: CHUNK_CONCURRENCY,
durationSec,
onSession: (uploadId: string) => persistSession(item, uploadId),
onProgress: (percent: number) => {
item.percent = percent
if (percent >= MERGING_PERCENT && item.status === 'uploading') {
item.status = 'merging'
}
}
}
const asset = resumeId
? await resumeUpload(item.file, resumeId, uploadOptions)
: await uploadFile(item.file, uploadOptions)
item.percent = MAX_PERCENT
item.status = 'done'
item.assetId = asset.id
item.url = asset.url
if (asset.durationSec != null) item.durationSec = asset.durationSec
clearSession(item.uploadId || resumeId || '')
} catch (error) {
item.status = 'failed'
item.error = extractErrorMessage(error)
}
}
function pump(): void {
if (paused.value) return
while (running < FILE_CONCURRENCY && !paused.value) {
const next = items.value.find((item) => item.status === 'waiting')
if (!next) return
// 立刻占位,避免同步 while 循环把同一项派发两次
next.status = 'uploading'
running += 1
const resumeId = next.uploadId || undefined
void runOne(next, resumeId).finally(() => {
running -= 1
if (!paused.value) pump()
})
}
}
/** 入队本地文件(调用方已完成类型/大小校验)。 */
function enqueue(files: File[]): void {
for (const file of files) {
items.value.push({
id: nextItemId(),
file,
fileName: file.name,
fileSize: file.size,
lastModified: file.lastModified,
percent: 0,
status: paused.value ? 'paused' : 'waiting',
error: '',
uploadId: ''
})
}
}
/** 开始或继续派发。 */
function start(): void {
paused.value = false
for (const item of items.value) {
if (item.status === 'paused') item.status = 'waiting'
}
void pump()
}
/** 暂停:不派新任务;进行中分片跑完。 */
function pause(): void {
paused.value = true
for (const item of items.value) {
if (item.status === 'waiting') item.status = 'paused'
}
}
/** 重试单个失败项。 */
function retry(id: string): void {
const item = items.value.find((row) => row.id === id)
if (!item || item.status !== 'failed') return
item.error = ''
item.percent = item.uploadId ? item.percent : 0
item.status = paused.value ? 'paused' : 'waiting'
if (!paused.value) void pump()
}
/** 重试全部失败项。 */
function retryAll(): void {
for (const item of items.value) {
if (item.status === 'failed') {
item.error = ''
item.status = paused.value ? 'paused' : 'waiting'
}
}
if (!paused.value) void pump()
}
/** 移除单项并清理会话。 */
function remove(id: string): void {
const index = items.value.findIndex((row) => row.id === id)
if (index < 0) return
const [removed] = items.value.splice(index, 1)
clearSession(removed.uploadId)
}
/** 清空队列并清理本队列会话。 */
function clear(): void {
for (const item of items.value) {
clearSession(item.uploadId)
}
items.value = []
paused.value = false
running = 0
}
/** 读取当前剧未完成会话(24h 内)。 */
function getPendingSessions(): UploadSessionRecord[] {
return listPendingSessions(String(options.getDramaId()))
}
/**
* 用本地重选文件匹配会话并入队续传。
* @returns matched / unmatched 会话列表
*/
async function resumeFromSessions(
sessions: UploadSessionRecord[],
files: File[]
): Promise<{ matched: UploadSessionRecord[]; unmatched: UploadSessionRecord[] }> {
const matched: UploadSessionRecord[] = []
const unmatched: UploadSessionRecord[] = []
for (const session of sessions) {
const file = matchFileForSession(session, files)
if (!file) {
unmatched.push(session)
continue
}
try {
const progressRes = await getUploadProgressApi(session.uploadId)
const progress = progressRes?.data
if (!progress) {
unmatched.push(session)
continue
}
if (String(progress.status || '').toLowerCase() === 'completed') {
clearSession(session.uploadId)
continue
}
items.value.push({
id: nextItemId(),
file,
fileName: file.name,
fileSize: file.size,
lastModified: file.lastModified,
percent: Math.min(MERGING_PERCENT, Number(progress.percent) || 0),
status: paused.value ? 'paused' : 'waiting',
error: '',
uploadId: session.uploadId
})
matched.push(session)
} catch (error) {
if (isHttpNotFound(error)) {
clearSession(session.uploadId)
} else {
unmatched.push(session)
}
}
}
return { matched, unmatched }
}
/** 放弃续传:清理给定会话。 */
function abandonSessions(sessions: UploadSessionRecord[]): void {
for (const session of sessions) {
clearSession(session.uploadId)
}
}
return {
items,
overallPercent,
hasActiveUpload,
doneItems,
paused,
enqueue,
start,
pause,
retry,
retryAll,
remove,
clear,
getPendingSessions,
resumeFromSessions,
abandonSessions
}
}
@@ -0,0 +1,9 @@
// Author: fruit.com.cn
/** 剧集视频大小上限,单文件上传与批量上传共用。 */
export const MAX_VIDEO_SIZE = 500 * 1024 * 1024
/** 允许的视频扩展名(小写,不含点),单文件与批量上传同源。 */
export const VIDEO_EXTENSIONS: string[] = ['mp4', 'mov', 'mkv', 'webm']
/** el-upload / input[type=file] 的 accept 属性,与 VIDEO_EXTENSIONS 保持一致。 */
export const VIDEO_ACCEPT_ATTR = '.mp4,.mov,.mkv,.webm,video/*'