284 lines
8.0 KiB
Vue
284 lines
8.0 KiB
Vue
<!-- 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 EpisodeTranscodePanel from './EpisodeTranscodePanel.vue'
|
||
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>
|
||
|
||
<EpisodeTranscodePanel v-if="editingId" :episode-id="editingId" :active="dialogVisible" />
|
||
|
||
<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>
|