Update MicUploadPage.jsx via upload script - 2026-09-20 17:32:53

This commit is contained in:
mike 2026-09-20 17:33:01 +08:00
parent f8ade1be39
commit 20ec6b2106
1 changed files with 328 additions and 0 deletions

328
MicUploadPage.jsx Normal file
View File

@ -0,0 +1,328 @@
// MIC产品上传 — 前端页面
//
// 接入方式(对照 App.jsx 里"后板图模板"菜单项的现成写法):
//
// 1. import { MicUploadPage } from './mic-upload/MicUploadPage';
// (或者把这个文件内容直接并入 App.jsx,看你想不想保持单文件习惯)
//
// 2. NAV_GROUPS 里"产品"分组,在 backpanel 那一项下面加一行(App.jsx 约2121行):
// { id: 'mic-upload', label: 'MIC产品上传', icon: UploadCloud },
// UploadCloud 需要从 lucide-react 多 import 一个图标。
//
// 3. 渲染switch里(App.jsx约3722-3724行 backpanel那块下面)加:
// {view === 'mic-upload' && <MicUploadPage products={products} />}
// products 就是 App.jsx 里已有的那个产品列表 state(约2225行 useState([]))。
//
// 本组件是单模式(一次只处理一个产品),批量队列以后再加。
import React, { useState, useRef, useCallback, useEffect } from 'react';
const VIEWPORT = { width: 1280, height: 800 };
function useMicSession() {
const [sessionId, setSessionId] = useState(null);
const [wsToken, setWsToken] = useState(null);
const [connected, setConnected] = useState(false);
const [logs, setLogs] = useState([]);
const wsRef = useRef(null);
const frameCallbackRef = useRef(null);
const appendLog = useCallback((entry) => {
setLogs((prev) => [...prev.slice(-300), entry]);
}, []);
const openWs = useCallback((id, token, onFrame) => {
frameCallbackRef.current = onFrame;
const proto = window.location.protocol === 'https:' ? 'wss' : 'ws';
const ws = new WebSocket(`${proto}://${window.location.host}/ws/mic-upload?sessionId=${id}&token=${token}`);
ws.onopen = () => setConnected(true);
ws.onclose = () => setConnected(false);
ws.onerror = () => setConnected(false);
ws.onmessage = (ev) => {
let msg;
try { msg = JSON.parse(ev.data); } catch (e) { return; }
if (msg.type === 'frame') {
frameCallbackRef.current && frameCallbackRef.current(msg.data);
} else if (msg.type === 'log') {
appendLog(msg);
}
};
wsRef.current = ws;
}, [appendLog]);
const start = useCallback(async (onFrame) => {
setLogs([]);
const res = await fetch('/api/mic-upload/sessions', { method: 'POST' });
const data = await res.json();
if (!res.ok) throw new Error(data.error || '启动会话失败');
setSessionId(data.sessionId);
setWsToken(data.wsToken);
openWs(data.sessionId, data.wsToken, onFrame);
return data.sessionId;
}, [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) {}
}
setSessionId(null);
setWsToken(null);
setConnected(false);
}, [sessionId]);
const sendInput = useCallback((msg) => {
if (wsRef.current && wsRef.current.readyState === WebSocket.OPEN) {
wsRef.current.send(JSON.stringify(msg));
}
}, []);
useEffect(() => () => { if (wsRef.current) wsRef.current.close(); }, []);
return { sessionId, wsToken, connected, logs, start, stop, sendInput };
}
// canvas <-> 远程浏览器坐标换算:canvas按容器宽度等比缩放显示,鼠标事件按缩放比换算回1280x800
function useRemoteCanvas(sendInput) {
const canvasRef = useRef(null);
const scaleRef = useRef(1);
const drawFrame = useCallback((base64Jpeg) => {
const canvas = canvasRef.current;
if (!canvas) return;
const ctx = canvas.getContext('2d');
const img = new Image();
img.onload = () => {
canvas.width = VIEWPORT.width;
canvas.height = VIEWPORT.height;
ctx.drawImage(img, 0, 0, VIEWPORT.width, VIEWPORT.height);
};
img.src = `data:image/jpeg;base64,${base64Jpeg}`;
}, []);
const toRemoteCoords = useCallback((e) => {
const canvas = canvasRef.current;
const rect = canvas.getBoundingClientRect();
scaleRef.current = VIEWPORT.width / rect.width;
const x = Math.round((e.clientX - rect.left) * scaleRef.current);
const y = Math.round((e.clientY - rect.top) * scaleRef.current);
return { x, y };
}, []);
const handlers = {
onMouseMove: (e) => { const { x, y } = toRemoteCoords(e); sendInput({ type: 'mousemove', x, y }); },
onMouseDown: (e) => { const { x, y } = toRemoteCoords(e); sendInput({ type: 'mousedown', x, y, button: 'left' }); },
onMouseUp: (e) => { const { x, y } = toRemoteCoords(e); sendInput({ type: 'mouseup', x, y, button: 'left' }); },
onWheel: (e) => { sendInput({ type: 'wheel', dx: e.deltaX, dy: e.deltaY }); e.preventDefault(); },
};
return { canvasRef, drawFrame, handlers };
}
// 捕获键盘输入的隐藏input——登录表单里的账号密码靠这个转发过去
function KeyCaptureInput({ sendInput }) {
const [value, setValue] = useState('');
return (
<input
className="mic-key-capture"
value={value}
placeholder="点这里输入账号/密码/验证码,会实时转发到远程浏览器"
onChange={(e) => {
const next = e.target.value;
if (next.length > value.length) {
sendInput({ type: 'text', text: next.slice(value.length) });
} else if (next.length < value.length) {
for (let i = 0; i < value.length - next.length; i++) sendInput({ type: 'keydown', key: 'Backspace' });
}
setValue(next);
}}
onKeyDown={(e) => {
if (e.key === 'Enter') { sendInput({ type: 'keydown', key: 'Enter' }); setValue(''); }
if (e.key === 'Tab') { sendInput({ type: 'keydown', key: 'Tab' }); e.preventDefault(); }
}}
style={{ width: '100%', padding: '8px 10px', fontSize: 13, border: '1px solid var(--line, #ddd)', borderRadius: 6 }}
/>
);
}
export function MicUploadPage({ products = [] }) {
const { sessionId, connected, logs, start, stop, sendInput } = useMicSession();
const { canvasRef, drawFrame, handlers } = useRemoteCanvas(sendInput);
const [phase, setPhase] = useState('idle'); // idle -> browsing -> ready -> filling -> filled -> submitted
const [selectedProductId, setSelectedProductId] = useState('');
const [search, setSearch] = useState('');
const [busy, setBusy] = useState(false);
const [error, setError] = useState('');
const logEndRef = useRef(null);
useEffect(() => { logEndRef.current?.scrollIntoView({ block: 'end' }); }, [logs]);
const filteredProducts = products.filter((p) => {
if (!search.trim()) return true;
const q = search.trim().toLowerCase();
return (p.name || '').toLowerCase().includes(q) || (p.brand || '').toLowerCase().includes(q);
});
async function handleOpenLogin() {
setError('');
setBusy(true);
try {
const id = await start(drawFrame);
setPhase('browsing');
const res = await fetch(`/api/mic-upload/sessions/${id}/login`, { method: 'POST' });
const data = await res.json();
if (!res.ok) throw new Error(data.error || '打开登录页失败');
} catch (e) {
setError(e.message);
} finally {
setBusy(false);
}
}
function handleLoginConfirmed() {
setPhase('ready');
}
async function handleFill() {
if (!sessionId) { setError('会话已断开,请重新打开登录页'); return; }
if (!selectedProductId) { setError('请先选择一个产品'); return; }
setError('');
setBusy(true);
setPhase('filling');
try {
const res = await fetch(`/api/mic-upload/sessions/${sessionId}/publish`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ productId: selectedProductId }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error || '自动填写失败');
setPhase('filled');
} catch (e) {
setError(e.message);
setPhase('ready');
} finally {
setBusy(false);
}
}
async function handleSubmit() {
if (!sessionId) { setError('会话已断开,请重新打开登录页'); return; }
setError('');
setBusy(true);
try {
const res = await fetch(`/api/mic-upload/sessions/${sessionId}/submit`, { method: 'POST' });
const data = await res.json();
if (!res.ok) throw new Error(data.error || '提交失败');
setPhase('submitted');
} catch (e) {
setError(e.message);
} finally {
setBusy(false);
}
}
async function handleClose() {
await stop();
setPhase('idle');
setSelectedProductId('');
}
return (
<div className="panel" style={{ display: 'flex', gap: 16, height: '100%' }}>
{/* 左:产品选择 + 流程控制 */}
<div style={{ width: 300, flexShrink: 0, display: 'flex', flexDirection: 'column', gap: 12 }}>
<div className="panel-title">MIC产品上传</div>
{phase === 'idle' && (
<button className="btn-primary" disabled={busy} onClick={handleOpenLogin}>
{busy ? '启动中...' : '新建会话并打开登录页'}
</button>
)}
{phase !== 'idle' && (
<>
<div style={{ fontSize: 12, color: 'var(--ink-soft, #888)' }}>
画面状态:{connected ? '已连接' : '连接中...'}
</div>
{(phase === 'browsing') && (
<>
<KeyCaptureInput sendInput={sendInput} />
<button className="btn-secondary" onClick={handleLoginConfirmed}>已登录,继续</button>
</>
)}
{(phase === 'ready' || phase === 'filling' || phase === 'filled' || phase === 'submitted') && (
<>
<input
placeholder="搜索产品(型号/品牌)"
value={search}
onChange={(e) => setSearch(e.target.value)}
style={{ padding: '6px 8px', fontSize: 13 }}
/>
<select
size={8}
value={selectedProductId}
onChange={(e) => setSelectedProductId(e.target.value)}
style={{ fontSize: 13 }}
>
{filteredProducts.map((p) => (
<option key={p.id} value={p.id}>{p.brand ? `[${p.brand}] ` : ''}{p.name}</option>
))}
</select>
</>
)}
{phase === 'ready' && (
<button className="btn-primary" disabled={busy || !selectedProductId} onClick={handleFill}>
{busy ? '正在填写...' : '开始自动填写'}
</button>
)}
{phase === 'filled' && (
<button className="btn-primary" disabled={busy} onClick={handleSubmit}>
{busy ? '提交中...' : '确认无误,提交发布'}
</button>
)}
{phase === 'submitted' && (
<div style={{ color: 'var(--success, #2a8f4d)', fontSize: 13 }}>✅ 已提交,请在画面里核对发布结果</div>
)}
<button className="btn-ghost" onClick={handleClose}>关闭会话</button>
</>
)}
{error && <div style={{ color: 'var(--danger, #d33)', fontSize: 13 }}>{error}</div>}
<div style={{ flex: 1, overflowY: 'auto', fontSize: 12, fontFamily: 'monospace', background: '#111', color: '#ddd', padding: 8, borderRadius: 6 }}>
{logs.map((l, i) => (
<div key={i} style={{ color: l.level === 'ERROR' ? '#ff6b6b' : l.level === 'WARNING' ? '#ffd166' : l.level === 'SUCCESS' ? '#6bd97a' : '#ddd' }}>
{l.msg}
</div>
))}
<div ref={logEndRef} />
</div>
</div>
{/* 右:远程浏览器画面 */}
<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>
) : (
<canvas
ref={canvasRef}
style={{ width: '100%', height: 'auto', cursor: 'default' }}
onMouseMove={handlers.onMouseMove}
onMouseDown={handlers.onMouseDown}
onMouseUp={handlers.onMouseUp}
onWheel={handlers.onWheel}
/>
)}
</div>
</div>
);
}