File size: 11,425 Bytes
cd872f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <link rel="icon" type="image/x-icon" href="data:image/x-icon;,">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>API 管理</title>
  <link rel="stylesheet" href="/static/shared-styles.css">
  <script src="/static/shared.js"></script>
  <style>
    .status-healthy {
      color: var(--success-color);
      animation: pulse 2s infinite;
    }

    .status-error {
      color: var(--error-color);
    }

    @keyframes pulse {
      0% {
        opacity: 1;
      }

      50% {
        opacity: 0.6;
      }

      100% {
        opacity: 1;
      }
    }

    .footer {
      margin-top: 2rem;
      color: var(--text-secondary);
      font-size: 0.9rem;
      text-align: center;
    }

    .copy-button {
      position: absolute;
      right: 0px;
      top: 0px;
      padding: 4px;
      background: transparent;
      min-height: auto;
    }

    .model-input-container {
      position: relative;
    }

    .custom-suffix {
      margin-top: 1rem;
    }

    .progress-container {
      margin-top: 1rem;
    }

    .usage-progress-container {
      width: 100%;
      height: 8px;
      background: var(--border-color);
      border-radius: 4px;
      margin: 8px 0;
      overflow: hidden;
    }

    .usage-progress-bar {
      height: 100%;
      transition: width 0.3s ease;
    }

    .progress-low {
      background: var(--success-color);
    }

    .progress-medium {
      background: #FFA726;
    }

    .progress-high {
      background: var(--error-color);
    }

    .usage-progress-bar.unlimited {
      background: repeating-linear-gradient(45deg,
          var(--success-color),
          var(--success-color) 10px,
          transparent 10px,
          transparent 20px);
      opacity: 0.5;
    }

    @media (max-width: 768px) {
      .copy-button {
        width: auto !important;
      }
    }
  </style>
</head>

<body>
  <div class="container">
    <div style="display: flex; justify-content: space-between; align-items: center;">
      <h1>API 管理</h1>
      <div id="serverStatus" class="status-healthy">Healthy</div>
    </div>

    <div class="form-group">
      <label for="authToken">AUTH Token</label>
      <input type="text" id="authToken" placeholder="请输入 AUTH Token">
    </div>

    <div class="button-group">
      <button onclick="calibrateToken()">校准 Token</button>
      <button onclick="getUserInfo()">获取用户信息</button>
      <button onclick="getModels()">获取模型列表</button>
    </div>

    <div class="form-group model-input-container">
      <label for="modelList">模型列表</label>
      <input type="text" id="modelList" readonly>
      <button class="copy-button" onclick="copyModelList()">📋</button>
    </div>

    <div class="form-group custom-suffix">
      <input type="checkbox" id="customSuffix" onchange="toggleCustomSuffix()">
      <label for="customSuffix">添加自定义后缀</label>
      <input type="text" id="suffixInput" placeholder="@OpenAI" style="display: none;">
    </div>
  </div>

  <div id="userInfoContainer" class="container" style="display: none;">
    <h2>用户信息</h2>
    <div id="userDetails"></div>
    <div id="usageProgressContainer" class="progress-container"></div>
  </div>

  <div id="message" class="message"></div>

  <footer class="footer">
    <div id="version"></div>
    <div id="uptime"></div>
  </footer>

  <script>
    // 全局变量
    let globalModels = [];

    // Token校准结果缓存
    const calibrationCache = new Map();

    // 服务器状态检查
    async function checkServerStatus() {
      try {
        const response = await fetch('/health');
        const data = await response.json();

        // 更新状态显示
        const statusElement = document.getElementById('serverStatus');
        statusElement.className = data.status === 'healthy' ? 'status-healthy' : 'status-error';
        statusElement.textContent = data.status === 'healthy' ? 'Healthy' : 'Error';

        // 更新版本和运行时间
        document.getElementById('version').textContent = `版本: ${data.version}`;
        document.getElementById('uptime').textContent = formatUptime(data.uptime);

        // 保存模型列表
        globalModels = data.models || [];

        return true;
      } catch (error) {
        const statusElement = document.getElementById('serverStatus');
        statusElement.className = 'status-error';
        statusElement.textContent = 'Error';
        showGlobalMessage('服务器状态检查失败', true);
        return false;
      }
    }

    // 定时检查服务器状态(5分钟)
    function startStatusCheck() {
      checkServerStatus();
      setInterval(checkServerStatus, 5 * 60 * 1000);
    }

    // 格式化运行时间
    function formatUptime(seconds) {
      const days = Math.floor(seconds / 86400);
      const hours = Math.floor((seconds % 86400) / 3600);
      const minutes = Math.floor((seconds % 3600) / 60);
      return `运行时间: ${days}${hours}${minutes}分`;
    }

    // 获取模型列表
    async function getModels() {
      const modelList = document.getElementById('modelList');
      const suffix = document.getElementById('customSuffix').checked ?
        document.getElementById('suffixInput').value : '';

      modelList.value = globalModels.map(model => model + suffix).join(',');
    }

    // 复制模型列表
    function copyModelList() {
      const modelList = document.getElementById('modelList');
      navigator.clipboard.writeText(modelList.value)
        .then(() => showGlobalMessage('已复制到剪贴板'))
        .catch(() => showGlobalMessage('复制失败', true));
    }

    // 切换自定义后缀输入框
    function toggleCustomSuffix() {
      const suffixInput = document.getElementById('suffixInput');
      suffixInput.style.display = document.getElementById('customSuffix').checked ? 'block' : 'none';
      if (document.getElementById('customSuffix').checked) {
        getModels();
      }
    }

    // Token相关请求
    async function makeTokenRequest(url, token) {
      try {
        const response = await fetch(url, {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify({ token })
        });

        if (!response.ok) {
          throw new Error(`HTTP error! status: ${response.status}`);
        }

        return await response.json();
      } catch (error) {
        showGlobalMessage(`请求失败: ${error.message}`, true);
        return null;
      }
    }

    // Token 校准
    async function calibrateToken() {
      const token = document.getElementById('authToken').value;
      if (!token) {
        showGlobalMessage('请输入 AUTH Token', true);
        return;
      }
      const result = await makeTokenRequest('/basic-calibration', token);
      if (result) {
        if (result.status === 'error') {
          showGlobalMessage(result.message, true);
        } else {
          showGlobalMessage('校准成功');
          // 缓存校准结果
          calibrationCache.set(token, {
            user_id: result.user_id,
            create_at: result.create_at,
            checksum_time: calibResult.checksum_time
          });
          updateUsageDisplay(null, calibrationCache.get(token));
        }
      }
    }

    // 获取用户信息
    async function getUserInfo() {
      const token = document.getElementById('authToken').value;
      if (!token) {
        showGlobalMessage('请输入 Token', true);
        return;
      }
      // 如果没有校准缓存,先进行校准
      if (!calibrationCache.has(token)) {
        const calibResult = await makeTokenRequest('/basic-calibration', token);
        if (calibResult && calibResult.status !== 'error') {
          calibrationCache.set(token, {
            user_id: calibResult.user_id,
            create_at: calibResult.create_at,
            checksum_time: calibResult.checksum_time
          });
        }
      }

      const result = await makeTokenRequest('/userinfo', token);
      if (result) {
        const container = document.getElementById('userInfoContainer');
        container.style.display = 'block';
        updateUsageDisplay(result, calibrationCache.get(token));
      }
    }

    // 更新使用情况显示
    function updateUsageDisplay(tokenInfo, calibInfo) {
      const userDetails = document.getElementById('userDetails');
      const progressContainer = document.getElementById('usageProgressContainer');

      // 清空现有内容
      userDetails.innerHTML = '';
      progressContainer.innerHTML = '';

      // 添加用户基本信息
      if (tokenInfo.user || calibInfo) {
        const user = tokenInfo.user || {};
        userDetails.innerHTML += `<p>用户ID: ${calibInfo ? calibInfo.user_id : user.id}</p><p>邮箱: ${user.email || ''}</p><p>用户名: ${user.name || ''}</p>${user.updated_at ? `<p>更新时间: ${new Date(user.updated_at).toLocaleString()}</p>` : ''}${calibInfo ? `<p>令牌创建时间: ${new Date(calibInfo.create_at).toLocaleString()}</p>` : ''}${calibInfo && calibInfo.checksum_time ? `<p>校验和时间区间: ${new Date(calibInfo.checksum_time * 1e6).toLocaleString()} - ${new Date((calibInfo.checksum_time + 1) * 1e6 - 1).toLocaleString()}</p>` : ''}`;
      }

      // 添加 Stripe 会员信息
      if (tokenInfo.stripe) {
        const stripe = tokenInfo.stripe;
        userDetails.innerHTML += `<p>会员类型: ${stripe.membership_type}</p>${stripe.payment_id ? `<p>付款 ID: ${stripe.payment_id}</p>` : ''}<p>试用剩余: ${stripe.days_remaining_on_trial} 天</p>`;
      }

      // 添加使用情况进度条
      if (tokenInfo.usage) {
        const usage = tokenInfo.usage;
        const models = {
          '高级模型': usage.premium,
          '标准模型': usage.standard,
          '未知模型': usage.unknown
        };

        Object.entries(models).forEach(([modelName, data]) => {
          if (data) {
            const isUnlimited = !data.max_requests;
            const percentage = isUnlimited ? 100 : (data.requests / data.max_requests * 100).toFixed(1);
            const progressClass = isUnlimited ? 'unlimited' : getProgressBarClass(parseFloat(percentage));

            progressContainer.innerHTML += `<div><p>${modelName}: ${data.requests}/${isUnlimited ? '∞' : data.max_requests} 请求 ${isUnlimited ? '' : `(${percentage}%)`}, ${data.tokens} tokens</p><div class="usage-progress-container"><div class="usage-progress-bar ${progressClass}" style="width: ${percentage}%"></div></div></div>`;
          }
        });
      }
    }

    // 获取进度条样式
    function getProgressBarClass(percentage) {
      if (percentage < 50) return 'progress-low';
      if (percentage < 80) return 'progress-medium';
      return 'progress-high';
    }

    // Token变更时清除缓存
    document.getElementById('authToken').addEventListener('change', (e) => {
      calibrationCache.delete(e.target.value); // 清除对应的缓存
    });

    // 页面加载完成后初始化
    document.addEventListener('DOMContentLoaded', () => {
      startStatusCheck();

      // 监听后缀输入变化
      document.getElementById('suffixInput').addEventListener('input', getModels);
    });
  </script>
</body>

</html>