File size: 6,513 Bytes
bbb6398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef8784d
 
 
 
 
 
bbb6398
 
ef8784d
bbb6398
ef8784d
bbb6398
 
 
ef8784d
bbb6398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**

 * API密钥管理器 - 批量操作模块

 * 包含批量选择、删除和复制等功能

 */

// 切换单个密钥的选择状态
function toggleKeySelection(keyId) {
    const index = this.selectedKeys.indexOf(keyId);
    if (index === -1) {
        // 添加到选中数组
        this.selectedKeys.push(keyId);
    } else {
        // 从选中数组中移除
        this.selectedKeys.splice(index, 1);
    }
    
    // 检查是否需要更新平台选择状态
    this.updatePlatformSelectionStates();
}

// 切换平台的选择状态
function togglePlatformSelection(platformId) {
    const index = this.selectedPlatforms.indexOf(platformId);
    
    if (index === -1) {
        // 添加到选中平台数组
        this.selectedPlatforms.push(platformId);
        
        // 选中该平台下的所有密钥
        this.apiKeys.forEach(key => {
            if (key.platform === platformId && !this.selectedKeys.includes(key.id)) {
                this.selectedKeys.push(key.id);
            }
        });
    } else {
        // 从选中平台数组中移除
        this.selectedPlatforms.splice(index, 1);
        
        // 取消选中该平台下的所有密钥
        this.selectedKeys = this.selectedKeys.filter(keyId => {
            const key = this.apiKeys.find(k => k.id === keyId);
            return key && key.platform !== platformId;
        });
    }
}

// 更新平台选择状态(基于已选择的密钥)
function updatePlatformSelectionStates() {
    const platforms = this.getPlatforms();
    
    // 清空当前选中的平台
    this.selectedPlatforms = [];
    
    // 对于每个平台,检查该平台下的所有密钥是否都被选中
    platforms.forEach(platform => {
        const platformKeys = this.apiKeys.filter(key => key.platform === platform.id);
        const allSelected = platformKeys.length > 0 && 
                            platformKeys.every(key => this.selectedKeys.includes(key.id));
        
        if (allSelected) {
            this.selectedPlatforms.push(platform.id);
        }
    });
}

// 获取所有可见密钥ID
function getAllVisibleKeyIds() {
    // 获取DOM中的所有可见密钥元素
    const keyElements = document.querySelectorAll('[data-key-id]');
    const keyIds = [];
    keyElements.forEach(el => {
        // 只收集可见元素的ID
        if (window.getComputedStyle(el).display !== 'none') {
            const keyId = el.getAttribute('data-key-id');
            if (keyId) keyIds.push(keyId);
        }
    });
    return keyIds;
}

// 切换全选/取消全选
function toggleSelectAll() {
    const allIds = this.getAllVisibleKeyIds();
    
    // 过滤出只属于选中平台的密钥ID
    const filteredIds = allIds.filter(id => {
        const key = this.apiKeys.find(k => k.id === id);
        return key && this.platformFilters[key.platform] === true;
    });
    
    if (this.isAllSelected) {
        // 如果当前是全选状态,则取消全选
        this.selectedKeys = this.selectedKeys.filter(id => !filteredIds.includes(id));
    } else {
        // 否则选中所有可见且属于选中平台的密钥
        // 先合并当前已选中的ID
        const newSelection = [...this.selectedKeys];
        // 添加所有未选中的可见ID
        filteredIds.forEach(id => {
            if (!newSelection.includes(id)) {
                newSelection.push(id);
            }
        });
        this.selectedKeys = newSelection;
    }
    
    // 更新平台选择状态
    this.updatePlatformSelectionStates();
}

// 批量删除API密钥
function bulkDeleteApiKeys() {
    if (this.selectedKeys.length === 0) return;
    
    // 设置批量删除标志
    this.isBulkDelete = true;
    
    // 显示确认对话框
    this.showDeleteConfirm = true;
}

// 批量复制API密钥
function bulkCopyApiKeys() {
    if (this.selectedKeys.length === 0) return;
    
    // 获取所有选中密钥的值
    const selectedKeyValues = [];
    this.selectedKeys.forEach(keyId => {
        // 查找对应的DOM元素
        const keyElement = document.querySelector(`[data-key-id="${keyId}"]`);
        if (keyElement) {
            // 获取密钥值 - 从DOM中提取
            const keyValueElement = keyElement.querySelector('.text-gray-500.font-mono');
            if (keyValueElement) {
                const keyValue = keyValueElement.textContent.trim();
                selectedKeyValues.push(keyValue);
            }
        }
    });
    
    // 如果成功提取了密钥值
    if (selectedKeyValues.length > 0) {
        // 将密钥值格式化为一行一个
        const formattedKeys = selectedKeyValues.join('\n');
        
        // 使用临时textarea元素复制文本
        const textarea = document.createElement('textarea');
        textarea.value = formattedKeys;
        textarea.style.position = 'fixed';
        document.body.appendChild(textarea);
        textarea.select();
        
        try {
            document.execCommand('copy');
            
            // 使用SweetAlert2显示复制成功动画
            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;
                }
            });
            
            Toast.fire({
                icon: 'success',
                title: `已成功复制 ${selectedKeyValues.length} 个密钥到剪贴板`,
                background: '#f0fdf4',
                iconColor: '#16a34a'
            });
        } catch (err) {
            console.error('复制失败:', err);
            Swal.fire({
                icon: 'error',
                title: '复制失败',
                text: '请手动复制内容',
                confirmButtonColor: '#0284c7'
            });
        } finally {
            document.body.removeChild(textarea);
        }
    } else {
        Swal.fire({
            icon: 'error',
            title: '复制失败',
            text: '无法获取所选密钥的值',
            confirmButtonColor: '#0284c7'
        });
    }
}