UNPKG

digitalsee-chatbox-sdk

Version:

A flexible chatbox SDK for AI conversations with support for text, file, and voice input

337 lines (302 loc) 12.7 kB
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>ChatBox SDK - 直接引入示例</title> <style> body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; margin: 0; padding: 20px; background: #f5f5f5; } .container { max-width: 1200px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); } .demo-section { margin: 20px 0; padding: 20px; border: 1px solid #e0e0e0; border-radius: 8px; } button { background: #1890ff; color: white; border: none; padding: 10px 20px; border-radius: 6px; cursor: pointer; margin: 5px; font-size: 14px; } button:hover { background: #40a9ff; } button:disabled { background: #d9d9d9; cursor: not-allowed; } #embedded-container { width: 100%; height: 400px; border: 1px solid #d9d9d9; border-radius: 6px; margin-top: 10px; } .code-block { background: #f6f8fa; padding: 15px; border-radius: 6px; border-left: 3px solid #1890ff; margin: 10px 0; font-family: 'Courier New', monospace; overflow-x: auto; } h1 { color: #262626; } h2 { color: #1890ff; margin-top: 30px; } .note { background: #fff2e8; border: 1px solid #ffbb96; padding: 12px; border-radius: 6px; margin: 10px 0; } </style> </head> <body> <div class="container"> <h1>🚀 DigitalSee ChatBox SDK - 直接引入示例</h1> <div class="note"> <strong>注意:</strong> 直接引入模式需要先加载React、ReactDOM和其他依赖库。 </div> <h2>📦 方式一:通过CDN引入(推荐)</h2> <div class="code-block"> &lt;!-- 1. 引入依赖库 --&gt; &lt;script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"&gt;&lt;/script&gt; &lt;script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"&gt;&lt;/script&gt; &lt;script src="https://unpkg.com/antd@5/dist/antd.min.js"&gt;&lt;/script&gt; &lt;script src="https://unpkg.com/lucide-react@latest/dist/umd/lucide-react.js"&gt;&lt;/script&gt; &lt;!-- 2. 引入SDK --&gt; &lt;script src="https://unpkg.com/digitalsee-chatbox-sdk@latest/dist/index.umd.js"&gt;&lt;/script&gt; </div> <h2>💻 方式二:本地文件引入</h2> <div class="code-block"> &lt;!-- 下载文件到本地后引入 --&gt; &lt;script src="./libs/react.production.min.js"&gt;&lt;/script&gt; &lt;script src="./libs/react-dom.production.min.js"&gt;&lt;/script&gt; &lt;script src="./libs/antd.min.js"&gt;&lt;/script&gt; &lt;script src="./libs/lucide-react.js"&gt;&lt;/script&gt; &lt;script src="./libs/digitalsee-chatbox-sdk.umd.js"&gt;&lt;/script&gt; </div> <h2>🎯 使用示例</h2> <div class="demo-section"> <h3>悬浮小部件</h3> <p>点击按钮创建悬浮聊天小部件:</p> <button onclick="createFloatingWidget()">创建悬浮小部件</button> <button onclick="destroyFloatingWidget()" id="destroyBtn" disabled>销毁小部件</button> </div> <div class="demo-section"> <h3>嵌入式聊天</h3> <p>将聊天界面嵌入到指定容器中:</p> <button onclick="createEmbeddedChat()">创建嵌入式聊天</button> <div id="embedded-container"></div> </div> <h2>📝 JavaScript代码</h2> <div class="code-block" id="js-code"> // 全局变量 let floatingWidgetInstance = null; let embeddedChatInstance = null; // 模拟AI回复函数 async function simulateAIResponse(message) { // 模拟网络延迟 await new Promise(resolve => setTimeout(resolve, 1000 + Math.random() * 2000)); const responses = [ "我理解您的问题,让我为您详细解答...", "根据您提供的信息,我建议...", "这是一个很好的问题!我的回答是...", "感谢您的咨询,基于我的分析...", "您说得对,关于这个问题...", `您提到的"${message}"确实很重要...` ]; return responses[Math.floor(Math.random() * responses.length)]; } // 创建悬浮小部件 function createFloatingWidget() { if (floatingWidgetInstance) { alert('悬浮小部件已存在!'); return; } try { floatingWidgetInstance = DigitalSeeChatboxSDK.ChatBoxSDK.createWidget({ title: '智能客服助手', placeholder: '请输入您的问题...', welcomeMessage: '您好!我是智能客服助手,有什么可以帮助您的吗?', enableFileUpload: true, enableVoiceInput: true, position: 'bottom-right', offset: { x: 20, y: 20 }, onSend: simulateAIResponse, onMessage: function(message) { console.log('收到消息:', message); }, onError: function(error) { console.error('聊天错误:', error); alert('发生错误: ' + error.message); } }); document.getElementById('destroyBtn').disabled = false; alert('悬浮小部件创建成功!请查看页面右下角。'); } catch (error) { console.error('创建悬浮小部件失败:', error); alert('创建失败: ' + error.message); } } // 销毁悬浮小部件 function destroyFloatingWidget() { if (floatingWidgetInstance) { floatingWidgetInstance.destroy(); floatingWidgetInstance = null; document.getElementById('destroyBtn').disabled = true; alert('悬浮小部件已销毁!'); } } // 创建嵌入式聊天 function createEmbeddedChat() { const container = document.getElementById('embedded-container'); if (embeddedChatInstance) { embeddedChatInstance.destroy(); } try { embeddedChatInstance = DigitalSeeChatboxSDK.ChatBoxSDK.createEmbedded(container, { title: '嵌入式聊天', placeholder: '在这里输入消息...', welcomeMessage: '欢迎使用嵌入式聊天功能!', enableFileUpload: true, enableVoiceInput: true, onSend: simulateAIResponse, onMessage: function(message) { console.log('嵌入式聊天消息:', message); } }); alert('嵌入式聊天创建成功!'); } catch (error) { console.error('创建嵌入式聊天失败:', error); alert('创建失败: ' + error.message); } } // 页面加载完成后的初始化 window.addEventListener('load', function() { if (typeof DigitalSeeChatboxSDK === 'undefined') { alert('SDK 未正确加载!请检查script标签是否正确引入。'); return; } console.log('ChatBox SDK 加载成功!', DigitalSeeChatboxSDK); }); </div> </div> <!-- 依赖库 CDN 引入 --> <script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script> <script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.production.min.js"></script> <script src="https://unpkg.com/antd@5/dist/antd.min.js"></script> <script src="https://unpkg.com/lucide-react@latest/dist/umd/lucide-react.js"></script> <!-- ChatBox SDK --> <script src="../dist/index.umd.js"></script> <script> // 全局变量 let floatingWidgetInstance = null; let embeddedChatInstance = null; // 模拟AI回复函数 async function simulateAIResponse(message) { // 模拟网络延迟 await new Promise(resolve => setTimeout(resolve, 1000 + Math.random() * 2000)); const responses = [ "我理解您的问题,让我为您详细解答...", "根据您提供的信息,我建议...", "这是一个很好的问题!我的回答是...", "感谢您的咨询,基于我的分析...", "您说得对,关于这个问题...", `您提到的"${message}"确实很重要...` ]; return responses[Math.floor(Math.random() * responses.length)]; } // 创建悬浮小部件 function createFloatingWidget() { if (floatingWidgetInstance) { alert('悬浮小部件已存在!'); return; } try { floatingWidgetInstance = DigitalSeeChatboxSDK.ChatBoxSDK.createWidget({ title: '智能客服助手', placeholder: '请输入您的问题...', welcomeMessage: '您好!我是智能客服助手,有什么可以帮助您的吗?', enableFileUpload: true, enableVoiceInput: true, position: 'bottom-right', offset: { x: 20, y: 20 }, onSend: simulateAIResponse, onMessage: function(message) { console.log('收到消息:', message); }, onError: function(error) { console.error('聊天错误:', error); alert('发生错误: ' + error.message); } }); document.getElementById('destroyBtn').disabled = false; alert('悬浮小部件创建成功!请查看页面右下角。'); } catch (error) { console.error('创建悬浮小部件失败:', error); alert('创建失败: ' + error.message); } } // 销毁悬浮小部件 function destroyFloatingWidget() { if (floatingWidgetInstance) { floatingWidgetInstance.destroy(); floatingWidgetInstance = null; document.getElementById('destroyBtn').disabled = true; alert('悬浮小部件已销毁!'); } } // 创建嵌入式聊天 function createEmbeddedChat() { const container = document.getElementById('embedded-container'); if (embeddedChatInstance) { embeddedChatInstance.destroy(); } try { embeddedChatInstance = DigitalSeeChatboxSDK.ChatBoxSDK.createEmbedded(container, { title: '嵌入式聊天', placeholder: '在这里输入消息...', welcomeMessage: '欢迎使用嵌入式聊天功能!', enableFileUpload: true, enableVoiceInput: true, onSend: simulateAIResponse, onMessage: function(message) { console.log('嵌入式聊天消息:', message); } }); alert('嵌入式聊天创建成功!'); } catch (error) { console.error('创建嵌入式聊天失败:', error); alert('创建失败: ' + error.message); } } // 页面加载完成后的初始化 window.addEventListener('load', function() { if (typeof DigitalSeeChatboxSDK === 'undefined') { alert('SDK 未正确加载!请检查script标签是否正确引入。'); return; } console.log('ChatBox SDK 加载成功!', DigitalSeeChatboxSDK); }); </script> </body> </html>