|
|
|
|
|
|
|
|
|
|
|
|
|
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.replace(/['"\,\(\)\[\]\s]/g, '');
|
|
|
|
|
|
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();
|
|
|
|
|
|
setTimeout(() => {
|
|
console.log(`正在请求更新API密钥: ${this.editKey.id}`);
|
|
fetch(`/api/keys/update/${this.editKey.id}`, {
|
|
method: 'POST'
|
|
}).then(response => {
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
}
|
|
return response.json();
|
|
}).then(data => {
|
|
console.log(`API密钥 ${this.editKey.id} 更新成功:`, data);
|
|
|
|
this.loadApiKeys();
|
|
}).catch(error => {
|
|
console.error(`自动更新API密钥 ${this.editKey.id} 时出错:`, error);
|
|
});
|
|
}, 200);
|
|
|
|
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;
|
|
}
|
|
}
|
|
|