/** * API密钥管理器 - 密钥删除模块 * 包含API密钥的删除功能 */ // 删除API密钥 function deleteApiKey(id, name) { this.deleteKeyId = id; this.deleteKeyName = name; this.showDeleteConfirm = true; } // 确认删除(单个或批量) async function confirmDelete() { if (this.isBulkDelete) { if (this.selectedKeys.length === 0) return; this.isDeleting = true; try { const response = await fetch('/api/keys/bulk-delete', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ ids: this.selectedKeys }), }); const data = await response.json(); if (data.success) { // 关闭模态框,清空选中数组 this.showDeleteConfirm = false; this.isBulkDelete = false; const deletedCount = data.deleted_count || this.selectedKeys.length; // 清空选中数组 this.selectedKeys = []; this.selectedPlatforms = []; // 使用Toast风格的通知提示 const Toast = Swal.mixin({ toast: true, position: 'top-end', showConfirmButton: false, timer: 1500, timerProgressBar: true, didOpen: (toast) => { toast.onmouseenter = Swal.stopTimer; toast.onmouseleave = Swal.resumeTimer; } }); // 重新加载API密钥数据而不刷新页面 this.loadApiKeys(); Toast.fire({ icon: 'success', title: `成功删除 ${deletedCount} 个API密钥`, background: '#fee2e2', iconColor: '#ef4444' }); } else { Swal.fire({ icon: 'error', title: '批量删除失败', text: data.error || '删除操作未能完成,请重试', confirmButtonColor: '#0284c7' }); } } catch (error) { console.error('批量删除API密钥失败:', error); Swal.fire({ icon: 'error', title: '服务器错误', text: '无法完成删除操作,请稍后重试', confirmButtonColor: '#0284c7' }); } finally { this.isDeleting = false; } } else { // 单个删除逻辑 if (!this.deleteKeyId) return; this.isDeleting = true; try { const response = await fetch(`/api/keys/${this.deleteKeyId}`, { method: 'DELETE', }); const data = await response.json(); if (data.success) { // 从本地数组中移除 (创建新数组) this.apiKeys = [...this.apiKeys.filter(key => key.id !== this.deleteKeyId)]; // 关闭模态框 this.showDeleteConfirm = false; // 使用Toast风格的通知提示 const Toast = Swal.mixin({ toast: true, position: 'top-end', showConfirmButton: false, timer: 1500, timerProgressBar: true, didOpen: (toast) => { toast.onmouseenter = Swal.stopTimer; toast.onmouseleave = Swal.resumeTimer; } }); // 重新加载API密钥数据而不刷新页面 this.loadApiKeys(); Toast.fire({ icon: 'success', title: 'API密钥已删除', background: '#fee2e2', iconColor: '#ef4444' }); } else { Swal.fire({ icon: 'error', title: '删除失败', text: data.message || '删除操作未能完成,请重试', confirmButtonColor: '#0284c7' }); } } catch (error) { console.error('删除API密钥失败:', error); Swal.fire({ icon: 'error', title: '服务器错误', text: '无法完成删除操作,请稍后重试', confirmButtonColor: '#0284c7' }); } finally { this.isDeleting = false; } } }