310 lines
9.6 KiB
HTML
310 lines
9.6 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="zh-CN">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>小狸 - AI 聊天</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
height: 100vh;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
}
|
|
.chat-container {
|
|
width: 90%;
|
|
max-width: 500px;
|
|
height: 80vh;
|
|
background: white;
|
|
border-radius: 20px;
|
|
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
|
display: flex;
|
|
flex-direction: column;
|
|
overflow: hidden;
|
|
}
|
|
.chat-header {
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
color: white;
|
|
padding: 20px;
|
|
text-align: center;
|
|
}
|
|
.chat-header h1 {
|
|
font-size: 24px;
|
|
margin-bottom: 5px;
|
|
}
|
|
.chat-header p {
|
|
font-size: 12px;
|
|
opacity: 0.9;
|
|
}
|
|
.chat-messages {
|
|
flex: 1;
|
|
overflow-y: auto;
|
|
padding: 20px;
|
|
background: #f5f5f5;
|
|
}
|
|
.message {
|
|
margin-bottom: 15px;
|
|
display: flex;
|
|
align-items: flex-start;
|
|
}
|
|
.message.user {
|
|
justify-content: flex-end;
|
|
}
|
|
.message.bot {
|
|
justify-content: flex-start;
|
|
}
|
|
.message-content {
|
|
max-width: 70%;
|
|
padding: 12px 16px;
|
|
border-radius: 18px;
|
|
font-size: 14px;
|
|
line-height: 1.5;
|
|
word-wrap: break-word;
|
|
}
|
|
.message.user .message-content {
|
|
background: #667eea;
|
|
color: white;
|
|
border-bottom-right-radius: 4px;
|
|
}
|
|
.message.bot .message-content {
|
|
background: white;
|
|
color: #333;
|
|
border-bottom-left-radius: 4px;
|
|
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
|
|
}
|
|
.avatar {
|
|
width: 36px;
|
|
height: 36px;
|
|
border-radius: 50%;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
font-size: 18px;
|
|
margin: 0 8px;
|
|
}
|
|
.chat-input {
|
|
padding: 15px 20px;
|
|
background: white;
|
|
border-top: 1px solid #eee;
|
|
display: flex;
|
|
gap: 10px;
|
|
}
|
|
.chat-input input {
|
|
flex: 1;
|
|
padding: 12px 16px;
|
|
border: 2px solid #e0e0e0;
|
|
border-radius: 25px;
|
|
font-size: 14px;
|
|
outline: none;
|
|
transition: border-color 0.3s;
|
|
}
|
|
.chat-input input:focus {
|
|
border-color: #667eea;
|
|
}
|
|
.chat-input button {
|
|
padding: 12px 24px;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
color: white;
|
|
border: none;
|
|
border-radius: 25px;
|
|
font-size: 14px;
|
|
cursor: pointer;
|
|
transition: transform 0.2s, box-shadow 0.2s;
|
|
}
|
|
.chat-input button:hover {
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
|
|
}
|
|
.chat-input button:disabled {
|
|
opacity: 0.6;
|
|
cursor: not-allowed;
|
|
transform: none;
|
|
}
|
|
.typing {
|
|
display: flex;
|
|
gap: 4px;
|
|
padding: 12px 16px;
|
|
}
|
|
.typing span {
|
|
width: 8px;
|
|
height: 8px;
|
|
background: #999;
|
|
border-radius: 50%;
|
|
animation: typing 1.4s infinite;
|
|
}
|
|
.typing span:nth-child(2) { animation-delay: 0.2s; }
|
|
.typing span:nth-child(3) { animation-delay: 0.4s; }
|
|
@keyframes typing {
|
|
0%, 60%, 100% { transform: translateY(0); }
|
|
30% { transform: translateY(-10px); }
|
|
}
|
|
.start-overlay {
|
|
position: fixed;
|
|
top: 0; left: 0; right: 0; bottom: 0;
|
|
background: rgba(0,0,0,0.6);
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: center;
|
|
z-index: 1000;
|
|
}
|
|
.start-overlay.hidden {
|
|
display: none;
|
|
}
|
|
.start-card {
|
|
background: white;
|
|
border-radius: 24px;
|
|
padding: 50px 40px;
|
|
text-align: center;
|
|
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
|
animation: popIn 0.5s ease;
|
|
}
|
|
@keyframes popIn {
|
|
0% { transform: scale(0.8); opacity: 0; }
|
|
100% { transform: scale(1); opacity: 1; }
|
|
}
|
|
.start-avatar {
|
|
font-size: 64px;
|
|
margin-bottom: 15px;
|
|
}
|
|
.start-card h2 {
|
|
color: #333;
|
|
font-size: 28px;
|
|
margin-bottom: 10px;
|
|
}
|
|
.start-card p {
|
|
color: #666;
|
|
font-size: 16px;
|
|
margin-bottom: 30px;
|
|
}
|
|
.start-btn {
|
|
padding: 16px 40px;
|
|
font-size: 18px;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
color: white;
|
|
border: none;
|
|
border-radius: 30px;
|
|
cursor: pointer;
|
|
transition: transform 0.2s, box-shadow 0.2s;
|
|
}
|
|
.start-btn:hover {
|
|
transform: translateY(-3px);
|
|
box-shadow: 0 10px 25px rgba(102, 126, 234, 0.5);
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="chat-container">
|
|
<div class="chat-header">
|
|
<h1>🐱 小狸</h1>
|
|
<p>你的 AI 小伙伴</p>
|
|
</div>
|
|
<div class="chat-messages" id="messages">
|
|
<div class="message bot">
|
|
<div class="avatar">🐱</div>
|
|
<div class="message-content">你好!我是小狸,很高兴见到你!有什么想聊的吗?</div>
|
|
</div>
|
|
</div>
|
|
<div class="chat-input">
|
|
<input type="text" id="userInput" placeholder="输入消息..." maxlength="100">
|
|
<button id="sendBtn" onclick="sendMessage()">发送</button>
|
|
</div>
|
|
<div class="start-overlay" id="startOverlay">
|
|
<div class="start-card">
|
|
<div class="start-avatar">🐱</div>
|
|
<h2>小狸已就绪!</h2>
|
|
<p>你的 AI 小伙伴正在等你聊天~</p>
|
|
<button class="start-btn" onclick="startChat()">👉 点击开始聊天</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
const messagesDiv = document.getElementById('messages');
|
|
const userInput = document.getElementById('userInput');
|
|
const sendBtn = document.getElementById('sendBtn');
|
|
const sessionId = 'session_' + Math.random().toString(36).substr(2, 9);
|
|
|
|
function addMessage(text, isUser) {
|
|
const div = document.createElement('div');
|
|
div.className = `message ${isUser ? 'user' : 'bot'}`;
|
|
div.innerHTML = `
|
|
${isUser ? '' : '<div class="avatar">🐱</div>'}
|
|
<div class="message-content">${escapeHtml(text)}</div>
|
|
${isUser ? '<div class="avatar">👤</div>' : ''}
|
|
`;
|
|
messagesDiv.appendChild(div);
|
|
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
|
}
|
|
|
|
function showTyping() {
|
|
const div = document.createElement('div');
|
|
div.className = 'message bot';
|
|
div.id = 'typing-indicator';
|
|
div.innerHTML = `
|
|
<div class="avatar">🐱</div>
|
|
<div class="message-content">
|
|
<div class="typing">
|
|
<span></span><span></span><span></span>
|
|
</div>
|
|
</div>
|
|
`;
|
|
messagesDiv.appendChild(div);
|
|
messagesDiv.scrollTop = messagesDiv.scrollHeight;
|
|
}
|
|
|
|
function hideTyping() {
|
|
const typing = document.getElementById('typing-indicator');
|
|
if (typing) typing.remove();
|
|
}
|
|
|
|
function escapeHtml(text) {
|
|
const div = document.createElement('div');
|
|
div.textContent = text;
|
|
return div.innerHTML;
|
|
}
|
|
|
|
async function sendMessage() {
|
|
const text = userInput.value.trim();
|
|
if (!text) return;
|
|
|
|
addMessage(text, true);
|
|
userInput.value = '';
|
|
sendBtn.disabled = true;
|
|
showTyping();
|
|
|
|
try {
|
|
const response = await fetch('/chat', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ message: text, session_id: sessionId })
|
|
});
|
|
const data = await response.json();
|
|
hideTyping();
|
|
addMessage(data.response, false);
|
|
} catch (error) {
|
|
hideTyping();
|
|
addMessage('抱歉,我出错了,请稍后再试~', false);
|
|
}
|
|
|
|
sendBtn.disabled = false;
|
|
}
|
|
|
|
userInput.addEventListener('keypress', (e) => {
|
|
if (e.key === 'Enter') sendMessage();
|
|
});
|
|
|
|
function startChat() {
|
|
document.getElementById('startOverlay').classList.add('hidden');
|
|
userInput.focus();
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|