111 lines
2.5 KiB
Markdown
111 lines
2.5 KiB
Markdown
|
|
# 快速验证步骤
|
|||
|
|
|
|||
|
|
## 1. 测试 Cookie 读取
|
|||
|
|
|
|||
|
|
在您的商城网站(yehwang 域名下的任何页面)打开浏览器控制台(F12),运行:
|
|||
|
|
|
|||
|
|
```javascript
|
|||
|
|
function getCookie(name) {
|
|||
|
|
const value = `; ${document.cookie}`;
|
|||
|
|
const parts = value.split(`; ${name}=`);
|
|||
|
|
if (parts.length === 2) return parts.pop().split(";").shift();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log("Token:", getCookie("token"));
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**预期结果**:应该能看到您的 JWT Token。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 2. 配置 Chatwoot Widget
|
|||
|
|
|
|||
|
|
在 `chatwoot-widget-integration.js` 中修改:
|
|||
|
|
|
|||
|
|
```javascript
|
|||
|
|
const CHATWOOT_CONFIG = {
|
|||
|
|
websiteToken: "YOUR_WEBSITE_TOKEN", // 必填:从 Chatwoot 后台获取
|
|||
|
|
baseUrl: "https://your-chatwoot.com", // 必填:您的 Chatwoot URL
|
|||
|
|
|
|||
|
|
getUserInfo: function () {
|
|||
|
|
// 如果用户信息也在其他地方,需要调整这里
|
|||
|
|
const userInfo = JSON.parse(localStorage.getItem("userInfo") || "{}");
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
email: userInfo.email || "user@example.com", // 用户邮箱
|
|||
|
|
name: userInfo.name || "User", // 用户姓名
|
|||
|
|
jwt_token: getCookie("token"), // 从 Cookie 读取
|
|||
|
|
};
|
|||
|
|
},
|
|||
|
|
};
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 3. 引入脚本
|
|||
|
|
|
|||
|
|
在商城页面的 `</body>` 之前添加:
|
|||
|
|
|
|||
|
|
```html
|
|||
|
|
<script src="/js/chatwoot-widget-integration.js"></script>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 4. 验证 Token 是否同步
|
|||
|
|
|
|||
|
|
1. 打开商城页面(已登录状态)
|
|||
|
|
2. 打开浏览器控制台
|
|||
|
|
3. 等待 2 秒后,应该看到:
|
|||
|
|
```
|
|||
|
|
检测到 yehwang 域名,检查 Cookie...
|
|||
|
|
=== 所有可访问的 Cookie ===
|
|||
|
|
token: eyJ0eXAiOiJqd3QifQ.eyJzdWIi...
|
|||
|
|
=== Token 状态 ===
|
|||
|
|
Token 存在: true
|
|||
|
|
Token 长度: xxx
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
4. 打开 Chatwoot 聊天窗口
|
|||
|
|
5. 在 Chatwoot 后台查看该 Contact 的自定义属性
|
|||
|
|
6. 应该能看到 `jwt_token` 字段
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 5. 测试订单查询
|
|||
|
|
|
|||
|
|
在 Chatwoot 聊天中输入:
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
我的订单 202071324 怎么样了?
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**预期结果**:AI 返回订单详情。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 常见问题
|
|||
|
|
|
|||
|
|
### Q: Cookie 读取为空?
|
|||
|
|
|
|||
|
|
A: 检查 Cookie 设置:
|
|||
|
|
- Domain: `.yehwang`
|
|||
|
|
- Path: `/`
|
|||
|
|
- SameSite: `Lax` 或 `None`
|
|||
|
|
- **不要**设置 `HttpOnly`(否则 JavaScript 无法读取)
|
|||
|
|
|
|||
|
|
### Q: 获取到 Token 但 Chatwoot 没有同步?
|
|||
|
|
|
|||
|
|
A: 检查:
|
|||
|
|
1. `getUserInfo()` 是否返回了 `email`(必需)
|
|||
|
|
2. Chatwoot 控制台是否有错误
|
|||
|
|
3. 刷新页面重新加载 Widget
|
|||
|
|
|
|||
|
|
### Q: 用户邮箱在哪里获取?
|
|||
|
|
|
|||
|
|
A: 如果邮箱不在 localStorage:
|
|||
|
|
- 方案 1: 从另一个 Cookie 读取
|
|||
|
|
- 方案 2: 在登录时写入 localStorage
|
|||
|
|
- 方案 3: 通过 API 获取
|
|||
|
|
- 方案 4: 使用用户 ID 代替(修改后端支持)
|