fix: bug
This commit is contained in:
@@ -123,6 +123,7 @@
|
||||
<div class="small-label" style="margin-top:6px">从联系人选择(多选可批量填入)</div>
|
||||
<div style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;margin-top:4px">
|
||||
<button type="button" class="secondary" id="btn-load-contact-list" style="padding:4px 10px;font-size:12px">加载联系人</button>
|
||||
<button type="button" class="secondary" id="btn-batch-import-remark" style="padding:4px 10px;font-size:12px" title="将当前已加载的联系人全部导入为客户,备注名使用 wxid">批量导入自动备注</button>
|
||||
<select id="c-wxid-select" multiple style="min-width:200px;min-height:80px;max-height:120px"></select>
|
||||
</div>
|
||||
</div>
|
||||
@@ -174,7 +175,14 @@
|
||||
<div class="small-label" style="margin-top:16px;margin-bottom:8px">发送图片消息(快捷方式)</div>
|
||||
<div class="mgmt-form-grid">
|
||||
<div class="field"><label>接收人 wxid</label><input id="img-to-user" placeholder="zhang499142409" /></div>
|
||||
<div class="field full"><label>图片内容(base64 或 URL)</label><input id="img-content" placeholder="图片 base64 或图片 URL" /></div>
|
||||
<div class="field full">
|
||||
<label>图片内容(base64 / URL / 本地图片)</label>
|
||||
<input id="img-content" placeholder="粘贴 base64、图片 URL,或从下方选择本地图片" />
|
||||
<div style="margin-top:6px;display:flex;align-items:center;gap:8px;flex-wrap:wrap">
|
||||
<input type="file" id="img-file" accept="image/*" style="font-size:12px" />
|
||||
<span id="img-file-name" class="small-label" style="color:var(--muted)"></span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field full"><label>附带文字(可选)</label><input id="img-text" placeholder="可选" /></div>
|
||||
<button type="button" class="primary" id="btn-send-image">发送图片</button>
|
||||
</div>
|
||||
@@ -198,6 +206,16 @@
|
||||
</div>
|
||||
<div id="panel-ai-reply" class="mgmt-panel">
|
||||
<p class="small-label" style="margin-bottom:12px">分级处理:仅<strong>超级管理员</strong>与<strong>白名单</strong>中的联系人会收到 AI 自动回复,其他消息一律不回复。</p>
|
||||
<div class="field full" style="margin-bottom:12px">
|
||||
<span class="small-label">AI 接管状态:</span>
|
||||
<span id="ai-reply-status-text">—</span>
|
||||
<button type="button" class="secondary" id="btn-ai-reply-status" style="margin-left:8px;padding:2px 8px;font-size:12px">检查状态</button>
|
||||
</div>
|
||||
<div class="field full" style="margin-bottom:12px">
|
||||
<span class="small-label">消息回调(7006 → 本服务):</span>
|
||||
<span id="callback-status-text">—</span>
|
||||
<button type="button" class="secondary" id="btn-callback-status" style="margin-left:8px;padding:2px 8px;font-size:12px">检查回调</button>
|
||||
</div>
|
||||
<div class="mgmt-form-grid">
|
||||
<div class="field full">
|
||||
<label>超级管理员 wxid(每行一个或逗号分隔)</label>
|
||||
@@ -256,7 +274,7 @@
|
||||
const tab = document.querySelector('.mgmt-tab[data-panel="' + panelId + '"]');
|
||||
if (tab) tab.classList.add('active');
|
||||
if (panelId === 'panel-greeting') loadCustomerTagsForGreeting();
|
||||
if (panelId === 'panel-ai-reply') loadAiReplyConfig();
|
||||
if (panelId === 'panel-ai-reply') { loadAiReplyConfig(); loadAiReplyStatus(); loadCallbackStatus(); }
|
||||
}
|
||||
|
||||
function _parseWxidLines(ta) {
|
||||
@@ -283,6 +301,52 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function loadAiReplyStatus() {
|
||||
const key = $('key').value.trim();
|
||||
const el = $('ai-reply-status-text');
|
||||
if (!el) return;
|
||||
if (!key) { el.textContent = '请先登录'; return; }
|
||||
el.textContent = '检测中…';
|
||||
try {
|
||||
const data = await callApi('/api/ai-reply-status?key=' + encodeURIComponent(key));
|
||||
el.textContent = data.ok ? '正常(WS 已连接,已配置白名单/超级管理员,已选模型)' : (data.message || '异常');
|
||||
el.style.color = data.ok ? 'var(--success, #22c55e)' : 'var(--muted, #94a3b8)';
|
||||
} catch (e) {
|
||||
el.textContent = '检查失败: ' + (e.message || e);
|
||||
el.style.color = 'var(--danger, #ef4444)';
|
||||
}
|
||||
}
|
||||
|
||||
async function loadCallbackStatus() {
|
||||
const key = getKey();
|
||||
const el = $('callback-status-text');
|
||||
if (!el) return;
|
||||
if (!key) { el.textContent = '请先登录'; el.style.color = 'var(--muted)'; return; }
|
||||
el.textContent = '检测中…';
|
||||
el.style.color = 'var(--muted)';
|
||||
try {
|
||||
const data = await callApi('/api/callback-status?key=' + encodeURIComponent(key));
|
||||
if (!data.configured) {
|
||||
el.textContent = '未配置(未设置 CALLBACK_BASE_URL,使用 WS 拉取消息)';
|
||||
el.style.color = 'var(--muted, #94a3b8)';
|
||||
return;
|
||||
}
|
||||
if (data.registered === true) {
|
||||
el.textContent = '已配置且已向 7006 注册成功,新消息将推送到: ' + (data.callback_url || '');
|
||||
el.style.color = 'var(--success, #22c55e)';
|
||||
} else if (data.registered === false) {
|
||||
el.textContent = '已配置但向 7006 注册失败,请检查网络或 7006 服务。回调地址: ' + (data.callback_url || '');
|
||||
el.style.color = 'var(--danger, #ef4444)';
|
||||
} else {
|
||||
el.textContent = '已配置,回调地址: ' + (data.callback_url || '');
|
||||
el.style.color = 'var(--muted, #94a3b8)';
|
||||
}
|
||||
} catch (e) {
|
||||
el.textContent = '检查失败: ' + (e.message || e);
|
||||
el.style.color = 'var(--danger, #ef4444)';
|
||||
}
|
||||
}
|
||||
|
||||
async function saveAiReplyConfig() {
|
||||
const key = getKey();
|
||||
if (!key) return;
|
||||
@@ -331,6 +395,7 @@
|
||||
}
|
||||
}
|
||||
|
||||
let lastLoadedContactList = [];
|
||||
async function loadContactListForWxidSelect() {
|
||||
const key = $('key').value.trim();
|
||||
const sel = $('c-wxid-select');
|
||||
@@ -338,8 +403,9 @@
|
||||
if (!key) { alert('请先登录'); return; }
|
||||
sel.innerHTML = '<option value="">加载中…</option>';
|
||||
try {
|
||||
const data = await callApi('/api/contact-list?key=' + encodeURIComponent(key));
|
||||
const data = await callApi('/api/contact-list?key=' + encodeURIComponent(key), { cache: 'no-store' });
|
||||
const list = data.items || [];
|
||||
lastLoadedContactList = list;
|
||||
if (data.error) {
|
||||
sel.innerHTML = '<option value="">获取失败</option>';
|
||||
alert('获取联系人失败:' + (data.error || '请检查网络或 key'));
|
||||
@@ -347,8 +413,9 @@
|
||||
}
|
||||
sel.innerHTML = list.length
|
||||
? list.map(c => {
|
||||
const w = (c.wxid || c.Wxid || '').toString();
|
||||
const r = (c.remark_name || c.RemarkName || c.NickName || w).toString();
|
||||
const w = (c.wxid || '').toString();
|
||||
const n = (c.nick_name || '').toString();
|
||||
const r = (n || c.remark_name || w).toString(); // 优先显示昵称
|
||||
return '<option value="' + escapeHtml(w) + '">' + escapeHtml(r) + ' (' + escapeHtml(w.slice(0, 20)) + (w.length > 20 ? '…' : '') + ')</option>';
|
||||
}).join('')
|
||||
: '<option value="">无联系人数据</option>';
|
||||
@@ -359,6 +426,22 @@
|
||||
}
|
||||
}
|
||||
|
||||
async function batchImportContactsAsCustomers() {
|
||||
const key = getKey();
|
||||
if (!key) { alert('请先登录'); return; }
|
||||
if (!lastLoadedContactList.length) { alert('请先点击「加载联系人」'); return; }
|
||||
if (!confirm('将 ' + lastLoadedContactList.length + ' 个联系人导入为客户(备注名使用 wxid),是否继续?')) return;
|
||||
try {
|
||||
for (const c of lastLoadedContactList) {
|
||||
const wxid = (c.wxid || '').trim();
|
||||
if (!wxid) continue;
|
||||
await callApi('/api/customers', { method: 'POST', body: JSON.stringify({ key, wxid, remark_name: (c.remark_name || wxid).trim() }) });
|
||||
}
|
||||
loadCustomers();
|
||||
alert('已导入 ' + lastLoadedContactList.length + ' 个客户');
|
||||
} catch (e) { alert('批量导入失败: ' + (e.message || e)); }
|
||||
}
|
||||
|
||||
async function loadCustomers() {
|
||||
const key = $('key').value.trim();
|
||||
if (!key) { $('customer-list').innerHTML = '<p class="small-label">请先登录。</p>'; return; }
|
||||
@@ -558,7 +641,7 @@
|
||||
const el = $('mass-friend-list');
|
||||
el.innerHTML = '<span class="small-label">加载中…</span>';
|
||||
try {
|
||||
const data = await callApi('/api/friends?key=' + encodeURIComponent(key));
|
||||
const data = await callApi('/api/friends?key=' + encodeURIComponent(key), { cache: 'no-store' });
|
||||
const list = data.items || [];
|
||||
if (!list.length) {
|
||||
el.innerHTML = '<span class="small-label">暂无联系人,请先在「客户档案」添加客户。</span>';
|
||||
@@ -602,12 +685,56 @@
|
||||
} catch (e) { alert('群发失败: ' + e.message); }
|
||||
}
|
||||
|
||||
function isImageUrl(s) {
|
||||
const t = (s || '').trim();
|
||||
return t.startsWith('http://') || t.startsWith('https://');
|
||||
}
|
||||
function stripDataUrlPrefix(s) {
|
||||
const m = (s || '').match(/^data:image\/[^;]+;base64,(.+)$/);
|
||||
return m ? m[1] : s;
|
||||
}
|
||||
function readFileAsBase64(file) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const r = new FileReader();
|
||||
r.onload = () => { resolve(stripDataUrlPrefix(r.result)); };
|
||||
r.onerror = () => reject(new Error('读取文件失败'));
|
||||
r.readAsDataURL(file);
|
||||
});
|
||||
}
|
||||
async function urlToBase64(url) {
|
||||
const res = await fetch(url, { mode: 'cors' });
|
||||
if (!res.ok) throw new Error('获取图片失败: ' + res.status);
|
||||
const blob = await res.blob();
|
||||
return new Promise((resolve, reject) => {
|
||||
const r = new FileReader();
|
||||
r.onload = () => resolve(stripDataUrlPrefix(r.result));
|
||||
r.onerror = () => reject(new Error('URL 转 base64 失败'));
|
||||
r.readAsDataURL(blob);
|
||||
});
|
||||
}
|
||||
async function resolveImageContentToBase64() {
|
||||
const fileInput = $('img-file');
|
||||
const textInput = $('img-content').value.trim();
|
||||
if (fileInput && fileInput.files && fileInput.files[0]) {
|
||||
return await readFileAsBase64(fileInput.files[0]);
|
||||
}
|
||||
if (!textInput) return null;
|
||||
if (isImageUrl(textInput)) return await urlToBase64(textInput);
|
||||
return stripDataUrlPrefix(textInput);
|
||||
}
|
||||
async function doSendImage() {
|
||||
const key = getKey();
|
||||
if (!key) return;
|
||||
const toUser = $('img-to-user').value.trim();
|
||||
const imageContent = $('img-content').value.trim();
|
||||
if (!toUser || !imageContent) { alert('请填写接收人 wxid 和图片内容'); return; }
|
||||
if (!toUser) { alert('请填写接收人 wxid'); return; }
|
||||
let imageContent;
|
||||
try {
|
||||
imageContent = await resolveImageContentToBase64();
|
||||
} catch (e) {
|
||||
alert('解析图片失败: ' + (e.message || e));
|
||||
return;
|
||||
}
|
||||
if (!imageContent) { alert('请填写或选择图片内容(base64、URL 或本地图片)'); return; }
|
||||
try {
|
||||
await callApi('/api/send-image', {
|
||||
method: 'POST',
|
||||
@@ -620,8 +747,10 @@
|
||||
});
|
||||
alert('图片已发送');
|
||||
$('img-content').value = '';
|
||||
if ($('img-file')) $('img-file').value = '';
|
||||
if ($('img-file-name')) $('img-file-name').textContent = '';
|
||||
$('img-text').value = '';
|
||||
} catch (e) { alert('发送图片失败: ' + e.message); }
|
||||
} catch (e) { alert('发送图片失败: ' + (e.message || e)); }
|
||||
}
|
||||
|
||||
async function doPushSend() {
|
||||
@@ -663,6 +792,7 @@
|
||||
document.querySelectorAll('.mgmt-tab').forEach(tab => tab.addEventListener('click', () => switchPanel(tab.dataset.panel)));
|
||||
$('btn-customer-save').addEventListener('click', saveCustomer);
|
||||
$('btn-load-contact-list').addEventListener('click', loadContactListForWxidSelect);
|
||||
$('btn-batch-import-remark').addEventListener('click', batchImportContactsAsCustomers);
|
||||
$('c-wxid-select').addEventListener('change', function() {
|
||||
const sel = $('c-wxid-select');
|
||||
const opts = sel ? sel.querySelectorAll('option:checked') : [];
|
||||
@@ -708,7 +838,14 @@
|
||||
$('btn-load-friends').addEventListener('click', loadFriendsForMass);
|
||||
$('btn-mass-send').addEventListener('click', doMassSend);
|
||||
$('btn-send-image').addEventListener('click', doSendImage);
|
||||
$('btn-ai-reply-save').addEventListener('click', saveAiReplyConfig);
|
||||
if ($('img-file') && $('img-file-name')) {
|
||||
$('img-file').addEventListener('change', function() {
|
||||
$('img-file-name').textContent = this.files && this.files[0] ? '已选: ' + this.files[0].name : '';
|
||||
});
|
||||
}
|
||||
$('btn-ai-reply-save').addEventListener('click', async () => { await saveAiReplyConfig(); loadAiReplyStatus(); });
|
||||
$('btn-ai-reply-status') && $('btn-ai-reply-status').addEventListener('click', loadAiReplyStatus);
|
||||
$('btn-callback-status') && $('btn-callback-status').addEventListener('click', loadCallbackStatus);
|
||||
$('btn-pt-add').addEventListener('click', addProductTag);
|
||||
$('btn-push-group-add').addEventListener('click', createPushGroup);
|
||||
$('btn-push-send').addEventListener('click', doPushSend);
|
||||
|
||||
Reference in New Issue
Block a user