client/micUploadRoutes.js

460 lines
22 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// MIC产品上传 — 路由与 WebSocket 桥接
//
// 在 server.js 里这样接入(server 变量是 app.listen(...) 的返回值,必须在它
// 创建之后调用):
//
// const { registerMicUpload } = require('./mic-upload/micUploadRoutes');
// registerMicUpload(app, server, {
// requireAuth, readData, writeData, callDeepSeekWithRetry, loadAiConfig,
// encrypt, decrypt,
// });
//
// (这几个名字要跟 server.js 里已有的函数对上:requireAuth/readData/writeData/
// callDeepSeekWithRetry/loadAiConfig/encrypt/decrypt 都是 server.js 里已经定义
// 好的,直接传引用过来即可,不用改动它们本身。encrypt/decrypt 是给保存的MIC账号
// 密码加密用的,跟 ai-config.json 那套是同一份 AES-256-GCM 实现。)
//
// 需要 `npm install playwright ws`,并在VPS上跑一次
// `npx playwright install --with-deps chromium`(无头浏览器本体,体积较大,
// 装的时候留意磁盘空间)。
const crypto = require('crypto');
const WebSocket = require('ws');
const { MicBrowserSession } = require('./micBrowserSession');
const { buildAttributeList, sanitizeValue, parsePackageDimensionsCm } = require('./micFieldMapping');
// 标题/关键词规则来自MIC官方说明(2026-09抓取):
// 标题 https://service.made-in-china.com/help/promotionskill/product/3243.htm
// 关键词 https://service.made-in-china.com/help/promotionskill/product/473.htm
const _TITLE_SYSTEM_PROMPT = `You write English product titles for Made-in-China.com B2B listings, following the platform's official rules:
- 5 to 12 words total.
- Suggested structure: [modifier(s)] + [ONE core noun] + for [application scope] + with [certifications/key feature] + (model/standard/feature). Omit the "for"/"with" parts if there's nothing relevant to fill them with — never leave a literal "for"/"with" with nothing after it.
- The whole title must revolve around a SINGLE core noun (the main product category term, e.g. "UPS", "Inverter", "Battery"). Do not stack multiple synonyms for the same thing.
- Do not write an overly bare/generic title with no modifiers — add descriptive modifiers (capacity, technology, application) so it matches varied buyer search phrasing.
- Precision matters more than keyword stuffing.
Output ONLY the title text itself. No quotes, no markdown, no explanation.`;
const _KW_SYSTEM_PROMPT_TMPL = (coreNoun) => `You write English search keywords for a Made-in-China.com product listing, following the platform's official rules:
- Format: [modifier] + [core noun]. The core noun in EVERY keyword must be "${coreNoun}" (the same core noun used in the product title) — do not switch to a different core noun.
- NEVER include the specific model number / SKU (e.g. "SR1K") in a keyword. Keywords are generic buyer search phrases, not product-specific identifiers — real buyers search "Rack Mount UPS", never "Rack Mount UPS SR1K".
- Return exactly 10 keywords as a JSON array of strings, ordered by importance: the first 3 are the most important/representative and should reflect the product's most defining traits.
- Keywords 4-10 should echo the title's modifiers with light variation (different buyer phrasing/synonyms for the same modifiers), not introduce unrelated product types.
- No duplicate or near-duplicate keywords: treat different spellings/spacing of the same modifier as duplicates too (e.g. "Rack Mount" and "Rackmount" count as the same modifier — only use one form).
- Each keyword: 2-4 words.
Example (official guide's own example, for format reference only):
Title core idea: "Luxury King Size Wooden Frame Headboard Hotel Bed"
Good keywords: "Hotel Bed", "Wooden Frame Headboard Bed", "King Size Bed"
(Notice: no model number, no SKU, no brand-specific text — just modifier + core noun.)
Output ONLY a JSON array of exactly 10 strings, nothing else.`;
// 把 buildAttributeList 生成的属性列表挑几条最有代表性的,拼成一段给AI看的
// 产品规格摘要——不用全部20条塞进去,模型上下文不需要那么细,挑前几条即可。
function summarizeSpecsForAi(attrList, max = 8) {
return attrList.slice(0, max).map((a) => `${a.name}: ${a.value}`).join('; ');
}
function localFallbackTitle(categoryName, model) {
// AI不可用时的兜底——尽量别太离谱,但明确标注这是兜底,提醒人工检查
return `${categoryName || 'Power Equipment'} ${model}`.trim();
}
function localFallbackKeywords(coreNoun) {
const noun = coreNoun || 'UPS';
return [
`${noun}`, `Industrial ${noun}`, `${noun} Supplier`, `${noun} Manufacturer`,
`High Efficiency ${noun}`, `${noun} for Sale`, `Wholesale ${noun}`, `${noun} Factory`,
`Custom ${noun}`, `${noun} Price`,
];
}
async function generateTitle(ctx, { categoryName, model, applicationScenarios, specsSummary }, log) {
let apiKey = '';
try {
const cfg = ctx.loadAiConfig();
apiKey = cfg && cfg.apiKey;
} catch (e) { /* 走兜底 */ }
if (!apiKey) {
log('未配置DeepSeek Key,标题使用简单兜底,建议人工检查/补充', 'WARNING');
return localFallbackTitle(categoryName, model);
}
const userLines = [
`Product category: ${categoryName || '(unknown)'}`,
`Model: ${model}`,
applicationScenarios ? `Application scenarios: ${applicationScenarios}` : null,
specsSummary ? `Key specs: ${specsSummary}` : null,
'Write the product title now.',
].filter(Boolean);
try {
const content = await ctx.callDeepSeekWithRetry(
apiKey,
[{ role: 'system', content: _TITLE_SYSTEM_PROMPT }, { role: 'user', content: userLines.join('\n') }],
{ temperature: 0.5, maxTokens: 100 },
2,
);
const title = String(content).trim().replace(/^["']|["']$/g, '');
const wordCount = title.split(/\s+/).filter(Boolean).length;
if (!title || wordCount < 3 || wordCount > 20) throw new Error(`AI标题长度异常(${wordCount}词): ${title}`);
log(`AI生成标题: ${title}`);
return title;
} catch (e) {
log(`AI标题生成失败,使用简单兜底,建议人工检查/补充: ${e.message}`, 'WARNING');
return localFallbackTitle(categoryName, model);
}
}
// coreNounHint:从生成好的标题里粗略摘一个核心名词给关键词prompt用(简单取标题
// 里最后一个大写开头的名词性单词,不追求完美,AI会自己判断,这里只是提示)
function guessCoreNoun(title) {
const words = (title || '').split(/\s+/).filter(Boolean);
const stop = new Set(['for', 'with', 'and', 'the', 'a', 'an', 'of']);
const forIdx = words.findIndex((w) => w.toLowerCase() === 'for');
const candidates = (forIdx > 0 ? words.slice(0, forIdx) : words).filter((w) => !stop.has(w.toLowerCase()));
return candidates.length ? candidates[candidates.length - 1] : (words[words.length - 1] || 'Product');
}
async function generateKeywords(ctx, title, model, log) {
let apiKey = '';
try {
const cfg = ctx.loadAiConfig(); // 同步函数,跟server.js里的定义一致
apiKey = cfg && cfg.apiKey;
} catch (e) { /* 没配置DeepSeek key时走本地兜底 */ }
const coreNoun = guessCoreNoun(title);
if (!apiKey) {
log('未配置DeepSeek Key,使用本地关键词兜底', 'WARNING');
return localFallbackKeywords(coreNoun);
}
const messages = [
{ role: 'system', content: _KW_SYSTEM_PROMPT_TMPL(coreNoun) },
{
role: 'user',
content: `Product title: ${title}\n(Model "${model}" is given only for your context — DO NOT put it in any keyword.)\nGenerate 10 keywords now.`,
},
];
try {
const content = await ctx.callDeepSeekWithRetry(apiKey, messages, { temperature: 0.7, maxTokens: 400 }, 2);
let raw = String(content).trim();
if (raw.startsWith('```')) {
raw = raw.split('```')[1];
if (raw.startsWith('json')) raw = raw.slice(4);
}
const parsed = JSON.parse(raw.trim());
if (!Array.isArray(parsed)) throw new Error('AI返回的不是数组');
const modelLower = String(model || '').trim().toLowerCase();
const seen = new Set();
const unique = [];
for (const kw of parsed) {
let s = String(kw).trim();
// 兜底:万一AI还是把型号塞进去了,这里强制去掉(防止prompt没完全管住)
if (modelLower && s.toLowerCase().includes(modelLower)) {
s = s.replace(new RegExp(model.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'ig'), '').replace(/\s+/g, ' ').trim();
}
if (s && !seen.has(s.toLowerCase())) { seen.add(s.toLowerCase()); unique.push(s); }
}
if (unique.length < 10) throw new Error(`只生成了${unique.length}个`);
log(`AI生成 10 个关键词: ${unique[0]} ...`);
return unique.slice(0, 10);
} catch (e) {
log(`AI关键词生成失败,使用本地兜底: ${e.message}`, 'WARNING');
return localFallbackKeywords(coreNoun);
}
}
function registerMicUpload(app, server, ctx) {
const { requireAuth, readData, writeData, encrypt, decrypt } = ctx;
// sessionId -> { session, token, wsClients: Set<ws>, createdAt }
const sessions = new Map();
function broadcast(entry, msg) {
const payload = JSON.stringify(msg);
for (const ws of entry.wsClients) {
if (ws.readyState === WebSocket.OPEN) ws.send(payload);
}
}
function getMicTemplates() {
const data = readData();
return data['crm:micTemplates'] || {};
}
// ---- 品类 -> addSimilar模板URL 配置(存进通用storage的 crm:micTemplates) ----
app.get('/api/mic-upload/templates', requireAuth, (req, res) => {
res.json({ value: getMicTemplates() });
});
app.put('/api/mic-upload/templates', requireAuth, (req, res) => {
const data = readData();
data['crm:micTemplates'] = req.body && req.body.value ? req.body.value : {};
writeData(data);
res.json({ ok: true });
});
// ---- MIC账号/密码(加密存储,跟ai-config.json同一套AES-256-GCM)----
// GET只返回是否已保存,从不回传明文;密码只在 autofill 接口里解密,且直接
// 由后端打进Playwright页面,不经过前端网络请求。
app.get('/api/mic-upload/credentials', requireAuth, (req, res) => {
const data = readData();
const cred = data['crm:micCredentials'] || {};
res.json({ hasEmail: !!cred.emailEnc, hasPassword: !!cred.passwordEnc });
});
app.put('/api/mic-upload/credentials', requireAuth, (req, res) => {
const { email, password } = req.body || {};
const data = readData();
const cred = data['crm:micCredentials'] || {};
if (typeof email === 'string' && email) cred.emailEnc = encrypt(email);
if (typeof password === 'string' && password) cred.passwordEnc = encrypt(password);
data['crm:micCredentials'] = cred;
writeData(data);
res.json({ ok: true, hasEmail: !!cred.emailEnc, hasPassword: !!cred.passwordEnc });
});
app.delete('/api/mic-upload/credentials', requireAuth, (req, res) => {
const data = readData();
delete data['crm:micCredentials'];
writeData(data);
res.json({ ok: true });
});
// ---- 会话生命周期 ----
// 改成单例:全局只有一个会话,固定id='default'。前端每次挂载都调这个接口,
// 如果浏览器已经在跑,直接复用(不重启、不影响登录态),只有没有会话时才
// 真正启动一个新的。这样避免"刷新页面=开一个新浏览器"导致旧进程占着
// profile目录的锁、登录态传不过去的问题——只要不主动点"关闭会话",
// 浏览器和登录态就一直在。
app.post('/api/mic-upload/sessions', requireAuth, async (req, res) => {
const id = 'default';
const existing = sessions.get(id);
if (existing && existing.session && !existing.session.closed) {
// 已经有一个活着的会话,直接复用——不new浏览器,登录态原样保留。
// token重新发一个(简单起见每次连接都发新的,旧token自然失效)。
existing.token = crypto.randomBytes(24).toString('hex');
return res.json({ sessionId: id, wsToken: existing.token, phase: existing.phase, reused: true });
}
const token = crypto.randomBytes(24).toString('hex');
const entry = { session: null, token, wsClients: new Set(), createdAt: Date.now(), phase: 'browsing' };
sessions.set(id, entry);
const session = new MicBrowserSession(id, {
onFrame: (jpegBase64) => broadcast(entry, { type: 'frame', data: jpegBase64 }),
onLog: (logMsg) => broadcast(entry, { type: 'log', ...logMsg }),
});
entry.session = session;
try {
await session.start();
res.json({ sessionId: id, wsToken: token, phase: entry.phase, reused: false });
} catch (e) {
sessions.delete(id);
res.status(500).json({ error: `浏览器启动失败: ${e.message}` });
}
});
// 页面刷新/切换菜单后,前端用这个查一下之前的会话还在不在、走到哪一步了,
// 在的话直接重连WebSocket接回去,不用重新走一遍登录流程。
app.get('/api/mic-upload/sessions/:id', requireAuth, (req, res) => {
const entry = sessions.get(req.params.id);
if (!entry) return res.status(404).json({ error: '会话不存在或已关闭' });
res.json({ ok: true, phase: entry.phase });
});
// 前端点了"已登录,继续"之后调一下,把状态记在后端,这样刷新页面/重连时
// 才知道该恢复到"已登录"还是"还在登录页"。
app.post('/api/mic-upload/sessions/:id/mark-ready', requireAuth, (req, res) => {
const entry = sessions.get(req.params.id);
if (!entry) return res.status(404).json({ error: '会话不存在或已关闭' });
entry.phase = 'ready';
res.json({ ok: true });
});
app.post('/api/mic-upload/sessions/:id/login', requireAuth, async (req, res) => {
const entry = sessions.get(req.params.id);
if (!entry) return res.status(404).json({ error: '会话不存在或已关闭' });
try {
await entry.session.gotoLogin();
res.json({ ok: true });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
// 把已保存的账号/密码填进"当前在远程页面里被点中、处于聚焦状态"的输入框。
// 密码全程只在服务端解密+发送keydown/insertText,不经过前端网络请求。
app.post('/api/mic-upload/sessions/:id/autofill', requireAuth, async (req, res) => {
const entry = sessions.get(req.params.id);
if (!entry) return res.status(404).json({ error: '会话不存在或已关闭' });
const { field } = req.body || {};
if (field !== 'email' && field !== 'password') {
return res.status(400).json({ error: 'field 必须是 email 或 password' });
}
const data = readData();
const cred = data['crm:micCredentials'] || {};
const encKey = field === 'email' ? 'emailEnc' : 'passwordEnc';
if (!cred[encKey]) {
return res.status(400).json({ error: `还没保存过${field === 'email' ? '账号' : '密码'},先在左侧填好并点保存` });
}
try {
const value = decrypt(cred[encKey]);
await entry.session.typeText(value);
entry.session.log(`已填入保存的${field === 'email' ? '账号' : '密码'}`);
res.json({ ok: true });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
// 从产品库读数据 -> 生成参数 -> 进入仿造发布页 -> 自动填表(不提交)
app.post('/api/mic-upload/sessions/:id/publish', requireAuth, async (req, res) => {
const entry = sessions.get(req.params.id);
if (!entry) return res.status(404).json({ error: '会话不存在或已关闭' });
const { productId } = req.body || {};
if (!productId) return res.status(400).json({ error: '缺少 productId' });
const session = entry.session;
try {
const data = readData();
let products = [];
try {
// crm:products 是前端 JSON.stringify 之后存进来的字符串,不是数组本身,
// 跟 crm:productCategories 是一个套路——之前漏了这一步 parse。
products = JSON.parse(data['crm:products'] || '[]');
} catch (e) {
return res.status(500).json({ error: `读取产品库失败(数据格式异常): ${e.message}` });
}
const product = products.find((p) => p.id === productId);
if (!product) return res.status(404).json({ error: '产品不存在' });
const templates = getMicTemplates();
const templateUrl = templates[product.type] || templates[product.category];
if (!templateUrl) {
return res.status(400).json({
error: `品类 "${product.type || product.category}" 还没配置MIC仿造模板URL,请先在"设置"里补上`,
});
}
const { model, attrList, warnings } = buildAttributeList(product);
for (const w of warnings) session.log(w, 'WARNING');
await session.gotoPublishTemplate(templateUrl);
const title = await generateTitle(ctx, {
categoryName: product.type || product.category,
model,
applicationScenarios: product.applicationScenarios,
specsSummary: summarizeSpecsForAi(attrList),
}, (m, l) => session.log(m, l));
await session.fillTitle(title);
await session.checkCenterWord();
const keywords = await generateKeywords(ctx, title, model, (m, l) => session.log(m, l));
await session.fillKeywords(keywords);
await session.fillModel(model);
// "规格"填产品尺寸:普通品类用 dimensions,电池系统品类没有这个字段,
// 退而用系统柜尺寸/模块尺寸——哪个有值就用哪个。
const specText = sanitizeValue(product.dimensions || product.cabinetDimensions || product.moduleDimensions || '');
await session.fillSpec(specText);
// 包裹尺寸/毛重:电池品类有专门的"包装尺寸(mm)"+"毛重(kg)"字段,优先用这个;
// 没有这两个数据的品类(比如UPS),退而用产品自身尺寸代替包裹尺寸、净重代替毛重。
const hasPackagingData = !!(product.grossWeight && product.packageDimensions);
const packageDimsSource = hasPackagingData ? sanitizeValue(product.packageDimensions) : specText;
await session.fillPackageDimensions(parsePackageDimensionsCm(packageDimsSource));
if (!hasPackagingData) {
session.log('没有单独的包装箱尺寸数据,包裹尺寸用产品自身尺寸代替,建议核对', 'WARNING');
}
session.log('包裹尺寸是按尺寸字符串里数字出现的顺序(默认"长x宽x高")换算填的,建议核对一下顺序对不对', 'WARNING');
const weightKg = sanitizeValue(hasPackagingData ? product.grossWeight : (product.netWeight || ''));
await session.fillPackageWeight(weightKg);
session.log(hasPackagingData ? '包裹毛重用了产品的毛重字段' : '没有专门的毛重数据,包裹毛重用净重代替');
await session.fillAttributes(attrList);
res.json({ ok: true, warnings, title, keywords });
} catch (e) {
session.log(`填表流程出错: ${e.message}`, 'ERROR');
res.status(500).json({ error: e.message });
}
});
app.post('/api/mic-upload/sessions/:id/submit', requireAuth, async (req, res) => {
const entry = sessions.get(req.params.id);
if (!entry) return res.status(404).json({ error: '会话不存在或已关闭' });
try {
await entry.session.submit();
res.json({ ok: true });
} catch (e) {
res.status(500).json({ error: e.message });
}
});
app.delete('/api/mic-upload/sessions/:id', requireAuth, async (req, res) => {
const entry = sessions.get(req.params.id);
if (!entry) return res.json({ ok: true });
await entry.session.close();
for (const ws of entry.wsClients) { try { ws.close(); } catch (e) {} }
sessions.delete(req.params.id);
res.json({ ok: true });
});
// ---- WebSocket:画面推流 + 登录阶段的鼠标/键盘转发 ----
// 鉴权用启动会话时发的一次性 token(不复用cookie/session,接线更简单)
const wss = new WebSocket.Server({ noServer: true });
server.on('upgrade', (req, socket, head) => {
let url;
try { url = new URL(req.url, 'http://localhost'); } catch (e) { socket.destroy(); return; }
if (url.pathname !== '/ws/mic-upload') return; // 交给其他upgrade handler(如果有)
const sessionId = url.searchParams.get('sessionId');
const token = url.searchParams.get('token');
const entry = sessions.get(sessionId);
if (!entry || entry.token !== token) { socket.destroy(); return; }
wss.handleUpgrade(req, socket, head, (ws) => {
entry.wsClients.add(ws);
ws.on('close', () => entry.wsClients.delete(ws));
ws.on('message', async (raw) => {
let msg;
try { msg = JSON.parse(raw); } catch (e) { return; }
const session = entry.session;
if (!session) return;
try {
switch (msg.type) {
case 'mousemove': await session.mouseMove(msg.x, msg.y); break;
case 'mousedown': await session.mouseDown(msg.x, msg.y, msg.button || 'left'); break;
case 'mouseup': await session.mouseUp(msg.x, msg.y, msg.button || 'left'); break;
case 'wheel': await session.wheel(msg.dx || 0, msg.dy || 0); break;
case 'keydown': await session.keyDown(msg.key); break;
case 'keyup': await session.keyUp(msg.key); break;
case 'text': await session.typeText(msg.text); break;
default: break;
}
} catch (e) { /* 单次输入转发失败不致命,忽略 */ }
});
});
});
// 之前这里有个"30分钟无连接自动清理"的定时器,但既然目标是长期保持登录、
// 单例复用,这个自动清理反而会在你没开着网页的时候把浏览器关掉、白白丢失
// 登录态——所以去掉了,改成完全手动:只有点"关闭会话"才会真正关闭浏览器。
// 代价是如果你长期不用也不点关闭,这个无头Chromium进程会一直占着内存
// (几十MB量级,对单人使用的VPS影响不大);真要回收就SSH上去手动
// `systemctl restart customer-crm` 即可,重启后浏览器会重新launch,
// 登录态因为是持久化profile,还在磁盘上,大概率还是已登录状态。
}
module.exports = { registerMicUpload };