pre / static /js /api-key-manager /main-manager.js
yangtb24's picture
Upload 70 files
2809c18 verified
// API密钥管理器主模块
// 定义API密钥管理器的主函数
function apiKeyManager() {
return {
// 数据状态
apiKeys: [],
platformStates: {},
platformFilters: {}, // 平台筛选状态
platformIds: [], // 所有平台ID列表
allPlatformsSelected: true, // 是否选择所有平台
currentView: 'valid', // 当前视图:'valid'(有效)或'invalid'(无效)
searchTerm: '',
showAddModal: false,
showDeleteConfirm: false,
isSubmitting: false,
isDeleting: false,
isLoading: true, // 添加加载状态
errorMessage: '',
copiedId: null,
deleteKeyId: null,
deleteKeyName: '',
selectedKeys: [], // 批量选择的密钥ID数组
selectedPlatforms: [], // 批量选择的平台ID数组
isBulkDelete: false, // 是否为批量删除操作
newKey: {
platform: '',
name: '',
key: ''
},
showEditModal: false,
editKey: {
id: '',
name: '',
key: '',
platform: ''
},
// 生命周期钩子
init() {
// 调用核心模块的初始化函数
initApiKeyManager.call(this);
},
// 加载API密钥
loadApiKeys() {
return loadApiKeys.apply(this, arguments);
},
// 添加API密钥
addApiKey() {
return addApiKey.apply(this, arguments);
},
// 删除API密钥
deleteApiKey(id, name) {
return deleteApiKey.apply(this, arguments);
},
// 切换单个密钥的选择状态
toggleKeySelection(keyId) {
return toggleKeySelection.apply(this, arguments);
},
// 切换平台的选择状态
togglePlatformSelection(platformId) {
return togglePlatformSelection.apply(this, arguments);
},
// 更新平台选择状态(基于已选择的密钥)
updatePlatformSelectionStates() {
return updatePlatformSelectionStates.apply(this, arguments);
},
// 获取所有可见密钥ID
getAllVisibleKeyIds() {
return getAllVisibleKeyIds.apply(this, arguments);
},
// 判断是否全部选中的计算属性
get isAllSelected() {
// 获取所有可见密钥的ID
const allVisibleKeyIds = this.getAllVisibleKeyIds();
// 过滤出只属于选中平台的密钥ID
const filteredIds = allVisibleKeyIds.filter(id => {
const key = this.apiKeys.find(k => k.id === id);
return key && this.platformFilters[key.platform] === true;
});
// 全选需要满足:有属于选中平台的可见密钥,且这些密钥都被选中
return filteredIds.length > 0 &&
filteredIds.every(id => this.selectedKeys.includes(id));
},
// 切换全选/取消全选
toggleSelectAll() {
return toggleSelectAll.apply(this, arguments);
},
// 批量删除API密钥
bulkDeleteApiKeys() {
return bulkDeleteApiKeys.apply(this, arguments);
},
// 批量复制API密钥
bulkCopyApiKeys() {
return bulkCopyApiKeys.apply(this, arguments);
},
// 确认删除(单个或批量)
confirmDelete() {
return confirmDelete.apply(this, arguments);
},
// 复制到剪贴板
copyToClipboard(text, id) {
return copyToClipboard.apply(this, arguments);
},
// 切换平台折叠状态
togglePlatform(platformId) {
return togglePlatform.apply(this, arguments);
},
// 获取平台API密钥数量
getPlatformKeyCount(platformId, filtered = false) {
return getPlatformKeyCount.apply(this, arguments);
},
// 平台是否有API密钥
hasPlatformKeys(platformId) {
return hasPlatformKeys.apply(this, arguments);
},
// 平台是否应该显示(基于筛选条件和当前视图)
isPlatformVisible(platformId) {
return isPlatformVisible.apply(this, arguments);
},
// 切换当前视图(有效/无效密钥)
switchView(view) {
if (this.currentView !== view) {
// 保存当前视图的平台展开状态
const currentStateKey = `platformStates_${this.currentView}`;
localStorage.setItem(currentStateKey, JSON.stringify(this.platformStates));
// 切换视图
this.currentView = view;
// 保存用户的视图选择到localStorage
localStorage.setItem('keyView', view);
// 加载新视图的平台展开状态
const newStateKey = `platformStates_${view}`;
const savedPlatformStates = localStorage.getItem(newStateKey);
if (savedPlatformStates) {
this.platformStates = JSON.parse(savedPlatformStates);
} else {
// 如果没有保存过这个视图的状态,使用默认值(全部展开)
const platforms = this.getPlatforms();
platforms.forEach(platform => {
this.platformStates[platform.id] = true;
});
}
// 重置选择状态
this.selectedKeys = [];
this.selectedPlatforms = [];
// 重新加载API密钥数据
this.loadApiKeys();
}
},
// 判断密钥是否在当前视图中可见
isKeyVisibleInCurrentView(key) {
if (this.currentView === 'valid') {
return key.success === true;
} else if (this.currentView === 'invalid') {
return key.success === false;
}
return true; // 默认都显示
},
// 获取所有平台
getPlatforms() {
return getPlatforms.apply(this, arguments);
},
// 切换平台筛选状态
togglePlatformFilter(platformId) {
return togglePlatformFilter.apply(this, arguments);
},
// 切换所有平台筛选状态
toggleAllPlatformFilters() {
return toggleAllPlatformFilters.apply(this, arguments);
},
// 搜索匹配
matchesSearch(name, key, notes) {
return matchesSearch.apply(this, arguments);
},
// 获取总API密钥数量
totalKeyCount() {
return totalKeyCount.apply(this, arguments);
},
// 打开编辑API密钥模态框
editApiKey(id, name, key) {
return editApiKey.apply(this, arguments);
},
// 更新API密钥
updateApiKey() {
return updateApiKey.apply(this, arguments);
},
// 显示通知
showNotification(message, type = 'info') {
return showNotification.apply(this, arguments);
}
};
}