Spaces:
Sleeping
Sleeping
yu commited on
Commit ·
386b316
1
Parent(s): 6921575
refactor: 优化删除功能和代码质量
Browse files- 提取 get_account_id() 辅助函数消除重复逻辑
- 简化 delete_account() 函数,从19行减少到10行
- 添加账户不存在的错误检查
- 统一前端页面刷新机制 refreshPage()
- 统一前端 API 错误处理 handleApiResponse()
- saveConfig 和 deleteAccount 使用统一的错误处理和刷新逻辑
- core/templates.py +30 -15
- main.py +13 -14
core/templates.py
CHANGED
|
@@ -692,6 +692,27 @@ def generate_admin_html(request: Request, multi_account_mgr, show_hide_tip: bool
|
|
| 692 |
<script>
|
| 693 |
let currentConfig = null;
|
| 694 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
async function showEditConfig() {{
|
| 696 |
const config = await fetch('/{main.PATH_PREFIX}/admin/accounts-config?key={main.ADMIN_KEY}').then(r => r.json());
|
| 697 |
currentConfig = config.accounts;
|
|
@@ -746,15 +767,12 @@ def generate_admin_html(request: Request, multi_account_mgr, show_hide_tip: bool
|
|
| 746 |
body: JSON.stringify(data)
|
| 747 |
}});
|
| 748 |
|
| 749 |
-
const result = await
|
| 750 |
-
|
| 751 |
-
|
| 752 |
-
|
| 753 |
-
location.reload();
|
| 754 |
-
}} else {{
|
| 755 |
-
throw new Error(result.detail || '更新失败');
|
| 756 |
-
}}
|
| 757 |
}} catch (error) {{
|
|
|
|
| 758 |
alert('更新失败: ' + error.message);
|
| 759 |
}}
|
| 760 |
}}
|
|
@@ -767,14 +785,11 @@ def generate_admin_html(request: Request, multi_account_mgr, show_hide_tip: bool
|
|
| 767 |
method: 'DELETE'
|
| 768 |
}});
|
| 769 |
|
| 770 |
-
const result = await
|
| 771 |
-
|
| 772 |
-
|
| 773 |
-
location.reload();
|
| 774 |
-
}} else {{
|
| 775 |
-
throw new Error(result.detail || '删除失败');
|
| 776 |
-
}}
|
| 777 |
}} catch (error) {{
|
|
|
|
| 778 |
alert('删除失败: ' + error.message);
|
| 779 |
}}
|
| 780 |
}}
|
|
|
|
| 692 |
<script>
|
| 693 |
let currentConfig = null;
|
| 694 |
|
| 695 |
+
// 统一的页面刷新函数(避免缓存)
|
| 696 |
+
function refreshPage() {{
|
| 697 |
+
window.location.href = window.location.pathname + '?t=' + Date.now();
|
| 698 |
+
}}
|
| 699 |
+
|
| 700 |
+
// 统一的错误处理函数
|
| 701 |
+
async function handleApiResponse(response) {{
|
| 702 |
+
if (!response.ok) {{
|
| 703 |
+
const errorText = await response.text();
|
| 704 |
+
let errorMsg;
|
| 705 |
+
try {{
|
| 706 |
+
const errorJson = JSON.parse(errorText);
|
| 707 |
+
errorMsg = errorJson.detail || errorJson.message || errorText;
|
| 708 |
+
}} catch {{
|
| 709 |
+
errorMsg = errorText;
|
| 710 |
+
}}
|
| 711 |
+
throw new Error(`HTTP ${{response.status}}: ${{errorMsg}}`);
|
| 712 |
+
}}
|
| 713 |
+
return await response.json();
|
| 714 |
+
}}
|
| 715 |
+
|
| 716 |
async function showEditConfig() {{
|
| 717 |
const config = await fetch('/{main.PATH_PREFIX}/admin/accounts-config?key={main.ADMIN_KEY}').then(r => r.json());
|
| 718 |
currentConfig = config.accounts;
|
|
|
|
| 767 |
body: JSON.stringify(data)
|
| 768 |
}});
|
| 769 |
|
| 770 |
+
const result = await handleApiResponse(response);
|
| 771 |
+
alert(`配置已更新!\\n当前账户数: ${{result.account_count}}`);
|
| 772 |
+
closeModal();
|
| 773 |
+
setTimeout(refreshPage, 1000);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 774 |
}} catch (error) {{
|
| 775 |
+
console.error('保存失败:', error);
|
| 776 |
alert('更新失败: ' + error.message);
|
| 777 |
}}
|
| 778 |
}}
|
|
|
|
| 785 |
method: 'DELETE'
|
| 786 |
}});
|
| 787 |
|
| 788 |
+
const result = await handleApiResponse(response);
|
| 789 |
+
alert(`账户已删除!\\n剩余账户数: ${{result.account_count}}`);
|
| 790 |
+
refreshPage();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 791 |
}} catch (error) {{
|
| 792 |
+
console.error('删除失败:', error);
|
| 793 |
alert('删除失败: ' + error.message);
|
| 794 |
}}
|
| 795 |
}}
|
main.py
CHANGED
|
@@ -457,7 +457,7 @@ def load_multi_account_config() -> MultiAccountManager:
|
|
| 457 |
raise ValueError(f"账户 {i} 缺少必需字段: {', '.join(missing_fields)}")
|
| 458 |
|
| 459 |
config = AccountConfig(
|
| 460 |
-
account_id=
|
| 461 |
secure_c_ses=acc["secure_c_ses"],
|
| 462 |
host_c_oses=acc.get("host_c_oses"),
|
| 463 |
csesidx=acc["csesidx"],
|
|
@@ -489,6 +489,10 @@ def reload_accounts():
|
|
| 489 |
multi_account_mgr = load_multi_account_config()
|
| 490 |
logger.info(f"[CONFIG] 配置已重载,当前账户数: {len(multi_account_mgr.accounts)}")
|
| 491 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 492 |
def update_accounts_config(accounts_data: list):
|
| 493 |
"""更新账户配置(保存到文件并重新加载)"""
|
| 494 |
save_accounts_to_file(accounts_data)
|
|
@@ -498,19 +502,14 @@ def delete_account(account_id: str):
|
|
| 498 |
"""删除单个账户"""
|
| 499 |
accounts_data = load_accounts_from_source()
|
| 500 |
|
| 501 |
-
#
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
acc_id = acc.get("secure_c_ses", f"account_{i}")
|
| 510 |
-
|
| 511 |
-
# 同时检查 account_id 是否匹配 ID 或 secure_c_ses
|
| 512 |
-
if acc_id != account_id and acc.get("secure_c_ses") != account_id:
|
| 513 |
-
filtered.append(acc)
|
| 514 |
|
| 515 |
save_accounts_to_file(filtered)
|
| 516 |
reload_accounts()
|
|
|
|
| 457 |
raise ValueError(f"账户 {i} 缺少必需字段: {', '.join(missing_fields)}")
|
| 458 |
|
| 459 |
config = AccountConfig(
|
| 460 |
+
account_id=get_account_id(acc, i),
|
| 461 |
secure_c_ses=acc["secure_c_ses"],
|
| 462 |
host_c_oses=acc.get("host_c_oses"),
|
| 463 |
csesidx=acc["csesidx"],
|
|
|
|
| 489 |
multi_account_mgr = load_multi_account_config()
|
| 490 |
logger.info(f"[CONFIG] 配置已重载,当前账户数: {len(multi_account_mgr.accounts)}")
|
| 491 |
|
| 492 |
+
def get_account_id(acc: dict, index: int) -> str:
|
| 493 |
+
"""获取账户ID(有显式ID则使用,否则生成默认ID)"""
|
| 494 |
+
return acc.get("id", f"account_{index}")
|
| 495 |
+
|
| 496 |
def update_accounts_config(accounts_data: list):
|
| 497 |
"""更新账户配置(保存到文件并重新加载)"""
|
| 498 |
save_accounts_to_file(accounts_data)
|
|
|
|
| 502 |
"""删除单个账户"""
|
| 503 |
accounts_data = load_accounts_from_source()
|
| 504 |
|
| 505 |
+
# 过滤掉要删除的账户
|
| 506 |
+
filtered = [
|
| 507 |
+
acc for i, acc in enumerate(accounts_data, 1)
|
| 508 |
+
if get_account_id(acc, i) != account_id
|
| 509 |
+
]
|
| 510 |
+
|
| 511 |
+
if len(filtered) == len(accounts_data):
|
| 512 |
+
raise ValueError(f"账户 {account_id} 不存在")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 513 |
|
| 514 |
save_accounts_to_file(filtered)
|
| 515 |
reload_accounts()
|