// MIC产品上传 — 路由与 WebSocket 桥接 // // 关键:HTTP路由必须注册在 server.js 里的SPA兜底路由(app.get('*', ...),一般 // 紧挨着 express.static(FRONTEND_DIST) 那几行)**之前**,否则会被那个兜底路由 // 先拦截,GET请求会收到index.html而不是JSON(PUT/POST/DELETE不受影响,因为 // SPA兜底通常只处理GET)。WebSocket部分则必须在 server = app.listen(...) 之后 // 才能接,所以拆成两次调用: // // // 放在 app.get('*', (req,res)=>{...}) 那几行【之前】: // const { registerMicUploadRoutes, attachMicUploadWebSocket } = require('./mic-upload/micUploadRoutes'); // const micSessions = registerMicUploadRoutes(app, { // requireAuth, readData, writeData, callDeepSeekWithRetry, loadAiConfig, encrypt, decrypt, // }); // // // ...中间保持原样:express.static / app.get('*', ...) / const server = app.listen(...) ... // // // 放在 const server = app.listen(...) 那几行【之后】: // attachMicUploadWebSocket(server, micSessions); // // requireAuth/readData/writeData/callDeepSeekWithRetry/loadAiConfig/encrypt/decrypt // 都是 server.js 里已经定义好的,直接传引用过来即可,不用改动它们本身。 // // 需要 `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, hasNonAscii } = 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 (the brand name below, if given, counts toward this). - If a brand name is provided in the user message, the title MUST start with that exact brand name as the very first word. If no brand is provided, do NOT include any brand name anywhere in the title. - Suggested structure: [Brand] + [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. You must also report which exact word in your title is the core noun (copy it verbatim, single word, as it literally appears in the title — e.g. "UPS", not "UPS unit"). The core noun is never the brand name. Output ONLY a JSON object, nothing else, in this exact shape: {"title": "...", "coreNoun": "..."}`; 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, and don't append unrelated trailing words after it. - 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, and every single one must actually be a coherent, grammatical buyer search phrase — sanity-check each one before outputting. Example (official guide's own example, for format reference only): Title core idea: "Luxury King Size Wooden Frame Headboard Hotel Bed" (core noun: "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, brand) { // AI不可用时的兜底——尽量别太离谱,但明确标注这是兜底,提醒人工检查 const prefix = brand ? `${brand} ` : ''; return { title: `${prefix}${categoryName || 'Power Equipment'} ${model}`.trim(), coreNoun: categoryName || 'Product' }; } 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`, ]; } // 返回 { title, coreNoun }——中心词直接由生成标题的同一次AI调用给出,不再靠 // 事后从标题字符串里猜(之前那套"for前面最后一个词"的算法碰到标题里"for" // 前面刚好是别的修饰词时就会猜错,产出一堆带着错误中心词的关键词)。 async function generateTitle(ctx, { categoryName, model, brand, 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, brand); } const userLines = [ brand ? `Brand: ${brand}` : 'Brand: (none — do not include any brand name in the title)', `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: 150 }, 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()); let title = String(parsed.title || '').trim(); const coreNoun = String(parsed.coreNoun || '').trim(); // 代码层兜底:品牌规则比较死板(只在恰好是SKE时加),不完全依赖AI听话—— // 该加没加上就补上,不该加却加了就去掉。 if (brand) { if (!title.toLowerCase().startsWith(brand.toLowerCase())) title = `${brand} ${title}`; } const wordCount = title.split(/\s+/).filter(Boolean).length; if (!title || wordCount < 3 || wordCount > 20) throw new Error(`AI标题长度异常(${wordCount}词): ${title}`); if (!coreNoun || !title.toLowerCase().includes(coreNoun.toLowerCase())) { throw new Error(`AI给的中心词"${coreNoun}"没有实际出现在标题里,不可信`); } log(`AI生成标题: ${title}(中心词: ${coreNoun})`); return { title, coreNoun }; } catch (e) { log(`AI标题生成失败,使用简单兜底,建议人工检查/补充: ${e.message}`, 'WARNING'); return localFallbackTitle(categoryName, model); } } async function generateKeywords(ctx, title, coreNoun, model, log) { let apiKey = ''; try { const cfg = ctx.loadAiConfig(); // 同步函数,跟server.js里的定义一致 apiKey = cfg && cfg.apiKey; } catch (e) { /* 没配置DeepSeek key时走本地兜底 */ } 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); } } // 净化/量词替换表只能处理已知的固定符号和单词,碰到整句中文描述(比如 // "三进单出(可调单进单出)"这种)没法靠替换表覆盖——这里兜底用AI把attrList里 // 净化后仍然残留非ASCII字符的值整句翻译成英文。一次性把所有需要翻译的值打包 // 成一个请求,而不是每条都单独调用,省时间也省token。 async function translateResidualChineseFields(ctx, attrList, log) { const indices = []; const toTranslate = []; attrList.forEach((item, i) => { if (hasNonAscii(item.value)) { indices.push(i); toTranslate.push(item.value); } }); if (toTranslate.length === 0) return attrList; let apiKey = ''; try { const cfg = ctx.loadAiConfig(); apiKey = cfg && cfg.apiKey; } catch (e) { /* 走兜底 */ } if (!apiKey) { log(`有 ${toTranslate.length} 个属性值仍含中文,但没配置DeepSeek Key无法自动翻译,需要人工检查后再提交`, 'WARNING'); return attrList; } const prompt = `Translate each of these product spec values (from a power equipment / UPS / battery datasheet) ` + `from Chinese (or mixed Chinese/English) into concise, natural English suitable for a B2B product listing. ` + `Keep numbers, units, model codes, and any English words unchanged — only translate the Chinese text. ` + `Return ONLY a JSON array of strings, same length and same order as the input, no explanation.\n\n` + `Values: ${JSON.stringify(toTranslate)}`; try { const content = await ctx.callDeepSeekWithRetry(apiKey, [{ role: 'user', content: prompt }], { temperature: 0.3, maxTokens: 600 }, 2); let raw = String(content).trim(); if (raw.startsWith('```')) { raw = raw.split('```')[1]; if (raw.startsWith('json')) raw = raw.slice(4); } const translated = JSON.parse(raw.trim()); if (!Array.isArray(translated) || translated.length !== toTranslate.length) { throw new Error(`AI返回的翻译数量(${Array.isArray(translated) ? translated.length : '非数组'})跟请求数量(${toTranslate.length})对不上`); } const result = attrList.map((item) => ({ ...item })); indices.forEach((idx, j) => { const newVal = sanitizeValue(translated[j]); // 翻译结果再跑一遍已知符号净化 log(`AI翻译属性值: "${attrList[idx].value}" -> "${newVal}"`); if (hasNonAscii(newVal)) { log(` ⚠️ 翻译后仍含非ASCII字符,需要人工检查这一条: ${result[idx].name}`, 'WARNING'); } result[idx] = { ...result[idx], value: newVal }; }); return result; } catch (e) { log(`AI翻译残留中文属性值失败,保留原文,需要人工检查后再提交: ${e.message}`, 'WARNING'); return attrList; } } // 拆成两个函数是因为路由必须注册在SPA兜底路由(app.get('*', ...))之前, // 而WebSocket的upgrade监听依赖 app.listen() 返回的 server 实例,只能在那之后 // 才能拿到——这两个时间点在server.js里天然不是同一个地方,所以拆开成两次调用, // 中间用同一个 sessions Map(靠闭包共享)保持状态一致。 function registerMicUploadRoutes(app, ctx) { const { requireAuth, readData, writeData, encrypt, decrypt } = ctx; // sessionId -> { session, token, wsClients: Set, 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.set('Cache-Control', 'no-store'); // 避免浏览器把"还没配置"那次的旧响应缓存住 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: rawAttrList, warnings } = buildAttributeList(product); for (const w of warnings) session.log(w, 'WARNING'); const attrList = await translateResidualChineseFields(ctx, rawAttrList, (m, l) => session.log(m, l)); await session.gotoPublishTemplate(templateUrl); // 只有品牌恰好是SKE才在标题里加品牌前缀,没有品牌或者品牌不是SKE都不加 const brandForTitle = (product.brand && String(product.brand).trim().toUpperCase() === 'SKE') ? 'SKE' : null; const { title, coreNoun } = await generateTitle(ctx, { categoryName: product.type || product.category, model, brand: brandForTitle, 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, coreNoun, model, (m, l) => session.log(m, l)); await session.fillKeywords(keywords); await session.fillModel(model); // "规格"填产品尺寸:普通品类用 dimensions,电池系统品类没有这个字段, // 退而用系统柜尺寸/模块尺寸——哪个有值就用哪个。CRM里存的是纯数字 // (如"438*415*87"),单位统一是mm,这里显式带上"mm"避免看的人误解单位。 const specText = sanitizeValue(product.dimensions || product.cabinetDimensions || product.moduleDimensions || ''); const specTextWithUnit = specText && !/mm/i.test(specText) ? `${specText} mm` : specText; await session.fillSpec(specTextWithUnit); // 包裹尺寸/毛重:电池品类有专门的"包装尺寸(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 }); }); return sessions; // 交给 attachMicUploadWebSocket 用同一份 } // 必须在 const server = app.listen(...) 之后调用(WS要挂在这个server实例上)。 // sessions 是 registerMicUploadRoutes(...) 的返回值,两次调用要用同一个。 function attachMicUploadWebSocket(server, sessions) { // ---- 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 = { registerMicUploadRoutes, attachMicUploadWebSocket };