531 lines
14 KiB
React
531 lines
14 KiB
React
/*
|
||
Copyright (C) 2025 QuantumNous
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Affero General Public License as
|
||
published by the Free Software Foundation, either version 3 of the
|
||
License, or (at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU Affero General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Affero General Public License
|
||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
||
For commercial licensing, please contact support@quantumnous.com
|
||
*/
|
||
|
||
import { useEffectEvent } from '../../hooks/common/useEffectEvent';
|
||
import React, { useState, useEffect } from 'react';
|
||
import { useTranslation } from 'react-i18next';
|
||
import {
|
||
Card,
|
||
Table,
|
||
Button,
|
||
Modal,
|
||
Form,
|
||
Toast,
|
||
Popconfirm,
|
||
Switch,
|
||
Tag,
|
||
Progress,
|
||
Empty,
|
||
} from '@/components/ui';
|
||
import {
|
||
IconPlus,
|
||
IconDelete,
|
||
IconEdit,
|
||
IconUpload,
|
||
IconPlayCircle,
|
||
IconEyeOpened,
|
||
} from '@/components/ui/icons';
|
||
import { API } from '../../helpers';
|
||
|
||
const TutorialVideoSetting = () => {
|
||
const { t } = useTranslation();
|
||
const [videos, setVideos] = useState([]);
|
||
const [loading, setLoading] = useState(false);
|
||
const [uploadModalVisible, setUploadModalVisible] = useState(false);
|
||
const [editModalVisible, setEditModalVisible] = useState(false);
|
||
const [previewModalVisible, setPreviewModalVisible] = useState(false);
|
||
const [currentVideo, setCurrentVideo] = useState(null);
|
||
const [uploading, setUploading] = useState(false);
|
||
const [uploadProgress, setUploadProgress] = useState(0);
|
||
|
||
// 加载视频列表
|
||
const loadVideos = async () => {
|
||
setLoading(true);
|
||
try {
|
||
const res = await API.get('/api/admin/tutorial/videos');
|
||
if (res.data.success) {
|
||
setVideos(res.data.data || []);
|
||
} else {
|
||
Toast.error(res.data.message || t('加载失败'));
|
||
}
|
||
} catch (error) {
|
||
console.error('Load videos error:', error);
|
||
Toast.error(t('加载视频列表失败'));
|
||
} finally {
|
||
setLoading(false);
|
||
}
|
||
};
|
||
|
||
const onMount = useEffectEvent(() => {
|
||
loadVideos();
|
||
});
|
||
useEffect(() => onMount(), [onMount]);
|
||
|
||
// 格式化时长
|
||
const formatDuration = (seconds) => {
|
||
if (!seconds) return '--';
|
||
const mins = Math.floor(seconds / 60);
|
||
const secs = seconds % 60;
|
||
return `${mins}:${secs.toString().padStart(2, '0')}`;
|
||
};
|
||
|
||
// 格式化文件大小
|
||
const formatFileSize = (bytes) => {
|
||
if (!bytes) return '--';
|
||
if (bytes < 1024 * 1024) {
|
||
return `${(bytes / 1024).toFixed(1)} KB`;
|
||
}
|
||
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
||
};
|
||
|
||
// 上传视频
|
||
const handleUpload = async (values) => {
|
||
const { title, description, video, thumbnail, duration } = values;
|
||
|
||
if (!video || video.length === 0) {
|
||
Toast.error(t('请选择视频文件'));
|
||
return;
|
||
}
|
||
|
||
// 检查视频文件实例是否存在
|
||
const videoFile = video[0]?.fileInstance;
|
||
if (!videoFile) {
|
||
Toast.error(t('视频文件无效,请重新选择'));
|
||
return;
|
||
}
|
||
|
||
const formData = new FormData();
|
||
formData.append('title', title);
|
||
formData.append('description', description || '');
|
||
formData.append('video', videoFile);
|
||
formData.append('duration', duration || 0);
|
||
|
||
if (thumbnail && thumbnail.length > 0 && thumbnail[0]?.fileInstance) {
|
||
formData.append('thumbnail', thumbnail[0].fileInstance);
|
||
}
|
||
|
||
setUploading(true);
|
||
setUploadProgress(0);
|
||
|
||
try {
|
||
const res = await API.post('/api/admin/tutorial/video/upload', formData, {
|
||
headers: { 'Content-Type': 'multipart/form-data' },
|
||
onUploadProgress: (progressEvent) => {
|
||
const progress = Math.round(
|
||
(progressEvent.loaded * 100) / progressEvent.total,
|
||
);
|
||
setUploadProgress(progress);
|
||
},
|
||
});
|
||
|
||
if (res.data.success) {
|
||
Toast.success(t('上传成功'));
|
||
setUploadModalVisible(false);
|
||
loadVideos();
|
||
} else {
|
||
Toast.error(res.data.message || t('上传失败'));
|
||
}
|
||
} catch (error) {
|
||
console.error('Upload error:', error);
|
||
Toast.error(t('上传失败'));
|
||
} finally {
|
||
setUploading(false);
|
||
setUploadProgress(0);
|
||
}
|
||
};
|
||
|
||
// 更新视频信息
|
||
const handleUpdate = async (values) => {
|
||
if (!currentVideo) return;
|
||
|
||
try {
|
||
const res = await API.put(
|
||
`/api/admin/tutorial/video/${currentVideo.id}`,
|
||
values,
|
||
);
|
||
if (res.data.success) {
|
||
Toast.success(t('更新成功'));
|
||
setEditModalVisible(false);
|
||
setCurrentVideo(null);
|
||
loadVideos();
|
||
} else {
|
||
Toast.error(res.data.message || t('更新失败'));
|
||
}
|
||
} catch (error) {
|
||
console.error('Update error:', error);
|
||
Toast.error(t('更新失败'));
|
||
}
|
||
};
|
||
|
||
// 删除视频
|
||
const handleDelete = async (id) => {
|
||
try {
|
||
const res = await API.delete(`/api/admin/tutorial/video/${id}`);
|
||
if (res.data.success) {
|
||
Toast.success(t('删除成功'));
|
||
loadVideos();
|
||
} else {
|
||
Toast.error(res.data.message || t('删除失败'));
|
||
}
|
||
} catch (error) {
|
||
console.error('Delete error:', error);
|
||
Toast.error(t('删除失败'));
|
||
}
|
||
};
|
||
|
||
// 切换状态
|
||
const handleToggleStatus = async (video) => {
|
||
const newStatus = video.status === 1 ? 0 : 1;
|
||
try {
|
||
const res = await API.put(`/api/admin/tutorial/video/${video.id}`, {
|
||
status: newStatus,
|
||
});
|
||
if (res.data.success) {
|
||
Toast.success(newStatus === 1 ? t('已启用') : t('已禁用'));
|
||
loadVideos();
|
||
} else {
|
||
Toast.error(res.data.message || t('操作失败'));
|
||
}
|
||
} catch (error) {
|
||
console.error('Toggle status error:', error);
|
||
Toast.error(t('操作失败'));
|
||
}
|
||
};
|
||
|
||
// 预览视频
|
||
const handlePreview = (video) => {
|
||
setCurrentVideo(video);
|
||
setPreviewModalVisible(true);
|
||
};
|
||
|
||
// 编辑视频
|
||
const handleEdit = (video) => {
|
||
setCurrentVideo(video);
|
||
setEditModalVisible(true);
|
||
};
|
||
|
||
const columns = [
|
||
{
|
||
title: t('排序'),
|
||
dataIndex: 'sort_order',
|
||
width: 70,
|
||
render: (text) => <Tag color='blue'>{text}</Tag>,
|
||
},
|
||
{
|
||
title: t('标题'),
|
||
dataIndex: 'title',
|
||
width: 200,
|
||
ellipsis: true,
|
||
},
|
||
{
|
||
title: t('描述'),
|
||
dataIndex: 'description',
|
||
width: 200,
|
||
ellipsis: true,
|
||
render: (text) => text || '-',
|
||
},
|
||
{
|
||
title: t('时长'),
|
||
dataIndex: 'duration',
|
||
width: 80,
|
||
render: (text) => formatDuration(text),
|
||
},
|
||
{
|
||
title: t('大小'),
|
||
dataIndex: 'file_size',
|
||
width: 100,
|
||
render: (text) => formatFileSize(text),
|
||
},
|
||
{
|
||
title: t('状态'),
|
||
dataIndex: 'status',
|
||
width: 80,
|
||
render: (status, record) => (
|
||
<Switch
|
||
checked={status === 1}
|
||
onChange={() => handleToggleStatus(record)}
|
||
checkedText={t('启用')}
|
||
uncheckedText={t('禁用')}
|
||
/>
|
||
),
|
||
},
|
||
{
|
||
title: t('操作'),
|
||
width: 180,
|
||
render: (_, record) => (
|
||
<div style={{ display: 'flex', gap: 8 }}>
|
||
<Button
|
||
icon={<IconEyeOpened />}
|
||
size='small'
|
||
theme='borderless'
|
||
onClick={() => handlePreview(record)}
|
||
/>
|
||
<Button
|
||
icon={<IconEdit />}
|
||
size='small'
|
||
theme='borderless'
|
||
onClick={() => handleEdit(record)}
|
||
/>
|
||
<Popconfirm
|
||
title={t('确定删除这个视频吗?')}
|
||
content={t('删除后将无法恢复')}
|
||
onConfirm={() => handleDelete(record.id)}
|
||
>
|
||
<Button
|
||
icon={<IconDelete />}
|
||
size='small'
|
||
theme='borderless'
|
||
type='danger'
|
||
/>
|
||
</Popconfirm>
|
||
</div>
|
||
),
|
||
},
|
||
];
|
||
|
||
return (
|
||
<div>
|
||
<Card
|
||
title={
|
||
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
|
||
<IconPlayCircle size='large' />
|
||
{t('教程视频管理')}
|
||
</div>
|
||
}
|
||
headerExtraContent={
|
||
<Button
|
||
icon={<IconPlus />}
|
||
theme='solid'
|
||
onClick={() => setUploadModalVisible(true)}
|
||
>
|
||
{t('上传视频')}
|
||
</Button>
|
||
}
|
||
>
|
||
<Table
|
||
// 列宽合计 910px,窄屏必须给横向滚动
|
||
scroll={{ x: 'max-content' }}
|
||
columns={columns}
|
||
dataSource={videos}
|
||
loading={loading}
|
||
rowKey='id'
|
||
pagination={false}
|
||
empty={
|
||
<Empty
|
||
image={
|
||
<IconPlayCircle
|
||
style={{ fontSize: 48, color: 'var(--ui-color-text-3)' }}
|
||
/>
|
||
}
|
||
title={t('暂无视频')}
|
||
description={t('点击上方按钮上传教程视频')}
|
||
/>
|
||
}
|
||
/>
|
||
</Card>
|
||
|
||
{/* 上传视频弹窗 */}
|
||
<Modal
|
||
title={t('上传教程视频')}
|
||
visible={uploadModalVisible}
|
||
onCancel={() => setUploadModalVisible(false)}
|
||
footer={null}
|
||
width={500}
|
||
>
|
||
<Form onSubmit={handleUpload}>
|
||
<Form.Input
|
||
field='title'
|
||
label={t('视频标题')}
|
||
placeholder={t('请输入视频标题')}
|
||
rules={[{ required: true, message: t('请输入视频标题') }]}
|
||
/>
|
||
<Form.TextArea
|
||
field='description'
|
||
label={t('视频描述')}
|
||
placeholder={t('请输入视频描述(可选)')}
|
||
rows={3}
|
||
/>
|
||
<Form.InputNumber
|
||
field='duration'
|
||
label={t('视频时长(秒)')}
|
||
placeholder={t('请输入视频时长')}
|
||
min={0}
|
||
/>
|
||
<Form.Upload
|
||
field='video'
|
||
label={t('视频文件')}
|
||
accept='video/mp4,video/webm,video/quicktime'
|
||
maxSize={500 * 1024} // 500MB in KB
|
||
limit={1}
|
||
rules={[{ required: true, message: t('请选择视频文件') }]}
|
||
>
|
||
<Button icon={<IconUpload />} theme='light'>
|
||
{t('选择视频 (mp4/webm/mov, 最大500MB)')}
|
||
</Button>
|
||
</Form.Upload>
|
||
<Form.Upload
|
||
field='thumbnail'
|
||
label={t('缩略图')}
|
||
accept='image/jpeg,image/png,image/webp'
|
||
maxSize={5 * 1024} // 5MB in KB
|
||
limit={1}
|
||
listType='picture'
|
||
>
|
||
<Button icon={<IconUpload />} theme='light'>
|
||
{t('选择缩略图(可选)')}
|
||
</Button>
|
||
</Form.Upload>
|
||
|
||
{uploading && (
|
||
<div style={{ marginBottom: 16 }}>
|
||
<Progress percent={uploadProgress} showInfo />
|
||
</div>
|
||
)}
|
||
|
||
<div
|
||
style={{
|
||
display: 'flex',
|
||
justifyContent: 'flex-end',
|
||
gap: 8,
|
||
marginTop: 16,
|
||
}}
|
||
>
|
||
<Button onClick={() => setUploadModalVisible(false)}>
|
||
{t('取消')}
|
||
</Button>
|
||
<Button type='primary' htmlType='submit' loading={uploading}>
|
||
{uploading ? t('上传中...') : t('上传')}
|
||
</Button>
|
||
</div>
|
||
</Form>
|
||
</Modal>
|
||
|
||
{/* 编辑视频弹窗 */}
|
||
<Modal
|
||
title={t('编辑视频信息')}
|
||
visible={editModalVisible}
|
||
onCancel={() => {
|
||
setEditModalVisible(false);
|
||
setCurrentVideo(null);
|
||
}}
|
||
footer={null}
|
||
width={500}
|
||
>
|
||
{currentVideo && (
|
||
<Form
|
||
onSubmit={handleUpdate}
|
||
initValues={{
|
||
title: currentVideo.title,
|
||
description: currentVideo.description,
|
||
sort_order: currentVideo.sort_order,
|
||
duration: currentVideo.duration,
|
||
}}
|
||
>
|
||
<Form.Input
|
||
field='title'
|
||
label={t('视频标题')}
|
||
placeholder={t('请输入视频标题')}
|
||
rules={[{ required: true, message: t('请输入视频标题') }]}
|
||
/>
|
||
<Form.TextArea
|
||
field='description'
|
||
label={t('视频描述')}
|
||
placeholder={t('请输入视频描述')}
|
||
rows={3}
|
||
/>
|
||
<Form.InputNumber
|
||
field='sort_order'
|
||
label={t('排序')}
|
||
placeholder={t('数字越小越靠前')}
|
||
min={0}
|
||
/>
|
||
<Form.InputNumber
|
||
field='duration'
|
||
label={t('视频时长(秒)')}
|
||
placeholder={t('请输入视频时长')}
|
||
min={0}
|
||
/>
|
||
|
||
<div
|
||
style={{
|
||
display: 'flex',
|
||
justifyContent: 'flex-end',
|
||
gap: 8,
|
||
marginTop: 16,
|
||
}}
|
||
>
|
||
<Button
|
||
onClick={() => {
|
||
setEditModalVisible(false);
|
||
setCurrentVideo(null);
|
||
}}
|
||
>
|
||
{t('取消')}
|
||
</Button>
|
||
<Button type='primary' htmlType='submit'>
|
||
{t('保存')}
|
||
</Button>
|
||
</div>
|
||
</Form>
|
||
)}
|
||
</Modal>
|
||
|
||
{/* 预览视频弹窗 */}
|
||
<Modal
|
||
title={currentVideo?.title || t('视频预览')}
|
||
visible={previewModalVisible}
|
||
onCancel={() => {
|
||
setPreviewModalVisible(false);
|
||
setCurrentVideo(null);
|
||
}}
|
||
footer={null}
|
||
width={800}
|
||
>
|
||
{currentVideo && (
|
||
<div
|
||
style={{
|
||
position: 'relative',
|
||
paddingTop: '56.25%',
|
||
background: '#000',
|
||
borderRadius: 8,
|
||
overflow: 'hidden',
|
||
}}
|
||
>
|
||
<video
|
||
src={currentVideo.signed_url}
|
||
poster={currentVideo.thumbnail_url}
|
||
controls
|
||
style={{
|
||
position: 'absolute',
|
||
top: 0,
|
||
left: 0,
|
||
width: '100%',
|
||
height: '100%',
|
||
objectFit: 'contain',
|
||
}}
|
||
/>
|
||
</div>
|
||
)}
|
||
</Modal>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default TutorialVideoSetting;
|