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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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
{% extends "base.html" %}

{% block title %}用户管理{% endblock %}

{% block content %}
<div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
    <div class="flex justify-between items-center mb-6">
        <h2 class="text-2xl font-bold text-gray-800">用户列表</h2>
        <button id="sycGatewayBtn" class="bg-green-500 hover:bg-green-600 text-white font-bold py-2 px-4 rounded">
            一键同步GPT网关
        </button>
        <button id="addUserBtn" class="bg-green-500 hover:bg-green-600 text-white font-bold py-2 px-4 rounded">
            添加用户
        </button>
    </div>

    <!-- 用户列表 -->
    <table class="min-w-full">
        <thead>
            <tr>
                <th

                    class="px-6 py-3 border-b-2 border-gray-200 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                    用户名
                </th>
                <th

                    class="px-6 py-3 border-b-2 border-gray-200 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                    角色
                </th>
                <th

                    class="px-6 py-3 border-b-2 border-gray-200 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                    绑定GPT账号
                </th>
                <th

                    class="px-6 py-3 border-b-2 border-gray-200 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                    GPT过期时间
                </th>
                <th

                    class="px-6 py-3 border-b-2 border-gray-200 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                    绑定Claude账号
                </th>
                <th

                    class="px-6 py-3 border-b-2 border-gray-200 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                    Claude过期时间
                </th>
                <th

                    class="px-6 py-3 border-b-2 border-gray-200 text-left text-xs font-semibold text-gray-600 uppercase tracking-wider">
                    操作
                </th>
            </tr>
        </thead>
        <tbody id="userTableBody">
            <!-- 用户数据将动态插入这里 -->
        </tbody>
    </table>

    <!-- 用户表单模态框 -->
    <div id="userModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden">
        <div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white">
            <div class="mt-3">
                <h3 class="text-lg font-medium text-gray-900 mb-4">用户信息</h3>
                <form id="userForm">
                    <input type="hidden" id="userId">
                    <div class="mb-4">
                        <label class="block text-gray-700 text-sm font-bold mb-2" for="username">
                            用户名
                        </label>
                        <input

                            class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"

                            id="username" type="text" required>
                    </div>
                    <div class="mb-4">
                        <label class="block text-gray-700 text-sm font-bold mb-2" for="password">
                            密码
                        </label>
                        <input

                            class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"

                            id="password" type="password">
                    </div>
                    <div class="mb-4">
                        <label class="block text-gray-700 text-sm font-bold mb-2" for="role">
                            角色
                        </label>
                        <select

                            class="shadow border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"

                            id="role" required>
                            <option value="admin">管理员</option>
                            <option value="user">普通用户</option>
                        </select>
                    </div>
                    <!-- 添加GPT过期时间选择 -->
                    <div class="mb-4">
                        <label class="block text-gray-700 text-sm font-bold mb-2" for="expirationDate">
                            GPT过期时间
                        </label>
                        <input

                            class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"

                            id="expirationDate" type="datetime-local">
                    </div>
                    <!-- 添加Claude过期时间选择 -->
                    <div class="mb-4">
                        <label class="block text-gray-700 text-sm font-bold mb-2" for="expirationClaude">
                            Claude过期时间
                        </label>
                        <input

                            class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"

                            id="expirationClaude" type="datetime-local">
                    </div>
                    <div class="flex items-center justify-end">
                        <button type="button" onclick="closeModal()"

                            class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline mr-2">
                            取消
                        </button>
                        <button type="submit"

                            class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
                            保存
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- 绑定账号模态框 -->
    <div id="bindModal" class="fixed inset-0 bg-gray-600 bg-opacity-50 hidden">
        <div class="relative top-20 mx-auto p-5 border w-96 shadow-lg rounded-md bg-white">
            <div class="mt-3">
                <h3 class="text-lg font-medium text-gray-900 mb-4">请选择要绑定的账号</h3>
                <form id="bindForm">
                    <input type="hidden" id="userId">
                    <div class="mb-4">
                        <select

                            class="shadow border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"

                            id="email">
                        </select>
                    </div>
                    <div class="mb-4">
                        <select

                            class="shadow border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"

                            id="claudeEmail">
                        </select>
                    </div>
                    <div class="flex items-center justify-end">
                        <button type="button" onclick="closeBindModal()"

                            class="bg-gray-500 hover:bg-gray-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline mr-2">
                            取消
                        </button>
                        <button type="submit"

                            class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
                            保存
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>


</div>
{% endblock %}
{% block extra_js %}
<script>

    let users = [];

    const bindModal = document.getElementById('bindModal');

    const modal = document.getElementById('userModal');

    const userForm = document.getElementById('userForm');

    const bindForm = document.getElementById('bindForm');

    const addUserBtn = document.getElementById('addUserBtn');

    const sycGateway = document.getElementById('sycGatewayBtn');

    // 加载用户列表

    function loadUsers() {

        fetch('/api/users')

            .then(response => response.json())

            .then(data => {

                users = data;

                renderUsers();

            })

            .catch(error => console.error('Error:', error));

    }



    // 渲染用户列表

    function renderUsers() {

        const tbody = document.getElementById('userTableBody');

        tbody.innerHTML = '';

        users.forEach(user => {

            const tr = document.createElement('tr');

            tr.innerHTML = `

                    <td class="px-6 py-4 whitespace-nowrap">${user.username}</td>

                    <td class="px-6 py-4 whitespace-nowrap">${user.role}</td>

                    <td class="px-6 py-4 whitespace-nowrap">${user.bind_email}</td>

                    <td class="px-6 py-4 whitespace-nowrap">${user.expiration_time}</td>

                    <td class="px-6 py-4 whitespace-nowrap">${user.bind_claude_email}</td>

                    <td class="px-6 py-4 whitespace-nowrap">${user.claude_expiration_time}</td>

                    <td class="px-6 py-4 whitespace-nowrap">

                        <button onclick="bindChatGPT('${user.id}')" class="text-blue-600 hover:text-blue-900 mr-4">绑定GPT</button>

                        <button onclick="bindClaude('${user.id}')" class="text-blue-600 hover:text-blue-900 mr-4">绑定Claude</button>

                        <button onclick="editUser('${user.id}')" class="text-blue-600 hover:text-blue-900 mr-4">编辑</button>

                        <button onclick="deleteUser('${user.id}')" class="text-red-600 hover:text-red-900">删除</button>

                        <button onclick="deleteBind('${user.id}')" class="text-red-600 hover:text-red-900">解绑GPT</button>

                        <button onclick="deleteClaudeBind('${user.id}')" class="text-red-600 hover:text-red-900">解绑Claude</button>

                    </td>

                `;

            tbody.appendChild(tr);

        });

    }



    // 打开用户模态框

    function openModal(user = null) {

        const form = document.getElementById('userForm');

        if (user) {

            document.getElementById('userId').value = user.id;

            document.getElementById('username').value = user.username;

            document.getElementById('password').value = '';

            document.getElementById('role').value = user.role;

            document.getElementById('expirationDate').value = user.expiration_time;

            document.getElementById('expirationClaude').value = user.claude_expiration_time;

        } else {

            form.reset();

            document.getElementById('userId').value = '';

        }

        modal.classList.remove('hidden');

    }



    // 关闭用户模态框

    function closeModal() {

        modal.classList.add('hidden');

    }



    // 编辑用户

    function editUser(userId) {

        const user = users.find(u => u.id === userId);

        if (user) {

            openModal(user);

        }

    }



    // 打开绑定模态框

    function openBindModal(user = null,type) {

        if(type == 1){

            url = "/api/all_email"

            fetch(url)

            .then(response => response.json())

            .then(data => {

                const emailSelect = document.getElementById('email');

                document.getElementById('email').classList.remove('hidden')

                document.getElementById('claudeEmail').classList.add('hidden')

                emailSelect.innerHTML = ''; 

                document.getElementById('userId').value = user.id;

                data.forEach(email => {

                    const option = document.createElement('option');

                    option.value = email; 

                    option.textContent = email; 

                    emailSelect.appendChild(option);

                });



                if (user && user.email) {

                    emailSelect.value = user.email; 

                }

            })

            .catch(error => console.error('Error:', error));

        }else{

            url = "/api/all_claude_email"

            fetch(url)

            .then(response => response.json())

            .then(data => {

                const emailSelect = document.getElementById('claudeEmail');

                document.getElementById('claudeEmail').classList.remove('hidden')

                document.getElementById('email').classList.add('hidden')

                emailSelect.innerHTML = ''; 

                document.getElementById('userId').value = user.id;

                data.forEach(email => {

                    const option = document.createElement('option');

                    option.value = email; 

                    option.textContent = email; 

                    emailSelect.appendChild(option);

                });



                if (user && user.email) {

                    emailSelect.value = user.email; 

                }

            })

            .catch(error => console.error('Error:', error));

        }

        

        bindModal.classList.remove('hidden');

    }



    // 关闭绑定模态框

    function closeBindModal() {

        bindModal.classList.add('hidden');

    }



    // 绑定GPT账号

    function bindChatGPT(userId) {

        const user = users.find(u => u.id === userId);

        if (user) {

            openBindModal(user,1);

        }

    }

    // 绑定Claude账号

    function bindClaude(userId) {

        const user = users.find(u => u.id === userId);

        if (user) {

            openBindModal(user,0);

        }

    }





    // 删除用户

    function deleteUser(userId) {

        if (confirm('确定要删除这个用户吗?')) {

            fetch(`/api/users/${userId}`, {

                method: 'DELETE'

            })

                .then(response => response.json())

                .then(data => {

                    if (data.success) {

                        loadUsers();

                    }

                })

                .catch(error => console.error('Error:', error));

        }

    }



    // 解绑GPT账号

    function deleteBind(userId) {

        if (confirm('确定要解除这个用户绑定的ChatGPT账号吗?')) {

            fetch(`/api/del_bind/${userId}`, {

                method: 'DELETE'

            })

                .then(response => response.json())

                .then(data => {

                    if (data.success) {

                        loadUsers();

                    }

                })

                .catch(error => console.error('Error:', error));

        }

    }



    // 解绑Claude账号

    function deleteClaudeBind(userId) {

        if (confirm('确定要解除这个用户绑定的Claude账号吗?')) {

            fetch(`/api/del_bindClaude/${userId}`, {

                method: 'DELETE'

            })

                .then(response => response.json())

                .then(data => {

                    if (data.success) {

                        loadUsers();

                    }

                })

                .catch(error => console.error('Error:', error));

        }

    }



    // 表单提交处理

    userForm.addEventListener('submit', function (e) {

        e.preventDefault();

        const userId = document.getElementById('userId').value;

        const userData = {

            username: document.getElementById('username').value,

            password: document.getElementById('password').value,

            expiration_time: document.getElementById('expirationDate').value,

            claude_expiration_time: document.getElementById('expirationClaude').value,

            role: document.getElementById('role').value

        };



        const url = userId ? `/api/users/${userId}` : '/api/users';

        const method = userId ? 'PUT' : 'POST';



        fetch(url, {

            method: method,

            headers: {

                'Content-Type': 'application/json'

            },

            body: JSON.stringify(userData)

        })

            .then(response => response.json())

            .then(data => {

                if (data.success) {

                    closeModal();

                    loadUsers();

                }

            })

            .catch(error => console.error('Error:', error));

    });





    // 表单提交处理

    bindForm.addEventListener('submit', function (e) {

        e.preventDefault();

        const userId = document.getElementById('userId').value;

        const emailSelect = document.getElementById('email');

        const emailSelectClaude = document.getElementById('claudeEmail');

        const bindEmail = {email: emailSelect.value};

        const bindClaudeEmail = {email: emailSelectClaude.value};

        

        const method = 'PUT';

        if(document.getElementById('claudeEmail').classList.contains('hidden')){

            url = `/api/bind/${userId}`;

            fetch(url, {

                method: method,

                headers: {

                    'Content-Type': 'application/json'

                },

                body: JSON.stringify(bindEmail)

            })

            .then(response => response.json())

            .then(data => {

                if (data.success) {

                    closeBindModal();

                    loadUsers();

                }

            })

            .catch(error => console.error('Error:', error));

        }else{

            url = `/api/bindClaude/${userId}`;

            fetch(url, {

                method: method,

                headers: {

                    'Content-Type': 'application/json'

                },

                body: JSON.stringify(bindClaudeEmail)

            })

            .then(response => response.json())

            .then(data => {

                if (data.success) {

                    closeBindModal();

                    loadUsers();

                }

            })

            .catch(error => console.error('Error:', error));

        }

        

    });



    // 添加用户按钮点击事件

    addUserBtn.addEventListener('click', () => openModal());



    // 一键同步

    function syc() {

    if (confirm('确定是否同步网关?')) {

        fetch(`/api/syc`, {

            method: 'GET'

        })

            .then(response => response.json())

            .then(data => {

                if (data.success) {

                    loadUsers();

                    alert('同步成功!'); // 添加弹窗

                } else {

                    alert('同步失败!'); // 可选,处理失败情况

                }

            })

            .catch(error => console.error('Error:', error));

    }

}





    // 一键同步按钮点击事件

    sycGateway.addEventListener('click', () => syc());





    // 初始加载用户列表

    loadUsers();

</script>
{% endblock %}