File size: 5,806 Bytes
ef8784d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99fc92f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef8784d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**

 * 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;
    }
}