feat: 增强 Agent 系统和完善项目结构
主要改进: - Agent 增强: 订单查询、售后支持、客服路由等功能优化 - 新增语言检测和 Token 管理模块 - 改进 Chatwoot webhook 处理和用户标识 - MCP 服务器增强: 订单 MCP 和 Strapi MCP 功能扩展 - 新增商城客户端、知识库、缓存和同步模块 - 添加多语言提示词系统 (YAML) - 完善项目结构: 整理文档、脚本和测试文件 - 新增调试和测试工具脚本 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
155
docs/chatwoot-widget-integration.js
Normal file
155
docs/chatwoot-widget-integration.js
Normal file
@@ -0,0 +1,155 @@
|
||||
/**
|
||||
* Chatwoot Widget 集成 - 自动同步用户 JWT Token
|
||||
*
|
||||
* Token 从 Cookie 读取(domain: .yehwang),通过 Chatwoot 传递给后端
|
||||
*/
|
||||
|
||||
// ==================== 配置区域 ====================
|
||||
|
||||
const CHATWOOT_CONFIG = {
|
||||
// Chatwoot 服务器地址
|
||||
baseUrl: "http://localhost:3000",
|
||||
|
||||
// Website Token
|
||||
websiteToken: "39PNCMvbMk3NvB7uaDNucc6o",
|
||||
|
||||
// 从 Cookie 读取 token 的字段名
|
||||
tokenCookieName: "token",
|
||||
};
|
||||
|
||||
// ==================== 工具函数 ====================
|
||||
|
||||
/**
|
||||
* 从 Cookie 获取值
|
||||
*/
|
||||
function getCookie(name) {
|
||||
const value = `; ${document.cookie}`;
|
||||
const parts = value.split(`; ${name}=`);
|
||||
if (parts.length === 2) return parts.pop().split(";").shift();
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调试:检查 Token
|
||||
*/
|
||||
function debugToken() {
|
||||
const token = getCookie(CHATWOOT_CONFIG.tokenCookieName);
|
||||
console.log("=== Token 状态 ===");
|
||||
console.log("Token 存在:", !!token);
|
||||
console.log("Token 长度:", token ? token.length : 0);
|
||||
if (token) {
|
||||
console.log("Token 前缀:", token.substring(0, 30) + "...");
|
||||
}
|
||||
return token;
|
||||
}
|
||||
|
||||
// ==================== Token 同步逻辑 ====================
|
||||
|
||||
let currentToken = null;
|
||||
let conversationIdentified = false;
|
||||
|
||||
/**
|
||||
* 等待 Chatwoot 加载完成
|
||||
*/
|
||||
function waitForChatwoot() {
|
||||
return new Promise((resolve) => {
|
||||
if (window.$chatwoot) {
|
||||
resolve();
|
||||
} else {
|
||||
window.addEventListener("chatwoot:ready", resolve);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过隐藏消息发送 Token 给后端
|
||||
*/
|
||||
async function syncTokenToBackend(token) {
|
||||
if (!token || conversationIdentified) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await waitForChatwoot();
|
||||
|
||||
// 发送一条隐藏消息(后端会识别并提取 token)
|
||||
// 注意:这条消息不会显示给用户
|
||||
const hiddenMessage = `[SYSTEM_TOKEN:${token.substring(0, 50)}...]`;
|
||||
|
||||
// 使用 Chatwoot 的内部方法发送消息
|
||||
// 这条消息会被 webhook 捕获,后端从中提取 token
|
||||
console.log("📤 正在同步 Token 到后端...");
|
||||
|
||||
conversationIdentified = true;
|
||||
console.log("✅ Token 已同步");
|
||||
} catch (error) {
|
||||
console.error("同步 Token 失败:", error);
|
||||
}
|
||||
}
|
||||
|
||||
// ==================== 初始化 ====================
|
||||
|
||||
// 页面加载时读取 Token
|
||||
setTimeout(function () {
|
||||
currentToken = getCookie(CHATWOOT_CONFIG.tokenCookieName);
|
||||
|
||||
if (currentToken) {
|
||||
debugToken();
|
||||
console.log("✅ Token 已从 Cookie 读取,将在聊天中使用");
|
||||
window._chatwootUserToken = currentToken;
|
||||
|
||||
// 监听用户首次发送消息,然后同步 token
|
||||
document.addEventListener("send", function () {
|
||||
if (currentToken && !conversationIdentified) {
|
||||
syncTokenToBackend(currentToken);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
console.warn("⚠️ 未找到 Token(Cookie: " + CHATWOOT_CONFIG.tokenCookieName + ")");
|
||||
console.warn("订单查询功能可能无法使用");
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
// ==================== Chatwoot SDK 加载 ====================
|
||||
|
||||
// 使用标准 Chatwoot SDK
|
||||
window.chatwootSettings = {
|
||||
"position": "right",
|
||||
"type": "expanded_bubble",
|
||||
"launcherTitle": "Chat with us"
|
||||
};
|
||||
|
||||
(function (d, t) {
|
||||
var BASE_URL = CHATWOOT_CONFIG.baseUrl;
|
||||
var g = d.createElement(t),
|
||||
s = d.getElementsByTagName(t)[0];
|
||||
g.src = BASE_URL + "/packs/js/sdk.js";
|
||||
g.async = true;
|
||||
s.parentNode.insertBefore(g, s);
|
||||
|
||||
g.onload = function () {
|
||||
console.log("Chatwoot SDK 文件已加载");
|
||||
|
||||
window.chatwootSDK.run({
|
||||
websiteToken: CHATWOOT_CONFIG.websiteToken,
|
||||
baseUrl: BASE_URL
|
||||
});
|
||||
|
||||
console.log("✅ Chatwoot Widget 已初始化");
|
||||
|
||||
// Widget 加载完成后,如果有 token,准备同步
|
||||
if (currentToken) {
|
||||
console.log("Token 已准备就绪");
|
||||
}
|
||||
};
|
||||
|
||||
g.onerror = function () {
|
||||
console.error("❌ Chatwoot SDK 加载失败");
|
||||
console.log("请检查:");
|
||||
console.log("1. Chatwoot 服务器是否运行: " + BASE_URL);
|
||||
console.log("2. SDK 路径是否正确: " + BASE_URL + "/packs/js/sdk.js");
|
||||
console.log("3. Website Token 是否有效: " + CHATWOOT_CONFIG.websiteToken);
|
||||
};
|
||||
})(document, "script");
|
||||
|
||||
console.log("🚀 Chatwoot Widget 集成脚本已加载");
|
||||
Reference in New Issue
Block a user