Download static/js/api-key-manager/api-key-editor.js from yangtb24/pre: direct link, hf CLI and curl.
- Browser
- Download file 5.81 kB
-
https://huggingface.co/spaces/yangtb24/pre/resolve/main/static/js/api-key-manager/api-key-editor.js
- Command line
-
hf download hf://spaces/yangtb24/pre/static/js/api-key-manager/api-key-editor.js
-
curl -L -o api-key-editor.js https://huggingface.co/spaces/yangtb24/pre/resolve/main/static/js/api-key-manager/api-key-editor.js
5.81 kB
| /** | |
| * API密钥管理器 - 密钥编辑模块 | |
| * 包含API密钥的编辑和更新功能 | |
| */ | |
| // 打开编辑API密钥模态框 | |
| function editApiKey(id, name, key, platform) { | |
| // 如果platform参数不存在,尝试从apiKeys中查找 | |
| 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); | |
| } | |
| // 更新API密钥 | |
| async function updateApiKey() { | |
| if (!this.editKey.key) { | |
| this.errorMessage = '请填写API密钥值。'; | |
| return; | |
| } | |
| this.isSubmitting = true; | |
| this.errorMessage = ''; | |
| try { | |
| // 检查修改后的key是否与同一平台下的其他key重复 | |
| const currentPlatform = this.editKey.platform; | |
| const currentId = this.editKey.id; | |
| // 过滤掉单引号、双引号、小括号、方括号和空格,与添加密钥时保持一致 | |
| const editedKey = this.editKey.key.replace(/['"\,\(\)\[\]\s]/g, ''); | |
| // 获取同平台下除当前key外的所有key | |
| const duplicateKey = this.apiKeys.find(apiKey => | |
| apiKey.platform === currentPlatform && | |
| apiKey.id !== currentId && | |
| apiKey.key === editedKey | |
| ); | |
| // 如果发现重复key,则自动删除当前key | |
| if (duplicateKey) { | |
| // 删除当前key | |
| const deleteResponse = await fetch(`/api/keys/${currentId}`, { | |
| method: 'DELETE', | |
| }); | |
| const deleteData = await deleteResponse.json(); | |
| if (deleteData.success) { | |
| // 关闭模态框 | |
| this.showEditModal = false; | |
| // 使用Toast风格的通知提示 | |
| 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; | |
| } | |
| }); | |
| // 重新加载API密钥数据而不刷新页面 | |
| 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; | |
| // 使用Toast风格的通知提示 | |
| 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; | |
| } | |
| }); | |
| // 重新加载API密钥数据而不刷新页面 | |
| this.loadApiKeys(); | |
| // 调用update API更新密钥状态 | |
| 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; | |
| } | |
| } | |