Update server.js via upload script - 2026-11-16 23:44:45

This commit is contained in:
mike 2026-11-16 23:45:01 +08:00
parent 339ee41154
commit 9321c01ca7
1 changed files with 51 additions and 7 deletions

View File

@ -1443,6 +1443,45 @@ async function fileTextFor(f) {
} catch (e) { return ''; } } catch (e) { return ''; }
} }
// 从 Excel / 邮件复制粘贴的富内容:按顺序的文字块和表格块(表格保留底色、字色、加粗、对齐、合并单元格、列宽)
const HEX_RE = /^#[0-9a-f]{6}$/i;
function cleanBlocks(raw) {
let list = raw;
if (typeof list === 'string') { try { list = JSON.parse(list); } catch (e) { return null; } }
if (!Array.isArray(list)) return null;
const out = [];
for (const b of list.slice(0, 20)) {
if (!b || typeof b !== 'object') continue;
if (b.t === 'text') { const v = clipTxt(b.v, 50000).trim(); if (v) out.push({ t: 'text', v }); continue; }
if (b.t !== 'table' || !Array.isArray(b.rows)) continue;
const rows = b.rows.slice(0, 300).map((r) => (Array.isArray(r) ? r.slice(0, 40) : []).map((c) => {
c = c || {};
const o = { v: clipTxt(c.v, 2000) };
if (HEX_RE.test(c.bg || '')) o.bg = c.bg.toLowerCase();
if (HEX_RE.test(c.color || '')) o.color = c.color.toLowerCase();
if (c.b) o.b = 1;
if (c.i) o.i = 1;
if (['left', 'center', 'right'].includes(c.align)) o.align = c.align;
const cs = parseInt(c.cs, 10), rs = parseInt(c.rs, 10);
if (cs > 1) o.cs = Math.min(cs, 40); if (rs > 1) o.rs = Math.min(rs, 300);
return o;
}));
if (!rows.some((r) => r.some((c) => c.v.trim()))) continue;
const t = { t: 'table', rows };
if (Array.isArray(b.widths)) t.widths = b.widths.slice(0, 40).map((w) => Math.max(20, Math.min(900, parseInt(w, 10) || 80)));
out.push(t);
}
return out.length ? out : null;
}
const blocksText = (bl) => bl.filter((b) => b.t === 'text').map((b) => b.v).join('\n\n');
const blocksTables = (bl) => bl.filter((b) => b.t === 'table').map((b) => b.rows.map((r) => r.map((c) => String(c.v).replace(/\s*\n\s*/g, ' ')).join('\t')).join('\n')).join('\n\n');
function applyBlocks(msg, raw) {
const bl = cleanBlocks(raw); if (!bl) return msg;
msg.blocks = bl; msg.text = clipTxt(blocksText(bl), CONVO_TEXT_MAX);
const tt = blocksTables(bl); if (tt) msg.tableText = clipTxt(tt, 60000);
return msg;
}
// WhatsApp 导出 → 一条条消息。日期格式各手机不一样(日/月/年、月/日/年、年-月-日),整份文件统一判断 // WhatsApp 导出 → 一条条消息。日期格式各手机不一样(日/月/年、月/日/年、年-月-日),整份文件统一判断
function waDateOrder(rows) { function waDateOrder(rows) {
let dayFirst = 0, monthFirst = 0; let dayFirst = 0, monthFirst = 0;
@ -1491,7 +1530,8 @@ app.post('/api/convos/:cid', requireAuth, (req, res) => {
const b = req.body || {}; const b = req.body || {};
const files = (req.files || []).map((f) => ({ ...f, originalname: fixUploadName(f.originalname) })); const files = (req.files || []).map((f) => ({ ...f, originalname: fixUploadName(f.originalname) }));
const text = clipTxt(b.text, CONVO_TEXT_MAX).trim(); const text = clipTxt(b.text, CONVO_TEXT_MAX).trim();
if (!text && !files.length) return res.status(400).json({ error: '请输入内容或添加附件' }); const hasBlocks = !!cleanBlocks(b.blocks);
if (!text && !files.length && !hasBlocks) return res.status(400).json({ error: '请输入内容或添加附件' });
try { try {
const saved = [], ft = []; const saved = [], ft = [];
for (const f of files) { saved.push(storeFile(cid, f)); const x = await fileTextFor(f); if (x) ft.push(`【${f.originalname}】\n${x}`); } for (const f of files) { saved.push(storeFile(cid, f)); const x = await fileTextFor(f); if (x) ft.push(`【${f.originalname}】\n${x}`); }
@ -1500,8 +1540,9 @@ app.post('/api/convos/:cid', requireAuth, (req, res) => {
channel: MSG_CHANNELS.includes(b.channel) ? b.channel : 'whatsapp', channel: MSG_CHANNELS.includes(b.channel) ? b.channel : 'whatsapp',
at: AT_RE.test(b.at || '') ? b.at : nowAt(), text, files: saved, createdAt: Date.now(), at: AT_RE.test(b.at || '') ? b.at : nowAt(), text, files: saved, createdAt: Date.now(),
}; };
if (hasBlocks) applyBlocks(msg, b.blocks);
if (ft.length) msg.fileText = clipTxt(ft.join('\n\n'), 40000); if (ft.length) msg.fileText = clipTxt(ft.join('\n\n'), 40000);
if (!text || isMostlyChinese(text)) msg.zh = ''; if (!msg.text || isMostlyChinese(msg.text)) msg.zh = '';
const all = readConvos(); convoOf(all, cid).items.push(msg); writeConvos(all); const all = readConvos(); convoOf(all, cid).items.push(msg); writeConvos(all);
res.json({ ok: true, item: msg }); res.json({ ok: true, item: msg });
} catch (e) { res.status(400).json({ error: e.message }); } } catch (e) { res.status(400).json({ error: e.message }); }
@ -1556,14 +1597,16 @@ app.post('/api/convos/:cid/import', requireAuth, (req, res) => {
const f = files[k]; if (!f) continue; const f = files[k]; if (!f) continue;
fl.push(storeFile(cid, f)); const tx = await fileTextFor(f); if (tx) ft.push(`【${f.originalname}】\n${tx}`); fl.push(storeFile(cid, f)); const tx = await fileTextFor(f); if (tx) ft.push(`【${f.originalname}】\n${tx}`);
} }
if (!text && !fl.length) continue; const hasBl = !!cleanBlocks(x.blocks);
if (!text && !fl.length && !hasBl) continue;
const msg = { const msg = {
id: newMsgId(), from: x.from === 'me' ? 'me' : 'customer', channel: MSG_CHANNELS.includes(x.channel) ? x.channel : (channel || 'whatsapp'), id: newMsgId(), from: x.from === 'me' ? 'me' : 'customer', channel: MSG_CHANNELS.includes(x.channel) ? x.channel : (channel || 'whatsapp'),
at: AT_RE.test(x.at || '') ? x.at : baseAt, text, files: fl, createdAt: t0 + i, at: AT_RE.test(x.at || '') ? x.at : baseAt, text, files: fl, createdAt: t0 + i,
}; };
if (hasBl) applyBlocks(msg, x.blocks);
if (x.who) msg.who = clipTxt(x.who, 60); if (x.who) msg.who = clipTxt(x.who, 60);
if (ft.length) msg.fileText = clipTxt(ft.join('\n\n'), 40000); if (ft.length) msg.fileText = clipTxt(ft.join('\n\n'), 40000);
if (!text || isMostlyChinese(text)) msg.zh = ''; if (!msg.text || isMostlyChinese(msg.text)) msg.zh = '';
out.push(msg); out.push(msg);
} }
} }
@ -1602,7 +1645,7 @@ app.post('/api/convos/:cid/import', requireAuth, (req, res) => {
if (!out.length) return res.status(400).json({ error: '没有读到消息' }); if (!out.length) return res.status(400).json({ error: '没有读到消息' });
// 和已有消息重复的(同时间、同发送方、同内容)跳过,方便重复导入同一个聊天的新导出 // 和已有消息重复的(同时间、同发送方、同内容)跳过,方便重复导入同一个聊天的新导出
const all = readConvos(); const c = convoOf(all, cid); const all = readConvos(); const c = convoOf(all, cid);
const key = (m) => `${m.at}|${m.from}|${String(m.text || '').slice(0, 200)}|${(m.files || []).map((f) => f.name).join(',')}`; const key = (m) => `${m.at}|${m.from}|${String(m.text || '').slice(0, 200)}|${String(m.tableText || '').slice(0, 200)}|${(m.files || []).map((f) => f.name).join(',')}`;
const have = new Set(c.items.map(key)); const have = new Set(c.items.map(key));
const fresh = []; const fresh = [];
for (const m of out) { for (const m of out) {
@ -1626,7 +1669,7 @@ app.put('/api/convos/:cid/:id', requireAuth, (req, res) => {
if (MSG_CHANNELS.includes(b.channel)) it.channel = b.channel; if (MSG_CHANNELS.includes(b.channel)) it.channel = b.channel;
if (b.from === 'me' || b.from === 'customer') { if (it.from) it.from = b.from; } if (b.from === 'me' || b.from === 'customer') { if (it.from) it.from = b.from; }
if (AT_RE.test(b.at || '')) { it.at = b.at; if (it.date) it.date = b.at.slice(0, 10); } if (AT_RE.test(b.at || '')) { it.at = b.at; if (it.date) it.date = b.at.slice(0, 10); }
if (typeof b.text === 'string' && b.text !== it.text) { it.text = clipTxt(b.text, CONVO_TEXT_MAX); if (it.from) { delete it.zh; if (!it.text.trim() || isMostlyChinese(it.text)) it.zh = ''; } } if (typeof b.text === 'string' && b.text !== it.text && !it.blocks) { it.text = clipTxt(b.text, CONVO_TEXT_MAX); if (it.from) { delete it.zh; if (!it.text.trim() || isMostlyChinese(it.text)) it.zh = ''; } }
if (typeof b.zh === 'string') it.zh = clipTxt(b.zh, CONVO_TEXT_MAX); if (typeof b.zh === 'string') it.zh = clipTxt(b.zh, CONVO_TEXT_MAX);
it.updatedAt = Date.now(); it.updatedAt = Date.now();
writeConvos(all); writeConvos(all);
@ -1691,7 +1734,8 @@ function convoContext(customer, items, budget, names = {}) {
const who = m.from === 'me' ? `${me}(我们)` : `${m.who && !sameName(m.who, cust) ? m.who : cust}(客户)`; const who = m.from === 'me' ? `${me}(我们)` : `${m.who && !sameName(m.who, cust) ? m.who : cust}(客户)`;
const att = (m.files || []).length ? `\n[附件:${m.files.map((f) => f.name).join('、')}]` : ''; const att = (m.files || []).length ? `\n[附件:${m.files.map((f) => f.name).join('、')}]` : '';
const ft = m.fileText ? `\n[附件内容摘录]\n${String(m.fileText).slice(0, 4000)}` : ''; const ft = m.fileText ? `\n[附件内容摘录]\n${String(m.fileText).slice(0, 4000)}` : '';
return `[${m.at} ${CONVO_CH_ZH[m.channel] || m.channel}] ${who}:${m.text || ''}${att}${ft}`; const tb = m.tableText ? `\n[表格]\n${String(m.tableText).slice(0, 6000)}` : '';
return `[${m.at} ${CONVO_CH_ZH[m.channel] || m.channel}] ${who}:${m.text || ''}${tb}${att}${ft}`;
}; };
const picked = []; let used = 0, skipped = 0; const picked = []; let used = 0, skipped = 0;
for (let i = sorted.length - 1; i >= 0; i--) { for (let i = sorted.length - 1; i >= 0; i--) {