Update MicUploadPage.jsx via upload script - 2026-09-21 00:31:10

This commit is contained in:
mike 2026-09-21 00:31:18 +08:00
parent e266e14f1e
commit c98c027fea
1 changed files with 185 additions and 3 deletions

View File

@ -10,8 +10,8 @@
// UploadCloud 需要从 lucide-react 多 import 一个图标。 // UploadCloud 需要从 lucide-react 多 import 一个图标。
// //
// 3. 渲染switch里(App.jsx约3722-3724行 backpanel那块下面)加: // 3. 渲染switch里(App.jsx约3722-3724行 backpanel那块下面)加:
// {view === 'mic-upload' && <MicUploadPage products={products} />} // {view === 'mic-upload' && <MicUploadPage products={products} productCategories={productCategories} />}
// products 就是 App.jsx 里已有的那个产品列表 state(约2225行 useState([]))。 // products / productCategories 都是 App.jsx 里已有的那两个state(约2225/2274行)。
// //
// 本组件是单模式(一次只处理一个产品),批量队列以后再加。 // 本组件是单模式(一次只处理一个产品),批量队列以后再加。
@ -145,7 +145,166 @@ function KeyCaptureInput({ sendInput }) {
); );
} }
export function MicUploadPage({ products = [] }) { // 已保存的MIC账号/密码——只存是否已保存(hasEmail/hasPassword)在前端state,
// 密码明文从不出现在前端:保存时直接PUT给后端加密存储;使用时点"填入"按钮,
// 后端解密后直接当键盘输入发给远程浏览器,不经过任何前端网络请求。
function SavedCredentialsPanel({ sessionId, sendAutofill }) {
const [hasEmail, setHasEmail] = useState(false);
const [hasPassword, setHasPassword] = useState(false);
const [emailInput, setEmailInput] = useState('');
const [passwordInput, setPasswordInput] = useState('');
const [saving, setSaving] = useState(false);
const [msg, setMsg] = useState('');
useEffect(() => {
fetch('/api/mic-upload/credentials')
.then((r) => r.json())
.then((d) => { setHasEmail(!!d.hasEmail); setHasPassword(!!d.hasPassword); })
.catch(() => {});
}, []);
async function handleSave() {
if (!emailInput && !passwordInput) return;
setSaving(true);
setMsg('');
try {
const res = await fetch('/api/mic-upload/credentials', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email: emailInput || undefined, password: passwordInput || undefined }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error || '保存失败');
setHasEmail(data.hasEmail);
setHasPassword(data.hasPassword);
setEmailInput('');
setPasswordInput('');
setMsg('已保存');
} catch (e) {
setMsg(e.message);
} finally {
setSaving(false);
}
}
return (
<div style={{ border: '1px solid var(--line, #ddd)', borderRadius: 6, padding: 8, display: 'flex', flexDirection: 'column', gap: 6 }}>
<div style={{ fontSize: 12, color: 'var(--ink-soft, #888)' }}>
已保存:账号{hasEmail ? '✓' : '未保存'} / 密码{hasPassword ? '✓' : '未保存'}
</div>
<input
placeholder="MIC账号邮箱(留空=不改)"
value={emailInput}
onChange={(e) => setEmailInput(e.target.value)}
style={{ padding: '6px 8px', fontSize: 13 }}
/>
<input
type="password"
placeholder="MIC密码(留空=不改)"
value={passwordInput}
onChange={(e) => setPasswordInput(e.target.value)}
style={{ padding: '6px 8px', fontSize: 13 }}
/>
<button className="btn-secondary" disabled={saving || (!emailInput && !passwordInput)} onClick={handleSave}>
{saving ? '保存中...' : '保存账号/密码'}
</button>
{msg && <div style={{ fontSize: 12 }}>{msg}</div>}
{sessionId && (
<div style={{ display: 'flex', gap: 6 }}>
<button className="btn-ghost" disabled={!hasEmail} style={{ flex: 1 }} onClick={() => sendAutofill('email')}>
填入账号
</button>
<button className="btn-ghost" disabled={!hasPassword} style={{ flex: 1 }} onClick={() => sendAutofill('password')}>
填入密码
</button>
</div>
)}
<div style={{ fontSize: 11, color: 'var(--ink-soft, #888)' }}>
用法:先点一下右侧画面里要填的那个输入框(让它获得焦点),再点这里的"填入"按钮。
</div>
</div>
);
}
// 品类 -> MIC仿造发布模板URL 配置面板。列表来自CRM"产品分组"里的品类树,
// 每个细分品类(type)一行,读写走 /api/mic-upload/templates。
function TemplateConfigPanel({ productCategories, onClose }) {
const [values, setValues] = useState({});
const [loading, setLoading] = useState(true);
const [saving, setSaving] = useState(false);
const [msg, setMsg] = useState('');
useEffect(() => {
fetch('/api/mic-upload/templates')
.then((r) => r.json())
.then((d) => setValues(d.value || {}))
.catch(() => {})
.finally(() => setLoading(false));
}, []);
async function handleSave() {
setSaving(true);
setMsg('');
try {
const res = await fetch('/api/mic-upload/templates', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ value: values }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error || '保存失败');
setMsg('已保存');
} catch (e) {
setMsg(e.message);
} finally {
setSaving(false);
}
}
return (
<div style={{ position: 'fixed', inset: 0, background: 'rgba(0,0,0,0.4)', display: 'flex', alignItems: 'center', justifyContent: 'center', zIndex: 50 }}>
<div style={{ background: '#fff', borderRadius: 8, padding: 20, width: 520, maxHeight: '80vh', overflowY: 'auto' }}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 10 }}>
<div className="panel-title">品类发布模板配置</div>
<button className="btn-ghost" onClick={onClose}>关闭</button>
</div>
<div style={{ fontSize: 12, color: 'var(--ink-soft, #888)', marginBottom: 10 }}>
每个品类填一个"仿造发布"模板产品的完整URL(形如
https://membercenter.made-in-china.com/productmanage.do?xcase=addSimilar&prodId=xxxx),
需要你先在MIC后台为每个品类找一个字段结构合适的已发布产品。留空的品类点"开始自动填写"时会报错提示先配置。
</div>
{loading ? (
<div>加载中...</div>
) : (
<>
{(productCategories || []).map((cat) => (
<div key={cat.id} style={{ marginBottom: 10 }}>
<div style={{ fontSize: 13, fontWeight: 600, marginBottom: 4 }}>{cat.name}</div>
{(cat.types || []).map((typeName) => (
<div key={typeName} style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 4 }}>
<div style={{ width: 120, fontSize: 12, flexShrink: 0 }}>{typeName}</div>
<input
placeholder="addSimilar模板URL"
value={values[typeName] || ''}
onChange={(e) => setValues((prev) => ({ ...prev, [typeName]: e.target.value }))}
style={{ flex: 1, padding: '4px 6px', fontSize: 12 }}
/>
</div>
))}
</div>
))}
<button className="btn-primary" disabled={saving} onClick={handleSave}>
{saving ? '保存中...' : '保存全部'}
</button>
{msg && <span style={{ marginLeft: 8, fontSize: 12 }}>{msg}</span>}
</>
)}
</div>
</div>
);
}
export function MicUploadPage({ products = [], productCategories = [] }) {
const { sessionId, connected, logs, start, stop, sendInput } = useMicSession(); const { sessionId, connected, logs, start, stop, sendInput } = useMicSession();
const { canvasRef, drawFrame, handlers } = useRemoteCanvas(sendInput); const { canvasRef, drawFrame, handlers } = useRemoteCanvas(sendInput);
@ -154,6 +313,7 @@ export function MicUploadPage({ products = [] }) {
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
const [busy, setBusy] = useState(false); const [busy, setBusy] = useState(false);
const [error, setError] = useState(''); const [error, setError] = useState('');
const [showTemplateConfig, setShowTemplateConfig] = useState(false);
const logEndRef = useRef(null); const logEndRef = useRef(null);
useEffect(() => { logEndRef.current?.scrollIntoView({ block: 'end' }); }, [logs]); useEffect(() => { logEndRef.current?.scrollIntoView({ block: 'end' }); }, [logs]);
@ -184,6 +344,22 @@ export function MicUploadPage({ products = [] }) {
setPhase('ready'); setPhase('ready');
} }
async function sendAutofill(field) {
if (!sessionId) { setError('会话已断开'); return; }
setError('');
try {
const res = await fetch(`/api/mic-upload/sessions/${sessionId}/autofill`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ field }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error || '填入失败');
} catch (e) {
setError(e.message);
}
}
async function handleFill() { async function handleFill() {
if (!sessionId) { setError('会话已断开,请重新打开登录页'); return; } if (!sessionId) { setError('会话已断开,请重新打开登录页'); return; }
if (!selectedProductId) { setError('请先选择一个产品'); return; } if (!selectedProductId) { setError('请先选择一个产品'); return; }
@ -234,6 +410,7 @@ export function MicUploadPage({ products = [] }) {
{/* 左:产品选择 + 流程控制 */} {/* 左:产品选择 + 流程控制 */}
<div style={{ width: 300, flexShrink: 0, display: 'flex', flexDirection: 'column', gap: 12 }}> <div style={{ width: 300, flexShrink: 0, display: 'flex', flexDirection: 'column', gap: 12 }}>
<div className="panel-title">MIC产品上传</div> <div className="panel-title">MIC产品上传</div>
<button className="btn-ghost" onClick={() => setShowTemplateConfig(true)}>配置发布模板</button>
{phase === 'idle' && ( {phase === 'idle' && (
<button className="btn-primary" disabled={busy} onClick={handleOpenLogin}> <button className="btn-primary" disabled={busy} onClick={handleOpenLogin}>
@ -249,6 +426,7 @@ export function MicUploadPage({ products = [] }) {
{(phase === 'browsing') && ( {(phase === 'browsing') && (
<> <>
<SavedCredentialsPanel sessionId={sessionId} sendAutofill={sendAutofill} />
<KeyCaptureInput sendInput={sendInput} /> <KeyCaptureInput sendInput={sendInput} />
<button className="btn-secondary" onClick={handleLoginConfirmed}>已登录,继续</button> <button className="btn-secondary" onClick={handleLoginConfirmed}>已登录,继续</button>
</> </>
@ -322,6 +500,10 @@ export function MicUploadPage({ products = [] }) {
/> />
)} )}
</div> </div>
{showTemplateConfig && (
<TemplateConfigPanel productCategories={productCategories} onClose={() => setShowTemplateConfig(false)} />
)}
</div> </div>
); );
} }