148 lines
4.0 KiB
JavaScript
148 lines
4.0 KiB
JavaScript
// utils/scriptService.js — 话术拉取、确认弹窗、客服自动回复匹配
|
|
import request from './request.js';
|
|
|
|
const DEFAULT_SCRIPTS = {
|
|
chat_image_confirm: {
|
|
title: '温馨提示',
|
|
content: '平台禁止任何违法违规行为,聊天内容仅限于商家与接单员正常业务对接。严禁赌博、诈骗及其他非法行为,违者后果自负。',
|
|
confirm_text: '我已知晓,继续发送',
|
|
cancel_text: '取消',
|
|
},
|
|
cs_welcome: {
|
|
content: '你好,请问有什么可以帮到您的?',
|
|
},
|
|
merchant_dispatch_confirm: {
|
|
title: '派单确认',
|
|
content: '请确认订单信息无误。派单成功后将扣除相应余额,请确保订单描述合法合规。',
|
|
confirm_text: '确认派单',
|
|
cancel_text: '取消',
|
|
},
|
|
};
|
|
|
|
const textCache = {};
|
|
const TEXT_CACHE_TTL = 5 * 60 * 1000;
|
|
|
|
function mergeScript(sceneKey, remote) {
|
|
const base = DEFAULT_SCRIPTS[sceneKey] || {};
|
|
if (!remote) return { ...base };
|
|
return { ...base, ...remote };
|
|
}
|
|
|
|
export async function fetchScripts(sceneKeys) {
|
|
if (!sceneKeys || !sceneKeys.length) return {};
|
|
|
|
const now = Date.now();
|
|
const cached = {};
|
|
const needFetch = [];
|
|
|
|
sceneKeys.forEach((key) => {
|
|
const entry = textCache[key];
|
|
if (entry && now - entry.time < TEXT_CACHE_TTL) {
|
|
cached[key] = entry.data;
|
|
} else {
|
|
needFetch.push(key);
|
|
}
|
|
});
|
|
|
|
if (!needFetch.length) {
|
|
const result = {};
|
|
sceneKeys.forEach((key) => {
|
|
result[key] = mergeScript(key, cached[key]);
|
|
});
|
|
return result;
|
|
}
|
|
|
|
try {
|
|
const res = await request({
|
|
url: '/peizhi/huashuhq',
|
|
method: 'POST',
|
|
data: { scene_keys: needFetch },
|
|
});
|
|
const payload = (res && res.data && res.data.code === 200) ? res.data.data || {} : {};
|
|
|
|
needFetch.forEach((key) => {
|
|
const raw = payload[key];
|
|
const normalized = raw && raw.item_type === 'auto_reply' ? raw : raw;
|
|
textCache[key] = { time: now, data: normalized };
|
|
cached[key] = normalized;
|
|
});
|
|
} catch (e) {
|
|
console.warn('fetchScripts failed', e);
|
|
}
|
|
|
|
const result = {};
|
|
sceneKeys.forEach((key) => {
|
|
result[key] = mergeScript(key, cached[key]);
|
|
});
|
|
return result;
|
|
}
|
|
|
|
export function showConfirmByScene(sceneKey, onConfirm, onCancel) {
|
|
fetchScripts([sceneKey]).then((data) => {
|
|
const script = data[sceneKey] || DEFAULT_SCRIPTS[sceneKey] || {};
|
|
wx.showModal({
|
|
title: script.title || '提示',
|
|
content: script.content || '',
|
|
confirmText: script.confirm_text || '确定',
|
|
cancelText: script.cancel_text || '取消',
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
onConfirm && onConfirm();
|
|
} else {
|
|
onCancel && onCancel();
|
|
}
|
|
},
|
|
fail: () => {
|
|
onCancel && onCancel();
|
|
},
|
|
});
|
|
}).catch(() => {
|
|
const script = DEFAULT_SCRIPTS[sceneKey] || {};
|
|
wx.showModal({
|
|
title: script.title || '提示',
|
|
content: script.content || '',
|
|
confirmText: script.confirm_text || '确定',
|
|
cancelText: script.cancel_text || '取消',
|
|
success: (res) => {
|
|
if (res.confirm) onConfirm && onConfirm();
|
|
else onCancel && onCancel();
|
|
},
|
|
});
|
|
});
|
|
}
|
|
|
|
export async function matchAutoReply(userText) {
|
|
if (!userText || !userText.trim()) {
|
|
return { matched: false, content: '' };
|
|
}
|
|
try {
|
|
const res = await request({
|
|
url: '/peizhi/huashu_match',
|
|
method: 'POST',
|
|
data: {
|
|
scene_key: 'cs_auto_reply',
|
|
user_text: userText.trim(),
|
|
},
|
|
});
|
|
if (res && res.data && res.data.code === 200 && res.data.data) {
|
|
return res.data.data;
|
|
}
|
|
} catch (e) {
|
|
console.warn('matchAutoReply failed', e);
|
|
}
|
|
return { matched: false, content: '' };
|
|
}
|
|
|
|
export async function getWelcomeText() {
|
|
const data = await fetchScripts(['cs_welcome']);
|
|
const script = data.cs_welcome || DEFAULT_SCRIPTS.cs_welcome;
|
|
return script.content || DEFAULT_SCRIPTS.cs_welcome.content;
|
|
}
|
|
|
|
export default {
|
|
fetchScripts,
|
|
showConfirmByScene,
|
|
matchAutoReply,
|
|
getWelcomeText,
|
|
};
|