Update App.jsx via upload script - 2026-11-07 01:29:49

This commit is contained in:
mike 2026-11-07 01:30:02 +08:00
parent c9320c36ff
commit 53a6d31f0b
1 changed files with 105 additions and 10 deletions

115
App.jsx
View File

@ -2015,6 +2015,7 @@ const Styles = () => (
.crm-sidebar-overlay { display: none; }
@media (max-width: 880px) {
.crm-shell { flex-direction: column; }
.crm-topbar-mobile { display: flex; }
.crm-sidebar {
position: fixed; top: 0; left: -260px; height: 100vh; width: 240px; z-index: 40;
@ -2471,6 +2472,8 @@ export default function CustomerCRM() {
});
}
const focus = useMemo(() => { try { const q = new URLSearchParams(window.location.search); return q.get('chat') ? { sid: q.get('chat'), k: q.get('k') || '' } : null; } catch (e) { return null; } }, []);
if (focus && (focus.k || authState === 'in')) return <ChatFocus sid={focus.sid} k={focus.k} authed={authState === 'in'} />;
if (authState === 'checking') {
return (
<div className="crm-root" style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: '100vh' }}>
@ -9843,7 +9846,9 @@ function ChatCenter({ onNotify, onSaveCustomer, initialSid }) {
);
}
function ChatConversation({ conv, onBack, onNotify, onSaveCustomer, onReload, onDeleted }) {
function ChatConversation({ conv, onBack, onNotify, onSaveCustomer, onReload, onDeleted, k, focus, authed = true }) {
const kq = k ? `?k=${encodeURIComponent(k)}` : '';
const [noteMode, setNoteMode] = useState(false);
const [text, setText] = useState('');
const [note, setNote] = useState('');
const [translate, setTranslate] = useState(true);
@ -9855,18 +9860,18 @@ function ChatConversation({ conv, onBack, onNotify, onSaveCustomer, onReload, on
const lastId = conv.msgs.length ? conv.msgs[conv.msgs.length - 1].id : 0;
useEffect(() => { if (bodyRef.current) bodyRef.current.scrollTop = bodyRef.current.scrollHeight; }, [lastId, conv.typing, conv.sid]);
useEffect(() => { setText(''); setNote(''); setPreview(''); }, [conv.sid]);
const act = async (p, body, ok) => { try { await chApi('POST', `/api/chat/admin/${conv.sid}/${p}`, body); if (ok) onNotify(ok); onReload(); } catch (e) { onNotify('失败:' + e.message); } };
const act = async (p, body, ok) => { try { await chApi('POST', `/api/chat/admin/${conv.sid}/${p}${kq}`, body); if (ok) onNotify(ok); onReload(); } catch (e) { onNotify('失败:' + e.message); } };
const send = async () => {
const v = text.trim(); if (!v) return;
setBusy(true);
try { await chApi('POST', `/api/chat/admin/${conv.sid}/send`, { text: v, translate }); setText(''); setPreview(''); onReload(); }
try { await chApi('POST', `/api/chat/admin/${conv.sid}/send${kq}`, { text: v, translate }); setText(''); setPreview(''); onReload(); }
catch (e) { onNotify('发送失败:' + e.message); }
setBusy(false);
};
const doPreview = async () => {
if (!text.trim()) return;
setBusy(true);
try { const j = await chApi('POST', `/api/chat/admin/${conv.sid}/translate`, { text }); setPreview(j.text); } catch (e) { onNotify('翻译失败:' + e.message); }
try { const j = await chApi('POST', `/api/chat/admin/${conv.sid}/translate${kq}`, { text }); setPreview(j.text); } catch (e) { onNotify('翻译失败:' + e.message); }
setBusy(false);
};
const saveExample = async (m) => {
@ -9881,7 +9886,19 @@ function ChatConversation({ conv, onBack, onNotify, onSaveCustomer, onReload, on
const t = CH_TIER[l.tier || 'unknown'] || CH_TIER.unknown;
const human = conv.mode === 'human';
return (
<div className="chc-pane">
<div className={`chc-pane ${focus ? 'focus' : ''}`}>
{focus ? (
<div className="chc-fhead">
{focus.back && <a className="chc-fback" href={focus.back} aria-label="全部对话">‹</a>}
<div className="chc-fwho" onClick={() => setLeadOpen((v) => !v)}>
<b>{chFlag(conv.ipCountry)} {l.company || l.name || l.country || '访客'}</b>
<small><span className="chc-tier" style={{ background: t[1] }}>{t[0]}</span> {l.score ? `${l.score} 分 · ` : ''}{human ? '你在接待' : conv.typing ? 'AI 正在输入…' : 'AI 接待中'}</small>
</div>
<button className="chc-fbtn" onClick={() => setShowOrig((v) => !v)}>{showOrig ? '中' : '原'}</button>
<button className="chc-fbtn" onClick={() => setLeadOpen((v) => !v)}>ⓘ</button>
{human ? <button className="chc-fbtn main" onClick={() => act('release', null, '已交还 AI')}>交还 AI</button> : <button className="chc-fbtn main" onClick={() => act('takeover', null, '已接管')}>接管</button>}
</div>
) : (
<div className="chc-head">
<button className="btn btn-sm chc-back" onClick={onBack}>← 返回</button>
<div className="chc-head-who">
@ -9894,10 +9911,12 @@ function ChatConversation({ conv, onBack, onNotify, onSaveCustomer, onReload, on
<button className="btn btn-sm" onClick={() => setShowOrig((v) => !v)}>{showOrig ? '看中文' : '看原文'}</button>
</div>
</div>
<div className="chc-status">
)}
{focus && l.needsHuman && !human && <div className="chc-status"><span className="need">AI 建议人工介入{l.handoff ? `:${l.handoff}` : ''}</span></div>}
{!focus && <div className="chc-status">
{human ? <span className="human">● 你在接待,AI 已暂停</span> : <span>● AI 正在接待{conv.typing ? '(正在输入…)' : ''}</span>}
{l.needsHuman && !human && <span className="need">AI 建议人工介入{l.handoff ? `:${l.handoff}` : ''}</span>}
</div>
</div>}
<div className="chc-split">
<div className="chc-body" ref={bodyRef}>
{conv.msgs.map((m) => (
@ -9907,7 +9926,7 @@ function ChatConversation({ conv, onBack, onNotify, onSaveCustomer, onReload, on
<p>{chLink(!showOrig && m.zh && m.from !== 'human' ? m.zh : m.text)}</p>
{m.from === 'human' && m.zh && <em>你写的:{m.zh}</em>}
{!showOrig && m.zh && m.from !== 'human' && <em className="orig">{m.text}</em>}
{m.from === 'human' && <button className="chc-star" onClick={() => saveExample(m)}>⭐ 存为范例</button>}
{m.from === 'human' && authed && <button className="chc-star" onClick={() => saveExample(m)}>⭐ 存为范例</button>}
</div>
)
))}
@ -9924,14 +9943,30 @@ function ChatConversation({ conv, onBack, onNotify, onSaveCustomer, onReload, on
<div className="chc-kv"><span>语言</span><b>{conv.custLang || conv.lang}</b></div>
{conv.pages && conv.pages.length > 0 && <div className="chc-pages"><span>看过的页面</span>{conv.pages.slice(-6).map((p, i) => <a key={i} href={p.page} target="_blank" rel="noreferrer">{p.page.replace(/^https?:\/\/[^/]+/, '')}</a>)}</div>}
<div className="chc-lead-act">
<button className="btn btn-sm btn-primary" onClick={() => onSaveCustomer(conv)}>存为客户档案</button>
{onSaveCustomer && <button className="btn btn-sm btn-primary" onClick={() => onSaveCustomer(conv)}>存为客户档案</button>}
{['importer', 'project', 'reseller', 'personal', 'spam'].map((k) => l.tier !== k && <button key={k} className="btn btn-sm" onClick={() => act('lead', { tier: k }, '已改为' + CH_TIER[k][0])}>标为{CH_TIER[k][0]}</button>)}
<button className="btn btn-sm" onClick={() => act('close', null, '已结束')}>结束对话</button>
<button className="btn btn-sm" style={{ color: 'var(--led-red)' }} onClick={async () => { if (!window.confirm('删除这个对话?')) return; await chApi('POST', `/api/chat/admin/${conv.sid}/delete`); onDeleted(); }}>删除</button>
{authed && onDeleted && <button className="btn btn-sm" style={{ color: 'var(--led-red)' }} onClick={async () => { if (!window.confirm('删除这个对话?')) return; await chApi('POST', `/api/chat/admin/${conv.sid}/delete`); onDeleted(); }}>删除</button>}
</div>
</div>
)}
</div>
{focus ? (
<div className={`chc-fcomp ${noteMode ? 'note' : ''}`}>
{preview && <div className="chc-preview"><small>客户会看到:</small>{preview}</div>}
<div className="chc-frow">
<textarea rows={1} value={noteMode ? note : text} placeholder={noteMode ? '悄悄话给 AI(客户看不到)' : human ? '用中文写,自动翻译给客户' : '回复就会接管(AI 暂停)'}
onChange={(e) => { if (noteMode) setNote(e.target.value); else { setText(e.target.value); setPreview(''); } e.target.style.height = 'auto'; e.target.style.height = Math.min(e.target.scrollHeight, 140) + 'px'; }} />
<button className="chc-fsend" disabled={busy || !(noteMode ? note : text).trim()} onClick={() => { if (noteMode) { act('note', { text: note.trim() }, '已告诉 AI'); setNote(''); } else send(); }}>{busy ? '…' : noteMode ? '告诉AI' : '发送'}</button>
</div>
<div className="chc-fopts">
<label><input type="checkbox" checked={translate} onChange={(e) => setTranslate(e.target.checked)} /> 自动翻译</label>
{!noteMode && translate && /[\u4e00-\u9fff]/.test(text) && <button onClick={doPreview} disabled={busy}>看翻译</button>}
<button className={noteMode ? 'on' : ''} onClick={() => setNoteMode((v) => !v)}>{noteMode ? '← 回复客户' : '悄悄话'}</button>
<button onClick={() => act('nudge', null, 'AI 正在回复')}>让 AI 回复</button>
</div>
</div>
) : (
<div className="chc-compose">
<textarea value={text} onChange={(e) => { setText(e.target.value); setPreview(''); }} placeholder={human ? '直接用中文写,发出去会自动翻译成客户的语言' : '在这里回复就会接管对话(AI 暂停);用中文写会自动翻译'} rows={2}
onKeyDown={(e) => { if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) send(); }} />
@ -9947,6 +9982,7 @@ function ChatConversation({ conv, onBack, onNotify, onSaveCustomer, onReload, on
<button className="btn btn-sm" onClick={() => act('nudge', null, 'AI 正在按你的意思回复')} title="AI 马上按悄悄话再回复客户一条(会交还给 AI)">让 AI 回复</button>
</div>
</div>
)}
</div>
);
}
@ -10061,6 +10097,37 @@ function ChatSettings({ onNotify }) {
);
}
// 手机推送链接打开的"只看这个对话"页面:全屏、不显示菜单和其他功能
function ChatFocus({ sid, k, authed }) {
const [conv, setConv] = useState(null);
const [err, setErr] = useState('');
const [toast, setToast] = useState('');
const verRef = useRef(0);
const kq = k ? `k=${encodeURIComponent(k)}` : '';
const load = async () => { try { const j = await chApi('GET', `/api/chat/admin/conv/${sid}?${kq}`); setConv(j.conv); verRef.current = j.conv.ver || verRef.current; setErr(''); } catch (e) { setErr(e.message); } };
useEffect(() => {
let alive = true;
(async () => {
await load();
while (alive) {
try { const j = await chApi('GET', `/api/chat/admin/conv/${sid}/wait?ver=${verRef.current}&${kq}`); if (!alive) break; if (j.ver !== verRef.current) { verRef.current = j.ver; await load(); } }
catch (e) { await new Promise((r) => setTimeout(r, 4000)); }
}
})();
return () => { alive = false; };
}, [sid]);
const notify = (m) => { setToast(m); setTimeout(() => setToast(''), 2000); };
return (
<div className="crm-root chc-focus-root">
<Styles />
<style>{CHC_CSS}</style>
{conv ? <ChatConversation conv={conv} k={k} authed={authed} focus={{ back: authed ? '/' : '' }} onNotify={notify} onReload={load} />
: <div className="chc-focus-msg">{err ? (err.includes('authenticated') ? '链接已失效,请登录后台查看' : '打开失败:' + err) : '加载中…'}{err && <a href="/">登录后台</a>}</div>}
{toast && <div className="chc-toast">{toast}</div>}
</div>
);
}
const CHC_CSS = `
.chc { display: flex; flex-direction: column; gap: 12px; min-height: calc(100vh - 120px); }
.chc-tabs { display: flex; gap: 6px; flex-wrap: wrap; }
@ -10137,4 +10204,32 @@ const CHC_CSS = `
.chc-split { position: relative; } .chc-lead { position: absolute; inset: 0; width: auto; background: var(--panel); z-index: 2; border: 0; }
.chc-m { max-width: 90%; }
}
/* —— 推送链接打开的全屏对话(手机) —— */
.chc-focus-root { min-height: 100vh; min-height: 100dvh; background: #f7f9fb; }
.chc-pane.focus { position: fixed; inset: 0; border: 0; border-radius: 0; }
.chc-pane.focus .chc-split { min-height: 0; }
.chc-pane.focus .chc-body { padding: 10px; }
.chc-pane.focus .chc-m { max-width: 88%; }
.chc-fhead { display: flex; align-items: center; gap: 6px; padding: 8px 8px 8px 10px; background: var(--ink); color: #fff; padding-top: max(8px, env(safe-area-inset-top)); }
.chc-fback { color: #fff; font-size: 28px; line-height: 1; text-decoration: none; padding: 0 6px 2px 0; }
.chc-fwho { flex: 1; min-width: 0; display: grid; gap: 2px; cursor: pointer; }
.chc-fwho b { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chc-fwho small { font-size: 11.5px; color: #c9d3e0; display: flex; align-items: center; gap: 4px; white-space: nowrap; overflow: hidden; }
.chc-fwho .chc-tier { font-size: 10.5px; padding: 1px 6px; }
.chc-fbtn { flex: none; min-width: 34px; height: 34px; padding: 0 9px; border-radius: 8px; border: 1px solid rgba(255,255,255,.25); background: transparent; color: #fff; font: 600 13px inherit; cursor: pointer; }
.chc-fbtn.main { background: #fff; color: var(--ink); border-color: #fff; }
.chc-fcomp { border-top: 1px solid var(--line); background: #fff; padding: 8px 8px max(8px, env(safe-area-inset-bottom)); display: grid; gap: 6px; }
.chc-fcomp.note { background: #fffbea; }
.chc-frow { display: flex; gap: 6px; align-items: flex-end; }
.chc-frow textarea { flex: 1; resize: none; border: 1px solid var(--line); border-radius: 18px; padding: 9px 14px; font: 15px/1.35 inherit; max-height: 140px; background: #fff; color: var(--ink); }
.chc-fsend { flex: none; height: 40px; padding: 0 14px; border-radius: 20px; border: 0; background: var(--led-green); color: #fff; font: 700 14px inherit; cursor: pointer; }
.chc-fcomp.note .chc-fsend { background: var(--led-amber); }
.chc-fsend:disabled { opacity: .5; }
.chc-fopts { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; font-size: 12.5px; }
.chc-fopts label { display: flex; align-items: center; gap: 4px; color: var(--ink-soft); margin-right: auto; }
.chc-fopts button { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 4px 10px; font: 12.5px inherit; color: var(--ink); cursor: pointer; }
.chc-fopts button.on { background: var(--led-amber-bg); border-color: var(--led-amber); }
.chc-pane.focus .chc-lead { position: absolute; inset: 0; width: auto; background: var(--panel); z-index: 2; border: 0; }
.chc-focus-msg { display: grid; place-items: center; gap: 12px; min-height: 100vh; color: var(--ink-soft); font-size: 14px; text-align: center; padding: 20px; }
.chc-toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: rgba(24,35,58,.92); color: #fff; padding: 8px 14px; border-radius: 8px; font-size: 13px; z-index: 50; }
`;