File size: 13,432 Bytes
5952dec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>Cursor To OpenAI - 管理员登录</title>
    <link rel="stylesheet" href="styles.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="default">
    <meta name="theme-color" content="#007aff">
    <script src="theme.js"></script>
</head>
<body>
    <div class="container">
        <div class="card header-card">
            <div style="display: flex; align-items: center; margin-bottom: 8px;">
                <i class="fas fa-link" style="font-size: 24px; margin-right: 12px;"></i>
                <h1>Cursor To OpenAI</h1>
            </div>
            <p>管理员登录系统 - 安全访问后台管理界面</p>
        </div>

        <!-- 登录表单卡片 -->
        <div class="card" id="loginCard">
            <div style="display: flex; align-items: center; margin-bottom: 16px;">
                <i class="fas fa-sign-in-alt" style="color: var(--ios-blue); font-size: 18px; margin-right: 10px;"></i>
                <h2 style="margin: 0;">管理员登录</h2>
            </div>
            <div id="loginMessage" class="message"></div>
            <form id="loginForm">
                <div class="form-group">
                    <label for="loginUsername">
                        <i class="fas fa-user" style="margin-right: 6px; color: var(--ios-gray);"></i>用户名
                    </label>
                    <input type="text" id="loginUsername" placeholder="请输入管理员用户名" required>
                </div>
                <div class="form-group">
                    <label for="loginPassword">
                        <i class="fas fa-lock" style="margin-right: 6px; color: var(--ios-gray);"></i>密码
                    </label>
                    <input type="password" id="loginPassword" placeholder="请输入密码" required>
                </div>
                <button type="submit">
                    <i class="fas fa-sign-in-alt" style="margin-right: 6px;"></i>登录
                </button>
            </form>
            <div class="toggle-form" id="toggleForm">
                <i class="fas fa-user-plus" style="margin-right: 6px; color: var(--ios-blue);"></i>还没有账号?点击注册
            </div>
        </div>

        <!-- 注册表单卡片 -->
        <div class="card" id="registerCard" style="display: none;">
            <div style="display: flex; align-items: center; margin-bottom: 16px;">
                <i class="fas fa-user-plus" style="color: var(--ios-green); font-size: 18px; margin-right: 10px;"></i>
                <h2 style="margin: 0;">注册管理员账号</h2>
            </div>
            <div id="registerMessage" class="message"></div>
            <form id="registerForm">
                <div class="form-group">
                    <label for="registerUsername">
                        <i class="fas fa-user" style="margin-right: 6px; color: var(--ios-gray);"></i>用户名
                    </label>
                    <input type="text" id="registerUsername" placeholder="请设置管理员用户名" required>
                </div>
                <div class="form-group">
                    <label for="registerPassword">
                        <i class="fas fa-lock" style="margin-right: 6px; color: var(--ios-gray);"></i>密码
                    </label>
                    <input type="password" id="registerPassword" placeholder="请设置密码" required>
                </div>
                <div class="form-group">
                    <label for="confirmPassword">
                        <i class="fas fa-check-circle" style="margin-right: 6px; color: var(--ios-gray);"></i>确认密码
                    </label>
                    <input type="password" id="confirmPassword" placeholder="请再次输入密码" required>
                </div>
                <button type="submit">
                    <i class="fas fa-user-plus" style="margin-right: 6px;"></i>注册
                </button>
            </form>
            <div class="toggle-form" id="toggleLoginForm">
                <i class="fas fa-sign-in-alt" style="margin-right: 6px; color: var(--ios-blue);"></i>已有账号?点击登录
            </div>
        </div>
        
        <div class="card">
            <div style="display: flex; align-items: center; margin-bottom: 16px;">
                <i class="fas fa-info-circle" style="color: var(--ios-blue); font-size: 18px; margin-right: 10px;"></i>
                <h2 style="margin: 0;">系统信息</h2>
            </div>
            <div class="info">
                <i class="fas fa-shield-alt" style="margin-right: 8px;"></i>
                Cursor To OpenAI 是一个管理自定义 API Key 与 Cursor Cookie 映射关系的高效工具。登录后可以进行API Key的管理和配置。
            </div>
        </div>
    </div>

    <script>

        // 获取元素

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

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

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

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

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

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

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

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



        // 切换表单显示

        toggleForm.addEventListener('click', () => {

            loginCard.style.display = 'none';

            registerCard.style.display = 'block';

            loginMessage.textContent = '';

        });



        toggleLoginForm.addEventListener('click', () => {

            registerCard.style.display = 'none';

            loginCard.style.display = 'block';

            registerMessage.textContent = '';

        });



        // 检查是否已有管理员账号

        async function checkAdminExists() {

            try {

                const response = await fetch('/v1/admin/check');

                const data = await response.json();

                

                if (data.exists) {

                    // 如果已有管理员,显示登录表单

                    loginCard.style.display = 'block';

                    registerCard.style.display = 'none';

                    toggleForm.style.display = 'none';

                } else {

                    // 如果没有管理员,显示注册表单

                    loginCard.style.display = 'none';

                    registerCard.style.display = 'block';

                    toggleLoginForm.style.display = 'none';

                    

                    // 添加首次使用的信息提示

                    registerMessage.innerHTML = `

                        <div class="info">

                            <i class="fas fa-info-circle" style="margin-right: 8px;"></i>

                            首次使用,请注册管理员账号

                        </div>`;

                }

            } catch (error) {

                console.error('检查管理员账号失败:', error);

                loginMessage.innerHTML = `

                    <div class="error">

                        <i class="fas fa-exclamation-circle" style="margin-right: 8px;"></i>

                        连接服务器失败,请检查网络连接

                    </div>`;

            }

        }



        // 登录处理

        loginForm.addEventListener('submit', async (e) => {

            e.preventDefault();

            

            const username = document.getElementById('loginUsername').value;

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

            

            // 添加登录中的状态提示

            loginMessage.innerHTML = `

                <div class="info">

                    <i class="fas fa-spinner fa-spin" style="margin-right: 8px;"></i>

                    登录中,请稍候...

                </div>`;

            

            try {

                const response = await fetch('/v1/admin/login', {

                    method: 'POST',

                    headers: {

                        'Content-Type': 'application/json',

                    },

                    body: JSON.stringify({ username, password }),

                });

                

                const data = await response.json();

                

                if (data.success) {

                    // 登录成功,保存token并跳转

                    localStorage.setItem('adminToken', data.token);

                    

                    loginMessage.innerHTML = `

                        <div class="info" style="background-color: rgba(52, 199, 89, 0.1); border-left: 3px solid var(--ios-green);">

                            <i class="fas fa-check-circle" style="margin-right: 8px;"></i>

                            登录成功,正在跳转...

                        </div>`;

                    

                    setTimeout(() => {

                        window.location.href = '/index.html';

                    }, 1000);

                } else {

                    loginMessage.innerHTML = `

                        <div class="error">

                            <i class="fas fa-exclamation-circle" style="margin-right: 8px;"></i>

                            ${data.message}

                        </div>`;

                }

            } catch (error) {

                console.error('登录失败:', error);

                loginMessage.innerHTML = `

                    <div class="error">

                        <i class="fas fa-exclamation-circle" style="margin-right: 8px;"></i>

                        登录失败,请稍后重试

                    </div>`;

            }

        });



        // 注册处理

        registerForm.addEventListener('submit', async (e) => {

            e.preventDefault();

            

            const username = document.getElementById('registerUsername').value;

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

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

            

            if (password !== confirmPassword) {

                registerMessage.innerHTML = `

                    <div class="error">

                        <i class="fas fa-exclamation-circle" style="margin-right: 8px;"></i>

                        两次输入的密码不一致

                    </div>`;

                return;

            }

            

            // 添加注册中的状态提示

            registerMessage.innerHTML = `

                <div class="info">

                    <i class="fas fa-spinner fa-spin" style="margin-right: 8px;"></i>

                    注册中,请稍候...

                </div>`;

            

            try {

                const response = await fetch('/v1/admin/register', {

                    method: 'POST',

                    headers: {

                        'Content-Type': 'application/json',

                    },

                    body: JSON.stringify({ username, password }),

                });

                

                const data = await response.json();

                

                if (data.success) {

                    // 注册成功,保存token并跳转

                    localStorage.setItem('adminToken', data.token);

                    

                    registerMessage.innerHTML = `

                        <div class="info" style="background-color: rgba(52, 199, 89, 0.1); border-left: 3px solid var(--ios-green);">

                            <i class="fas fa-check-circle" style="margin-right: 8px;"></i>

                            注册成功,正在跳转...

                        </div>`;

                    

                    setTimeout(() => {

                        window.location.href = '/index.html';

                    }, 1000);

                } else {

                    registerMessage.innerHTML = `

                        <div class="error">

                            <i class="fas fa-exclamation-circle" style="margin-right: 8px;"></i>

                            ${data.message}

                        </div>`;

                }

            } catch (error) {

                console.error('注册失败:', error);

                registerMessage.innerHTML = `

                    <div class="error">

                        <i class="fas fa-exclamation-circle" style="margin-right: 8px;"></i>

                        注册失败,请稍后重试

                    </div>`;

            }

        });



        // 页面加载时检查管理员账号

        document.addEventListener('DOMContentLoaded', checkAdminExists);

    </script>
</body>
</html>