/* 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 . 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) => {text}, }, { 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) => ( handleToggleStatus(record)} checkedText={t('启用')} uncheckedText={t('禁用')} /> ), }, { title: t('操作'), width: 180, render: (_, record) => (
), }, ]; return (
{t('教程视频管理')}
} headerExtraContent={ } > } title={t('暂无视频')} description={t('点击上方按钮上传教程视频')} /> } /> {/* 上传视频弹窗 */} setUploadModalVisible(false)} footer={null} width={500} >
{uploading && (
)}
{/* 编辑视频弹窗 */} { setEditModalVisible(false); setCurrentVideo(null); }} footer={null} width={500} > {currentVideo && (
)}
{/* 预览视频弹窗 */} { setPreviewModalVisible(false); setCurrentVideo(null); }} footer={null} width={800} > {currentVideo && (
)}
); }; export default TutorialVideoSetting;