mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
完善保存配置功能
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
</div>
|
||||
|
||||
<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('history')">历史对话</button>
|
||||
<div class="delete-container">
|
||||
@@ -81,6 +81,18 @@ const handleDelete = () => {
|
||||
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>
|
||||
|
||||
<style scoped>
|
||||
|
||||
@@ -110,7 +110,7 @@ import { ref, onMounted } from 'vue';
|
||||
import { useRouter, useRoute } from 'vue-router';
|
||||
import NavBar from './NavBar.vue';
|
||||
import RoleTemplates from '../utils/RoleTemplates';
|
||||
import { API_BASE_URL } from '../config/api';
|
||||
import apiClient from '../utils/api'; // 替换 API_BASE_URL 导入
|
||||
|
||||
const router = useRouter();
|
||||
const route = useRoute();
|
||||
@@ -125,7 +125,6 @@ const activeMemoryTab = ref('recent');
|
||||
const memoryContent = ref('');
|
||||
const selectedModel = ref('qianwen');
|
||||
|
||||
const baseUrl = API_BASE_URL;
|
||||
const moduleOptions = ref({
|
||||
LLM: [],
|
||||
TTS: [],
|
||||
@@ -170,15 +169,15 @@ const loadModuleOptions = async () => {
|
||||
|
||||
const refreshModuleOptions = async () => {
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/api/config/module-options`);
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
moduleOptions.value = data.data;
|
||||
const response = await apiClient.get('/api/config/module-options');
|
||||
if (response.data.success) {
|
||||
moduleOptions.value = response.data.data;
|
||||
// Update cache
|
||||
localStorage.setItem('moduleOptions', JSON.stringify(data.data));
|
||||
localStorage.setItem('moduleOptions', JSON.stringify(response.data.data));
|
||||
}
|
||||
} catch (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`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify(config)
|
||||
});
|
||||
const response = await apiClient.post('/api/config/save_device_config', config);
|
||||
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
if (response.data.success) {
|
||||
// Save the original description and nickname to local storage
|
||||
localStorage.setItem(`deviceConfig_${deviceId.value}`, JSON.stringify({
|
||||
selected_module: selectedModules.value,
|
||||
@@ -226,46 +218,57 @@ const saveConfig = async () => {
|
||||
}));
|
||||
alert('保存成功');
|
||||
} else {
|
||||
throw new Error(data.message || '保存失败');
|
||||
throw new Error(response.data.message || '保存失败');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error saving config:', error);
|
||||
alert(error.message || '保存失败');
|
||||
alert(error.response?.data?.message || '保存失败');
|
||||
}
|
||||
};
|
||||
|
||||
const loadDeviceConfig = async () => {
|
||||
try {
|
||||
// First try to get config from local storage
|
||||
// 首先尝试从 localStorage 获取配置
|
||||
const localConfig = localStorage.getItem(`deviceConfig_${deviceId.value}`);
|
||||
if (localConfig) {
|
||||
const config = JSON.parse(localConfig);
|
||||
selectedModules.value = config.selected_module || {};
|
||||
selectedModules.value = config.selected_module || {
|
||||
LLM: '',
|
||||
TTS: '',
|
||||
VAD: '',
|
||||
ASR: ''
|
||||
};
|
||||
roleDescription.value = config.prompt || '';
|
||||
nickname.value = config.nickname || '小智'; // Load saved nickname
|
||||
return;
|
||||
nickname.value = config.nickname || '小智';
|
||||
return; // 如果找到本地配置就直接返回
|
||||
}
|
||||
|
||||
// If no local config, fetch from server
|
||||
const response = await fetch(`${baseUrl}/api/config/devices`);
|
||||
const data = await response.json();
|
||||
if (data.success) {
|
||||
const deviceConfig = data.data.find(d => d.id === deviceId.value);
|
||||
if (deviceConfig) {
|
||||
selectedModules.value = deviceConfig.config.selected_module || {};
|
||||
// 如果没有本地配置,则从服务器获取
|
||||
const response = await apiClient.get('/api/config/devices');
|
||||
if (response.data.success && response.data.data) {
|
||||
const deviceConfig = response.data.data[deviceId.value];
|
||||
|
||||
if (deviceConfig && deviceConfig.config) {
|
||||
selectedModules.value = deviceConfig.config.selected_module || {
|
||||
LLM: '',
|
||||
TTS: '',
|
||||
VAD: '',
|
||||
ASR: ''
|
||||
};
|
||||
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({
|
||||
selected_module: deviceConfig.config.selected_module,
|
||||
prompt: deviceConfig.config.prompt,
|
||||
nickname: deviceConfig.config.nickname
|
||||
selected_module: selectedModules.value,
|
||||
prompt: roleDescription.value,
|
||||
nickname: nickname.value
|
||||
}));
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error loading device config:', error);
|
||||
alert(error.response?.data?.message || '加载设备配置失败');
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -163,6 +163,14 @@ const formatLastActivity = (timestamp) => {
|
||||
};
|
||||
|
||||
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}`);
|
||||
};
|
||||
|
||||
|
||||
@@ -94,7 +94,7 @@ class ConfigHandler:
|
||||
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:
|
||||
return web.json_response({
|
||||
'success': False,
|
||||
|
||||
Reference in New Issue
Block a user