From 0db4e00ef7429213505ed8647ae6f54b8ae76c09 Mon Sep 17 00:00:00 2001 From: mike Date: Tue, 22 Sep 2026 14:20:25 +0800 Subject: [PATCH] Update MicUploadPage.jsx via upload script - 2026-09-22 14:20:15 --- MicUploadPage.jsx | 85 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 10 deletions(-) diff --git a/MicUploadPage.jsx b/MicUploadPage.jsx index cd70a74..3794d4b 100644 --- a/MicUploadPage.jsx +++ b/MicUploadPage.jsx @@ -83,11 +83,19 @@ function useMicSession() { } }, []); + // 画面卡住(比如网络抖了一下)时用——只重开WebSocket这一根管子,浏览器/ + // 会话本身不受影响,不需要重新POST /sessions,token还是原来那个。 + const refreshWs = useCallback(() => { + if (!sessionId || !wsToken) return; + if (wsRef.current) { try { wsRef.current.close(); } catch (e) {} wsRef.current = null; } + openWs(sessionId, wsToken, frameCallbackRef.current); + }, [sessionId, wsToken, openWs]); + // 组件卸载(切到别的菜单)时只关WebSocket连接,后端的浏览器会话继续留着—— // 是单例的,切回来时挂载effect会重新POST /sessions,自动接上同一个。 useEffect(() => () => { if (wsRef.current) wsRef.current.close(); }, []); - return { sessionId, wsToken, connected, logs, connect, stop, sendInput }; + return { sessionId, wsToken, connected, logs, connect, stop, sendInput, refreshWs }; } // canvas <-> 远程浏览器坐标换算:canvas按容器宽度等比缩放显示,鼠标事件按缩放比换算回1280x800 @@ -313,11 +321,12 @@ function TemplateConfigPanel({ productCategories, onClose }) { } export function MicUploadPage({ products = [], productCategories = [] }) { - const { sessionId, connected, logs, connect, stop, sendInput } = useMicSession(); + const { sessionId, connected, logs, connect, stop, sendInput, refreshWs } = useMicSession(); const { canvasRef, drawFrame, handlers } = useRemoteCanvas(sendInput); // connecting -> browsing -> ready -> filling -> filled -> submitted(还有个 error 态) const [phase, setPhase] = useState('connecting'); + const [reviewSecondsLeft, setReviewSecondsLeft] = useState(0); const [queueIds, setQueueIds] = useState([]); // 选中要批量处理的产品,按点击顺序 const [queueIndex, setQueueIndex] = useState(-1); // 当前处理到队列里第几个,-1=还没开始 const [search, setSearch] = useState(''); @@ -356,6 +365,10 @@ export function MicUploadPage({ products = [], productCategories = [] }) { useEffect(() => { logEndRef.current?.scrollIntoView({ block: 'end' }); }, [logs]); + useEffect(() => () => { + if (reviewTimerRef.current) clearInterval(reviewTimerRef.current); + }, []); + const filteredProducts = products.filter((p) => { if (!search.trim()) return true; const q = search.trim().toLowerCase(); @@ -416,9 +429,12 @@ export function MicUploadPage({ products = [], productCategories = [] }) { } } - // 按队列顺序处理第idx个产品:导航到模板+AI生成标题关键词+填表,不自动提交 - // 全自动:填完一个立刻自动提交,提交完立刻自动填下一个,队列跑完才停—— - // 不需要每个产品手动点一次"提交"。 + const reviewTimerRef = useRef(null); + + // 按队列顺序处理第idx个产品:导航到模板+AI生成标题关键词+填表,填完后不会 + // 立刻提交——留几秒钟给你在右侧画面里看一眼/顺手点进某个框改一下(画面 + // 上的鼠标点击、打字任何时候都能转发过去,不限于登录阶段),倒计时结束 + // 没人管它才会自动提交,省得每次都要手动点。 async function runFillForIndex(idx) { if (!sessionId) { setError('会话已断开,请重新打开登录页'); return; } const productId = queueIds[idx]; @@ -435,7 +451,7 @@ export function MicUploadPage({ products = [], productCategories = [] }) { }); const data = await res.json(); if (!res.ok) throw new Error(data.error || '自动填写失败'); - await submitOneAuto(idx); + startReviewCountdown(idx); } catch (e) { setError(e.message); setPhase('ready'); @@ -443,7 +459,34 @@ export function MicUploadPage({ products = [], productCategories = [] }) { } } + const REVIEW_SECONDS = 8; + + function startReviewCountdown(idx) { + setBusy(false); + setPhase('review'); + setReviewSecondsLeft(REVIEW_SECONDS); + if (reviewTimerRef.current) clearInterval(reviewTimerRef.current); + reviewTimerRef.current = setInterval(() => { + setReviewSecondsLeft((s) => { + if (s <= 1) { + clearInterval(reviewTimerRef.current); + reviewTimerRef.current = null; + submitOneAuto(idx); + return 0; + } + return s - 1; + }); + }, 1000); + } + + function pauseReview() { + if (reviewTimerRef.current) { clearInterval(reviewTimerRef.current); reviewTimerRef.current = null; } + setPhase('paused'); // 暂停后倒计时不会自己继续,得你手动点"确认提交" + } + async function submitOneAuto(idx) { + if (reviewTimerRef.current) { clearInterval(reviewTimerRef.current); reviewTimerRef.current = null; } + setBusy(true); setPhase('submitting'); try { const res = await fetch(`/api/mic-upload/sessions/${sessionId}/submit`, { method: 'POST' }); @@ -473,7 +516,7 @@ export function MicUploadPage({ products = [], productCategories = [] }) { function toggleQueue(productId) { // 提交完一批之后phase停在'submitted',这时候点选新产品要能重新回到'ready' // 才会显示"开始自动填写"按钮;处理中(filling/submitting)不允许中途改选,忽略点击。 - if (phase === 'filling' || phase === 'submitting') return; + if (phase === 'filling' || phase === 'submitting' || phase === 'review' || phase === 'paused') return; if (phase === 'submitted') setPhase('ready'); setQueueIds((prev) => ( prev.includes(productId) ? prev.filter((id) => id !== productId) : [...prev, productId] @@ -481,6 +524,7 @@ export function MicUploadPage({ products = [], productCategories = [] }) { } async function handleClose() { + if (reviewTimerRef.current) { clearInterval(reviewTimerRef.current); reviewTimerRef.current = null; } await stop(); setQueueIds([]); setQueueIndex(-1); @@ -506,8 +550,9 @@ export function MicUploadPage({ products = [], productCategories = [] }) { {phase !== 'connecting' && phase !== 'error' && ( <> -
- 画面状态:{connected ? '已连接' : '连接中...'} +
+ 画面状态:{connected ? '已连接' : '连接中...'} +
{(phase === 'browsing') && ( @@ -518,7 +563,7 @@ export function MicUploadPage({ products = [], productCategories = [] }) { )} - {(phase === 'ready' || phase === 'filling' || phase === 'submitting' || phase === 'submitted') && ( + {(phase === 'ready' || phase === 'filling' || phase === 'submitting' || phase === 'review' || phase === 'paused' || phase === 'submitted') && ( <> )} + {(phase === 'review' || phase === 'paused') && ( +
+
+ {phase === 'review' + ? `已填完,${reviewSecondsLeft}秒后自动提交——这段时间可以在右侧画面里点进任意框直接改` + : '已暂停,改好了再点提交'} + {queueIds.length > 1 && `(第 ${queueIndex + 1} / ${queueIds.length} 个)`} +
+ +
+ {phase === 'review' && ( + + )} + +
+
+ )} + {phase === 'submitted' && (
✅ 已全部提交,请在画面里核对发布结果
)}