pre / static /js /api-key-manager /platform-utils.js
yangtb24's picture
Upload 70 files
2809c18 verified
/**
* API密钥管理器 - 平台工具模块
* 包含与平台相关的功能函数
*/
// 切换平台折叠状态
function togglePlatform(platformId) {
this.platformStates[platformId] = !this.platformStates[platformId];
// 保存展开状态到localStorage - 分开存储不同视图的状态
const stateKey = `platformStates_${this.currentView}`;
localStorage.setItem(stateKey, JSON.stringify(this.platformStates));
}
// 获取平台API密钥数量
function getPlatformKeyCount(platformId, filtered = false) {
return this.apiKeys.filter(key => {
// 基础过滤 - 匹配平台
if (key.platform !== platformId) return false;
// 视图过滤
if (this.currentView === 'valid' && key.success !== true) return false;
if (this.currentView === 'invalid' && key.success !== false) return false;
// 搜索过滤
if (filtered && this.searchTerm !== '' && !this.matchesSearch(key.name, key.key)) return false;
return true;
}).length;
}
// 获取平台有效API密钥数量
function getPlatformValidKeyCount(platformId) {
return this.apiKeys.filter(key =>
key.platform === platformId && key.success === true
).length;
}
// 获取平台无效API密钥数量
function getPlatformInvalidKeyCount(platformId) {
return this.apiKeys.filter(key =>
key.platform === platformId && key.success === false
).length;
}
// 平台是否有API密钥(考虑当前视图状态)
function hasPlatformKeys(platformId) {
return this.getPlatformKeyCount(platformId) > 0;
}
// 平台是否应该显示(基于筛选条件和当前视图状态)
function isPlatformVisible(platformId) {
// 首先检查平台是否在筛选条件中
if (this.platformFilters[platformId] !== true) {
return false;
}
// 然后检查当前视图下该平台是否有密钥
const hasKeysInCurrentView = this.apiKeys.some(key => {
if (key.platform !== platformId) return false;
if (this.currentView === 'valid' && key.success !== true) return false;
if (this.currentView === 'invalid' && key.success !== false) return false;
return true;
});
return hasKeysInCurrentView;
}
// 获取所有平台
function getPlatforms() {
return JSON.parse(platformsData);
}
// 获取所有平台样式配置
function getPlatformStyles() {
return JSON.parse(platformStylesData);
}
// 获取所有平台的ID
function getPlatformIds() {
return this.getPlatforms().map(platform => platform.id);
}
// 获取特定平台的样式
function getPlatformStyle(platformId) {
const styles = getPlatformStyles();
return styles[platformId] || {};
}
// 切换平台筛选状态
function togglePlatformFilter(platformId) {
this.platformFilters[platformId] = !this.platformFilters[platformId];
// 如果取消平台筛选,同时取消该平台及其下所有密钥的选择
if (this.platformFilters[platformId] === false) {
// 如果平台在选中列表中,移除它
const platformIndex = this.selectedPlatforms.indexOf(platformId);
if (platformIndex !== -1) {
this.selectedPlatforms.splice(platformIndex, 1);
}
// 取消选中该平台下的所有密钥
this.selectedKeys = this.selectedKeys.filter(keyId => {
const key = this.apiKeys.find(k => k.id === keyId);
return key && key.platform !== platformId;
});
}
// 检查是否所有平台都被选中
const platforms = this.getPlatforms();
this.allPlatformsSelected = platforms.every(platform =>
this.platformFilters[platform.id] === true
);
// 保存筛选状态到localStorage
localStorage.setItem('platformFilters', JSON.stringify(this.platformFilters));
}
// 切换所有平台筛选状态
function toggleAllPlatformFilters() {
const newState = !this.allPlatformsSelected;
this.allPlatformsSelected = newState;
// 将所有平台设置为相同的状态
const platforms = this.getPlatforms();
platforms.forEach(platform => {
this.platformFilters[platform.id] = newState;
});
// 如果取消全部平台筛选,同时取消所有平台和密钥的选择
if (newState === false) {
// 清空选中的平台
this.selectedPlatforms = [];
// 清空选中的密钥
this.selectedKeys = [];
}
// 保存筛选状态到localStorage
localStorage.setItem('platformFilters', JSON.stringify(this.platformFilters));
}