@@ -1259,6 +1259,41 @@ def collect_account_entries() -> List[dict]:
12591259
12601260 return entries
12611261
1262+ def is_current_account_saved (current_record_key : str ) -> bool :
1263+ """检查当前账号是否已经存在于存档中"""
1264+ if not current_record_key :
1265+ return False
1266+
1267+ accounts_dir = get_accounts_dir ()
1268+ if not accounts_dir .exists ():
1269+ return False
1270+
1271+ for auth_file in sorted (accounts_dir .glob ('auth_*.json' )):
1272+ auth_data = load_auth_data_from_path (auth_file )
1273+ if not auth_data :
1274+ continue
1275+ info = get_account_info (auth_data , str (auth_file ))
1276+ if info and info .get ('record_key' ) == current_record_key :
1277+ return True
1278+ return False
1279+
1280+ def ensure_current_account_saved () -> bool :
1281+ """自动存档当前未存档账号"""
1282+ auth_data = load_current_auth ()
1283+ if not auth_data :
1284+ return False
1285+
1286+ info = get_account_info (auth_data , str (get_auth_file ()))
1287+ if not info :
1288+ return False
1289+
1290+ record_key = info .get ('record_key' , '' )
1291+ if is_current_account_saved (record_key ):
1292+ return True
1293+
1294+ save_name = info .get ('email' , 'account' )
1295+ return save_current_auth (save_name )
1296+
12621297def clone_display_info (info : dict , entry : dict ) -> dict :
12631298 """克隆用于展示的账号信息"""
12641299 display = dict (info )
@@ -1384,6 +1419,7 @@ def build_view_all_rows(
13841419
13851420def load_live_account_rows (show_progress : bool = False ) -> List [dict ]:
13861421 """加载并实时刷新账号列表"""
1422+ ensure_current_account_saved ()
13871423 entries = collect_account_entries ()
13881424 refresh_jobs = build_refresh_jobs (entries )
13891425 results = refresh_jobs_live (refresh_jobs , show_progress = show_progress )
@@ -1397,13 +1433,22 @@ def get_remaining_percent(acc: dict, window: str) -> int:
13971433 return - 1
13981434 return max (0 , 100 - int (used_percent ))
13991435
1436+ def get_remaining_count (acc : dict , window : str ) -> int :
1437+ """获取指定窗口的剩余数量"""
1438+ key = 'hourly_remaining' if window == 'hourly' else 'weekly_remaining'
1439+ value = acc .get (key , '' )
1440+ try :
1441+ return int (value )
1442+ except (TypeError , ValueError ):
1443+ return - 1
1444+
14001445def sort_accounts_for_agent (rows : List [dict ]) -> List [dict ]:
1401- """按 5 小时余量、每周余量排序账号 """
1446+ """按 5 小时剩余数量、每周剩余数量排序账号 """
14021447 return sorted (
14031448 rows ,
14041449 key = lambda acc : (
1405- - get_remaining_percent (acc , 'hourly' ),
1406- - get_remaining_percent (acc , 'weekly' ),
1450+ - get_remaining_count (acc , 'hourly' ),
1451+ - get_remaining_count (acc , 'weekly' ),
14071452 str (acc .get ('email' , '' )),
14081453 ),
14091454 )
@@ -1464,12 +1509,8 @@ def resolve_account_selector(rows: List[dict], selector: str) -> Optional[dict]:
14641509
14651510def print_view_all_actions (rows : List [dict ]):
14661511 """打印查看余量页面底部操作"""
1467- current_unsaved = any (row .get ('is_current' ) and not row .get ('is_saved' ) for row in rows )
1468-
14691512 print (f"{ Colors .BOLD } 操作面板{ Colors .ENDC } " )
14701513 print (f"{ Colors .DIM } { '─' * 40 } { Colors .ENDC } " )
1471- if current_unsaved :
1472- print (f" { Colors .CYAN } [S]{ Colors .ENDC } 存档当前账号" )
14731514 print (f" { Colors .CYAN } [编号]{ Colors .ENDC } 切换账号" )
14741515 print (f" { Colors .CYAN } [0]{ Colors .ENDC } 退出工具" )
14751516 print (f" { Colors .DIM } [Enter]{ Colors .ENDC } 刷新当前页面" )
@@ -1481,7 +1522,10 @@ def view_all_accounts():
14811522 clear_screen ()
14821523 print_header ()
14831524 print (f"\n { Colors .CYAN } >>> 查看所有账号余量{ Colors .ENDC } " )
1484- rows = load_live_account_rows (show_progress = True )
1525+ rows = sort_accounts_for_agent (load_live_account_rows (show_progress = True ))
1526+ clear_screen ()
1527+ print_header ()
1528+ print (f"\n { Colors .CYAN } >>> 查看所有账号余量{ Colors .ENDC } " )
14851529 if rows :
14861530 print_accounts_table (rows , "账号列表" )
14871531 else :
@@ -1502,27 +1546,10 @@ def view_all_accounts():
15021546 if choice == '0' :
15031547 return
15041548
1505- current_row = next ((row for row in rows if row .get ('is_current' )), None )
1506- current_unsaved = bool (current_row and not current_row .get ('is_saved' ))
1507-
1508- if choice .lower () == 's' :
1509- if not current_unsaved :
1510- print (f"\n { Colors .YELLOW } 当前账号已存档,无需重复保存{ Colors .ENDC } " )
1511- input (f"{ Colors .DIM } 按回车键继续...{ Colors .ENDC } " )
1512- continue
1513-
1514- save_name = current_row .get ('email' , 'account' )
1515- if save_current_auth (save_name ):
1516- print (f"\n { Colors .GREEN } ✓ 当前账号已存档: { save_name } { Colors .ENDC } " )
1517- else :
1518- print (f"\n { Colors .RED } ✗ 当前账号存档失败{ Colors .ENDC } " )
1519- input (f"{ Colors .DIM } 按回车键继续...{ Colors .ENDC } " )
1520- continue
1521-
15221549 try :
15231550 idx = int (choice ) - 1
15241551 except ValueError :
1525- print (f"\n { Colors .RED } 请输入编号、S 或直接回车{ Colors .ENDC } " )
1552+ print (f"\n { Colors .RED } 请输入编号、0 或直接回车{ Colors .ENDC } " )
15261553 input (f"{ Colors .DIM } 按回车键继续...{ Colors .ENDC } " )
15271554 continue
15281555
0 commit comments