Update MicUploadPage.jsx via upload script - 2026-09-21 00:49:02
This commit is contained in:
parent
839d799451
commit
6c7ad29ba7
|
|
@ -18,6 +18,7 @@
|
|||
import React, { useState, useRef, useCallback, useEffect } from 'react';
|
||||
|
||||
const VIEWPORT = { width: 1280, height: 800 };
|
||||
const STORAGE_KEY = 'mic-upload-session';
|
||||
|
||||
function useMicSession() {
|
||||
const [sessionId, setSessionId] = useState(null);
|
||||
|
|
@ -58,14 +59,37 @@ function useMicSession() {
|
|||
setSessionId(data.sessionId);
|
||||
setWsToken(data.wsToken);
|
||||
openWs(data.sessionId, data.wsToken, onFrame);
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify({ sessionId: data.sessionId, wsToken: data.wsToken }));
|
||||
return data.sessionId;
|
||||
}, [openWs]);
|
||||
|
||||
// 页面刷新/从别的菜单切回来时调用:看localStorage里有没有存过还没关闭的会话,
|
||||
// 有的话直接问后端"还在不在、走到哪一步了",在的话重连WebSocket接回去,
|
||||
// 不用重新新建浏览器、不用重新登录。返回 phase(不在/已过期时返回 null)。
|
||||
const restore = useCallback(async (onFrame) => {
|
||||
const saved = localStorage.getItem(STORAGE_KEY);
|
||||
if (!saved) return null;
|
||||
let parsed;
|
||||
try { parsed = JSON.parse(saved); } catch (e) { localStorage.removeItem(STORAGE_KEY); return null; }
|
||||
try {
|
||||
const res = await fetch(`/api/mic-upload/sessions/${parsed.sessionId}`);
|
||||
if (!res.ok) { localStorage.removeItem(STORAGE_KEY); return null; }
|
||||
const data = await res.json();
|
||||
setSessionId(parsed.sessionId);
|
||||
setWsToken(parsed.wsToken);
|
||||
openWs(parsed.sessionId, parsed.wsToken, onFrame);
|
||||
return data.phase || 'browsing';
|
||||
} catch (e) {
|
||||
return null;
|
||||
}
|
||||
}, [openWs]);
|
||||
|
||||
const stop = useCallback(async () => {
|
||||
if (wsRef.current) { try { wsRef.current.close(); } catch (e) {} wsRef.current = null; }
|
||||
if (sessionId) {
|
||||
try { await fetch(`/api/mic-upload/sessions/${sessionId}`, { method: 'DELETE' }); } catch (e) {}
|
||||
}
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
setSessionId(null);
|
||||
setWsToken(null);
|
||||
setConnected(false);
|
||||
|
|
@ -77,9 +101,11 @@ function useMicSession() {
|
|||
}
|
||||
}, []);
|
||||
|
||||
// 组件卸载(切到别的菜单)时只关WebSocket连接,后端的浏览器会话继续留着——
|
||||
// sessionId已经存进了localStorage,切回来时用 restore() 重新接上就行。
|
||||
useEffect(() => () => { if (wsRef.current) wsRef.current.close(); }, []);
|
||||
|
||||
return { sessionId, wsToken, connected, logs, start, stop, sendInput };
|
||||
return { sessionId, wsToken, connected, logs, start, restore, stop, sendInput };
|
||||
}
|
||||
|
||||
// canvas <-> 远程浏览器坐标换算:canvas按容器宽度等比缩放显示,鼠标事件按缩放比换算回1280x800
|
||||
|
|
@ -305,7 +331,7 @@ function TemplateConfigPanel({ productCategories, onClose }) {
|
|||
}
|
||||
|
||||
export function MicUploadPage({ products = [], productCategories = [] }) {
|
||||
const { sessionId, connected, logs, start, stop, sendInput } = useMicSession();
|
||||
const { sessionId, connected, logs, start, restore, stop, sendInput } = useMicSession();
|
||||
const { canvasRef, drawFrame, handlers } = useRemoteCanvas(sendInput);
|
||||
|
||||
const [phase, setPhase] = useState('idle'); // idle -> browsing -> ready -> filling -> filled -> submitted
|
||||
|
|
@ -316,6 +342,17 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
|
|||
const [showTemplateConfig, setShowTemplateConfig] = useState(false);
|
||||
const logEndRef = useRef(null);
|
||||
|
||||
// 挂载时(首次进这个页面,或者从别的菜单切回来)先看看有没有一个还活着的
|
||||
// 会话可以接回去,有就直接恢复到对应阶段,不用重新新建/重新登录。
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
restore(drawFrame).then((restoredPhase) => {
|
||||
if (!cancelled && restoredPhase) setPhase(restoredPhase);
|
||||
});
|
||||
return () => { cancelled = true; };
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
useEffect(() => { logEndRef.current?.scrollIntoView({ block: 'end' }); }, [logs]);
|
||||
|
||||
const filteredProducts = products.filter((p) => {
|
||||
|
|
@ -340,7 +377,10 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
|
|||
}
|
||||
}
|
||||
|
||||
function handleLoginConfirmed() {
|
||||
async function handleLoginConfirmed() {
|
||||
if (sessionId) {
|
||||
try { await fetch(`/api/mic-upload/sessions/${sessionId}/mark-ready`, { method: 'POST' }); } catch (e) {}
|
||||
}
|
||||
setPhase('ready');
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue