mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-28 01:53:53 +08:00
完善保存配置功能
This commit is contained in:
@@ -16,7 +16,7 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="device-actions">
|
<div class="device-actions">
|
||||||
<button class="action-btn primary" @click="$emit('configure')">配置角色</button>
|
<button class="action-btn primary" @click="handleConfigure">配置角色</button>
|
||||||
<button class="action-btn" @click="$emit('voiceprint')">声纹识别</button>
|
<button class="action-btn" @click="$emit('voiceprint')">声纹识别</button>
|
||||||
<button class="action-btn" @click="$emit('history')">历史对话</button>
|
<button class="action-btn" @click="$emit('history')">历史对话</button>
|
||||||
<div class="delete-container">
|
<div class="delete-container">
|
||||||
@@ -81,6 +81,18 @@ const handleDelete = () => {
|
|||||||
emit('delete');
|
emit('delete');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleConfigure = () => {
|
||||||
|
// 保存设备配置到 localStorage,确保 RoleSetting 可以访问
|
||||||
|
if (props.deviceId && props.deviceConfig) {
|
||||||
|
localStorage.setItem(`deviceConfig_${props.deviceId}`, JSON.stringify({
|
||||||
|
selected_module: props.selectedModules,
|
||||||
|
prompt: props.deviceConfig.prompt || '',
|
||||||
|
nickname: props.deviceConfig.nickname || '小智'
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
emit('configure');
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ import { ref, onMounted } from 'vue';
|
|||||||
import { useRouter, useRoute } from 'vue-router';
|
import { useRouter, useRoute } from 'vue-router';
|
||||||
import NavBar from './NavBar.vue';
|
import NavBar from './NavBar.vue';
|
||||||
import RoleTemplates from '../utils/RoleTemplates';
|
import RoleTemplates from '../utils/RoleTemplates';
|
||||||
import { API_BASE_URL } from '../config/api';
|
import apiClient from '../utils/api'; // 替换 API_BASE_URL 导入
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const route = useRoute();
|
const route = useRoute();
|
||||||
@@ -125,7 +125,6 @@ const activeMemoryTab = ref('recent');
|
|||||||
const memoryContent = ref('');
|
const memoryContent = ref('');
|
||||||
const selectedModel = ref('qianwen');
|
const selectedModel = ref('qianwen');
|
||||||
|
|
||||||
const baseUrl = API_BASE_URL;
|
|
||||||
const moduleOptions = ref({
|
const moduleOptions = ref({
|
||||||
LLM: [],
|
LLM: [],
|
||||||
TTS: [],
|
TTS: [],
|
||||||
@@ -170,15 +169,15 @@ const loadModuleOptions = async () => {
|
|||||||
|
|
||||||
const refreshModuleOptions = async () => {
|
const refreshModuleOptions = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${baseUrl}/api/config/module-options`);
|
const response = await apiClient.get('/api/config/module-options');
|
||||||
const data = await response.json();
|
if (response.data.success) {
|
||||||
if (data.success) {
|
moduleOptions.value = response.data.data;
|
||||||
moduleOptions.value = data.data;
|
|
||||||
// Update cache
|
// Update cache
|
||||||
localStorage.setItem('moduleOptions', JSON.stringify(data.data));
|
localStorage.setItem('moduleOptions', JSON.stringify(response.data.data));
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error refreshing module options:', error);
|
console.error('Error refreshing module options:', error);
|
||||||
|
alert(error.response?.data?.message || '刷新配置选项失败');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -208,16 +207,9 @@ const saveConfig = async () => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await fetch(`${baseUrl}/api/config/save_device_config`, {
|
const response = await apiClient.post('/api/config/save_device_config', config);
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify(config)
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
if (response.data.success) {
|
||||||
if (data.success) {
|
|
||||||
// Save the original description and nickname to local storage
|
// Save the original description and nickname to local storage
|
||||||
localStorage.setItem(`deviceConfig_${deviceId.value}`, JSON.stringify({
|
localStorage.setItem(`deviceConfig_${deviceId.value}`, JSON.stringify({
|
||||||
selected_module: selectedModules.value,
|
selected_module: selectedModules.value,
|
||||||
@@ -226,46 +218,57 @@ const saveConfig = async () => {
|
|||||||
}));
|
}));
|
||||||
alert('保存成功');
|
alert('保存成功');
|
||||||
} else {
|
} else {
|
||||||
throw new Error(data.message || '保存失败');
|
throw new Error(response.data.message || '保存失败');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error saving config:', error);
|
console.error('Error saving config:', error);
|
||||||
alert(error.message || '保存失败');
|
alert(error.response?.data?.message || '保存失败');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const loadDeviceConfig = async () => {
|
const loadDeviceConfig = async () => {
|
||||||
try {
|
try {
|
||||||
// First try to get config from local storage
|
// 首先尝试从 localStorage 获取配置
|
||||||
const localConfig = localStorage.getItem(`deviceConfig_${deviceId.value}`);
|
const localConfig = localStorage.getItem(`deviceConfig_${deviceId.value}`);
|
||||||
if (localConfig) {
|
if (localConfig) {
|
||||||
const config = JSON.parse(localConfig);
|
const config = JSON.parse(localConfig);
|
||||||
selectedModules.value = config.selected_module || {};
|
selectedModules.value = config.selected_module || {
|
||||||
|
LLM: '',
|
||||||
|
TTS: '',
|
||||||
|
VAD: '',
|
||||||
|
ASR: ''
|
||||||
|
};
|
||||||
roleDescription.value = config.prompt || '';
|
roleDescription.value = config.prompt || '';
|
||||||
nickname.value = config.nickname || '小智'; // Load saved nickname
|
nickname.value = config.nickname || '小智';
|
||||||
return;
|
return; // 如果找到本地配置就直接返回
|
||||||
}
|
}
|
||||||
|
|
||||||
// If no local config, fetch from server
|
// 如果没有本地配置,则从服务器获取
|
||||||
const response = await fetch(`${baseUrl}/api/config/devices`);
|
const response = await apiClient.get('/api/config/devices');
|
||||||
const data = await response.json();
|
if (response.data.success && response.data.data) {
|
||||||
if (data.success) {
|
const deviceConfig = response.data.data[deviceId.value];
|
||||||
const deviceConfig = data.data.find(d => d.id === deviceId.value);
|
|
||||||
if (deviceConfig) {
|
if (deviceConfig && deviceConfig.config) {
|
||||||
selectedModules.value = deviceConfig.config.selected_module || {};
|
selectedModules.value = deviceConfig.config.selected_module || {
|
||||||
|
LLM: '',
|
||||||
|
TTS: '',
|
||||||
|
VAD: '',
|
||||||
|
ASR: ''
|
||||||
|
};
|
||||||
roleDescription.value = deviceConfig.config.prompt || '';
|
roleDescription.value = deviceConfig.config.prompt || '';
|
||||||
nickname.value = deviceConfig.config.nickname || '小智'; // Load nickname from server
|
nickname.value = deviceConfig.config.nickname || '小智';
|
||||||
|
|
||||||
// Save to local storage
|
// 保存到 localStorage
|
||||||
localStorage.setItem(`deviceConfig_${deviceId.value}`, JSON.stringify({
|
localStorage.setItem(`deviceConfig_${deviceId.value}`, JSON.stringify({
|
||||||
selected_module: deviceConfig.config.selected_module,
|
selected_module: selectedModules.value,
|
||||||
prompt: deviceConfig.config.prompt,
|
prompt: roleDescription.value,
|
||||||
nickname: deviceConfig.config.nickname
|
nickname: nickname.value
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error loading device config:', error);
|
console.error('Error loading device config:', error);
|
||||||
|
alert(error.response?.data?.message || '加载设备配置失败');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -163,6 +163,14 @@ const formatLastActivity = (timestamp) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleRoleConfig = (device) => {
|
const handleRoleConfig = (device) => {
|
||||||
|
// 在跳转前保存完整的设备配置到 localStorage
|
||||||
|
localStorage.setItem(`deviceConfig_${device.id}`, JSON.stringify({
|
||||||
|
selected_module: device.config.selected_module || {},
|
||||||
|
prompt: device.config.prompt || '',
|
||||||
|
nickname: device.config.nickname || '小智'
|
||||||
|
}));
|
||||||
|
|
||||||
|
// 跳转到角色配置页面
|
||||||
router.push(`/role-setting/${device.id}`);
|
router.push(`/role-setting/${device.id}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ class ConfigHandler:
|
|||||||
username = request['username'] # 从请求中获取用户名
|
username = request['username'] # 从请求中获取用户名
|
||||||
|
|
||||||
# 检查设备所有权
|
# 检查设备所有权
|
||||||
user_devices = self.user_manager.get_user_devices(username)
|
user_devices = await self.user_manager.get_user_devices(username)
|
||||||
if device_id not in user_devices:
|
if device_id not in user_devices:
|
||||||
return web.json_response({
|
return web.json_response({
|
||||||
'success': False,
|
'success': False,
|
||||||
|
|||||||
Reference in New Issue
Block a user