diff --git a/App.jsx b/App.jsx index 6d55e72..9ccb4d8 100644 --- a/App.jsx +++ b/App.jsx @@ -2286,6 +2286,18 @@ const Styles = () => ( .cv-imp-h { display: grid; gap: 2px; } .cv-imp-h small { color: var(--ink-soft); font-size: 12px; } .cv-imp-me { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; margin-left: auto; } .cv-imp-me .input { width: 110px; height: 30px; } + .cv-imp-lbl { font-size: 12px; color: var(--ink-soft); margin-left: 6px; } + .cv-prev { border: 1px solid var(--line); border-radius: 8px; background: var(--panel); max-height: 300px; overflow: auto; } + .cv-prev-h { position: sticky; top: 0; background: var(--panel); font-size: 12.5px; font-weight: 700; padding: 6px 10px; border-bottom: 1px solid var(--line); } + .cv-prev-h small { font-weight: 400; color: var(--ink-soft); } + .cv-prev-r { display: flex; gap: 8px; align-items: flex-start; padding: 5px 10px; border-bottom: 1px solid var(--line); font-size: 12.5px; } + .cv-prev-r:last-child { border-bottom: 0; } + .cv-prev-who { flex: none; border: 0; border-radius: 99px; padding: 1px 10px; font-size: 11.5px; font-weight: 700; cursor: pointer; color: #fff; background: #c2410c; max-width: 130px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } + .cv-prev-r.me .cv-prev-who { background: #047857; } + .cv-prev-at { flex: none; color: var(--ink-soft); font-size: 11.5px; width: 82px; padding-top: 1px; } + .cv-prev-t { flex: 1; min-width: 0; white-space: pre-wrap; word-break: break-word; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; } + .cv-prev-img { height: 54px; border-radius: 5px; margin-right: auto; } + .cv-prev-r .cv-ico { margin-left: auto; flex: none; } .cv-chat { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: #efeae2; position: relative; } .cv-chat.drag::after { content: '松开鼠标,把文件加到要发送的消息里'; position: absolute; inset: 0; background: rgba(37,99,235,.10); border: 2px dashed #2563eb; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-weight: 700; color: #1d4ed8; pointer-events: none; } .cv-msgs { max-height: 620px; min-height: 180px; overflow-y: auto; padding: 14px 14px 8px; display: flex; flex-direction: column; gap: 6px; @@ -6352,6 +6364,78 @@ const cvDayLabel = (d) => { const cvIsImg = (f) => /^image\//.test(f.mime || '') || /\.(jpe?g|png|webp|gif|bmp)$/i.test(f.stored || f.name || ''); const cvSize = (n) => (n > 1048576 ? `${(n / 1048576).toFixed(1)} MB` : `${Math.max(1, Math.round((n || 0) / 1024))} KB`); +// 把粘贴进来的聊天文字拆成一条条消息。支持: +// · WhatsApp 导出 / 复制:12/11/2026, 14:05 - Anton: … · [12/11/2026, 14:05:33] Anton: … · [14:05, 12/11/2026] Anton: … +// · 微信电脑版复制:一行"名字 2026/11/12 14:05:33"(或"名字 14:05"),下面几行是内容 +// · 每行"名字: 内容"(名字是客户或我) +// · 都认不出就当成一条消息 +const CV_WA1 = /^\[?(\d{1,4}[./-]\d{1,2}[./-]\d{2,4}),?\s+(\d{1,2}:\d{2}(?::\d{2})?(?:\s?[APap]\.?\s?[Mm]\.?)?)\]?\s*(?:-\s*)?([^:\n]{1,60}):\s?([\s\S]*)$/; +const CV_WA2 = /^\[(\d{1,2}:\d{2}(?::\d{2})?(?:\s?[APap]\.?\s?[Mm]\.?)?),\s*(\d{1,4}[./-]\d{1,2}[./-]\d{2,4})\]\s*([^:\n]{1,60}):\s?([\s\S]*)$/; +const CV_WX = /^(\S.{0,38}?)\s+(?:(\d{4}[/.-]\d{1,2}[/.-]\d{1,2})\s+)?(\d{1,2}:\d{2})(?::\d{2})?\s*$/; +const cvPad = (n) => String(n).padStart(2, '0'); +function cvToAt(d, t, order) { + const p = d.split(/[./-]/).map(Number); + let y, m, day; + if (p[0] > 31) [y, m, day] = p; else if (order === 'mdy') [m, day, y] = p; else [day, m, y] = p; + if (y < 100) y += 2000; + const tm = t.match(/(\d{1,2}):(\d{2})/); let h = +tm[1]; + if (/p/i.test(t) && h < 12) h += 12; if (/a/i.test(t) && h === 12) h = 0; + if (!y || !m || !day || m > 12 || day > 31) return null; + return `${y}-${cvPad(m)}-${cvPad(day)} ${cvPad(h)}:${tm[2]}`; +} +const cvSameName = (a, b) => { a = String(a || '').toLowerCase().trim(); b = String(b || '').toLowerCase().trim(); return !!a && !!b && (a === b || a.split(/\s+/)[0] === b.split(/\s+/)[0] || a.includes(b) || b.includes(a)); }; +function cvParsePaste(raw, names) { + const t = String(raw || '').replace(/[‎‏‪-‮]/g, '').replace(/\r\n?/g, '\n').trim(); + if (!t) return []; + const lines = t.split('\n'); + const out = []; + // 1) WhatsApp + const wa = []; + let cur = null; + for (const l of lines) { + let m = l.match(CV_WA1), g = null; + if (m) g = { d: m[1], t: m[2], who: m[3], x: m[4] }; + else if ((m = l.match(CV_WA2))) g = { d: m[2], t: m[1], who: m[3], x: m[4] }; + if (g) { cur = { ...g, who: g.who.trim() }; wa.push(cur); } else if (cur) cur.x += '\n' + l; + } + if (wa.length) { + let dmy = 0, mdy = 0; + for (const r of wa) { const p = r.d.split(/[./-]/).map(Number); if (p[0] <= 31) { if (p[0] > 12) dmy++; else if (p[1] > 12) mdy++; } } + const order = mdy > dmy ? 'mdy' : 'dmy'; + return wa.map(r => ({ who: r.who, at: cvToAt(r.d, r.t, order), text: r.x.trim() })).filter(r => r.text); + } + // 2) 微信:名字 + 时间 单独一行 + const isName = (n) => cvSameName(n, names.me) || cvSameName(n, names.customer) || (names.extra || []).some(x => cvSameName(n, x)); + const heads = lines.map(l => l.match(CV_WX)).map(m => (m && !/[.!?。!?,,]$/.test(m[1]) ? m : null)); + if (heads.filter(Boolean).length >= 1 && heads.some((m, i) => m && (isName(m[1]) || heads.filter(Boolean).length >= 2))) { + cur = null; + lines.forEach((l, i) => { + const m = heads[i]; + if (m) { + let at = null; + if (m[2]) { const p = m[2].split(/[/.-]/).map(Number); at = `${p[0]}-${cvPad(p[1])}-${cvPad(p[2])} ${cvPad(+m[3].split(':')[0])}:${m[3].split(':')[1]}`; } + cur = { who: m[1].trim(), at, time: m[2] ? null : m[3], text: '' }; out.push(cur); + } else if (cur) cur.text += (cur.text ? '\n' : '') + l; + else out.push(cur = { who: '', at: null, text: l }); + }); + return out.map(r => ({ ...r, text: r.text.trim() })).filter(r => r.text); + } + // 3) 名字: 内容 + const named = lines.map(l => l.match(/^([^::\n]{1,30})[::]\s?(.*)$/)); + if (named.some(m => m && isName(m[1].trim()))) { + cur = null; + lines.forEach((l, i) => { + const m = named[i]; + if (m && isName(m[1].trim())) { cur = { who: m[1].trim(), at: null, text: m[2] }; out.push(cur); } + else if (cur) cur.text += '\n' + l; + else out.push(cur = { who: '', at: null, text: l }); + }); + return out.map(r => ({ ...r, text: r.text.trim() })).filter(r => r.text); + } + // 4) 一整条 + return [{ who: '', at: null, text: t }]; +} + function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { const custName = String(c.contact || '').replace(/\s*[((].*$/, '').trim() || c.company || '客户'; const [data, setData] = useState(null); @@ -6432,17 +6516,42 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { } catch (e) { onNotify('发送失败:' + e.message); } finally { setSending(false); } } + const newImp = () => ({ text: '', files: [], defFrom: 'customer', channel: 'whatsapp', at: '', over: {}, removed: {}, busy: false }); + const impAddFiles = (list) => { + const a = Array.from(list || []).map(f => ({ file: f, url: /^image\//.test(f.type) ? URL.createObjectURL(f) : '' })); + if (a.length) setImp(x => ({ ...x, files: [...x.files, ...a].slice(0, 30) })); + }; + const closeImp = () => { setImp(x => { (x && x.files || []).forEach(f => f.url && URL.revokeObjectURL(f.url)); return null; }); }; + const impRows = (() => { + if (!imp) return []; + const base = imp.at || cvNow(); + const parsed = cvParsePaste(imp.text, { me: CV_ME, customer: custName, extra: [c.contact, c.company] }); + const rows = parsed.map((r, i) => ({ + key: 't' + i, who: r.who, text: r.text, + at: r.at || (r.time ? `${base.slice(0, 10)} ${r.time.padStart(5, '0')}` : null), + from: imp.over['t' + i] || (r.who ? (cvSameName(r.who, CV_ME) ? 'me' : 'customer') : imp.defFrom), + })); + imp.files.forEach((f, i) => rows.push({ key: 'f' + i, file: i, f, text: '', at: null, from: imp.over['f' + i] || imp.defFrom })); + return rows.filter(r => !imp.removed[r.key]); + })(); async function doImport() { - if (!imp.text.trim() && !imp.files.length) { onNotify('请选择导出的聊天文件,或粘贴聊天文字'); return; } + if (!impRows.length) { onNotify('先把聊天内容复制粘贴进来'); return; } const fd = new FormData(); - fd.append('text', imp.text); fd.append('me', imp.me || CV_ME); - imp.files.forEach(f => fd.append('files', f)); + const used = []; + const messages = impRows.map(r => { + const m = { from: r.from, channel: imp.channel, at: r.at || '', text: r.text, who: r.who || '' }; + if (r.file !== undefined) { m.files = [used.length]; used.push(imp.files[r.file].file); } + return m; + }); + fd.append('messages', JSON.stringify(messages)); + fd.append('at', (imp.at || cvNow()).replace('T', ' ')); + used.forEach(f => fd.append('files', f)); setImp(x => ({ ...x, busy: true })); try { const j = await convoApi('POST', `/api/convos/${c.id}/import`, fd, true); setData(d => ({ ...(d || {}), items: [...((d && d.items) || []), ...j.items] })); - setImp(null); scrollEnd(); setTrState(''); - onNotify(`已导入 ${j.items.length} 条消息${j.skipped ? `(${j.skipped} 条已存在,跳过)` : ''}${!j.meFound && j.people && j.people.length ? `。注意:聊天里没找到"${imp.me}",发送人有:${j.people.join('、')}` : ''}`); + closeImp(); scrollEnd(); setTrState(''); + onNotify(`已导入 ${j.items.length} 条消息${j.skipped ? `(${j.skipped} 条已存在,跳过)` : ''}`); } catch (e) { onNotify('导入失败:' + e.message); setImp(x => x && ({ ...x, busy: false })); } } async function saveEdit() { @@ -6504,7 +6613,7 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { )} - + @@ -6512,19 +6621,47 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { {imp && ( -
-
导入整段聊天WhatsApp:聊天 → ⋮ → 更多 → 导出聊天(选"附带媒体"会连图片一起导入,得到 .zip);邮件:下载原始邮件 .eml。也可以直接把 WhatsApp 聊天文字粘贴进来。
+
{ const fs = Array.from(e.clipboardData.files || []); if (fs.length) { e.preventDefault(); impAddFiles(fs); } }} + onDragOver={e => { e.preventDefault(); }} + onDrop={e => { e.preventDefault(); e.stopPropagation(); impAddFiles(e.dataTransfer.files); }}> +
粘贴导入聊天记录在 WhatsApp / 微信 / 邮件里选中聊天内容 → 复制 → 在下面 Ctrl+V 粘贴。图片、文件也可以直接 Ctrl+V 粘贴(或拖进来),每个是一条消息。下面会先预览,确认谁说的再导入。
- - { const a = Array.from(e.target.files || []); setImp(x => ({ ...x, files: [...x.files, ...a].slice(0, 5) })); e.target.value = ''; }} /> - {imp.files.map((f, i) => {f.name})} - +
+ {CV_PICK.map(k => ( + + ))} +
+ 没写名字的消息算 +
+ + +
+
-