Update MicUploadPage.jsx via upload script - 2026-09-22 14:20:15

This commit is contained in:
mike 2026-09-22 14:20:25 +08:00
parent 95d5de6e86
commit 0db4e00ef7
1 changed files with 75 additions and 10 deletions

View File

@ -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连接,后端的浏览器会话继续留着—— // 组件卸载(切到别的菜单)时只关WebSocket连接,后端的浏览器会话继续留着——
// 是单例的,切回来时挂载effect会重新POST /sessions,自动接上同一个。 // 是单例的,切回来时挂载effect会重新POST /sessions,自动接上同一个。
useEffect(() => () => { if (wsRef.current) wsRef.current.close(); }, []); 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 // canvas <-> 远程浏览器坐标换算:canvas按容器宽度等比缩放显示,鼠标事件按缩放比换算回1280x800
@ -313,11 +321,12 @@ function TemplateConfigPanel({ productCategories, onClose }) {
} }
export function MicUploadPage({ products = [], productCategories = [] }) { 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); const { canvasRef, drawFrame, handlers } = useRemoteCanvas(sendInput);
// connecting -> browsing -> ready -> filling -> filled -> submitted(还有个 error 态) // connecting -> browsing -> ready -> filling -> filled -> submitted(还有个 error 态)
const [phase, setPhase] = useState('connecting'); const [phase, setPhase] = useState('connecting');
const [reviewSecondsLeft, setReviewSecondsLeft] = useState(0);
const [queueIds, setQueueIds] = useState([]); // 选中要批量处理的产品,按点击顺序 const [queueIds, setQueueIds] = useState([]); // 选中要批量处理的产品,按点击顺序
const [queueIndex, setQueueIndex] = useState(-1); // 当前处理到队列里第几个,-1=还没开始 const [queueIndex, setQueueIndex] = useState(-1); // 当前处理到队列里第几个,-1=还没开始
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
@ -356,6 +365,10 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
useEffect(() => { logEndRef.current?.scrollIntoView({ block: 'end' }); }, [logs]); useEffect(() => { logEndRef.current?.scrollIntoView({ block: 'end' }); }, [logs]);
useEffect(() => () => {
if (reviewTimerRef.current) clearInterval(reviewTimerRef.current);
}, []);
const filteredProducts = products.filter((p) => { const filteredProducts = products.filter((p) => {
if (!search.trim()) return true; if (!search.trim()) return true;
const q = search.trim().toLowerCase(); 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) { async function runFillForIndex(idx) {
if (!sessionId) { setError('会话已断开,请重新打开登录页'); return; } if (!sessionId) { setError('会话已断开,请重新打开登录页'); return; }
const productId = queueIds[idx]; const productId = queueIds[idx];
@ -435,7 +451,7 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
}); });
const data = await res.json(); const data = await res.json();
if (!res.ok) throw new Error(data.error || '自动填写失败'); if (!res.ok) throw new Error(data.error || '自动填写失败');
await submitOneAuto(idx); startReviewCountdown(idx);
} catch (e) { } catch (e) {
setError(e.message); setError(e.message);
setPhase('ready'); 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) { async function submitOneAuto(idx) {
if (reviewTimerRef.current) { clearInterval(reviewTimerRef.current); reviewTimerRef.current = null; }
setBusy(true);
setPhase('submitting'); setPhase('submitting');
try { try {
const res = await fetch(`/api/mic-upload/sessions/${sessionId}/submit`, { method: 'POST' }); const res = await fetch(`/api/mic-upload/sessions/${sessionId}/submit`, { method: 'POST' });
@ -473,7 +516,7 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
function toggleQueue(productId) { function toggleQueue(productId) {
// 提交完一批之后phase停在'submitted',这时候点选新产品要能重新回到'ready' // 提交完一批之后phase停在'submitted',这时候点选新产品要能重新回到'ready'
// 才会显示"开始自动填写"按钮;处理中(filling/submitting)不允许中途改选,忽略点击。 // 才会显示"开始自动填写"按钮;处理中(filling/submitting)不允许中途改选,忽略点击。
if (phase === 'filling' || phase === 'submitting') return; if (phase === 'filling' || phase === 'submitting' || phase === 'review' || phase === 'paused') return;
if (phase === 'submitted') setPhase('ready'); if (phase === 'submitted') setPhase('ready');
setQueueIds((prev) => ( setQueueIds((prev) => (
prev.includes(productId) ? prev.filter((id) => id !== productId) : [...prev, productId] prev.includes(productId) ? prev.filter((id) => id !== productId) : [...prev, productId]
@ -481,6 +524,7 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
} }
async function handleClose() { async function handleClose() {
if (reviewTimerRef.current) { clearInterval(reviewTimerRef.current); reviewTimerRef.current = null; }
await stop(); await stop();
setQueueIds([]); setQueueIds([]);
setQueueIndex(-1); setQueueIndex(-1);
@ -506,8 +550,9 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
{phase !== 'connecting' && phase !== 'error' && ( {phase !== 'connecting' && phase !== 'error' && (
<> <>
<div style={{ fontSize: 12, color: 'var(--ink-soft, #888)' }}> <div style={{ fontSize: 12, color: 'var(--ink-soft, #888)', display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
画面状态:{connected ? '已连接' : '连接中...'} <span>画面状态:{connected ? '已连接' : '连接中...'}</span>
<button className="btn-ghost" style={{ fontSize: 11, padding: '2px 6px' }} onClick={refreshWs}>刷新画面</button>
</div> </div>
{(phase === 'browsing') && ( {(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') && (
<> <>
<input <input
placeholder="搜索产品(型号/品牌)" placeholder="搜索产品(型号/品牌)"
@ -583,6 +628,26 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
</div> </div>
)} )}
{(phase === 'review' || phase === 'paused') && (
<div style={{ border: '1px solid var(--brand, #d9822b)', borderRadius: 6, padding: 8, display: 'flex', flexDirection: 'column', gap: 6 }}>
<div style={{ fontSize: 12 }}>
{phase === 'review'
? `已填完,${reviewSecondsLeft}秒后自动提交——这段时间可以在右侧画面里点进任意框直接改`
: '已暂停,改好了再点提交'}
{queueIds.length > 1 && `(第 ${queueIndex + 1} / ${queueIds.length} 个)`}
</div>
<KeyCaptureInput sendInput={sendInput} />
<div style={{ display: 'flex', gap: 6 }}>
{phase === 'review' && (
<button className="btn-ghost" style={{ flex: 1 }} onClick={pauseReview}>暂停检查</button>
)}
<button className="btn-primary" style={{ flex: 1 }} disabled={busy} onClick={() => submitOneAuto(queueIndex)}>
立即提交
</button>
</div>
</div>
)}
{phase === 'submitted' && ( {phase === 'submitted' && (
<div style={{ color: 'var(--success, #2a8f4d)', fontSize: 13 }}>✅ 已全部提交,请在画面里核对发布结果</div> <div style={{ color: 'var(--success, #2a8f4d)', fontSize: 13 }}>✅ 已全部提交,请在画面里核对发布结果</div>
)} )}