Update MicUploadPage.jsx via upload script - 2026-09-21 11:14:29

This commit is contained in:
mike 2026-09-21 11:14:40 +08:00
parent d299567dd3
commit 5a121d0cbb
1 changed files with 71 additions and 23 deletions

View File

@ -240,7 +240,7 @@ function TemplateConfigPanel({ productCategories, onClose }) {
const [msg, setMsg] = useState('');
useEffect(() => {
fetch('/api/mic-upload/templates')
fetch('/api/mic-upload/templates', { cache: 'no-store' })
.then((r) => r.json())
.then((d) => setValues(d.value || {}))
.catch(() => {})
@ -315,7 +315,8 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
// connecting -> browsing -> ready -> filling -> filled -> submitted(还有个 error 态)
const [phase, setPhase] = useState('connecting');
const [selectedProductId, setSelectedProductId] = useState('');
const [queueIds, setQueueIds] = useState([]); // 选中要批量处理的产品,按点击顺序
const [queueIndex, setQueueIndex] = useState(-1); // 当前处理到队列里第几个,-1=还没开始
const [search, setSearch] = useState('');
const [busy, setBusy] = useState(false);
const [error, setError] = useState('');
@ -412,9 +413,11 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
}
}
async function handleFill() {
// 按队列顺序处理第idx个产品:导航到模板+AI生成标题关键词+填表,不自动提交
async function runFillForIndex(idx) {
if (!sessionId) { setError('会话已断开,请重新打开登录页'); return; }
if (!selectedProductId) { setError('请先选择一个产品'); return; }
const productId = queueIds[idx];
if (!productId) { setError('队列为空'); return; }
setError('');
setBusy(true);
setPhase('filling');
@ -422,10 +425,11 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
const res = await fetch(`/api/mic-upload/sessions/${sessionId}/publish`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ productId: selectedProductId }),
body: JSON.stringify({ productId }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error || '自动填写失败');
setQueueIndex(idx);
setPhase('filled');
} catch (e) {
setError(e.message);
@ -435,6 +439,11 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
}
}
async function handleFill() {
if (queueIds.length === 0) { setError('请先选择至少一个产品'); return; }
await runFillForIndex(0);
}
async function handleSubmit() {
if (!sessionId) { setError('会话已断开,请重新打开登录页'); return; }
setError('');
@ -443,17 +452,37 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
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');
const nextIdx = queueIndex + 1;
if (nextIdx < queueIds.length) {
// 队列里还有下一个——自动跳去填下一个,提交仍然要人工再点一次确认
setBusy(false);
await runFillForIndex(nextIdx);
} else {
setPhase('submitted');
setQueueIds([]);
setQueueIndex(-1);
setBusy(false);
}
} catch (e) {
setError(e.message);
} finally {
setBusy(false);
}
}
function toggleQueue(productId) {
// 提交完一批之后phase停在'submitted',这时候点选新产品要能重新回到'ready'
// 才会显示"开始自动填写"按钮;处理中(filling/filled)不允许中途改选,忽略点击。
if (phase === 'filling' || phase === 'filled') return;
if (phase === 'submitted') setPhase('ready');
setQueueIds((prev) => (
prev.includes(productId) ? prev.filter((id) => id !== productId) : [...prev, productId]
));
}
async function handleClose() {
await stop();
setSelectedProductId('');
setQueueIds([]);
setQueueIndex(-1);
// 关掉之后自动重新连接一次——这会真正启动一个全新的浏览器(旧的已经关了),
// 相当于手动"重启"这个模块,不用刷新整个页面。
await handleRetryConnect();
@ -501,11 +530,12 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
<div style={{ padding: 8, fontSize: 12, color: 'var(--ink-soft, #888)' }}>没有匹配的产品</div>
)}
{filteredProducts.map((p) => {
const selected = selectedProductId === p.id;
const orderIdx = queueIds.indexOf(p.id);
const selected = orderIdx >= 0;
return (
<div
key={p.id}
onClick={() => setSelectedProductId(p.id)}
onClick={() => toggleQueue(p.id)}
style={{
display: 'flex', justifyContent: 'space-between', alignItems: 'center',
padding: '6px 8px', fontSize: 13, cursor: 'pointer',
@ -514,32 +544,50 @@ export function MicUploadPage({ products = [], productCategories = [] }) {
}}
>
<span>{p.brand ? `[${p.brand}] ` : ''}{p.name}</span>
<span style={{ fontWeight: 700, color: selected ? 'var(--brand, #d9822b)' : '#999', width: 16, textAlign: 'center' }}>
{selected ? '✓' : '+'}
<span style={{
fontWeight: 700, color: selected ? 'var(--brand, #d9822b)' : '#999',
minWidth: 16, textAlign: 'center', fontSize: 11,
}}>
{selected ? `#${orderIdx + 1}` : '+'}
</span>
</div>
);
})}
</div>
</>
)}
{phase === 'ready' && (
<>
<button className="btn-primary" disabled={busy} onClick={handleFill}>
{busy ? '正在填写...' : '开始自动填写'}
</button>
{!selectedProductId && (
{queueIds.length > 0 && (
<div style={{ fontSize: 11, color: 'var(--ink-soft, #888)' }}>
提示:先在上面的列表里点选一个产品(不是只搜索),选中后这里才会真正开始填写。
已选 {queueIds.length} 个,按点选顺序依次处理。再点一下已选的行可以取消。
</div>
)}
</>
)}
{phase === 'ready' && (
<>
<button className="btn-primary" disabled={busy || queueIds.length === 0} onClick={handleFill}>
{busy ? '正在填写...' : queueIds.length > 1 ? `开始批量填写(共${queueIds.length}个)` : '开始自动填写'}
</button>
{queueIds.length === 0 && (
<div style={{ fontSize: 11, color: 'var(--ink-soft, #888)' }}>
提示:先在上面的列表里点选一个或多个产品(不是只搜索),选中后这里才会真正开始填写。
</div>
)}
</>
)}
{(phase === 'filling' || phase === 'filled') && queueIds.length > 1 && (
<div style={{ fontSize: 12, color: 'var(--ink-soft, #888)' }}>
批量进度:第 {queueIndex + 1} / {queueIds.length} 个
</div>
)}
{phase === 'filled' && (
<button className="btn-primary" disabled={busy} onClick={handleSubmit}>
{busy ? '提交中...' : '确认无误,提交发布'}
{busy
? '提交中...'
: queueIndex + 1 < queueIds.length
? '确认无误,提交并填写下一个'
: '确认无误,提交发布'}
</button>
)}