|
1 | 1 | <!DOCTYPE html> |
2 | 2 | <html lang="zh-CN"> |
3 | 3 | <head> |
| 4 | + <!-- 设置网页的字符编码为 UTF-8,确保中文显示正常 --> |
4 | 5 | <meta charset="UTF-8"> |
| 6 | + <!-- 设置页面视口,以适配移动端设备 --> |
5 | 7 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 8 | + <!-- 设置网页标题 --> |
6 | 9 | <title>ChatGPT 接口调用示例</title> |
7 | 10 | <style> |
8 | | - /* 样式文件 */ |
| 11 | + /* 设置整个网页的字体样式和布局 */ |
9 | 12 | body { |
10 | 13 | font-family: Arial, sans-serif; |
11 | 14 | display: flex; |
|
17 | 20 | overflow: hidden; |
18 | 21 | } |
19 | 22 |
|
| 23 | + /* 聊天容器样式 */ |
20 | 24 | .chat-container { |
21 | 25 | width: 90%; |
22 | 26 | max-width: 600px; |
|
30 | 34 | box-sizing: border-box; |
31 | 35 | } |
32 | 36 |
|
| 37 | + /* 聊天记录框样式 */ |
33 | 38 | .chat-box { |
34 | 39 | flex: 1; |
35 | 40 | overflow-y: auto; |
|
40 | 45 | margin-bottom: 10px; |
41 | 46 | } |
42 | 47 |
|
| 48 | + /* 聊天消息的通用样式 */ |
43 | 49 | .message { |
44 | 50 | margin: 10px 0; |
45 | 51 | font-size: 16px; |
46 | 52 | line-height: 1.5; |
47 | | - white-space: pre-wrap; /* 支持换行 */ |
| 53 | + white-space: pre-wrap; |
48 | 54 | position: relative; |
49 | 55 | } |
50 | 56 |
|
| 57 | + /* 用户消息样式 */ |
51 | 58 | .user-message { |
52 | 59 | text-align: right; |
53 | 60 | color: #007bff; |
54 | 61 | font-weight: bold; |
55 | 62 | } |
56 | 63 |
|
| 64 | + /* AI 消息样式 */ |
57 | 65 | .assistant-message { |
58 | 66 | text-align: left; |
59 | 67 | color: #333; |
60 | 68 | font-style: italic; |
61 | 69 | } |
62 | 70 |
|
| 71 | + /* 删除按钮样式 */ |
63 | 72 | .delete-button { |
64 | 73 | position: absolute; |
65 | 74 | top: 50%; |
|
75 | 84 | display: none; |
76 | 85 | } |
77 | 86 |
|
| 87 | + /* 删除按钮的悬停效果 */ |
78 | 88 | .delete-button:hover { |
79 | 89 | background-color: #cc0000; |
80 | 90 | } |
81 | 91 |
|
| 92 | + /* 输入框容器样式 */ |
82 | 93 | .input-container { |
83 | 94 | display: flex; |
84 | 95 | align-items: center; |
|
87 | 98 | border-radius: 8px; |
88 | 99 | } |
89 | 100 |
|
| 101 | + /* 输入框样式 */ |
90 | 102 | input[type="text"] { |
91 | 103 | flex: 1; |
92 | 104 | padding: 10px; |
|
98 | 110 | box-sizing: border-box; |
99 | 111 | } |
100 | 112 |
|
| 113 | + /* 按钮样式 */ |
101 | 114 | button { |
102 | 115 | padding: 10px 20px; |
103 | 116 | font-size: 16px; |
|
110 | 123 | transition: background-color 0.3s; |
111 | 124 | } |
112 | 125 |
|
| 126 | + /* 按钮的悬停效果 */ |
113 | 127 | button:hover { |
114 | 128 | background-color: #0056b3; |
115 | 129 | } |
116 | 130 | </style> |
117 | 131 | </head> |
118 | 132 | <body> |
| 133 | +<!-- 主容器 --> |
119 | 134 | <div class="chat-container"> |
120 | | - <div class="chat-box" id="chatBox"> |
121 | | - <!-- 聊天记录 --> |
122 | | - </div> |
| 135 | + <!-- 聊天记录框 --> |
| 136 | + <div class="chat-box" id="chatBox"></div> |
| 137 | + <!-- 输入框容器 --> |
123 | 138 | <div class="input-container"> |
| 139 | + <!-- 用户输入框 --> |
124 | 140 | <input type="text" id="userInput" placeholder="请输入您的问题..." /> |
| 141 | + <!-- 发送按钮 --> |
125 | 142 | <button onclick="sendMessage()">发送</button> |
126 | 143 | </div> |
127 | 144 | </div> |
128 | 145 | <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) : []; |
137 | 150 | } |
138 | 151 |
|
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)); |
142 | 155 | } |
143 | 156 |
|
144 | | - // 加载历史记录 |
| 157 | + // 加载历史聊天记录并显示到聊天框 |
145 | 158 | function loadHistory() { |
146 | 159 | const chatBox = document.getElementById('chatBox'); |
147 | | - const history = getHistoryFromCookies(); |
| 160 | + const history = getHistoryFromLocalStorage(); |
148 | 161 | history.forEach(({ role, content }) => { |
149 | 162 | addMessage(role, content); |
150 | 163 | }); |
151 | 164 | } |
152 | 165 |
|
153 | | - // 添加消息 |
| 166 | + // 添加一条聊天消息到聊天框 |
154 | 167 | function addMessage(role, content) { |
155 | 168 | const chatBox = document.getElementById('chatBox'); |
156 | 169 | const message = document.createElement('div'); |
|
164 | 177 | deleteButton.onclick = () => deleteMessage(message, content); |
165 | 178 | message.appendChild(deleteButton); |
166 | 179 |
|
167 | | - // 点击显示删除按钮 |
| 180 | + // 点击消息显示删除按钮 |
168 | 181 | message.onclick = () => { |
169 | 182 | deleteButton.style.display = deleteButton.style.display === 'inline-block' ? 'none' : 'inline-block'; |
170 | 183 | }; |
171 | 184 |
|
172 | 185 | chatBox.appendChild(message); |
173 | 186 | } |
174 | 187 |
|
175 | | - // 删除单条消息 |
| 188 | + // 删除指定消息并更新历史记录 |
176 | 189 | function deleteMessage(element, content) { |
177 | 190 | const chatBox = document.getElementById('chatBox'); |
178 | 191 | chatBox.removeChild(element); |
179 | 192 |
|
180 | | - // 更新历史记录 |
181 | | - const history = getHistoryFromCookies(); |
| 193 | + const history = getHistoryFromLocalStorage(); |
182 | 194 | const updatedHistory = history.filter(item => item.content !== content); |
183 | | - saveHistoryToCookies(updatedHistory); |
| 195 | + saveHistoryToLocalStorage(updatedHistory); |
184 | 196 | } |
185 | 197 |
|
| 198 | + // 发送消息到接口 |
186 | 199 | async function sendMessage() { |
187 | 200 | const chatBox = document.getElementById('chatBox'); |
188 | 201 | const userInput = document.getElementById('userInput').value; |
189 | 202 |
|
190 | 203 | if (userInput.trim() === '') return; |
191 | 204 |
|
192 | | - // 用户输入显示 |
193 | 205 | addMessage('user', userInput); |
194 | | - |
195 | | - // 清空输入框 |
196 | 206 | document.getElementById('userInput').value = ''; |
197 | 207 |
|
198 | | - // 显示加载中提示 |
199 | 208 | const loadingMessage = document.createElement('div'); |
200 | 209 | loadingMessage.className = 'message assistant-message'; |
201 | 210 | loadingMessage.textContent = '加载中,较长文字等待时间较长'; |
202 | 211 | chatBox.appendChild(loadingMessage); |
203 | 212 |
|
204 | | - // 请求参数 |
205 | 213 | const payload = { |
206 | 214 | model: "gpt-4o-mini", |
207 | 215 | messages: [{ role: "user", content: userInput }], |
208 | 216 | stream: false |
209 | 217 | }; |
210 | 218 |
|
211 | 219 | try { |
212 | | - // API 请求 |
213 | 220 | const response = await fetch("https://apiserver.alcex.cn/v1/chat/completions", { |
214 | 221 | method: "POST", |
215 | 222 | headers: { |
|
222 | 229 | const result = await response.json(); |
223 | 230 | const content = result.choices[0].message.content; |
224 | 231 |
|
225 | | - // 显示返回内容 |
226 | | - loadingMessage.remove(); // 移除加载提示 |
| 232 | + loadingMessage.remove(); |
227 | 233 | addMessage('assistant', content); |
228 | 234 |
|
229 | | - // 保存会话到 Cookie |
230 | | - const history = getHistoryFromCookies(); |
| 235 | + const history = getHistoryFromLocalStorage(); |
231 | 236 | history.push({ role: 'user', content: userInput }); |
232 | 237 | history.push({ role: 'assistant', content }); |
233 | | - saveHistoryToCookies(history); |
| 238 | + saveHistoryToLocalStorage(history); |
234 | 239 | } catch (error) { |
235 | 240 | console.error("Error:", error); |
236 | 241 | loadingMessage.textContent = '请求失败,请稍后再试。'; |
237 | 242 | } |
238 | 243 |
|
239 | | - // 自动滚动到底部 |
240 | 244 | chatBox.scrollTop = chatBox.scrollHeight; |
241 | 245 | } |
242 | 246 |
|
243 | | - // 加载历史记录 |
| 247 | + // 页面加载时自动加载历史记录 |
244 | 248 | loadHistory(); |
245 | 249 | </script> |
246 | 250 | </body> |
|
0 commit comments