yangtb24's picture
Upload 70 files
2809c18 verified
/**
* API密钥管理器 - 核心模块
* 包含数据状态定义和初始化功能
*/
function initApiKeyManager() {
// 初始化各平台的折叠状态和筛选状态
const platforms = JSON.parse(platformsData);
// 初始化平台ID列表
this.platformIds = platforms.map(platform => platform.id);
// 从localStorage读取当前视图状态,如果有的话
const savedView = localStorage.getItem('keyView');
if (savedView === 'valid' || savedView === 'invalid') {
this.currentView = savedView;
}
// 从localStorage读取对应视图的平台展开状态,如果有的话
const stateKey = `platformStates_${this.currentView}`;
const savedPlatformStates = localStorage.getItem(stateKey);
const parsedPlatformStates = savedPlatformStates ? JSON.parse(savedPlatformStates) : {};
// 从localStorage读取平台筛选状态,如果有的话
const savedPlatformFilters = localStorage.getItem('platformFilters');
const parsedPlatformFilters = savedPlatformFilters ? JSON.parse(savedPlatformFilters) : {};
// 初始化平台状态,优先使用保存的状态
platforms.forEach(platform => {
this.platformStates[platform.id] = parsedPlatformStates[platform.id] !== undefined
? parsedPlatformStates[platform.id]
: true;
this.platformFilters[platform.id] = parsedPlatformFilters[platform.id] !== undefined
? parsedPlatformFilters[platform.id]
: true;
});
// 检查是否所有平台都被选中
this.allPlatformsSelected = platforms.every(platform =>
this.platformFilters[platform.id] === true
);
// 加载API密钥
this.loadApiKeys();
// 初始化剪贴板JS
new ClipboardJS('[data-clipboard-text]');
// 设置平台类型为上次选择的值(如果有)
const lastPlatform = localStorage.getItem('lastSelectedPlatform');
if (lastPlatform) {
this.newKey.platform = lastPlatform;
}
// 这里不再恢复搜索词状态,只保留平台状态和页面位置
// 监听来自快捷键的打开模态框事件
window.addEventListener('open-add-modal', () => {
this.showAddModal = true;
// 聚焦到平台选择框,方便用户立即操作
setTimeout(() => {
document.getElementById('platform').focus();
}, 100);
});
// 监听模态框内的键盘事件
document.addEventListener('keydown', (e) => {
// Alt+Enter 提交表单
if (e.altKey && e.key === 'Enter') {
if (this.showAddModal) {
e.preventDefault();
this.addApiKey();
} else if (this.showEditModal) {
e.preventDefault();
this.updateApiKey();
}
}
// Esc 关闭模态框
if (e.key === 'Escape') {
if (this.showAddModal) {
this.showAddModal = false;
} else if (this.showEditModal) {
this.showEditModal = false;
}
}
});
}