diff --git a/App.jsx b/App.jsx index b67b07e..a965a7c 100644 --- a/App.jsx +++ b/App.jsx @@ -2220,6 +2220,63 @@ const Styles = () => ( .visit-pdf { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; width: 150px; height: 120px; border: 1px solid #f5b5ac; border-radius: 8px; background: #fef1ef; color: #b42318; text-decoration: none; font-weight: 700; } .visit-pdf small { font-weight: 500; color: #b42318; } @media (max-width: 760px) { .visit-card { grid-template-columns: 1fr; } .visit-when { border-right: 0; border-bottom: 1px dashed var(--line); padding: 0 0 10px; } } + /* 沟通记录 */ + .cv-wrap { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; background: var(--panel); margin-top: 6px; } + .cv-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; } + .cv-add { display: grid; gap: 8px; padding: 12px; border: 1.5px dashed var(--line); border-radius: 10px; background: var(--bg); margin-bottom: 12px; } + .cv-add.drag { border-color: #2563eb; background: #eef4ff; } + .cv-add-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; } + .cv-chips { display: flex; gap: 5px; flex-wrap: wrap; } + .cv-chip { border: 1px solid var(--line); background: var(--panel); border-radius: 99px; padding: 3px 10px; font-size: 12px; cursor: pointer; color: var(--ink); } + .cv-chip.on { background: var(--ink); border-color: var(--ink); color: #fff; } + .cv-files { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; } + .cv-file { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 2px 8px; color: var(--ink); text-decoration: none; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } + .cv-file button { border: 0; background: none; cursor: pointer; padding: 0 0 0 2px; color: var(--ink-soft); display: inline-flex; } + .cv-ai { border: 1px solid #c7d7fe; background: linear-gradient(180deg, #f5f8ff, #fff 60%); border-radius: 12px; padding: 12px 14px; margin-bottom: 12px; } + .cv-ai-top { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; } + .cv-ai-badge { display: inline-flex; gap: 4px; align-items: center; font-size: 12px; font-weight: 700; color: #3538cd; } + .cv-stage { font-size: 12px; font-weight: 700; background: #3538cd; color: #fff; padding: 2px 10px; border-radius: 99px; } + .cv-intent { font-size: 12px; font-weight: 700; padding: 2px 10px; border-radius: 99px; } + .cv-intent.i-h { background: #dcfce7; color: #166534; } .cv-intent.i-m { background: #fef3c7; color: #92400e; } .cv-intent.i-l { background: #eef2f7; color: #475467; } + .cv-ai-meta { font-size: 11.5px; color: var(--ink-soft); } + .cv-stale { font-size: 11.5px; font-weight: 600; color: #b42318; background: #fef1ef; padding: 2px 8px; border-radius: 6px; } + .cv-sum { margin: 10px 0 4px; font-size: 13.5px; line-height: 1.6; } + .cv-reason { font-size: 12.5px; color: var(--ink-soft); margin-bottom: 8px; } + .cv-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 18px; margin-top: 8px; } + .cv-ai h4 { margin: 0 0 4px; font-size: 12.5px; color: #3538cd; } + .cv-grid ul, .cv-grid p { margin: 0; font-size: 13px; line-height: 1.55; } + .cv-grid ul { padding-left: 18px; } + .cv-grid .warn h4 { color: #b54708; } .cv-grid .risk h4 { color: #b42318; } + .cv-tbl { width: 100%; border-collapse: collapse; font-size: 12.5px; } + .cv-tbl td { border-bottom: 1px solid var(--line); padding: 3px 6px 3px 0; vertical-align: top; } + .cv-next { margin-top: 12px; display: grid; gap: 5px; } + .cv-act { display: flex; gap: 8px; align-items: baseline; font-size: 13px; } + .cv-act small { color: var(--ink-soft); margin-left: auto; white-space: nowrap; } + .cv-pri { font-size: 11px; font-weight: 700; padding: 1px 7px; border-radius: 5px; flex: none; } + .cv-pri.p-h { background: #fee2e2; color: #991b1b; } .cv-pri.p-m { background: #fef3c7; color: #92400e; } .cv-pri.p-l { background: #eef2f7; color: #475467; } + .cv-fu { margin-top: 12px; border-top: 1px dashed #c7d7fe; padding-top: 10px; } + .cv-fu-h { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; } + .cv-fu-sub { font-size: 12.5px; font-weight: 600; margin: 4px 0; } + .cv-fu-msg { white-space: pre-wrap; font-family: inherit; font-size: 13px; line-height: 1.55; background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin: 6px 0; max-height: 320px; overflow: auto; } + .cv-tags { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 10px; } + .cv-tags span { font-size: 11.5px; background: #eef2ff; color: #3538cd; border-radius: 99px; padding: 2px 9px; } + .cv-ask { display: flex; gap: 6px; margin-bottom: 8px; } + .cv-ask .input { flex: 1; } + .cv-answer { font-size: 13px; background: #f5f8ff; border: 1px solid #c7d7fe; border-radius: 8px; padding: 8px 12px; margin-bottom: 10px; line-height: 1.55; white-space: pre-wrap; } + .cv-answer b { display: block; font-size: 12px; color: #3538cd; margin-bottom: 2px; } + .cv-filter { display: flex; justify-content: space-between; gap: 8px; align-items: center; flex-wrap: wrap; margin: 4px 0 8px; } + .cv-list { display: grid; gap: 8px; } + .cv-item { border: 1px solid var(--line); border-left: 4px solid; border-radius: 9px; padding: 8px 10px; background: var(--bg); } + .cv-item-h { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; font-size: 13px; } + .cv-item-h small { color: var(--ink-soft); font-size: 11.5px; } + .cv-ch { color: #fff; font-size: 11px; font-weight: 700; padding: 1px 8px; border-radius: 5px; } + .cv-title { font-weight: 600; } + .cv-note { font-size: 11.5px; color: #b54708; margin-top: 4px; } + .cv-text { white-space: pre-wrap; word-break: break-word; font-family: inherit; font-size: 12.5px; line-height: 1.5; margin: 6px 0 0; color: var(--ink); max-height: 120px; overflow: hidden; } + .cv-text.open { max-height: 480px; overflow: auto; } + .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; } } .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; } @@ -3144,6 +3201,7 @@ function MainApp({ username, onLogout }) { if (!window.confirm('确定删除该客户档案?此操作不可恢复。')) return; setCustomers(prev => prev.filter(c => c.id !== id)); if (selectedId === id) setSelectedId(null); + fetch(`/api/convos/${id}`, { method: 'DELETE', credentials: 'same-origin' }).catch(() => {}); // 沟通记录一起删 notify('已删除'); } function togglePreferredContact(id, channel) { @@ -4158,6 +4216,8 @@ function MainApp({ username, onLogout }) { onTogglePreferred={(channel) => togglePreferredContact(selected.id, channel)} onToggleVip={() => toggleVip(selected.id)} onToggleOrdered={() => toggleOrdered(selected.id)} + onNotify={notify} + onPatchCustomer={(patch) => setCustomers(prev => prev.map(x => (x.id === selected.id ? { ...x, ...patch } : x)))} attachmentNote={attachmentNote} setAttachmentNote={setAttachmentNote} attachmentCategory={attachmentCategory} @@ -6191,6 +6251,280 @@ function folderPathLabel(folders, folderId) { return parts.join(' / '); } +/* ------------------------------ 客户沟通记录 + AI 分析 ------------------------------ */ +// 记录和客户的沟通(邮件、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' }, + phone: { label: '电话', color: '#9333ea' }, + meeting: { label: '会议 / 面谈', color: '#c2410c' }, + other: { label: '其他', color: '#64748b' }, +}; +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); + +function ConversationPanel({ customer: c, onNotify, onPatchCustomer }) { + 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 [drag, setDrag] = useState(false); + const [open, setOpen] = useState({}); + const [editId, setEditId] = useState(null); + const [edit, setEdit] = useState(null); + const [q, setQ] = useState(''); + const [chFilter, setChFilter] = useState('all'); + const [aiBusy, setAiBusy] = useState(false); + const [aiSecs, setAiSecs] = useState(0); + const [ask, setAsk] = useState(''); + const [answer, setAnswer] = useState(null); + const [asking, setAsking] = useState(false); + const fileRef = useRef(null); + + const load = async () => { + try { setData(await convoApi('GET', `/api/convos/${c.id}`)); } + catch (e) { setData({ items: [], analysis: null }); onNotify('读取沟通记录失败:' + e.message); } + }; + useEffect(() => { setData(null); setAnswer(null); setAdding(false); setEditId(null); 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 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; } + 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); + 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); } + } + async function saveEdit() { + try { + const j = await convoApi('PUT', `/api/convos/${c.id}/${editId}`, edit); + setData(d => ({ ...d, items: d.items.map(x => (x.id === editId ? j.item : x)) })); + setEditId(null); onNotify('已保存'); + } 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('已删除'); } + catch (e) { onNotify('删除失败:' + e.message); } + } + async function analyze() { + setAiBusy(true); setAiSecs(0); + 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`); + for (;;) { + await new Promise(r => setTimeout(r, 2000)); + const j = await convoApi('GET', `/api/ai/jobs/${jobId}`); + if (j.status === 'running') continue; + if (j.status === 'done' && j.data && j.data.analysis) { setData(d => ({ ...d, analysis: j.data.analysis })); onNotify('AI 分析完成'); } + else throw new Error((j.data && j.data.error) || 'AI 分析失败'); + break; + } + } catch (e) { onNotify('AI 分析失败:' + e.message); } + finally { clearInterval(tick); setAiBusy(false); } + } + async function doAsk() { + if (!ask.trim()) return; + setAsking(true); setAnswer(null); + try { const j = await convoApi('POST', `/api/convos/${c.id}/ask`, { question: ask.trim() }); setAnswer({ q: ask.trim(), a: j.answer }); } + catch (e) { onNotify('提问失败:' + e.message); } + finally { setAsking(false); } + } + 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 R = an && an.result; + const waDigits = String(c.phone || '').replace(/[^\d]/g, ''); + + return ( +
+
+ 沟通记录{items.length ? `(${items.length})` : ''} +
+ + +
+
+ + {adding && ( +
{ e.preventDefault(); setDrag(true); }} onDragLeave={() => setDrag(false)} + onDrop={e => { e.preventDefault(); setDrag(false); addFiles(e.dataTransfer.files); }}> +
+
+ + {Object.entries(CONVO_CH).map(([k, v]) => ( + + ))} +
+ setForm(f => ({ ...f, date: e.target.value }))} title="沟通日期(邮件会自动用邮件日期)" /> +
+ setForm(f => ({ ...f, title: e.target.value }))} /> +