mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-25 00:23:53 +08:00
增加通过认证码绑定设备和管理账户的功能,需要开启私有设备配置
增加登录用户鉴权
This commit is contained in:
+107
-32
@@ -4,18 +4,22 @@ import logging
|
||||
from aiohttp import web
|
||||
from core.utils.util import get_project_dir
|
||||
from config.private_config import PrivateConfig
|
||||
from manager.api.user_manager import UserManager # 添加导入
|
||||
from core.utils.auth_code_gen import AuthCodeGenerator # 添加导入
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class ConfigHandler:
|
||||
def __init__(self):
|
||||
def __init__(self, session_manager):
|
||||
self.session_manager = session_manager
|
||||
self.user_manager = UserManager() # 添加 user_manager 实例
|
||||
self.private_config_path = get_project_dir() + 'data/.private_config.yaml'
|
||||
self.config_path = get_project_dir() + 'config.yaml'
|
||||
# 如果存在.config.yaml文件,则使用该文件
|
||||
if os.path.exists(get_project_dir() + "data/.config.yaml"):
|
||||
self.config_path = get_project_dir() + "data/.config.yaml"
|
||||
with open(self.config_path, 'r', encoding='utf-8') as f:
|
||||
self.config = yaml.safe_load(f)
|
||||
self.config = yaml.safe_load(f)
|
||||
|
||||
async def get_module_options(self, request):
|
||||
"""Get all available module options from config.yaml"""
|
||||
@@ -45,46 +49,41 @@ class ConfigHandler:
|
||||
})
|
||||
|
||||
async def get_private_configs(self, request):
|
||||
"""获取所有私有配置设备列表及其配置"""
|
||||
"""只返回用户绑定的设备配置"""
|
||||
try:
|
||||
username = request['username']
|
||||
logger.info(f"Getting devices for user: {username}")
|
||||
|
||||
# 从用户管理器获取用户的设备列表
|
||||
user_devices = await self.user_manager.get_user_devices(username)
|
||||
logger.info(f"User {username} has devices: {user_devices}")
|
||||
|
||||
# 读取所有配置
|
||||
all_configs = {}
|
||||
if os.path.exists(self.private_config_path):
|
||||
with open(self.private_config_path, 'r', encoding='utf-8') as f:
|
||||
all_configs = yaml.safe_load(f) or {}
|
||||
else:
|
||||
all_configs = {}
|
||||
|
||||
# 转换配置为前端友好的格式
|
||||
devices = []
|
||||
for device_id, config in all_configs.items():
|
||||
device_info = {
|
||||
'id': device_id,
|
||||
'config': {
|
||||
'selected_module': config.get('selected_module', {}),
|
||||
'prompt': config.get('prompt', ''),
|
||||
'last_chat_time': config.get('last_chat_time', ''),
|
||||
'nickname': config.get('nickname', '小智'),
|
||||
'modules': {
|
||||
'LLM': config.get('LLM', {}),
|
||||
'TTS': config.get('TTS', {}),
|
||||
'ASR': config.get('ASR', {}),
|
||||
'VAD': config.get('VAD', {})
|
||||
}
|
||||
}
|
||||
}
|
||||
devices.append(device_info)
|
||||
|
||||
|
||||
# 只返回用户有权限的设备配置
|
||||
user_configs = {
|
||||
device_id: config
|
||||
for device_id, config in all_configs.items()
|
||||
if device_id in user_devices
|
||||
}
|
||||
|
||||
logger.info(f"Returning {len(user_configs)} device configs for user {username}")
|
||||
return web.json_response({
|
||||
'success': True,
|
||||
'data': devices,
|
||||
'data': user_configs,
|
||||
'message': '获取成功'
|
||||
})
|
||||
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error getting private configs: {str(e)}", exc_info=True)
|
||||
logger.error(f"Error getting devices for user {request.get('username')}: {str(e)}", exc_info=True)
|
||||
return web.json_response({
|
||||
'success': False,
|
||||
'message': '获取配置失败'
|
||||
})
|
||||
'message': f'获取设备列表失败: {str(e)}'
|
||||
}, status=400)
|
||||
|
||||
async def save_device_config(self, request):
|
||||
"""保存单个设备的配置"""
|
||||
@@ -92,6 +91,16 @@ class ConfigHandler:
|
||||
data = await request.json()
|
||||
device_id = data.get('id')
|
||||
config = data.get('config')
|
||||
username = request['username'] # 从请求中获取用户名
|
||||
|
||||
# 检查设备所有权
|
||||
user_devices = self.user_manager.get_user_devices(username)
|
||||
if device_id not in user_devices:
|
||||
return web.json_response({
|
||||
'success': False,
|
||||
'message': '无权操作此设备'
|
||||
}, status=403)
|
||||
|
||||
logger.info(f"Device config updated: {device_id} :\n{config}")
|
||||
if not device_id or not config:
|
||||
return web.json_response({
|
||||
@@ -130,10 +139,20 @@ class ConfigHandler:
|
||||
try:
|
||||
data = await request.json()
|
||||
device_id = data.get('device_id')
|
||||
|
||||
username = request['username']
|
||||
|
||||
# 检查设备所有权
|
||||
user_devices = await self.user_manager.get_user_devices(username)
|
||||
if device_id not in user_devices:
|
||||
return web.json_response({
|
||||
'success': False,
|
||||
'message': '无权删除此设备'
|
||||
}, status=403)
|
||||
|
||||
# 使用PrivateConfig处理配置删除
|
||||
private_config = PrivateConfig(device_id, self.config)
|
||||
success = await private_config.delete_config()
|
||||
await self.user_manager.remove_device(username, device_id)
|
||||
|
||||
if not success:
|
||||
raise Exception("Failed to delete device config")
|
||||
@@ -149,3 +168,59 @@ class ConfigHandler:
|
||||
'success': False,
|
||||
'message': f'删除配置失败: {str(e)}'
|
||||
})
|
||||
|
||||
async def bind_device(self, request):
|
||||
"""绑定设备到用户"""
|
||||
try:
|
||||
data = await request.json()
|
||||
auth_code = data.get('auth_code')
|
||||
username = request['username']
|
||||
|
||||
if not auth_code or len(auth_code) != 6:
|
||||
return web.json_response({
|
||||
'success': False,
|
||||
'message': '请输入6位认证码'
|
||||
}, status=400)
|
||||
|
||||
# 读取所有设备配置
|
||||
with open(self.private_config_path, 'r', encoding='utf-8') as f:
|
||||
all_configs = yaml.safe_load(f) or {}
|
||||
|
||||
# 查找匹配认证码的设备
|
||||
device_found = None
|
||||
for device_id, config in all_configs.items():
|
||||
if config.get('auth_code') == auth_code and not config.get('owner'):
|
||||
device_found = device_id
|
||||
break
|
||||
|
||||
if not device_found:
|
||||
return web.json_response({
|
||||
'success': False,
|
||||
'message': '认证码无效或设备已被绑定'
|
||||
}, status=400)
|
||||
|
||||
# 使用 PrivateConfig 进行绑定
|
||||
private_config = PrivateConfig(device_found, self.config, AuthCodeGenerator())
|
||||
await private_config.load_or_create()
|
||||
|
||||
# 绑定设备到用户 - 修改为异步调用
|
||||
success = await private_config.bind_user(username)
|
||||
if success:
|
||||
# 同时更新用户的设备列表 - 修改为异步调用
|
||||
await self.user_manager.add_device(username, device_found)
|
||||
return web.json_response({
|
||||
'success': True,
|
||||
'message': '设备绑定成功'
|
||||
})
|
||||
else:
|
||||
return web.json_response({
|
||||
'success': False,
|
||||
'message': '设备绑定失败'
|
||||
}, status=500)
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error binding device: {str(e)}", exc_info=True)
|
||||
return web.json_response({
|
||||
'success': False,
|
||||
'message': f'绑定设备失败: {str(e)}'
|
||||
}, status=500)
|
||||
|
||||
Reference in New Issue
Block a user