Update MicUploadPage.jsx via upload script - 2026-09-21 01:11:06
This commit is contained in:
parent
4e3b2131e0
commit
d58c0cc1f4
|
|
@ -18,7 +18,6 @@
|
|||
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);
|
||||
|
|
@ -51,7 +50,10 @@ function useMicSession() {
|
|||
wsRef.current = ws;
|
||||
}, [appendLog]);
|
||||
|
||||
const start = useCallback(async (onFrame) => {
|
||||
// 单例会话:POST这个接口既是"新建"也是"接上已经在跑的那个",后端会自己判断
|
||||
// 要不要真的启动新浏览器。所以组件挂载时(首次进这个页面/刷新/从别的菜单
|
||||
// 切回来)都直接调它就够了,不需要额外猜"之前的会话还在不在"。
|
||||
const connect = useCallback(async (onFrame) => {
|
||||
setLogs([]);
|
||||
const res = await fetch('/api/mic-upload/sessions', { method: 'POST' });
|
||||
const data = await res.json();
|
||||
|
|
@ -59,29 +61,7 @@ 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;
|
||||
}
|
||||
return data; // { sessionId, wsToken, phase, reused }
|
||||
}, [openWs]);
|
||||
|
||||
const stop = useCallback(async () => {
|
||||
|
|
@ -89,7 +69,6 @@ function useMicSession() {
|
|||
if (sessionId) {
|
||||
try { await fetch(`/api/mic-upload/sessions/${sessionId}`, { method: 'DELETE' }); } catch (e) {}
|
||||
}
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
setSessionId(null);
|
||||
setWsToken(null);
|
||||
setConnected(false);
|
||||
|
|
@ -102,10 +81,10 @@ function useMicSession() {
|
|||
}, []);
|
||||
|
||||
// 组件卸载(切到别的菜单)时只关WebSocket连接,后端的浏览器会话继续留着——
|
||||
// sessionId已经存进了localStorage,切回来时用 restore() 重新接上就行。
|
||||
// 是单例的,切回来时挂载effect会重新POST /sessions,自动接上同一个。
|
||||
useEffect(() => () => { if (wsRef.current) wsRef.current.close(); }, []);
|
||||
|
||||
return { sessionId, wsToken, connected, logs, start, restore, stop, sendInput };
|
||||
return { sessionId, wsToken, connected, logs, connect, stop, sendInput };
|
||||
}
|
||||
|
||||
// canvas <-> 远程浏览器坐标换算:canvas按容器宽度等比缩放显示,鼠标事件按缩放比换算回1280x800
|
||||
|
|
@ -331,10 +310,11 @@ function TemplateConfigPanel({ productCategories, onClose }) {
|
|||
}
|
||||
|
||||
export function MicUploadPage({ products = [], productCategories = [] }) {
|
||||
const { sessionId, connected, logs, start, restore, stop, sendInput } = useMicSession();
|
||||
const { sessionId, connected, logs, connect, stop, sendInput } = useMicSession();
|
||||
const { canvasRef, drawFrame, handlers } = useRemoteCanvas(sendInput);
|
||||
|
||||
const [phase, setPhase] = useState('idle'); // idle -> browsing -> ready -> filling -> filled -> submitted
|
||||
// connecting -> browsing -> ready -> filling -> filled -> submitted(还有个 error 态)
|
||||
const [phase, setPhase] = useState('connecting');
|
||||
const [selectedProductId, setSelectedProductId] = useState('');
|
||||
const [search, setSearch] = useState('');
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
|
@ -342,13 +322,30 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
|
|||
const [showTemplateConfig, setShowTemplateConfig] = useState(false);
|
||||
const logEndRef = useRef(null);
|
||||
|
||||
// 挂载时(首次进这个页面,或者从别的菜单切回来)先看看有没有一个还活着的
|
||||
// 会话可以接回去,有就直接恢复到对应阶段,不用重新新建/重新登录。
|
||||
// 挂载时(首次进这个页面/刷新/从别的菜单切回来)都直接"接上"后端那个长期
|
||||
// 存在的单例浏览器会话——已经登录过的话这里会直接跳到 ready,不用再点
|
||||
// "新建会话"。只有第一次真正创建全新浏览器时才会自动顺手打开登录页。
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
restore(drawFrame).then((restoredPhase) => {
|
||||
if (!cancelled && restoredPhase) setPhase(restoredPhase);
|
||||
});
|
||||
async function boot() {
|
||||
try {
|
||||
const data = await connect(drawFrame);
|
||||
if (cancelled) return;
|
||||
if (data.phase === 'ready') {
|
||||
setPhase('ready');
|
||||
} else if (!data.reused) {
|
||||
// 全新浏览器,页面是空白的,顺手直接打开登录页
|
||||
await fetch(`/api/mic-upload/sessions/${data.sessionId}/login`, { method: 'POST' });
|
||||
if (!cancelled) setPhase('browsing');
|
||||
} else {
|
||||
// 接上了一个还没登录完的旧会话,画面上是它原来的样子,不用重新导航
|
||||
setPhase('browsing');
|
||||
}
|
||||
} catch (e) {
|
||||
if (!cancelled) { setError(e.message); setPhase('error'); }
|
||||
}
|
||||
}
|
||||
boot();
|
||||
return () => { cancelled = true; };
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
|
@ -361,15 +358,30 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
|
|||
return (p.name || '').toLowerCase().includes(q) || (p.brand || '').toLowerCase().includes(q);
|
||||
});
|
||||
|
||||
async function handleOpenLogin() {
|
||||
async function handleRetryConnect() {
|
||||
setError('');
|
||||
setPhase('connecting');
|
||||
try {
|
||||
const data = await connect(drawFrame);
|
||||
if (data.phase === 'ready') setPhase('ready');
|
||||
else setPhase('browsing');
|
||||
} catch (e) {
|
||||
setError(e.message);
|
||||
setPhase('error');
|
||||
}
|
||||
}
|
||||
|
||||
// 万一MIC那边的登录态失效了(cookie过期之类),手动强制跳回登录页用这个,
|
||||
// 不用整个重新连接会话。
|
||||
async function handleManualGotoLogin() {
|
||||
if (!sessionId) { setError('会话已断开'); return; }
|
||||
setError('');
|
||||
setBusy(true);
|
||||
try {
|
||||
const id = await start(drawFrame);
|
||||
setPhase('browsing');
|
||||
const res = await fetch(`/api/mic-upload/sessions/${id}/login`, { method: 'POST' });
|
||||
const res = await fetch(`/api/mic-upload/sessions/${sessionId}/login`, { method: 'POST' });
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || '打开登录页失败');
|
||||
setPhase('browsing');
|
||||
} catch (e) {
|
||||
setError(e.message);
|
||||
} finally {
|
||||
|
|
@ -441,8 +453,10 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
|
|||
|
||||
async function handleClose() {
|
||||
await stop();
|
||||
setPhase('idle');
|
||||
setSelectedProductId('');
|
||||
// 关掉之后自动重新连接一次——这会真正启动一个全新的浏览器(旧的已经关了),
|
||||
// 相当于手动"重启"这个模块,不用刷新整个页面。
|
||||
await handleRetryConnect();
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
@ -452,13 +466,15 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
|
|||
<div className="panel-title">MIC产品上传</div>
|
||||
<button className="btn-ghost" onClick={() => setShowTemplateConfig(true)}>配置发布模板</button>
|
||||
|
||||
{phase === 'idle' && (
|
||||
<button className="btn-primary" disabled={busy} onClick={handleOpenLogin}>
|
||||
{busy ? '启动中...' : '新建会话并打开登录页'}
|
||||
</button>
|
||||
{phase === 'connecting' && (
|
||||
<div style={{ fontSize: 13, color: 'var(--ink-soft, #888)' }}>连接中...</div>
|
||||
)}
|
||||
|
||||
{phase !== 'idle' && (
|
||||
{phase === 'error' && (
|
||||
<button className="btn-primary" onClick={handleRetryConnect}>重试连接</button>
|
||||
)}
|
||||
|
||||
{phase !== 'connecting' && phase !== 'error' && (
|
||||
<>
|
||||
<div style={{ fontSize: 12, color: 'var(--ink-soft, #888)' }}>
|
||||
画面状态:{connected ? '已连接' : '连接中...'}
|
||||
|
|
@ -509,7 +525,13 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
|
|||
<div style={{ color: 'var(--success, #2a8f4d)', fontSize: 13 }}>✅ 已提交,请在画面里核对发布结果</div>
|
||||
)}
|
||||
|
||||
<button className="btn-ghost" onClick={handleClose}>关闭会话</button>
|
||||
{phase !== 'browsing' && (
|
||||
<button className="btn-ghost" disabled={busy} onClick={handleManualGotoLogin}>
|
||||
重新打开登录页(登录态失效时用)
|
||||
</button>
|
||||
)}
|
||||
|
||||
<button className="btn-ghost" onClick={handleClose}>关闭会话(只是关掉浏览器进程释放内存,登录态存在磁盘上,下次重新连接大概率还是已登录)</button>
|
||||
</>
|
||||
)}
|
||||
|
||||
|
|
@ -527,9 +549,13 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
|
|||
|
||||
{/* 右:远程浏览器画面 */}
|
||||
<div style={{ flex: 1, minWidth: 0, background: '#000', borderRadius: 8, overflow: 'hidden', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
|
||||
{phase === 'idle' ? (
|
||||
<div style={{ color: '#888' }}>点击左侧"新建会话"开始</div>
|
||||
) : (
|
||||
{phase === 'connecting' && (
|
||||
<div style={{ color: '#888' }}>连接中...</div>
|
||||
)}
|
||||
{phase === 'error' && (
|
||||
<div style={{ color: '#888' }}>连接失败,点左侧"重试连接"</div>
|
||||
)}
|
||||
{phase !== 'connecting' && phase !== 'error' && (
|
||||
<canvas
|
||||
ref={canvasRef}
|
||||
style={{ width: '100%', height: 'auto', cursor: 'default' }}
|
||||
|
|
|
|||
Loading…
Reference in New Issue