mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-27 01:23:55 +08:00
update: 添加主页路由和提示词配置界面
This commit is contained in:
@@ -12,10 +12,69 @@ class ConfigServer:
|
|||||||
self.setup_routes()
|
self.setup_routes()
|
||||||
|
|
||||||
def setup_routes(self):
|
def setup_routes(self):
|
||||||
|
self.app.router.add_get('/', self.index)
|
||||||
self.app.router.add_get('/api/prompt', self.get_prompt)
|
self.app.router.add_get('/api/prompt', self.get_prompt)
|
||||||
self.app.router.add_post('/api/prompt', self.update_prompt)
|
self.app.router.add_post('/api/prompt', self.update_prompt)
|
||||||
self.app.router.add_options('/api/prompt', self.handle_options)
|
self.app.router.add_options('/api/prompt', self.handle_options)
|
||||||
|
|
||||||
|
async def index(self, request):
|
||||||
|
html = '''
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>小智提示词配置</title>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<style>
|
||||||
|
body { max-width: 800px; margin: 20px auto; padding: 0 20px; font-family: Arial, sans-serif; }
|
||||||
|
textarea { width: 100%; height: 300px; margin: 20px 0; padding: 10px; }
|
||||||
|
button { padding: 10px 20px; font-size: 16px; }
|
||||||
|
.success { color: green; }
|
||||||
|
.error { color: red; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>小智提示词配置</h1>
|
||||||
|
<textarea id="prompt"></textarea>
|
||||||
|
<div>
|
||||||
|
<button onclick="updatePrompt()">更新提示词</button>
|
||||||
|
<span id="status"></span>
|
||||||
|
</div>
|
||||||
|
<script>
|
||||||
|
// 加载当前prompt
|
||||||
|
fetch('/api/prompt')
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
document.getElementById('prompt').value = data.prompt;
|
||||||
|
});
|
||||||
|
|
||||||
|
function updatePrompt() {
|
||||||
|
const prompt = document.getElementById('prompt').value;
|
||||||
|
fetch('/api/prompt', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({prompt: prompt})
|
||||||
|
})
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(data => {
|
||||||
|
const status = document.getElementById('status');
|
||||||
|
if(data.success) {
|
||||||
|
status.textContent = '更新成功!';
|
||||||
|
status.className = 'success';
|
||||||
|
} else {
|
||||||
|
status.textContent = '更新失败: ' + data.error;
|
||||||
|
status.className = 'error';
|
||||||
|
}
|
||||||
|
setTimeout(() => status.textContent = '', 3000);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
'''
|
||||||
|
return web.Response(text=html, content_type='text/html')
|
||||||
|
|
||||||
async def handle_options(self, request):
|
async def handle_options(self, request):
|
||||||
headers = {
|
headers = {
|
||||||
'Access-Control-Allow-Origin': '*',
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
|||||||
Reference in New Issue
Block a user