diff --git a/src/ui-manager.js b/src/ui-manager.js index f1213fb..2f7276a 100644 --- a/src/ui-manager.js +++ b/src/ui-manager.js @@ -478,6 +478,46 @@ export async function handleUIApiRequests(method, pathParam, req, res, currentCo return true; } + // Update admin password + if (method === 'POST' && pathParam === '/api/admin-password') { + try { + const body = await getRequestBody(req); + const { password } = body; + + if (!password || password.trim() === '') { + res.writeHead(400, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + error: { + message: '密码不能为空' + } + })); + return true; + } + + // 写入密码到 pwd 文件 + const pwdFilePath = path.join(process.cwd(), 'pwd'); + await fs.writeFile(pwdFilePath, password.trim(), 'utf8'); + + console.log('[UI API] Admin password updated successfully'); + + res.writeHead(200, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + success: true, + message: '后台登录密码已更新' + })); + return true; + } catch (error) { + console.error('[UI API] Failed to update admin password:', error); + res.writeHead(500, { 'Content-Type': 'application/json' }); + res.end(JSON.stringify({ + error: { + message: '更新密码失败: ' + error.message + } + })); + return true; + } + } + // Get configuration if (method === 'GET' && pathParam === '/api/config') { let systemPrompt = ''; diff --git a/static/app/config-manager.js b/static/app/config-manager.js index d7214d7..27a9fd2 100644 --- a/static/app/config-manager.js +++ b/static/app/config-manager.js @@ -133,6 +133,9 @@ async function saveConfiguration() { systemPrompt: document.getElementById('systemPrompt')?.value || '', }; + // 获取后台登录密码(如果有输入) + const adminPassword = document.getElementById('adminPassword')?.value || ''; + // 根据不同提供商保存不同的配置 const provider = document.getElementById('modelProvider')?.value; @@ -194,6 +197,21 @@ async function saveConfiguration() { try { await window.apiClient.post('/config', config); + + // 如果输入了新密码,单独保存密码 + if (adminPassword) { + try { + await window.apiClient.post('/admin-password', { password: adminPassword }); + // 清空密码输入框 + const adminPasswordEl = document.getElementById('adminPassword'); + if (adminPasswordEl) adminPasswordEl.value = ''; + showToast('后台密码已更新,下次登录生效', 'success'); + } catch (pwdError) { + console.error('Failed to save admin password:', pwdError); + showToast('保存后台密码失败: ' + pwdError.message, 'error'); + } + } + await window.apiClient.post('/reload-config'); showToast('配置已保存', 'success'); diff --git a/static/index.html b/static/index.html index 6a11099..ce62cbf 100644 --- a/static/index.html +++ b/static/index.html @@ -711,6 +711,18 @@ + + +