Upload app.js
Browse files- backend/src/app.js +333 -1
backend/src/app.js
CHANGED
@@ -50,7 +50,339 @@ app.use('/data', express.static(path.join(__dirname, '../../frontend/public/mock
|
|
50 |
|
51 |
// 直接提供测试页面路由
|
52 |
app.get('/test.html', (req, res) => {
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
});
|
55 |
|
56 |
// API路由
|
|
|
50 |
|
51 |
// 直接提供测试页面路由
|
52 |
app.get('/test.html', (req, res) => {
|
53 |
+
try {
|
54 |
+
// 检查文件是否存在
|
55 |
+
const testFilePath = path.join(__dirname, '../../test.html');
|
56 |
+
console.log('Looking for test.html at:', testFilePath);
|
57 |
+
|
58 |
+
// 发送测试页面
|
59 |
+
res.sendFile(testFilePath, (err) => {
|
60 |
+
if (err) {
|
61 |
+
console.error('Error serving test.html:', err);
|
62 |
+
// 如果文件不存在,返回一个简单的测试页面
|
63 |
+
res.send(`
|
64 |
+
<!DOCTYPE html>
|
65 |
+
<html>
|
66 |
+
<head><title>Test Page</title></head>
|
67 |
+
<body>
|
68 |
+
<h1>PPTist API Test</h1>
|
69 |
+
<p>Test file not found at: ${testFilePath}</p>
|
70 |
+
<button onclick="fetch('/api/health').then(r=>r.json()).then(d=>alert(JSON.stringify(d)))">Test Health</button>
|
71 |
+
</body>
|
72 |
+
</html>
|
73 |
+
`);
|
74 |
+
}
|
75 |
+
});
|
76 |
+
} catch (error) {
|
77 |
+
console.error('Test page error:', error);
|
78 |
+
res.status(500).send('Test page error: ' + error.message);
|
79 |
+
}
|
80 |
+
});
|
81 |
+
|
82 |
+
// 添加内置测试页面端点
|
83 |
+
app.get('/test', (req, res) => {
|
84 |
+
res.send(`
|
85 |
+
<!DOCTYPE html>
|
86 |
+
<html lang="zh-CN">
|
87 |
+
<head>
|
88 |
+
<meta charset="UTF-8">
|
89 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
90 |
+
<title>PPTist API 测试页面</title>
|
91 |
+
<style>
|
92 |
+
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
|
93 |
+
.container { max-width: 800px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; }
|
94 |
+
h1 { color: #333; text-align: center; margin-bottom: 30px; }
|
95 |
+
.test-section { margin: 20px 0; padding: 15px; background: #f9f9f9; border-radius: 5px; }
|
96 |
+
.test-button { background: #5b9bd5; color: white; border: none; padding: 10px 20px; margin: 5px; border-radius: 5px; cursor: pointer; }
|
97 |
+
.test-button:hover { background: #4a8bc2; }
|
98 |
+
.result { margin: 10px 0; padding: 10px; background: #e8e8e8; border-radius: 3px; font-family: monospace; font-size: 12px; }
|
99 |
+
.success { background: #d4edda; color: #155724; }
|
100 |
+
.error { background: #f8d7da; color: #721c24; }
|
101 |
+
.login-form { margin: 15px 0; }
|
102 |
+
.login-form input { padding: 8px; margin: 5px; border: 1px solid #ddd; border-radius: 3px; }
|
103 |
+
</style>
|
104 |
+
</head>
|
105 |
+
<body>
|
106 |
+
<div class="container">
|
107 |
+
<h1>🚀 PPTist API 测试控制台</h1>
|
108 |
+
|
109 |
+
<div class="test-section">
|
110 |
+
<h3>🔗 基础连接测试</h3>
|
111 |
+
<button class="test-button" onclick="testHealth()">健康检查</button>
|
112 |
+
<button class="test-button" onclick="testGitHubStatus()">GitHub状态</button>
|
113 |
+
<button class="test-button" onclick="testGitHubConnection()">GitHub连接</button>
|
114 |
+
<div id="basic-results"></div>
|
115 |
+
</div>
|
116 |
+
|
117 |
+
<div class="test-section">
|
118 |
+
<h3>🔐 用户认证测试</h3>
|
119 |
+
<div class="login-form">
|
120 |
+
<input type="text" id="username" placeholder="用户名" value="PS01">
|
121 |
+
<input type="password" id="password" placeholder="密码" value="admin_cybercity2025">
|
122 |
+
<button class="test-button" onclick="testLogin()">登录</button>
|
123 |
+
<button class="test-button" onclick="clearToken()">清除Token</button>
|
124 |
+
</div>
|
125 |
+
<div id="token-info">当前Token: 未登录</div>
|
126 |
+
<button class="test-button" onclick="testVerifyToken()">验证Token</button>
|
127 |
+
<div id="auth-results"></div>
|
128 |
+
</div>
|
129 |
+
|
130 |
+
<div class="test-section">
|
131 |
+
<h3>📄 PPT管理测试</h3>
|
132 |
+
<button class="test-button" onclick="testPPTRoute()">PPT路由测试</button>
|
133 |
+
<button class="test-button" onclick="testPPTList()">获取PPT列表</button>
|
134 |
+
<button class="test-button" onclick="testCreatePPT()">创建PPT</button>
|
135 |
+
<button class="test-button" onclick="testSavePPT()">保存PPT</button>
|
136 |
+
<div id="ppt-results"></div>
|
137 |
+
</div>
|
138 |
+
|
139 |
+
<div class="test-section">
|
140 |
+
<h3>🌐 公共分享测试</h3>
|
141 |
+
<button class="test-button" onclick="testGenerateShareLink()">生成分享链接</button>
|
142 |
+
<div id="share-results"></div>
|
143 |
+
</div>
|
144 |
+
</div>
|
145 |
+
|
146 |
+
<script>
|
147 |
+
let currentToken = localStorage.getItem('pptist_test_token') || '';
|
148 |
+
let testPptId = '';
|
149 |
+
|
150 |
+
function updateTokenDisplay() {
|
151 |
+
const display = document.getElementById('token-info');
|
152 |
+
if (currentToken) {
|
153 |
+
display.textContent = '当前Token: ' + currentToken.substring(0, 20) + '...';
|
154 |
+
} else {
|
155 |
+
display.textContent = '当前Token: 未登录';
|
156 |
+
}
|
157 |
+
}
|
158 |
+
|
159 |
+
function clearToken() {
|
160 |
+
currentToken = '';
|
161 |
+
localStorage.removeItem('pptist_test_token');
|
162 |
+
updateTokenDisplay();
|
163 |
+
addResult('auth-results', '✅ Token已清除', 'success');
|
164 |
+
}
|
165 |
+
|
166 |
+
function addResult(containerId, message, type = '') {
|
167 |
+
const container = document.getElementById(containerId);
|
168 |
+
const div = document.createElement('div');
|
169 |
+
div.className = 'result ' + type;
|
170 |
+
div.textContent = '[' + new Date().toLocaleTimeString() + '] ' + message;
|
171 |
+
container.appendChild(div);
|
172 |
+
}
|
173 |
+
|
174 |
+
async function testHealth() {
|
175 |
+
try {
|
176 |
+
const response = await fetch('/api/health');
|
177 |
+
const data = await response.json();
|
178 |
+
addResult('basic-results', '✅ 健康检查成功: ' + JSON.stringify(data), 'success');
|
179 |
+
} catch (error) {
|
180 |
+
addResult('basic-results', '❌ 健康检查失败: ' + error.message, 'error');
|
181 |
+
}
|
182 |
+
}
|
183 |
+
|
184 |
+
async function testGitHubStatus() {
|
185 |
+
try {
|
186 |
+
const response = await fetch('/api/github/status');
|
187 |
+
const data = await response.json();
|
188 |
+
addResult('basic-results', '✅ GitHub状态: ' + JSON.stringify(data, null, 2), 'success');
|
189 |
+
} catch (error) {
|
190 |
+
addResult('basic-results', '❌ GitHub状态失败: ' + error.message, 'error');
|
191 |
+
}
|
192 |
+
}
|
193 |
+
|
194 |
+
async function testGitHubConnection() {
|
195 |
+
try {
|
196 |
+
const response = await fetch('/api/github/test');
|
197 |
+
const data = await response.json();
|
198 |
+
addResult('basic-results', '✅ GitHub连接测试: ' + JSON.stringify(data, null, 2), 'success');
|
199 |
+
} catch (error) {
|
200 |
+
addResult('basic-results', '❌ GitHub连接失败: ' + error.message, 'error');
|
201 |
+
}
|
202 |
+
}
|
203 |
+
|
204 |
+
async function testLogin() {
|
205 |
+
const username = document.getElementById('username').value;
|
206 |
+
const password = document.getElementById('password').value;
|
207 |
+
|
208 |
+
try {
|
209 |
+
const response = await fetch('/api/auth/login', {
|
210 |
+
method: 'POST',
|
211 |
+
headers: { 'Content-Type': 'application/json' },
|
212 |
+
body: JSON.stringify({ username, password })
|
213 |
+
});
|
214 |
+
|
215 |
+
const data = await response.json();
|
216 |
+
|
217 |
+
if (response.ok && data.token) {
|
218 |
+
currentToken = data.token;
|
219 |
+
localStorage.setItem('pptist_test_token', currentToken);
|
220 |
+
updateTokenDisplay();
|
221 |
+
addResult('auth-results', '✅ 登录成功: ' + data.user.username + ' (' + data.user.role + ')', 'success');
|
222 |
+
} else {
|
223 |
+
addResult('auth-results', '❌ 登录失败: ' + (data.error || '未知错误'), 'error');
|
224 |
+
}
|
225 |
+
} catch (error) {
|
226 |
+
addResult('auth-results', '❌ 登录请求失败: ' + error.message, 'error');
|
227 |
+
}
|
228 |
+
}
|
229 |
+
|
230 |
+
async function testVerifyToken() {
|
231 |
+
if (!currentToken) {
|
232 |
+
addResult('auth-results', '❌ 请先登录', 'error');
|
233 |
+
return;
|
234 |
+
}
|
235 |
+
|
236 |
+
try {
|
237 |
+
const response = await fetch('/api/auth/verify', {
|
238 |
+
headers: { 'Authorization': 'Bearer ' + currentToken }
|
239 |
+
});
|
240 |
+
|
241 |
+
const data = await response.json();
|
242 |
+
|
243 |
+
if (response.ok) {
|
244 |
+
addResult('auth-results', '✅ Token验证成功: ' + JSON.stringify(data), 'success');
|
245 |
+
} else {
|
246 |
+
addResult('auth-results', '❌ Token验证失败: ' + data.error, 'error');
|
247 |
+
}
|
248 |
+
} catch (error) {
|
249 |
+
addResult('auth-results', '❌ Token验证请求失败: ' + error.message, 'error');
|
250 |
+
}
|
251 |
+
}
|
252 |
+
|
253 |
+
async function testPPTRoute() {
|
254 |
+
try {
|
255 |
+
const response = await fetch('/api/ppt/test');
|
256 |
+
const data = await response.json();
|
257 |
+
addResult('ppt-results', '✅ PPT路由测试成功: ' + JSON.stringify(data), 'success');
|
258 |
+
} catch (error) {
|
259 |
+
addResult('ppt-results', '❌ PPT路由测试失败: ' + error.message, 'error');
|
260 |
+
}
|
261 |
+
}
|
262 |
+
|
263 |
+
async function testPPTList() {
|
264 |
+
if (!currentToken) {
|
265 |
+
addResult('ppt-results', '❌ 请先登录', 'error');
|
266 |
+
return;
|
267 |
+
}
|
268 |
+
|
269 |
+
try {
|
270 |
+
const response = await fetch('/api/ppt/list', {
|
271 |
+
headers: { 'Authorization': 'Bearer ' + currentToken }
|
272 |
+
});
|
273 |
+
|
274 |
+
const data = await response.json();
|
275 |
+
|
276 |
+
if (response.ok) {
|
277 |
+
addResult('ppt-results', '✅ PPT列表获取成功: ' + JSON.stringify(data), 'success');
|
278 |
+
} else {
|
279 |
+
addResult('ppt-results', '❌ PPT列表获取失败: ' + data.error, 'error');
|
280 |
+
}
|
281 |
+
} catch (error) {
|
282 |
+
addResult('ppt-results', '❌ PPT列表请求失败: ' + error.message, 'error');
|
283 |
+
}
|
284 |
+
}
|
285 |
+
|
286 |
+
async function testCreatePPT() {
|
287 |
+
if (!currentToken) {
|
288 |
+
addResult('ppt-results', '❌ 请先登录', 'error');
|
289 |
+
return;
|
290 |
+
}
|
291 |
+
|
292 |
+
try {
|
293 |
+
const response = await fetch('/api/ppt/create', {
|
294 |
+
method: 'POST',
|
295 |
+
headers: {
|
296 |
+
'Content-Type': 'application/json',
|
297 |
+
'Authorization': 'Bearer ' + currentToken
|
298 |
+
},
|
299 |
+
body: JSON.stringify({ title: '测试PPT - ' + new Date().toLocaleString() })
|
300 |
+
});
|
301 |
+
|
302 |
+
const data = await response.json();
|
303 |
+
|
304 |
+
if (response.ok && data.pptId) {
|
305 |
+
testPptId = data.pptId;
|
306 |
+
addResult('ppt-results', '✅ PPT创建成功: ' + testPptId, 'success');
|
307 |
+
} else {
|
308 |
+
addResult('ppt-results', '❌ PPT创建失败: ' + (data.error || '未知错误'), 'error');
|
309 |
+
}
|
310 |
+
} catch (error) {
|
311 |
+
addResult('ppt-results', '❌ PPT创建请求失败: ' + error.message, 'error');
|
312 |
+
}
|
313 |
+
}
|
314 |
+
|
315 |
+
async function testSavePPT() {
|
316 |
+
if (!currentToken) {
|
317 |
+
addResult('ppt-results', '❌ 请先登录', 'error');
|
318 |
+
return;
|
319 |
+
}
|
320 |
+
|
321 |
+
if (!testPptId) {
|
322 |
+
addResult('ppt-results', '❌ 请先创建PPT', 'error');
|
323 |
+
return;
|
324 |
+
}
|
325 |
+
|
326 |
+
const pptData = {
|
327 |
+
pptId: testPptId,
|
328 |
+
title: '测试PPT - 已保存',
|
329 |
+
slides: [{ id: 'slide-1', elements: [], background: { type: 'solid', color: '#ffffff' } }],
|
330 |
+
theme: { backgroundColor: '#ffffff', themeColor: '#5b9bd5' }
|
331 |
+
};
|
332 |
+
|
333 |
+
try {
|
334 |
+
const response = await fetch('/api/ppt/save', {
|
335 |
+
method: 'POST',
|
336 |
+
headers: {
|
337 |
+
'Content-Type': 'application/json',
|
338 |
+
'Authorization': 'Bearer ' + currentToken
|
339 |
+
},
|
340 |
+
body: JSON.stringify(pptData)
|
341 |
+
});
|
342 |
+
|
343 |
+
const data = await response.json();
|
344 |
+
|
345 |
+
if (response.ok) {
|
346 |
+
addResult('ppt-results', '✅ PPT保存成功: ' + JSON.stringify(data), 'success');
|
347 |
+
} else {
|
348 |
+
addResult('ppt-results', '❌ PPT保存失败: ' + (data.error || '未知错误'), 'error');
|
349 |
+
}
|
350 |
+
} catch (error) {
|
351 |
+
addResult('ppt-results', '❌ PPT保存请求失败: ' + error.message, 'error');
|
352 |
+
}
|
353 |
+
}
|
354 |
+
|
355 |
+
async function testGenerateShareLink() {
|
356 |
+
if (!testPptId) {
|
357 |
+
addResult('share-results', '❌ 请先创建并保存PPT', 'error');
|
358 |
+
return;
|
359 |
+
}
|
360 |
+
|
361 |
+
try {
|
362 |
+
const response = await fetch('/api/public/generate-share-link', {
|
363 |
+
method: 'POST',
|
364 |
+
headers: { 'Content-Type': 'application/json' },
|
365 |
+
body: JSON.stringify({ userId: 'PS01', pptId: testPptId, slideIndex: 0 })
|
366 |
+
});
|
367 |
+
|
368 |
+
const data = await response.json();
|
369 |
+
|
370 |
+
if (response.ok) {
|
371 |
+
addResult('share-results', '✅ 分享链接生成成功: ' + JSON.stringify(data, null, 2), 'success');
|
372 |
+
} else {
|
373 |
+
addResult('share-results', '❌ 分享链接生成失败: ' + (data.error || '未知错误'), 'error');
|
374 |
+
}
|
375 |
+
} catch (error) {
|
376 |
+
addResult('share-results', '❌ 分享链接生成请求失败: ' + error.message, 'error');
|
377 |
+
}
|
378 |
+
}
|
379 |
+
|
380 |
+
// 初始化
|
381 |
+
updateTokenDisplay();
|
382 |
+
</script>
|
383 |
+
</body>
|
384 |
+
</html>
|
385 |
+
`);
|
386 |
});
|
387 |
|
388 |
// API路由
|