diff --git a/App.jsx b/App.jsx index e315383..6d55e72 100644 --- a/App.jsx +++ b/App.jsx @@ -5,7 +5,7 @@ import { Building2, Globe, Phone, Save, RefreshCw, LayoutDashboard, Users, Package, Layers, Zap, Tag, Inbox, Paperclip, Menu, LogOut, GripVertical, BookOpen, FileText, FolderTree, Lock, Sparkles, Printer, Image as ImageIcon, - ShieldCheck, Settings2, Boxes, Download, Wifi, Radio, QrCode, Folder, FolderPlus, Factory, UploadCloud, ClipboardList, MessageCircle, CalendarDays, MapPin, Wrench, Hash + ShieldCheck, Settings2, Boxes, Download, Wifi, Radio, QrCode, Folder, FolderPlus, Factory, UploadCloud, ClipboardList, MessageCircle, CalendarDays, MapPin, Wrench, Hash, Send, Languages } from 'lucide-react'; import { MicUploadPage } from './mic-upload/MicUploadPage'; import { ImageToolPage } from './mic-upload/ImageToolPage'; @@ -2278,6 +2278,73 @@ const Styles = () => ( .cv-item-f { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; margin-top: 4px; } .cv-edit { display: grid; gap: 8px; } @media (max-width: 760px) { .cv-grid { grid-template-columns: 1fr; } } + /* 沟通记录:聊天样式 */ + .cv-search { display: inline-flex; align-items: center; gap: 4px; border: 1px solid var(--line); border-radius: 8px; padding: 0 6px; background: var(--bg); color: var(--ink-soft); height: 30px; } + .cv-search input { border: 0; outline: 0; background: transparent; width: 120px; font-size: 12.5px; color: var(--ink); } + .cv-search button { border: 0; background: none; cursor: pointer; color: var(--ink-soft); display: inline-flex; padding: 0; } + .cv-imp { display: grid; gap: 8px; padding: 12px; border: 1.5px dashed var(--line); border-radius: 10px; background: var(--bg); margin-bottom: 12px; } + .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-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; + background-color: #efeae2; background-image: radial-gradient(rgba(0,0,0,.035) 1px, transparent 1px); background-size: 18px 18px; } + .cv-empty { margin: auto; text-align: center; color: #54656f; font-size: 13px; line-height: 1.8; background: rgba(255,255,255,.75); padding: 12px 18px; border-radius: 10px; } + .cv-day { display: flex; justify-content: center; margin: 8px 0 4px; } + .cv-day span { background: #fff; color: #54656f; font-size: 11.5px; padding: 3px 12px; border-radius: 8px; box-shadow: 0 1px .5px rgba(0,0,0,.13); } + .cv-row { display: flex; align-items: flex-start; gap: 4px; } + .cv-row.me { flex-direction: row-reverse; } + .cv-bub { position: relative; max-width: min(72%, 560px); background: #fff; border-radius: 0 10px 10px 10px; padding: 6px 9px 4px; box-shadow: 0 1px .5px rgba(0,0,0,.13); font-size: 13.5px; line-height: 1.5; color: #111b21; } + .cv-row.me .cv-bub { background: #d9fdd3; border-radius: 10px 0 10px 10px; } + .cv-bub-edit { max-width: 100%; width: 100%; display: grid; gap: 6px; padding: 10px; } + .cv-bub-h { display: flex; align-items: center; gap: 10px; justify-content: space-between; margin-bottom: 2px; } + .cv-who { font-size: 12.5px; color: #c2410c; } + .cv-row.me .cv-who { color: #047857; } + .cv-chtag { font-size: 10.5px; font-weight: 700; border: 1px solid; border-radius: 99px; padding: 0 7px; line-height: 16px; background: rgba(255,255,255,.6); white-space: nowrap; } + .cv-txt { white-space: pre-wrap; word-break: break-word; } + .cv-zh { white-space: pre-wrap; word-break: break-word; margin-top: 4px; padding-top: 4px; border-top: 1px dashed rgba(0,0,0,.15); color: #3b4a54; font-size: 13px; } + .cv-zh.wait { color: #8696a0; font-size: 12px; } + .cv-zh.wait button { border: 0; background: none; color: #2563eb; cursor: pointer; padding: 0; font-size: 12px; } + .cv-time { text-align: right; font-size: 10.5px; color: #667781; margin-top: 1px; } + .cv-tools { display: flex; flex-direction: column; gap: 2px; opacity: 0; transition: opacity .15s; padding-top: 2px; } + .cv-row:hover .cv-tools { opacity: 1; } + .cv-ico { border: 0; background: rgba(255,255,255,.85); border-radius: 6px; width: 24px; height: 22px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; color: #54656f; } + .cv-ico:hover { background: #fff; color: #111b21; } + .cv-imgs { display: grid; gap: 3px; margin: 2px 0 4px; } + .cv-imgs.n1 { grid-template-columns: 1fr; } .cv-imgs.n2, .cv-imgs.n4 { grid-template-columns: 1fr 1fr; } .cv-imgs.n3 { grid-template-columns: 1fr 1fr 1fr; } + .cv-imgs img { display: block; width: 100%; max-height: 260px; object-fit: cover; border-radius: 6px; background: #f0f2f5; } + .cv-imgs.n1 img { width: auto; max-width: min(100%, 300px); max-height: 260px; } + .cv-imgs.n2, .cv-imgs.n3, .cv-imgs.n4 { width: min(100%, 320px); } + .cv-doc { display: flex; align-items: center; gap: 8px; background: rgba(0,0,0,.05); border-radius: 7px; padding: 7px 9px; margin: 2px 0 4px; text-decoration: none; color: inherit; min-width: 220px; } + .cv-doc:hover { background: rgba(0,0,0,.08); } + .cv-doc-i { position: relative; width: 30px; height: 34px; border-radius: 4px; background: #64748b; color: #fff; display: inline-flex; align-items: center; justify-content: center; flex: none; } + .cv-doc-i.pdf { background: #e5252a; } + .cv-doc-i i { position: absolute; bottom: 1px; font-size: 7.5px; font-style: normal; font-weight: 700; } + .cv-doc-i svg { margin-top: -6px; } + .cv-doc-n { display: grid; min-width: 0; } .cv-doc-n b { font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .cv-doc-n small { font-size: 11px; color: #667781; } + .cv-note-card { align-self: center; width: min(92%, 680px); background: #fff8e1; border: 1px solid #f5e1a4; border-radius: 10px; padding: 8px 10px; font-size: 12.5px; } + .cv-note-h { display: flex; gap: 8px; align-items: center; } .cv-note-h .cv-ico { margin-left: auto; } + .cv-note-t { white-space: pre-wrap; word-break: break-word; font-family: inherit; margin: 4px 0; max-height: 360px; overflow: auto; } + .cv-more { border: 0; background: none; color: #2563eb; cursor: pointer; padding: 0; font-size: 12px; } + .cv-comp { background: #f0f2f5; border-top: 1px solid rgba(0,0,0,.06); padding: 8px 10px 10px; display: grid; gap: 6px; } + .cv-comp-top { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; } + .cv-seg { display: inline-flex; background: #fff; border: 1px solid var(--line); border-radius: 99px; padding: 2px; } + .cv-seg button { border: 0; background: none; border-radius: 99px; padding: 3px 12px; font-size: 12.5px; font-weight: 600; cursor: pointer; color: #54656f; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } + .cv-seg button.on { background: #c2410c; color: #fff; } + .cv-seg button.on.me { background: #047857; } + .cv-at { display: inline-flex; align-items: center; gap: 4px; margin-left: auto; font-size: 12px; color: #54656f; background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 2px 6px; } + .cv-at input { border: 0; outline: 0; font-size: 12px; background: transparent; color: var(--ink); width: 168px; } + .cv-at button { border: 0; background: none; cursor: pointer; padding: 0; display: inline-flex; color: #54656f; } + .cv-comp-row { display: flex; gap: 6px; align-items: flex-end; } + .cv-comp-row textarea { flex: 1; resize: none; border: 0; outline: 0; border-radius: 10px; padding: 9px 12px; font-family: inherit; font-size: 13.5px; line-height: 1.45; background: #fff; min-height: 40px; } + .cv-clip { border: 0; background: none; width: 36px; height: 40px; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; color: #54656f; border-radius: 50%; flex: none; } + .cv-clip:hover { background: rgba(0,0,0,.06); } + .cv-send { border: 0; width: 40px; height: 40px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; cursor: pointer; color: #fff; background: #c2410c; flex: none; } + .cv-send.me { background: #00a884; } + .cv-send:disabled { opacity: .5; cursor: default; } + .cv-sel { width: auto; height: 30px; font-size: 12.5px; } + @media (max-width: 760px) { .cv-bub { max-width: 86%; } .cv-at { margin-left: 0; } } .brand-vis { display: inline-flex; border: 1px solid var(--line); border-radius: 99px; overflow: hidden; font-weight: 500; } .brand-vis button { display: inline-flex; align-items: center; gap: 3px; border: 0; background: transparent; font-size: 11px; padding: 1px 8px; cursor: pointer; color: var(--ink-soft); } .brand-vis button.on { background: var(--ink); color: #fff; } @@ -6260,79 +6327,138 @@ function folderPathLabel(folders, folderId) { // 记录和客户的沟通(邮件、WhatsApp、微信、电话、会议……):粘贴文字,或拖入 WhatsApp 导出(.txt / .zip)、邮件(.eml)、截图(自动识别文字)、PDF、Word。 // 数据存在服务器的 conversations.json(不放进客户列表);AI 分析读这个客户的全部记录,给出总结、阶段、意向、需求、顾虑、下一步和跟进消息草稿。 const CONVO_CH = { - email: { label: '邮件', color: '#2563eb' }, whatsapp: { label: 'WhatsApp', color: '#16a34a' }, - wechat: { label: '微信', color: '#15803d' }, + wechat: { label: '微信', color: '#07c160' }, + email: { label: '邮件', color: '#2563eb' }, phone: { label: '电话', color: '#9333ea' }, meeting: { label: '会议 / 面谈', color: '#c2410c' }, other: { label: '其他', color: '#64748b' }, }; +const CV_PICK = ['whatsapp', 'wechat', 'email']; // 输入框可选的渠道 +const CV_ME = 'Mike'; const convoApi = async (method, url, body, isForm) => { const r = await fetch(url, { method, credentials: 'same-origin', headers: isForm || !body ? undefined : { 'Content-Type': 'application/json' }, body: isForm ? body : body ? JSON.stringify(body) : undefined }); const j = await r.json().catch(() => ({})); if (!r.ok) throw new Error(j.error || `请求失败(${r.status})`); return j; }; -const cvToday = () => new Date(Date.now() - new Date().getTimezoneOffset() * 60000).toISOString().slice(0, 10); +const cvNow = () => new Date(Date.now() - new Date().getTimezoneOffset() * 60000).toISOString().slice(0, 16); // 本地时间 YYYY-MM-DDTHH:mm +const cvDayLabel = (d) => { + const today = cvNow().slice(0, 10), y = new Date(Date.now() - 86400000 - new Date().getTimezoneOffset() * 60000).toISOString().slice(0, 10); + if (d === today) return '今天'; if (d === y) return '昨天'; + const [Y, M, D] = d.split('-').map(Number); + return `${Y} 年 ${M} 月 ${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`); function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { + const custName = String(c.contact || '').replace(/\s*[((].*$/, '').trim() || c.company || '客户'; const [data, setData] = useState(null); - const [adding, setAdding] = useState(false); - const [form, setForm] = useState({ channel: '', date: cvToday(), title: '', text: '', files: [] }); - const [saving, setSaving] = useState(false); + const [from, setFrom] = useState('customer'); + const [channel, setChannel] = useState('whatsapp'); + const [at, setAt] = useState(''); // 空 = 发送时用当前时间 + const [text, setText] = useState(''); + const [files, setFiles] = useState([]); + const [sending, setSending] = useState(false); const [drag, setDrag] = useState(false); - const [open, setOpen] = useState({}); + const [showZh, setShowZh] = useState(true); const [editId, setEditId] = useState(null); const [edit, setEdit] = useState(null); const [q, setQ] = useState(''); - const [chFilter, setChFilter] = useState('all'); + const [openNote, setOpenNote] = useState({}); + const [imp, setImp] = useState(null); // 导入面板:{ text, files, me, busy } + const [trState, setTrState] = useState(''); // '' | 'busy' | 'error' const [aiBusy, setAiBusy] = useState(false); const [aiSecs, setAiSecs] = useState(0); + const [aiOpen, setAiOpen] = useState(true); const [ask, setAsk] = useState(''); const [answer, setAnswer] = useState(null); const [asking, setAsking] = useState(false); - const fileRef = useRef(null); + const fileRef = useRef(null), impRef = useRef(null), boxRef = useRef(null), taRef = useRef(null), trRun = useRef(false), cidRef = useRef(c.id); + const scrollEnd = () => setTimeout(() => { const b = boxRef.current; if (b) b.scrollTop = b.scrollHeight; }, 30); const load = async () => { - try { setData(await convoApi('GET', `/api/convos/${c.id}`)); } + try { const d = await convoApi('GET', `/api/convos/${c.id}`); if (cidRef.current !== c.id) return; setData(d); scrollEnd(); } catch (e) { setData({ items: [], analysis: null }); onNotify('读取沟通记录失败:' + e.message); } }; - useEffect(() => { setData(null); setAnswer(null); setAdding(false); setEditId(null); load(); }, [c.id]); + useEffect(() => { + cidRef.current = c.id; + setData(null); setAnswer(null); setEditId(null); setImp(null); setText(''); setFiles([]); setAt(''); setTrState(''); + load(); + }, [c.id]); const items = (data && data.items) || []; const an = data && data.analysis; const newer = an ? items.filter(x => (x.createdAt || 0) > an.at || (x.updatedAt || 0) > an.at).length : 0; + const untranslated = items.filter(m => m.from !== 'note' && m.zh === undefined).length; - const addFiles = (list) => setForm(f => ({ ...f, files: [...f.files, ...Array.from(list || [])].slice(0, 10) })); - async function submit() { - if (!form.text.trim() && !form.files.length) { onNotify('请粘贴沟通内容或添加文件'); return; } + // 自动把还没翻译的消息翻成中文(每次 30 条,直到全部完成) + async function translateAll() { + if (trRun.current) return; + trRun.current = true; setTrState('busy'); + const cid = c.id; + try { + for (let guard = 0; guard < 200; guard++) { + const j = await convoApi('POST', `/api/convos/${cid}/translate`, {}); + if (cidRef.current !== cid) break; + if (j.done && j.done.length) { + const map = new Map(j.done.map(x => [x.id, x.zh])); + setData(d => (d ? { ...d, items: d.items.map(m => (map.has(m.id) ? { ...m, zh: map.get(m.id) } : m)) } : d)); + } + if (!j.remaining || !(j.done && j.done.length)) break; + } + setTrState(''); + } catch (e) { setTrState('error'); } + finally { trRun.current = false; } + } + useEffect(() => { if (untranslated > 0 && !trRun.current && trState !== 'error') translateAll(); }, [untranslated, data && c.id]); + + const addFiles = (list) => { const a = Array.from(list || []); if (a.length) setFiles(f => [...f, ...a].slice(0, 10)); }; + async function send() { + if (!text.trim() && !files.length) { taRef.current && taRef.current.focus(); return; } const fd = new FormData(); - ['channel', 'date', 'title', 'text'].forEach(k => fd.append(k, form[k] || '')); - form.files.forEach(f => fd.append('files', f)); - setSaving(true); + fd.append('from', from); fd.append('channel', channel); fd.append('text', text); + fd.append('at', (at || cvNow()).replace('T', ' ')); + files.forEach(f => fd.append('files', f)); + setSending(true); try { const j = await convoApi('POST', `/api/convos/${c.id}`, fd, true); setData(d => ({ ...(d || {}), items: [...((d && d.items) || []), j.item] })); - setForm({ channel: '', date: cvToday(), title: '', text: '', files: [] }); - setAdding(false); - onNotify(`已添加:${CONVO_CH[j.item.channel]?.label || ''}${j.item.stats?.messages ? `,${j.item.stats.messages} 条消息` : ''}`); - } catch (e) { onNotify('添加失败:' + e.message); } - finally { setSaving(false); } + setText(''); setFiles([]); setAt(''); + setFrom(f => (f === 'customer' ? 'me' : 'customer')); // 一问一答:发完自动切换到另一方 + scrollEnd(); + setTimeout(() => taRef.current && taRef.current.focus(), 50); + } catch (e) { onNotify('发送失败:' + e.message); } + finally { setSending(false); } + } + async function doImport() { + if (!imp.text.trim() && !imp.files.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)); + 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('、')}` : ''}`); + } catch (e) { onNotify('导入失败:' + e.message); setImp(x => x && ({ ...x, busy: false })); } } async function saveEdit() { try { - const j = await convoApi('PUT', `/api/convos/${c.id}/${editId}`, edit); + const j = await convoApi('PUT', `/api/convos/${c.id}/${editId}`, { ...edit, at: edit.at.replace('T', ' ') }); setData(d => ({ ...d, items: d.items.map(x => (x.id === editId ? j.item : x)) })); - setEditId(null); onNotify('已保存'); + setEditId(null); } catch (e) { onNotify('保存失败:' + e.message); } } - async function del(it) { - if (!window.confirm(`删除这条${CONVO_CH[it.channel]?.label || ''}记录(${it.date}${it.title ? ' · ' + it.title : ''})?附件也会一起删除。`)) return; - try { await convoApi('DELETE', `/api/convos/${c.id}/${it.id}`); setData(d => ({ ...d, items: d.items.filter(x => x.id !== it.id) })); onNotify('已删除'); } + async function del(m) { + if (!window.confirm(`删除这条消息?${(m.files || []).length ? '附件也会一起删除。' : ''}`)) return; + try { await convoApi('DELETE', `/api/convos/${c.id}/${m.id}`); setData(d => ({ ...d, items: d.items.filter(x => x.id !== m.id) })); } catch (e) { onNotify('删除失败:' + e.message); } } async function analyze() { - setAiBusy(true); setAiSecs(0); + setAiBusy(true); setAiSecs(0); setAiOpen(true); const t0 = Date.now(), tick = setInterval(() => setAiSecs(Math.round((Date.now() - t0) / 1000)), 1000); try { const { jobId } = await convoApi('POST', `/api/convos/${c.id}/analyze`); @@ -6357,51 +6483,48 @@ function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { const copy = (t) => { navigator.clipboard.writeText(t).then(() => onNotify('已复制'), () => onNotify('复制失败,请手动选择')); }; const nq = q.trim().toLowerCase(); - const shown = [...items] - .filter(x => chFilter === 'all' || x.channel === chFilter) - .filter(x => !nq || [x.title, x.text, ...(x.files || []).map(f => f.name)].some(v => String(v || '').toLowerCase().includes(nq))) - .sort((a, b) => String(b.date).localeCompare(String(a.date)) || (b.createdAt || 0) - (a.createdAt || 0)); + const sorted = [...items].sort((a, b) => String(a.at).localeCompare(String(b.at)) || (a.createdAt || 0) - (b.createdAt || 0)); + const shown = nq ? sorted.filter(m => [m.text, m.zh, m.title, ...(m.files || []).map(f => f.name)].some(v => String(v || '').toLowerCase().includes(nq))) : sorted; const R = an && an.result; const waDigits = String(c.phone || '').replace(/[^\d]/g, ''); + const nameOf = (m) => (m.from === 'me' ? CV_ME : (m.who && m.who !== CV_ME ? m.who : custName)); + const fileUrl = (f) => `/uploads/${c.id}/${encodeURIComponent(f.stored)}`; + let lastDay = ''; return (
沟通记录{items.length ? `(${items.length})` : ''} -
- - } +
+ )} + + +
- {adding && ( -
{ e.preventDefault(); setDrag(true); }} onDragLeave={() => setDrag(false)} - onDrop={e => { e.preventDefault(); setDrag(false); addFiles(e.dataTransfer.files); }}> + {imp && ( +
+
导入整段聊天WhatsApp:聊天 → ⋮ → 更多 → 导出聊天(选"附带媒体"会连图片一起导入,得到 .zip);邮件:下载原始邮件 .eml。也可以直接把 WhatsApp 聊天文字粘贴进来。
-
- - {Object.entries(CONVO_CH).map(([k, v]) => ( - - ))} -
- setForm(f => ({ ...f, date: e.target.value }))} title="沟通日期(邮件会自动用邮件日期)" /> + + { 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})} +
- setForm(f => ({ ...f, title: e.target.value }))} /> -