|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function addApiKey() {
|
|
if (!this.newKey.platform || !this.newKey.key) {
|
|
this.errorMessage = '请填写所有必填字段。';
|
|
return;
|
|
}
|
|
|
|
|
|
if (!this.newKey.name.trim()) {
|
|
const date = new Date();
|
|
const dateStr = date.toLocaleDateString('zh-CN', {
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit'
|
|
}).replace(/\//g, '-');
|
|
const timeStr = date.toLocaleTimeString('zh-CN', {
|
|
hour: '2-digit',
|
|
minute: '2-digit'
|
|
});
|
|
this.newKey.name = `${dateStr} ${timeStr}`;
|
|
}
|
|
|
|
|
|
localStorage.setItem('lastSelectedPlatform', this.newKey.platform);
|
|
|
|
this.isSubmitting = true;
|
|
this.errorMessage = '';
|
|
|
|
try {
|
|
|
|
const lines = this.newKey.key
|
|
.split('\n')
|
|
.map(line => line.replace(/['"\(\)\[\]\s]/g, ''))
|
|
.filter(line => line.length > 0);
|
|
|
|
|
|
let keysWithDuplicates = [];
|
|
for (const line of lines) {
|
|
const lineKeys = line.split(',')
|
|
.filter(item => item.length > 0);
|
|
|
|
|
|
keysWithDuplicates.push(...lineKeys);
|
|
}
|
|
|
|
if (keysWithDuplicates.length === 0) {
|
|
this.errorMessage = '请输入至少一个有效的API密钥。';
|
|
this.isSubmitting = false;
|
|
return;
|
|
}
|
|
|
|
|
|
const inputDuplicatesCount = keysWithDuplicates.length - new Set(keysWithDuplicates).size;
|
|
const keys = [...new Set(keysWithDuplicates)];
|
|
|
|
|
|
const currentPlatform = this.newKey.platform;
|
|
const existingKeys = this.apiKeys
|
|
.filter(apiKey => apiKey.platform === currentPlatform)
|
|
.map(apiKey => apiKey.key);
|
|
|
|
const uniqueKeys = keys.filter(key => !existingKeys.includes(key));
|
|
const duplicateCount = keys.length - uniqueKeys.length;
|
|
|
|
|
|
if (uniqueKeys.length === 0) {
|
|
this.errorMessage = '所有输入的API密钥在当前平台中已存在。';
|
|
this.isSubmitting = false;
|
|
return;
|
|
}
|
|
|
|
|
|
const keysData = uniqueKeys.map(keyText => ({
|
|
platform: this.newKey.platform,
|
|
name: this.newKey.name,
|
|
key: keyText
|
|
}));
|
|
|
|
|
|
const skippedCount = duplicateCount;
|
|
const addedCount = uniqueKeys.length;
|
|
|
|
|
|
const response = await fetch('/api/keys/bulk-add', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ keys: keysData }),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
|
|
this.showAddModal = false;
|
|
this.newKey = {
|
|
platform: this.newKey.platform,
|
|
name: '',
|
|
key: ''
|
|
};
|
|
|
|
|
|
const Toast = Swal.mixin({
|
|
toast: true,
|
|
position: 'top-end',
|
|
showConfirmButton: false,
|
|
timer: 2500,
|
|
timerProgressBar: true,
|
|
didOpen: (toast) => {
|
|
toast.onmouseenter = Swal.stopTimer;
|
|
toast.onmouseleave = Swal.resumeTimer;
|
|
}
|
|
});
|
|
|
|
|
|
this.loadApiKeys();
|
|
|
|
|
|
if (data.keys && data.keys.length > 0) {
|
|
|
|
setTimeout(() => {
|
|
data.keys.forEach(key => {
|
|
console.log(`正在请求更新API密钥: ${key.id}`);
|
|
fetch(`/api/keys/update/${key.id}`, {
|
|
method: 'POST'
|
|
}).then(response => {
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
return response.json();
|
|
}).then(data => {
|
|
console.log(`API密钥 ${key.id} 更新成功:`, data);
|
|
|
|
this.loadApiKeys();
|
|
}).catch(error => {
|
|
console.error(`自动更新API密钥 ${key.id} 时出错:`, error);
|
|
});
|
|
});
|
|
}, 200);
|
|
}
|
|
|
|
|
|
let title = `已添加 ${addedCount} 个API密钥`;
|
|
|
|
|
|
if (inputDuplicatesCount > 0 && skippedCount > 0) {
|
|
|
|
title += `,跳过 ${inputDuplicatesCount} 个输入重复和 ${skippedCount} 个已存在密钥`;
|
|
} else if (inputDuplicatesCount > 0) {
|
|
|
|
title += `,跳过 ${inputDuplicatesCount} 个输入重复密钥`;
|
|
} else if (skippedCount > 0) {
|
|
|
|
title += `,跳过 ${skippedCount} 个已存在密钥`;
|
|
}
|
|
|
|
Toast.fire({
|
|
icon: 'success',
|
|
title: title,
|
|
background: '#f0fdf4',
|
|
iconColor: '#16a34a'
|
|
});
|
|
} else {
|
|
|
|
this.errorMessage = data.error || `添加操作失败: ${data.message || '未知错误'}`;
|
|
}
|
|
} catch (error) {
|
|
console.error('添加API密钥失败:', error);
|
|
this.errorMessage = '服务器错误,请重试。';
|
|
} finally {
|
|
this.isSubmitting = false;
|
|
}
|
|
}
|
|
|