feat: 实现API密钥的单独和批量删除功能

本次更新引入了删除API密钥的功能,包括前端界面和后端逻辑。

主要变更:

- **API路由 (`app/router/config_routes.py`):**
    - 添加了新的API端点 `/keys/{key_to_delete}` 用于删除单个密钥。
    - 添加了新的API端点 `/keys/delete-selected` 用于批量删除选定的密钥。
    - 增加了对请求体 `DeleteKeysRequest` 的Pydantic模型定义。
    - 在删除操作前进行身份验证。

- **配置服务 (`app/service/config/config_service.py`):**
    - 实现了 `delete_key` 方法来处理单个密钥的删除逻辑。
    - 实现了 `delete_selected_keys` 方法来处理批量密钥的删除逻辑。
    - 确保在删除操作后更新配置。

- **密钥管理器 (`app/service/key/key_manager.py`):**
    - 更新了 `remove_key` 方法,以确保从活动密钥列表中正确移除密钥。
    - 改进了 `reset_instance` 方法,在重置时保留下一个密钥提示(`_preserved_next_key_in_cycle`),以防止在配置重载后立即丢失轮换状态。

- **前端JavaScript (`app/static/js/keys_status.js`):**
    - 添加了 `showSingleKeyDeleteConfirmModal` 函数,用于显示单个密钥删除的确认模态框。
    - 添加了 `executeSingleKeyDelete` 函数,用于执行单个密钥的删除请求。
    - 添加了 `showDeleteConfirmationModal` 函数,用于显示批量删除密钥的确认模态框。
    - 添加了 `executeDeleteSelectedKeys` 函数,用于执行批量删除密钥的请求。
    - 更新了UI交互,包括按钮状态(加载中、禁用)和结果通知。

- **HTML模板 (`app/templates/keys_status.html`):**
    - 为有效密钥和无效密钥列表中的每个密钥添加了“删除”按钮。
    - 为有效密钥和无效密钥列表添加了“批量删除”按钮。
    - 添加了用于单个密钥删除和批量删除的确认模态框HTML结构。
    - 调整了现有模态框的样式,以提高视觉一致性。

这些更改增强了密钥管理功能,允许用户更灵活地管理其API密钥。
This commit is contained in:
snaily
2025-05-08 21:58:26 +08:00
parent f1f568afca
commit 920228d3aa
5 changed files with 635 additions and 19 deletions
+150 -5
View File
@@ -802,6 +802,13 @@ endblock %} {% block head_extra_styles %}
>
<i class="fas fa-copy"></i> 批量复制
</button>
<button
class="flex items-center gap-2 bg-red-800 hover:bg-red-900 text-white px-2.5 py-1 rounded-lg text-xs font-medium transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed"
onclick="event.stopPropagation(); showDeleteConfirmationModal('valid', event)"
disabled
>
<i class="fas fa-trash-alt"></i> 批量删除
</button>
</div>
<div class="key-content p-4 bg-white bg-opacity-40">
@@ -882,6 +889,13 @@ endblock %} {% block head_extra_styles %}
<i class="fas fa-chart-pie"></i>
详情
</button>
<button
class="flex items-center gap-1 bg-red-800 hover:bg-red-900 text-white px-2.5 py-1 rounded-lg text-xs font-medium transition-all duration-200"
onclick="showSingleKeyDeleteConfirmModal('{{ key }}', this)"
>
<i class="fas fa-trash-alt"></i>
删除
</button>
</div>
</div>
</div>
@@ -972,6 +986,13 @@ endblock %} {% block head_extra_styles %}
>
<i class="fas fa-copy"></i> 批量复制
</button>
<button
class="flex items-center gap-2 bg-red-800 hover:bg-red-900 text-white px-2.5 py-1 rounded-lg text-xs font-medium transition-all duration-200 disabled:opacity-50 disabled:cursor-not-allowed"
onclick="event.stopPropagation(); showDeleteConfirmationModal('invalid', event)"
disabled
>
<i class="fas fa-trash-alt"></i> 批量删除
</button>
</div>
<div class="key-content p-4 bg-white bg-opacity-40">
@@ -1050,6 +1071,13 @@ endblock %} {% block head_extra_styles %}
<i class="fas fa-chart-pie"></i>
详情
</button>
<button
class="flex items-center gap-1 bg-red-800 hover:bg-red-900 text-white px-2.5 py-1 rounded-lg text-xs font-medium transition-all duration-200"
onclick="showSingleKeyDeleteConfirmModal('{{ key }}', this)"
>
<i class="fas fa-trash-alt"></i>
删除
</button>
</div>
</div>
</div>
@@ -1131,31 +1159,44 @@ endblock %} {% block head_extra_styles %}
>
<div
class="bg-white rounded-lg p-6 shadow-xl max-w-md w-full animate-fade-in"
style="
background-color: rgba(70, 50, 150, 0.95);
color: #ffffff;
border-color: rgba(120, 100, 200, 0.4);
"
>
<div class="flex items-center justify-between mb-4">
<h3 class="text-lg font-semibold text-gray-800" id="verifyModalTitle">
<h3
class="text-lg font-semibold"
id="verifyModalTitle"
style="
color: #ffffff;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
font-weight: 600;
"
>
批量验证密钥
</h3>
<button
onclick="closeVerifyModal()"
class="text-gray-500 hover:text-gray-700 focus:outline-none"
class="text-gray-300 hover:text-white focus:outline-none"
>
<i class="fas fa-times"></i>
</button>
</div>
<div class="mb-6">
<p class="text-gray-600" id="verifyModalMessage"></p>
<p style="color: #f8fafc" id="verifyModalMessage"></p>
</div>
<div class="flex justify-end gap-3">
<button
onclick="closeVerifyModal()"
class="px-3 py-1.5 text-xs font-medium bg-slate-500 hover:bg-slate-600 text-white rounded-lg transition-colors"
class="px-3 py-1.5 text-xs font-medium bg-slate-600 hover:bg-slate-700 text-white rounded-lg transition-colors"
>
取消
</button>
<button
id="confirmVerifyBtn"
class="px-3 py-1.5 text-xs font-medium bg-teal-600 hover:bg-teal-700 text-white rounded-lg transition-colors"
class="px-3 py-1.5 text-xs font-medium bg-teal-700 hover:bg-teal-800 text-white rounded-lg transition-colors"
>
确认验证
</button>
@@ -1163,6 +1204,110 @@ endblock %} {% block head_extra_styles %}
</div>
</div>
<!-- 删除确认模态框 -->
<div
id="deleteConfirmModal"
class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden"
>
<div
class="bg-white rounded-lg p-6 shadow-xl max-w-md w-full animate-fade-in"
style="
background-color: rgba(70, 50, 150, 0.95);
color: #ffffff;
border-color: rgba(120, 100, 200, 0.4);
"
>
<div class="flex items-center justify-between mb-4">
<h3
class="text-lg font-semibold"
id="deleteConfirmModalTitle"
style="
color: #ffffff;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
font-weight: 600;
"
>
确认删除
</h3>
<button
onclick="closeDeleteConfirmationModal()"
class="text-gray-300 hover:text-white focus:outline-none"
>
<i class="fas fa-times"></i>
</button>
</div>
<div class="mb-6">
<p style="color: #f8fafc" id="deleteConfirmModalMessage"></p>
</div>
<div class="flex justify-end gap-3">
<button
onclick="closeDeleteConfirmationModal()"
class="px-3 py-1.5 text-xs font-medium bg-slate-600 hover:bg-slate-700 text-white rounded-lg transition-colors"
>
取消
</button>
<button
id="confirmDeleteBtn"
class="px-3 py-1.5 text-xs font-medium bg-red-700 hover:bg-red-800 text-white rounded-lg transition-colors"
>
确认删除
</button>
</div>
</div>
</div>
<!-- 新增:单个密钥删除确认模态框 -->
<div
id="singleKeyDeleteConfirmModal"
class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden"
>
<div
class="rounded-lg p-6 shadow-xl max-w-md w-full animate-fade-in"
style="
background-color: rgba(70, 50, 150, 0.95);
color: #ffffff;
border-color: rgba(120, 100, 200, 0.4);
"
>
<div class="flex items-center justify-between mb-4">
<h3
class="text-lg font-semibold"
id="singleKeyDeleteConfirmModalTitle"
style="
color: #ffffff;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
font-weight: 600;
"
>
确认删除密钥
</h3>
<button
onclick="closeSingleKeyDeleteConfirmModal()"
class="text-gray-300 hover:text-white focus:outline-none"
>
<i class="fas fa-times"></i>
</button>
</div>
<div class="mb-6">
<p style="color: #f8fafc" id="singleKeyDeleteConfirmModalMessage"></p>
</div>
<div class="flex justify-end gap-3">
<button
onclick="closeSingleKeyDeleteConfirmModal()"
class="px-3 py-1.5 text-xs font-medium bg-slate-600 hover:bg-slate-700 text-white rounded-lg transition-colors"
>
取消
</button>
<button
id="confirmSingleKeyDeleteBtn"
class="px-3 py-1.5 text-xs font-medium bg-red-700 hover:bg-red-800 text-white rounded-lg transition-colors"
>
确认删除
</button>
</div>
</div>
</div>
<!-- 操作结果模态框 -->
<div
id="resultModal"