Repository navigation
Expand file tree
/
Copy pathtest.html
More file actions
236 lines (218 loc) · 7.23 KB
/
Copy pathtest.html
File metadata and controls
236 lines (218 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ChatBuff - 测试页面</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 800px;
margin: 50px auto;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: #333;
}
.container {
background: white;
border-radius: 20px;
padding: 40px;
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
}
h1 {
text-align: center;
color: #667eea;
margin-bottom: 10px;
}
.subtitle {
text-align: center;
color: #999;
margin-bottom: 30px;
}
.input-group {
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: bold;
color: #555;
}
input[type="text"] {
width: 100%;
padding: 12px;
border: 2px solid #e0e0e0;
border-radius: 8px;
font-size: 16px;
transition: border-color 0.3s;
}
input[type="text"]:focus {
outline: none;
border-color: #667eea;
}
button {
width: 100%;
padding: 14px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
border-radius: 8px;
font-size: 18px;
font-weight: bold;
cursor: pointer;
transition: transform 0.2s;
}
button:hover {
transform: translateY(-2px);
}
button:disabled {
background: #ccc;
cursor: not-allowed;
}
.result {
margin-top: 30px;
display: none;
}
.suggestions {
background: #f8f9fa;
padding: 20px;
border-radius: 10px;
margin-bottom: 20px;
}
.suggestion-item {
padding: 12px;
margin: 10px 0;
background: white;
border-left: 4px solid #667eea;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s;
}
.suggestion-item:hover {
transform: translateX(5px);
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.quotes {
background: #fff3cd;
padding: 20px;
border-radius: 10px;
}
.quote-item {
margin: 15px 0;
padding: 15px;
background: white;
border-radius: 8px;
border-left: 4px solid #ffc107;
}
.quote-text {
font-style: italic;
margin-bottom: 8px;
color: #333;
}
.quote-meta {
font-size: 14px;
color: #666;
}
.loading {
text-align: center;
color: #667eea;
font-weight: bold;
}
.error {
background: #f8d7da;
color: #721c24;
padding: 15px;
border-radius: 8px;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<h1>🚀 ChatBuff</h1>
<p class="subtitle">你的实时社交副驾驶</p>
<div class="input-group">
<label for="userInput">输入你想说的话:</label>
<input type="text" id="userInput" placeholder="例如:今天心情不好" />
</div>
<button onclick="getSuggestion()">获取建议</button>
<div id="loading" class="loading" style="display: none;">
正在思考中...
</div>
<div id="result" class="result">
<h3>💡 建议回复</h3>
<div id="suggestions" class="suggestions"></div>
<h3>📚 相关金句</h3>
<div id="quotes" class="quotes"></div>
</div>
<div id="error" class="error" style="display: none;"></div>
</div>
<script>
async function getSuggestion() {
const input = document.getElementById('userInput').value;
if (!input.trim()) {
alert('请输入内容!');
return;
}
const loading = document.getElementById('loading');
const result = document.getElementById('result');
const error = document.getElementById('error');
const button = document.querySelector('button');
// 显示加载状态
loading.style.display = 'block';
result.style.display = 'none';
error.style.display = 'none';
button.disabled = true;
try {
const response = await fetch('http://127.0.0.1:8000/api/suggestion', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text: input })
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
// 显示建议
const suggestionsDiv = document.getElementById('suggestions');
suggestionsDiv.innerHTML = data.suggestions.map((s, i) =>
`<div class="suggestion-item" onclick="copyText('${s.replace(/'/g, "\\'")}')">
${i + 1}. ${s}
</div>`
).join('');
// 显示相关金句
const quotesDiv = document.getElementById('quotes');
quotesDiv.innerHTML = data.related_quotes.map((q, i) =>
`<div class="quote-item">
<div class="quote-text">「${q.quote}」</div>
<div class="quote-meta">
—— ${q.author},出自《${q.source}》<br>
适用场景:${q.context}
</div>
</div>`
).join('');
result.style.display = 'block';
} catch (err) {
error.textContent = '获取建议失败: ' + err.message;
error.style.display = 'block';
} finally {
loading.style.display = 'none';
button.disabled = false;
}
}
function copyText(text) {
navigator.clipboard.writeText(text).then(() => {
alert('已复制: ' + text);
});
}
// 支持回车键
document.getElementById('userInput').addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
getSuggestion();
}
});
</script>
</body>
</html>