Initial import of weiyuan frontend
This commit is contained in:
@@ -0,0 +1,268 @@
|
||||
/*
|
||||
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 React, { useState, useEffect, useRef } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Typography, Card, Empty, Spin, Toast } from '@douyinfe/semi-ui';
|
||||
import { IconPlayCircle, IconClock, IconFile } from '@douyinfe/semi-icons';
|
||||
import { API } from '../../../helpers';
|
||||
import './VideoTutorial.css';
|
||||
|
||||
const { Title, Text, Paragraph } = Typography;
|
||||
|
||||
const VideoTutorial = () => {
|
||||
const { t } = useTranslation();
|
||||
const [videos, setVideos] = useState([]);
|
||||
const [currentVideo, setCurrentVideo] = useState(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [videoError, setVideoError] = useState(false);
|
||||
const videoRef = useRef(null);
|
||||
|
||||
// 获取视频列表
|
||||
useEffect(() => {
|
||||
loadVideos();
|
||||
}, []);
|
||||
|
||||
const loadVideos = async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await API.get('/api/tutorial/videos');
|
||||
if (res.data.success && res.data.data) {
|
||||
setVideos(res.data.data);
|
||||
if (res.data.data.length > 0) {
|
||||
setCurrentVideo(res.data.data[0]);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to load tutorial videos:', error);
|
||||
Toast.error(t('加载视频失败'));
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
// 格式化时长
|
||||
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 handleSelectVideo = (video) => {
|
||||
setCurrentVideo(video);
|
||||
setVideoError(false);
|
||||
// 重置视频播放
|
||||
if (videoRef.current) {
|
||||
videoRef.current.load();
|
||||
}
|
||||
};
|
||||
|
||||
// 视频加载错误处理
|
||||
const handleVideoError = () => {
|
||||
setVideoError(true);
|
||||
Toast.error(t('视频加载失败,请刷新页面重试'));
|
||||
};
|
||||
|
||||
// 刷新视频URL
|
||||
const refreshVideoUrl = async () => {
|
||||
setVideoError(false);
|
||||
await loadVideos();
|
||||
};
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className='video-tutorial-loading'>
|
||||
<Spin size='large' />
|
||||
<Text type='secondary' style={{ marginTop: 16 }}>
|
||||
{t('加载视频中...')}
|
||||
</Text>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (videos.length === 0) {
|
||||
return (
|
||||
<div className='video-tutorial-empty'>
|
||||
<Empty
|
||||
image={
|
||||
<IconPlayCircle
|
||||
size='extra-large'
|
||||
style={{ fontSize: 64, color: 'var(--semi-color-text-3)' }}
|
||||
/>
|
||||
}
|
||||
title={t('暂无视频教程')}
|
||||
description={t('管理员还没有上传视频教程,请查看图文教程')}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='video-tutorial'>
|
||||
{/* 视频播放器区域 */}
|
||||
<div className='video-player-section'>
|
||||
<Card className='video-player-card'>
|
||||
{currentVideo && (
|
||||
<>
|
||||
<div className='video-player-wrapper'>
|
||||
{videoError ? (
|
||||
<div className='video-error'>
|
||||
<IconPlayCircle size='extra-large' />
|
||||
<Text type='danger'>{t('视频加载失败')}</Text>
|
||||
<button
|
||||
className='video-retry-btn'
|
||||
onClick={refreshVideoUrl}
|
||||
>
|
||||
{t('点击重试')}
|
||||
</button>
|
||||
</div>
|
||||
) : (
|
||||
<video
|
||||
ref={videoRef}
|
||||
key={currentVideo.id}
|
||||
src={currentVideo.signed_url}
|
||||
poster={currentVideo.thumbnail_url}
|
||||
controls
|
||||
controlsList='nodownload noplaybackrate'
|
||||
disablePictureInPicture
|
||||
onContextMenu={(e) => e.preventDefault()}
|
||||
onError={handleVideoError}
|
||||
className='video-player'
|
||||
>
|
||||
{t('您的浏览器不支持视频播放')}
|
||||
</video>
|
||||
)}
|
||||
</div>
|
||||
<div className='video-info'>
|
||||
<Title heading={4} className='video-title'>
|
||||
{currentVideo.title}
|
||||
</Title>
|
||||
{currentVideo.description && (
|
||||
<Paragraph type='secondary' className='video-description'>
|
||||
{currentVideo.description}
|
||||
</Paragraph>
|
||||
)}
|
||||
<div className='video-meta'>
|
||||
{currentVideo.duration > 0 && (
|
||||
<span className='video-meta-item'>
|
||||
<IconClock size='small' />
|
||||
{formatDuration(currentVideo.duration)}
|
||||
</span>
|
||||
)}
|
||||
{currentVideo.file_size > 0 && (
|
||||
<span className='video-meta-item'>
|
||||
<IconFile size='small' />
|
||||
{formatFileSize(currentVideo.file_size)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</Card>
|
||||
</div>
|
||||
|
||||
{/* 视频列表 */}
|
||||
{videos.length > 1 && (
|
||||
<div className='video-list-section'>
|
||||
<Title heading={5} className='video-list-title'>
|
||||
{t('视频列表')}
|
||||
</Title>
|
||||
<div className='video-list'>
|
||||
{videos.map((video, index) => (
|
||||
<div
|
||||
key={video.id}
|
||||
className={`video-list-item ${currentVideo?.id === video.id ? 'active' : ''}`}
|
||||
onClick={() => handleSelectVideo(video)}
|
||||
>
|
||||
<div className='video-list-thumbnail'>
|
||||
{video.thumbnail_url ? (
|
||||
<img src={video.thumbnail_url} alt={video.title} />
|
||||
) : (
|
||||
<div className='video-thumbnail-placeholder'>
|
||||
<IconPlayCircle size='large' />
|
||||
</div>
|
||||
)}
|
||||
<span className='video-list-index'>{index + 1}</span>
|
||||
{video.duration > 0 && (
|
||||
<span className='video-list-duration'>
|
||||
{formatDuration(video.duration)}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className='video-list-info'>
|
||||
<Text
|
||||
strong
|
||||
ellipsis={{ showTooltip: true }}
|
||||
className='video-list-name'
|
||||
>
|
||||
{video.title}
|
||||
</Text>
|
||||
{video.description && (
|
||||
<Text
|
||||
type='tertiary'
|
||||
size='small'
|
||||
ellipsis
|
||||
className='video-list-desc'
|
||||
>
|
||||
{video.description}
|
||||
</Text>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 提示信息 */}
|
||||
<div className='video-tips'>
|
||||
<Card className='video-tips-card'>
|
||||
<div className='video-tips-content'>
|
||||
<span className='video-tips-icon'>💡</span>
|
||||
<div>
|
||||
<Text strong>{t('观看提示')}</Text>
|
||||
<Paragraph
|
||||
type='secondary'
|
||||
style={{ marginBottom: 0, marginTop: 4 }}
|
||||
>
|
||||
{t(
|
||||
'建议在 WiFi 环境下观看视频教程。如遇视频加载缓慢,请刷新页面重试。',
|
||||
)}
|
||||
</Paragraph>
|
||||
</div>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default VideoTutorial;
|
||||
Reference in New Issue
Block a user