File size: 5,070 Bytes
ef8784d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**

 * 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;
        }
    }
}