pre / static /js /api-key-manager /bulk-actions.js
yangtb24's picture
Upload 64 files
834bfcd verified
/**
* API密钥管理器 - 批量操作模块
* 包含批量选择、删除和复制等功能
*/
// 切换单个密钥的选择状态
function toggleKeySelection(keyId) {
// 检查密钥是否符合当前视图(有效/无效)
const key = this.apiKeys.find(k => k.id === keyId);
// 如果密钥不存在或者密钥状态与当前视图不符,不进行任何操作
if (!key || (this.currentView === 'valid' && key.success !== true) ||
(this.currentView === 'invalid' && key.success !== false)) {
return;
}
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.currentView === 'valid' && key.success === true) ||
(this.currentView === 'invalid' && key.success === false))) {
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 &&
((this.currentView === 'valid' && key.success === true) ||
(this.currentView === 'invalid' && key.success === false))
);
const allSelected = platformKeys.length > 0 &&
platformKeys.every(key => this.selectedKeys.includes(key.id));
if (allSelected) {
this.selectedPlatforms.push(platform.id);
}
});
}
// 获取所有可见密钥ID
function getAllVisibleKeyIds() {
// 基于当前视图状态和JavaScript筛选而不是仅依赖DOM
const visibleIds = this.apiKeys
.filter(key => {
// 应用当前视图筛选
if (this.currentView === 'valid' && key.success !== true) return false;
if (this.currentView === 'invalid' && key.success !== false) return false;
// 应用平台筛选
if (!this.platformFilters[key.platform]) return false;
// 应用搜索筛选
if (this.searchTerm && !this.matchesSearch(key.name, key.key)) return false;
return true;
})
.map(key => key.id);
return visibleIds;
}
// 切换全选/取消全选
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'
});
}
}