diff --git a/src/integrations/config_manager.py b/src/integrations/config_manager.py index b7efbce..3e14968 100644 --- a/src/integrations/config_manager.py +++ b/src/integrations/config_manager.py @@ -44,16 +44,74 @@ class ConfigManager: return { "app_id": feishu.app_id or "", "app_secret": feishu.app_secret or "", + "app_token": feishu.app_token or "", "table_id": feishu.table_id or "", "verification_token": feishu.verification_token or "", "encrypt_key": feishu.encrypt_key or "", - "status": "active" if (feishu.app_id and feishu.app_secret) else "inactive" + "status": "active" if (feishu.app_id and feishu.app_secret and feishu.app_token and feishu.table_id) else "inactive" } def update_feishu_config(self, **kwargs) -> bool: - """更新飞书配置(仅用于兼容性,实际应修改 .env 文件)""" - logger.warning("飞书配置应该在 .env 文件中修改,而不是通过 API 更新") - return False + """更新飞书配置并同步到 .env 文件""" + try: + import os + from src.config.unified_config import reload_config + + env_path = os.path.join(os.getcwd(), '.env') + env_content = "" + + # 读取现有内容 + if os.path.exists(env_path): + with open(env_path, 'r', encoding='utf-8') as f: + lines = f.readlines() + else: + lines = [] + + # 准备更新的映射 + mapping = { + 'app_id': 'FEISHU_APP_ID', + 'app_secret': 'FEISHU_APP_SECRET', + 'app_token': 'FEISHU_APP_TOKEN', + 'table_id': 'FEISHU_TABLE_ID', + 'verification_token': 'FEISHU_VERIFICATION_TOKEN', + 'encrypt_key': 'FEISHU_ENCRYPT_KEY' + } + + updates = {mapping[k]: v for k, v in kwargs.items() if k in mapping and v is not None} + + # 更新或添加行 + new_lines = [] + seen_keys = set() + + for line in lines: + key_part = line.split('=')[0].strip() + if key_part in updates: + new_lines.append(f"{key_part}={updates[key_part]}\n") + seen_keys.add(key_part) + else: + new_lines.append(line) + + # 添加原文件中不存在的键 + for key, value in updates.items(): + if key not in seen_keys: + if new_lines and not new_lines[-1].endswith('\n'): + new_lines.append('\n') + new_lines.append(f"{key}={value}\n") + + # 写回文件 + with open(env_path, 'w', encoding='utf-8') as f: + f.writelines(new_lines) + + # 重新加载全局配置 + reload_config() + # 更新本地引用的统一配置实例 + self._unified_config = get_config() + + logger.info("飞书配置已成功更新并保存到 .env 文件") + return True + except Exception as e: + logger.error(f"更新飞书配置失败: {e}") + return False def get_system_config(self) -> Dict[str, Any]: """获取系统配置"""