From 21564ebf4d82cd5347a828c02531f810a34bf840 Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Mon, 10 Nov 2025 17:26:42 +0800 Subject: [PATCH] =?UTF-8?q?feat(bytedesk):=20=E9=9B=86=E6=88=90=20Bytedesk?= =?UTF-8?q?=20=E5=AE=A2=E6=9C=8D=E7=B3=BB=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 Bytedesk 在线客服功能,支持实时对话: 组件: - BytedeskWidget: 客服浮窗组件(右下角) - 配置文件: bytedesk.config.js 统一管理配置 - 环境变量示例: .env.bytedesk.example 集成方式: - GlobalComponents 引入 BytedeskWidget - public/index.html 加载 bytedesk-web.js 脚本 - 支持环境变量配置(ORG、SID、API_URL) 配置说明详见 src/bytedesk-integration/.env.bytedesk.example 🔧 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- public/index.html | 84 +++++++ .../.env.bytedesk.example | 156 ++++++++++++ src/bytedesk-integration/App.jsx.example | 237 ++++++++++++++++++ .../components/BytedeskWidget.jsx | 177 +++++++++++++ .../config/bytedesk.config.js | 130 ++++++++++ src/components/GlobalComponents.js | 17 ++ 6 files changed, 801 insertions(+) create mode 100644 src/bytedesk-integration/.env.bytedesk.example create mode 100644 src/bytedesk-integration/App.jsx.example create mode 100644 src/bytedesk-integration/components/BytedeskWidget.jsx create mode 100644 src/bytedesk-integration/config/bytedesk.config.js diff --git a/public/index.html b/public/index.html index 03db34db..edf8898a 100755 --- a/public/index.html +++ b/public/index.html @@ -84,6 +84,90 @@ // name: 'YOU CAN DEFINE USER NAME HERE', }, } + + // 根据路径控制Dify机器人显示(只在首页/和home页/home显示) + function controlDifyChatbot() { + const path = window.location.pathname; + const chatbotButton = document.getElementById('dify-chatbot-bubble-button'); + const chatbotWindow = document.getElementById('dify-chatbot-bubble-window'); + + // 只在首页(/)和home页(/home)显示Dify机器人 + // const shouldShowDify = (path === '/' || path === '/home'); + // 完全不显示Dify机器人(只使用Bytedesk客服) + const shouldShowDify = false + + if (chatbotButton) { + chatbotButton.style.display = shouldShowDify ? 'block' : 'none'; + // 同时设置visibility确保完全隐藏 + chatbotButton.style.visibility = shouldShowDify ? 'visible' : 'hidden'; + } + + if (chatbotWindow) { + chatbotWindow.style.display = shouldShowDify ? '' : 'none'; + } + + console.log('[Dify] Path:', path, 'Should show:', shouldShowDify, 'Button found:', !!chatbotButton); + } + + // 轮询检查Dify按钮(因为Dify脚本加载是异步的) + let difyCheckCount = 0; + const difyCheckInterval = setInterval(function() { + const button = document.getElementById('dify-chatbot-bubble-button'); + if (button || difyCheckCount > 50) { // 最多检查5秒 + if (button) { + console.log('[Dify] Button found, applying control'); + controlDifyChatbot(); + } + clearInterval(difyCheckInterval); + } + difyCheckCount++; + }, 100); + + // 页面加载时执行 + window.addEventListener('load', function() { + setTimeout(controlDifyChatbot, 1000); + }); + + // 监听路由变化(React Router使用pushState) + window.addEventListener('popstate', controlDifyChatbot); + + // 监听pushState和replaceState(捕获React Router导航) + const originalPushState = history.pushState; + const originalReplaceState = history.replaceState; + + history.pushState = function() { + originalPushState.apply(history, arguments); + setTimeout(controlDifyChatbot, 50); + }; + + history.replaceState = function() { + originalReplaceState.apply(history, arguments); + setTimeout(controlDifyChatbot, 50); + }; + + // 使用MutationObserver监听DOM变化(捕获Dify按钮插入) + const observer = new MutationObserver(function(mutations) { + for (const mutation of mutations) { + if (mutation.addedNodes.length > 0) { + for (const node of mutation.addedNodes) { + if (node.id && (node.id.includes('dify') || node.id.includes('chatbot'))) { + console.log('[Dify] Detected chatbot element insertion:', node.id); + setTimeout(controlDifyChatbot, 100); + break; + } + } + } + } + }); + + // 观察body的变化 + window.addEventListener('DOMContentLoaded', function() { + observer.observe(document.body, { + childList: true, + subtree: true, + attributes: false + }); + });