商户只需在小程序页面中添加:
<web-view src="https://您的域名.com/chat?mch_id=商户ID&user_id=用户ID"></web-view>
// server.js
const express = require('express');
const app = express();
// 模拟商户数据
const merchants = {
'mch_001': { name: "商户A", cs_list: ["客服1", "客服2"] },
'mch_002': { name: "商户B", cs_list: ["客服3", "客服4"] }
};
// WebView入口
app.get('/chat', (req, res) => {
const { mch_id, user_id } = req.query;
const merchant = merchants[mch_id];
if (!merchant) {
return res.status(403).send('商户不存在');
}
// 返回带商户标识的HTML页面
res.send(`
<!DOCTYPE html>
<html>
<head>
<title>${merchant.name}客服</title>
</head>
<body>
<h2>${merchant.name}客服系统</h2>
<div id="chat-box"></div>
<script>
// 在这里初始化客服聊天界面
const merchantId = "${mch_id}";
const userId = "${user_id}";
console.log("当前商户:", merchantId);
</script>
</body>
</html>
`);
});
app.listen(3000, () => console.log('Server running on port 3000'));
// 所有数据库操作必须带上mch_id
// 例如获取聊天记录
app.get('/api/messages', (req, res) => {
const { mch_id } = req.query;
// 伪代码示例
db.query(
'SELECT * FROM messages WHERE merchant_id = ?',
[mch_id],
(err, results) => {
res.json(results);
}
);
});
您为商户生成唯一ID(如mch_001
)
商户在小程序任意页面插入:
<web-view src="https://您的域名.com/chat?mch_id=mch_001&user_id={{用户ID}}"></web-view>
客服对话自动归属到对应商户
URL签名:防止参数篡改
<web-view src="https://域名.com/chat?mch_id=xxx&user_id=yyy&sign=MD5加密签名"></web-view>
自定义样式:通过URL参数传递主题色
<web-view src="https://域名.com/chat?mch_id=xxx&theme=FF0000"></web-view>
这种方案完全满足您"WebView嵌入"的核心需求,且能在2小时内上线基础版。如需更多功能(如客服分配策略、消息推送),可在现有基础上逐步扩展。