@@ -1436,6 +1436,7 @@ def show_api_documentation(self):
14361436 def on_lib_change (self ):
14371437 """处理库选择变更"""
14381438 selected_lib = self .lib_var .get ()
1439+ self .add_log (f"库选择变更: { selected_lib } " )
14391440
14401441 # 检查所选库是否已安装
14411442 if selected_lib == "wxauto" and not self .check_wxauto_status ():
@@ -1450,14 +1451,38 @@ def on_lib_change(self):
14501451
14511452 try :
14521453 # 加载当前配置
1454+ self .add_log (f"正在加载配置文件..." )
14531455 config = config_manager .load_app_config ()
1456+ self .add_log (f"当前配置: { config } " )
14541457
14551458 # 更新库配置
1459+ old_lib = config .get ('wechat_lib' , 'wxauto' )
14561460 config ['wechat_lib' ] = selected_lib
1461+ self .add_log (f"更新库配置: { old_lib } -> { selected_lib } " )
14571462
14581463 # 保存配置
1464+ self .add_log (f"正在保存配置文件..." )
14591465 config_manager .save_app_config (config )
14601466
1467+ # 验证配置是否成功保存
1468+ try :
1469+ new_config = config_manager .load_app_config ()
1470+ saved_lib = new_config .get ('wechat_lib' , 'wxauto' )
1471+ if saved_lib != selected_lib :
1472+ self .add_log (f"警告:配置保存后验证失败,期望值: { selected_lib } ,实际值: { saved_lib } " )
1473+ messagebox .showwarning ("配置验证" , f"配置可能未正确保存,请检查配置文件权限" )
1474+ else :
1475+ self .add_log (f"配置保存成功并验证通过: { saved_lib } " )
1476+ except Exception as ve :
1477+ self .add_log (f"配置验证失败: { str (ve )} " )
1478+
1479+ # 同时更新.env文件中的配置(作为备份)
1480+ try :
1481+ self .update_env_file ('WECHAT_LIB' , selected_lib )
1482+ self .add_log (f"已更新.env文件中的WECHAT_LIB配置" )
1483+ except Exception as env_e :
1484+ self .add_log (f"更新.env文件失败: { str (env_e )} " )
1485+
14611486 # 标记配置已修改
14621487 global CONFIG_MODIFIED
14631488 CONFIG_MODIFIED = True
@@ -1475,6 +1500,31 @@ def on_lib_change(self):
14751500 if self .api_running :
14761501 messagebox .showinfo ("需要重启" , "库已切换,需要重启服务才能生效" )
14771502
1503+ def update_env_file (self , key , value ):
1504+ """更新.env文件中的配置"""
1505+ env_file = ".env"
1506+ lines = []
1507+ key_found = False
1508+
1509+ # 读取现有内容
1510+ if os .path .exists (env_file ):
1511+ with open (env_file , "r" , encoding = "utf-8" ) as f :
1512+ lines = f .readlines ()
1513+
1514+ # 更新或添加配置
1515+ for i , line in enumerate (lines ):
1516+ if line .strip ().startswith (f"{ key } =" ):
1517+ lines [i ] = f"{ key } ={ value } \n "
1518+ key_found = True
1519+ break
1520+
1521+ if not key_found :
1522+ lines .append (f"{ key } ={ value } \n " )
1523+
1524+ # 写回文件
1525+ with open (env_file , "w" , encoding = "utf-8" ) as f :
1526+ f .writelines (lines )
1527+
14781528 # 这些方法已被移除,配置现在通过插件配置对话框进行管理
14791529
14801530 def start_api_service (self ):
0 commit comments