import React, { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { AlertCircle, Loader2 } from 'lucide-react'; import { shareApiUrl } from '@/lib/env'; import { message } from 'antd'; interface VideoShareFormProps { onCancel: () => void; onShare: (videoUrl: string, videoName: string) => void; error?: string | null; platform?: string; videoUrl?: string; videoName?: string; } // 平台上传接口映射(统一使用小写 key) const PLATFORM_UPLOAD_API: Record = { 'youtube': '/api/video-share/youtube/upload', 'tiktok': '/api/video-share/tiktok/upload', 'reddit': '/api/video-share/reddit/upload', 'twitter': '/api/video-share/x/upload', // Instagram 开发中,暂不支持 // 'instagram': '/api/video-share/instagram/upload', }; export const VideoShareForm: React.FC = ({ onCancel, onShare, error = null, platform = '', videoUrl: initialVideoUrl = '', videoName: initialVideoName = '' }) => { const [videoUrl, setVideoUrl] = useState(initialVideoUrl); const [videoName, setVideoName] = useState(initialVideoName); const [uploading, setUploading] = useState(false); /** * 获取用户ID */ const getUserId = () => { const currentUser = JSON.parse(localStorage.getItem('currentUser') || '{}'); return currentUser.id || currentUser.userId; }; /** * 上传视频到指定平台 */ const uploadVideoToPlatform = async (platformName: string, contentUrl: string, title: string) => { const userId = getUserId(); if (!userId) { message.error('用户ID不存在,请先登录'); return false; } // 统一转换成小写后查找 API 路径 const normalizedPlatform = platformName.toLowerCase().replace(/\s+/g, '').replace(/[()]/g, ''); const apiPath = PLATFORM_UPLOAD_API[normalizedPlatform]; if (!apiPath) { message.error(`平台 ${platformName} 暂不支持或正在开发中`); return false; } try { const response = await fetch(`${shareApiUrl}${apiPath}`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${localStorage.getItem('token') || ''}`, 'ngrok-skip-browser-warning': 'true' }, body: JSON.stringify({ user_id: userId, content_url: contentUrl, title: title }) }); const data = await response.json(); if (data.successful && data.code === 0) { message.success(`视频已成功上传到 ${platformName}!`); return true; } else { message.error(data.message || `上传到 ${platformName} 失败`); return false; } } catch (error) { console.error(`上传到 ${platformName} 失败:`, error); message.error(`上传失败:${error instanceof Error ? error.message : '网络错误'}`); return false; } }; const handleShare = async () => { if (!videoUrl || !videoName) { message.warning('请填写完整的视频信息'); return; } setUploading(true); try { const success = await uploadVideoToPlatform(platform, videoUrl, videoName); if (success) { onShare(videoUrl, videoName); } } finally { setUploading(false); } }; return (

转发视频{platform && ` - ${platform}`}

{/* 错误信息显示 */} {error && (

授权失败

{error}

)}
setVideoUrl(e.target.value)} placeholder="请输入视频链接" className="bg-white/10 border-white/20 text-white placeholder:text-white/50" disabled={!!error} />
setVideoName(e.target.value)} placeholder="请输入视频名称" className="bg-white/10 border-white/20 text-white placeholder:text-white/50" disabled={!!error} />
{!error && ( )}
); };