|
|
|
|
|
|
|
|
|
|
|
|
|
async function loadApiKeys() {
|
|
this.isLoading = true;
|
|
const startTime = Date.now();
|
|
try {
|
|
|
|
const response = await fetch('/?ajax=1');
|
|
const html = await response.text();
|
|
|
|
|
|
const tempContainer = document.createElement('div');
|
|
tempContainer.innerHTML = html;
|
|
|
|
|
|
const newKeyListHtml = tempContainer.querySelector('.space-y-6').outerHTML;
|
|
|
|
|
|
document.querySelector('.space-y-6').outerHTML = newKeyListHtml;
|
|
|
|
|
|
initScrollContainers();
|
|
|
|
|
|
const jsonResponse = await fetch('/api/keys');
|
|
const data = await jsonResponse.json();
|
|
this.apiKeys = [...(data.api_keys || [])];
|
|
|
|
|
|
this.selectedKeys = [];
|
|
this.selectedPlatforms = [];
|
|
|
|
|
|
const elapsedTime = Date.now() - startTime;
|
|
const minLoadTime = 200;
|
|
|
|
if (elapsedTime < minLoadTime) {
|
|
await new Promise(resolve => setTimeout(resolve, minLoadTime - elapsedTime));
|
|
}
|
|
} catch (error) {
|
|
console.error('加载API密钥失败:', error);
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: '加载失败',
|
|
text: '无法加载API密钥,请刷新页面重试',
|
|
confirmButtonColor: '#0284c7'
|
|
});
|
|
} finally {
|
|
this.isLoading = false;
|
|
}
|
|
}
|
|
|
|
|
|
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 results = [];
|
|
let allSuccess = true;
|
|
|
|
|
|
const skippedCount = duplicateCount;
|
|
const addedCount = uniqueKeys.length;
|
|
|
|
for (const keyText of uniqueKeys) {
|
|
const keyData = {
|
|
platform: this.newKey.platform,
|
|
name: this.newKey.name,
|
|
key: keyText
|
|
};
|
|
|
|
const response = await fetch('/api/keys', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(keyData),
|
|
});
|
|
|
|
const data = await response.json();
|
|
results.push(data);
|
|
|
|
if (!data.success) {
|
|
allSuccess = false;
|
|
}
|
|
}
|
|
|
|
if (allSuccess) {
|
|
|
|
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();
|
|
|
|
|
|
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 {
|
|
|
|
const successCount = results.filter(r => r.success).length;
|
|
const failCount = results.length - successCount;
|
|
|
|
this.errorMessage = `添加操作部分失败: 成功 ${successCount} 个, 失败 ${failCount} 个`;
|
|
}
|
|
} catch (error) {
|
|
console.error('添加API密钥失败:', error);
|
|
this.errorMessage = '服务器错误,请重试。';
|
|
} finally {
|
|
this.isSubmitting = false;
|
|
}
|
|
}
|
|
|
|
|
|
function deleteApiKey(id, name) {
|
|
this.deleteKeyId = id;
|
|
this.deleteKeyName = name;
|
|
this.showDeleteConfirm = true;
|
|
}
|
|
|
|
|
|
async function confirmDelete() {
|
|
if (this.isBulkDelete) {
|
|
if (this.selectedKeys.length === 0) return;
|
|
|
|
this.isDeleting = true;
|
|
|
|
try {
|
|
const response = await fetch('/api/keys/bulk-delete', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({ ids: this.selectedKeys }),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
|
|
this.showDeleteConfirm = false;
|
|
this.isBulkDelete = false;
|
|
const deletedCount = data.deleted_count || this.selectedKeys.length;
|
|
|
|
|
|
this.selectedKeys = [];
|
|
this.selectedPlatforms = [];
|
|
|
|
|
|
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;
|
|
}
|
|
});
|
|
|
|
|
|
this.loadApiKeys();
|
|
|
|
Toast.fire({
|
|
icon: 'success',
|
|
title: `成功删除 ${deletedCount} 个API密钥`,
|
|
background: '#fee2e2',
|
|
iconColor: '#ef4444'
|
|
});
|
|
} else {
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: '批量删除失败',
|
|
text: data.error || '删除操作未能完成,请重试',
|
|
confirmButtonColor: '#0284c7'
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error('批量删除API密钥失败:', error);
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: '服务器错误',
|
|
text: '无法完成删除操作,请稍后重试',
|
|
confirmButtonColor: '#0284c7'
|
|
});
|
|
} finally {
|
|
this.isDeleting = false;
|
|
}
|
|
} else {
|
|
|
|
if (!this.deleteKeyId) return;
|
|
|
|
this.isDeleting = true;
|
|
|
|
try {
|
|
const response = await fetch(`/api/keys/${this.deleteKeyId}`, {
|
|
method: 'DELETE',
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
|
|
this.apiKeys = [...this.apiKeys.filter(key => key.id !== this.deleteKeyId)];
|
|
|
|
|
|
this.showDeleteConfirm = false;
|
|
|
|
|
|
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;
|
|
}
|
|
});
|
|
|
|
|
|
this.loadApiKeys();
|
|
|
|
Toast.fire({
|
|
icon: 'success',
|
|
title: 'API密钥已删除',
|
|
background: '#fee2e2',
|
|
iconColor: '#ef4444'
|
|
});
|
|
} else {
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: '删除失败',
|
|
text: data.message || '删除操作未能完成,请重试',
|
|
confirmButtonColor: '#0284c7'
|
|
});
|
|
}
|
|
} catch (error) {
|
|
console.error('删除API密钥失败:', error);
|
|
Swal.fire({
|
|
icon: 'error',
|
|
title: '服务器错误',
|
|
text: '无法完成删除操作,请稍后重试',
|
|
confirmButtonColor: '#0284c7'
|
|
});
|
|
} finally {
|
|
this.isDeleting = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
function editApiKey(id, name, key, platform) {
|
|
|
|
if (!platform) {
|
|
const apiKey = this.apiKeys.find(key => key.id === id);
|
|
if (apiKey) {
|
|
platform = apiKey.platform;
|
|
}
|
|
}
|
|
|
|
this.editKey = {
|
|
id: id,
|
|
name: name,
|
|
key: key,
|
|
platform: platform
|
|
};
|
|
this.showEditModal = true;
|
|
this.errorMessage = '';
|
|
|
|
|
|
setTimeout(() => {
|
|
document.getElementById('edit-name').focus();
|
|
}, 100);
|
|
}
|
|
|
|
|
|
async function updateApiKey() {
|
|
if (!this.editKey.key) {
|
|
this.errorMessage = '请填写API密钥值。';
|
|
return;
|
|
}
|
|
|
|
this.isSubmitting = true;
|
|
this.errorMessage = '';
|
|
|
|
try {
|
|
|
|
const currentPlatform = this.editKey.platform;
|
|
const currentId = this.editKey.id;
|
|
const editedKey = this.editKey.key.trim();
|
|
|
|
|
|
const duplicateKey = this.apiKeys.find(apiKey =>
|
|
apiKey.platform === currentPlatform &&
|
|
apiKey.id !== currentId &&
|
|
apiKey.key === editedKey
|
|
);
|
|
|
|
|
|
if (duplicateKey) {
|
|
|
|
const deleteResponse = await fetch(`/api/keys/${currentId}`, {
|
|
method: 'DELETE',
|
|
});
|
|
|
|
const deleteData = await deleteResponse.json();
|
|
|
|
if (deleteData.success) {
|
|
|
|
this.showEditModal = false;
|
|
|
|
|
|
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();
|
|
|
|
Toast.fire({
|
|
icon: 'info',
|
|
title: '发现重复密钥,已自动删除',
|
|
background: '#e0f2fe',
|
|
iconColor: '#0284c7'
|
|
});
|
|
|
|
return;
|
|
} else {
|
|
this.errorMessage = '发现重复密钥,但自动删除失败,请手动处理。';
|
|
this.isSubmitting = false;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
const response = await fetch(`/api/keys/${this.editKey.id}`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
name: this.editKey.name,
|
|
key: editedKey
|
|
}),
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (data.success) {
|
|
|
|
this.showEditModal = false;
|
|
|
|
|
|
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;
|
|
}
|
|
});
|
|
|
|
|
|
this.loadApiKeys();
|
|
|
|
Toast.fire({
|
|
icon: 'success',
|
|
title: 'API密钥已更新',
|
|
background: '#f0fdf4',
|
|
iconColor: '#16a34a'
|
|
});
|
|
} else {
|
|
this.errorMessage = data.error || '更新失败,请重试。';
|
|
}
|
|
} catch (error) {
|
|
console.error('更新API密钥失败:', error);
|
|
this.errorMessage = '服务器错误,请重试。';
|
|
} finally {
|
|
this.isSubmitting = false;
|
|
}
|
|
}
|
|
|