Skip to content

Commit 97df498

Browse files
authored
Update chatgpt.html
1 parent 1dea9d2 commit 97df498

1 file changed

Lines changed: 41 additions & 37 deletions

File tree

chatgpt.html

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
<!DOCTYPE html>
22
<html lang="zh-CN">
33
<head>
4+
<!-- 设置网页的字符编码为 UTF-8,确保中文显示正常 -->
45
<meta charset="UTF-8">
6+
<!-- 设置页面视口,以适配移动端设备 -->
57
<meta name="viewport" content="width=device-width, initial-scale=1.0">
8+
<!-- 设置网页标题 -->
69
<title>ChatGPT 接口调用示例</title>
710
<style>
8-
/* 样式文件 */
11+
/* 设置整个网页的字体样式和布局 */
912
body {
1013
font-family: Arial, sans-serif;
1114
display: flex;
@@ -17,6 +20,7 @@
1720
overflow: hidden;
1821
}
1922

23+
/* 聊天容器样式 */
2024
.chat-container {
2125
width: 90%;
2226
max-width: 600px;
@@ -30,6 +34,7 @@
3034
box-sizing: border-box;
3135
}
3236

37+
/* 聊天记录框样式 */
3338
.chat-box {
3439
flex: 1;
3540
overflow-y: auto;
@@ -40,26 +45,30 @@
4045
margin-bottom: 10px;
4146
}
4247

48+
/* 聊天消息的通用样式 */
4349
.message {
4450
margin: 10px 0;
4551
font-size: 16px;
4652
line-height: 1.5;
47-
white-space: pre-wrap; /* 支持换行 */
53+
white-space: pre-wrap;
4854
position: relative;
4955
}
5056

57+
/* 用户消息样式 */
5158
.user-message {
5259
text-align: right;
5360
color: #007bff;
5461
font-weight: bold;
5562
}
5663

64+
/* AI 消息样式 */
5765
.assistant-message {
5866
text-align: left;
5967
color: #333;
6068
font-style: italic;
6169
}
6270

71+
/* 删除按钮样式 */
6372
.delete-button {
6473
position: absolute;
6574
top: 50%;
@@ -75,10 +84,12 @@
7584
display: none;
7685
}
7786

87+
/* 删除按钮的悬停效果 */
7888
.delete-button:hover {
7989
background-color: #cc0000;
8090
}
8191

92+
/* 输入框容器样式 */
8293
.input-container {
8394
display: flex;
8495
align-items: center;
@@ -87,6 +98,7 @@
8798
border-radius: 8px;
8899
}
89100

101+
/* 输入框样式 */
90102
input[type="text"] {
91103
flex: 1;
92104
padding: 10px;
@@ -98,6 +110,7 @@
98110
box-sizing: border-box;
99111
}
100112

113+
/* 按钮样式 */
101114
button {
102115
padding: 10px 20px;
103116
font-size: 16px;
@@ -110,47 +123,47 @@
110123
transition: background-color 0.3s;
111124
}
112125

126+
/* 按钮的悬停效果 */
113127
button:hover {
114128
background-color: #0056b3;
115129
}
116130
</style>
117131
</head>
118132
<body>
133+
<!-- 主容器 -->
119134
<div class="chat-container">
120-
<div class="chat-box" id="chatBox">
121-
<!-- 聊天记录 -->
122-
</div>
135+
<!-- 聊天记录框 -->
136+
<div class="chat-box" id="chatBox"></div>
137+
<!-- 输入框容器 -->
123138
<div class="input-container">
139+
<!-- 用户输入框 -->
124140
<input type="text" id="userInput" placeholder="请输入您的问题..." />
141+
<!-- 发送按钮 -->
125142
<button onclick="sendMessage()">发送</button>
126143
</div>
127144
</div>
128145
<script>
129-
// 从 Cookie 获取历史会话
130-
function getHistoryFromCookies() {
131-
const cookies = document.cookie.split('; ').reduce((acc, cookie) => {
132-
const [key, value] = cookie.split('=');
133-
acc[key] = decodeURIComponent(value);
134-
return acc;
135-
}, {});
136-
return cookies.chatHistory ? JSON.parse(cookies.chatHistory) : [];
146+
// 从 Local Storage 中获取历史记录
147+
function getHistoryFromLocalStorage() {
148+
const history = localStorage.getItem('chatHistory');
149+
return history ? JSON.parse(history) : [];
137150
}
138151

139-
// 保存会话到 Cookie
140-
function saveHistoryToCookies(history) {
141-
document.cookie = `chatHistory=${encodeURIComponent(JSON.stringify(history))}; path=/; max-age=${60 * 60 * 24 * 365 * 10}`;
152+
// 将历史记录保存到 Local Storage
153+
function saveHistoryToLocalStorage(history) {
154+
localStorage.setItem('chatHistory', JSON.stringify(history));
142155
}
143156

144-
// 加载历史记录
157+
// 加载历史聊天记录并显示到聊天框
145158
function loadHistory() {
146159
const chatBox = document.getElementById('chatBox');
147-
const history = getHistoryFromCookies();
160+
const history = getHistoryFromLocalStorage();
148161
history.forEach(({ role, content }) => {
149162
addMessage(role, content);
150163
});
151164
}
152165

153-
// 添加消息
166+
// 添加一条聊天消息到聊天框
154167
function addMessage(role, content) {
155168
const chatBox = document.getElementById('chatBox');
156169
const message = document.createElement('div');
@@ -164,52 +177,46 @@
164177
deleteButton.onclick = () => deleteMessage(message, content);
165178
message.appendChild(deleteButton);
166179

167-
// 点击显示删除按钮
180+
// 点击消息显示删除按钮
168181
message.onclick = () => {
169182
deleteButton.style.display = deleteButton.style.display === 'inline-block' ? 'none' : 'inline-block';
170183
};
171184

172185
chatBox.appendChild(message);
173186
}
174187

175-
// 删除单条消息
188+
// 删除指定消息并更新历史记录
176189
function deleteMessage(element, content) {
177190
const chatBox = document.getElementById('chatBox');
178191
chatBox.removeChild(element);
179192

180-
// 更新历史记录
181-
const history = getHistoryFromCookies();
193+
const history = getHistoryFromLocalStorage();
182194
const updatedHistory = history.filter(item => item.content !== content);
183-
saveHistoryToCookies(updatedHistory);
195+
saveHistoryToLocalStorage(updatedHistory);
184196
}
185197

198+
// 发送消息到接口
186199
async function sendMessage() {
187200
const chatBox = document.getElementById('chatBox');
188201
const userInput = document.getElementById('userInput').value;
189202

190203
if (userInput.trim() === '') return;
191204

192-
// 用户输入显示
193205
addMessage('user', userInput);
194-
195-
// 清空输入框
196206
document.getElementById('userInput').value = '';
197207

198-
// 显示加载中提示
199208
const loadingMessage = document.createElement('div');
200209
loadingMessage.className = 'message assistant-message';
201210
loadingMessage.textContent = '加载中,较长文字等待时间较长';
202211
chatBox.appendChild(loadingMessage);
203212

204-
// 请求参数
205213
const payload = {
206214
model: "gpt-4o-mini",
207215
messages: [{ role: "user", content: userInput }],
208216
stream: false
209217
};
210218

211219
try {
212-
// API 请求
213220
const response = await fetch("https://apiserver.alcex.cn/v1/chat/completions", {
214221
method: "POST",
215222
headers: {
@@ -222,25 +229,22 @@
222229
const result = await response.json();
223230
const content = result.choices[0].message.content;
224231

225-
// 显示返回内容
226-
loadingMessage.remove(); // 移除加载提示
232+
loadingMessage.remove();
227233
addMessage('assistant', content);
228234

229-
// 保存会话到 Cookie
230-
const history = getHistoryFromCookies();
235+
const history = getHistoryFromLocalStorage();
231236
history.push({ role: 'user', content: userInput });
232237
history.push({ role: 'assistant', content });
233-
saveHistoryToCookies(history);
238+
saveHistoryToLocalStorage(history);
234239
} catch (error) {
235240
console.error("Error:", error);
236241
loadingMessage.textContent = '请求失败,请稍后再试。';
237242
}
238243

239-
// 自动滚动到底部
240244
chatBox.scrollTop = chatBox.scrollHeight;
241245
}
242246

243-
// 加载历史记录
247+
// 页面加载时自动加载历史记录
244248
loadHistory();
245249
</script>
246250
</body>

0 commit comments

Comments
 (0)