-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
703 lines (614 loc) · 21.4 KB
/
index.html
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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="keywords" content="OpenAI API-KEY Billing Checker">
<title>OpenAI API-KEY 信息查询</title>
<link rel="icon" href="assets/openai.ico"/>
<script defer src="/_vercel/insights/script.js"></script>
<style>
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.loader {
border: 2px solid #f3f3f3;
border-top: 2px solid #3498db;
border-radius: 50%;
width: 24px;
height: 24px;
animation: spin 2s linear infinite;
display: none;
margin: 20px;
}
table {
margin: auto;
border-collapse: collapse;
}
th,
td {
padding: 8px;
text-align: center;
color: #000000;
border: 1px solid #000000;
}
th:first-child,
td:first-child {
width: 520px;
}
h1 {
text-align: center;
color: #000000;
}
form {
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 20px;
}
textarea#api-key-input {
width: 500px;
font-size: 16px;
padding: 10px;
margin-right: 20px;
border-radius: 5px;
border: none;
resize: vertical;
rows: 8;
}
select#api-url-select,
input#custom-url-input {
width: 100%;
height: 50px;
font-size: 16px;
background-color: #212121;
border-radius: 5px;
color: #ffffff;
border: none;
}
input#custom-url-input {
display: none;
width: 85%;
height: 25px;
}
button {
height: 50px;
font-size: 16px;
background-color: #ff8c00;
color: #ffffff;
border: none;
border-radius: 5px;
cursor: pointer;
}
.status-ok {
color: #2d8d2d;
}
.status-error {
color: #ed0808;
}
tr:nth-child(even) {
/* 将偶数行背景色改为深蓝色 */
background-color: #f1d39c;
}
tr:nth-child(odd) {
/* 将偶数行背景色改为深蓝色 */
background-color: #f1d39c;
}
</style>
<style>
body {
background-size: cover;
background-color: #222222;
display: flex; justify-content: center; align-items: center; height: 90vh;
}
.content {
width: 100%;
max-width: 1100px;
margin: auto;
padding: 40px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
background-color: rgba(0, 0, 0, 0.63);
}
.container {
display: flex;
justify-content: center;
align-items: center;
height: 50vh;
}
h1 {
text-align: center;
font-size: 36px;
margin-bottom: 30px;
}
form {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
}
label {
font-size: 24px;
color: #ff089e;
margin-right: 10px;
}
input[type="text"] {
border: 2px solid #ddd;
border-radius: 6px;
padding: 12px 16px;
font-size: 16px;
background-color: #f9f9f9;
margin-right: 10px;
flex: 1;
transition: border-color 0.3s ease;
}
input[type="text"]:focus {
outline: none;
border-color: #4299e5;
}
button[type="submit"] {
background-color: #4299e5;
color: #fff;
border: none;
border-radius: 8px;
font-size: 16px;
padding: 14px 28px;
cursor: pointer;
transition: all 0.3s ease;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
button[type="submit"]:hover,
button[type="submit"]:focus {
background-color: #357dbf;
outline: none;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
grid-gap: 20px;
margin-top: 40px;
}
.grid-item {
padding: 20px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
text-align: center;
transition: transform 0.3s ease;
background-color:#fff;
}
.grid-item:hover {
transform: translateY(-10px);
}
.grid-item p:nth-child(1) {
font-size: 28px;
font-weight: bold;
margin-bottom: 10px;
color: #4299e5;
animation: pulse 1s ease-in-out infinite alternate;
}
.grid-item p:nth-child(2) {
font-size: 16px;
color: #666;
}
@keyframes pulse {
from {
opacity: 0.6;
transform: scale(1);
}
to {
opacity: 1;
transform: scale(1.2);
}
}
.text-gradient {
background-image: linear-gradient(to right, #4299e5, #00c458);
-webkit-background-clip: text;
color: transparent;
}
.text-gradient {
animation: gradient-animation 2s linear infinite;
-webkit-background-clip: text;
color: transparent;
}
@keyframes gradient-animation {
from {background-image: linear-gradient(to right, #4299e5, #00c458);}
to {background-image: linear-gradient(to right, #00c458, #4299e5);}
}
#result-table {
border-collapse: collapse;
width: 100%;
margin-top: 20px;
font-size: 14px;
}
#result-table th {
background-color: #6fa83a;
color: #000;
font-weight: 400;
height: 20px;
/*padding: 10px 25px;*/
text-align: center;
border: 1px solid #090303;
}
#result-table td {
height: 20px;
text-align: center;
}
#result-table tbody tr:nth-child(even) {
background-color: #f9f9f9;
}
#result-table .table-header {
width: 25%;
}
#result-table .table-data {
width: 25%;
font-weight: bold;
color: #333;
}
.form-group {
margin-bottom: 1rem;
}
.form-control {
font-family: inherit;
font-size: 15px;
display: block;
width: 100%;
height: calc(5.5rem + 2px);
padding: 0.375rem 0.75rem;
line-height: 1.5;
color: #495057;
background-color: #fff;
border: 1px solid #ced4da;
border-radius: 0.25rem;
transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
}
.form-control:hover,
.form-control:active,
.form-control:focus {
border-color: #80bdff;
outline: 0;
box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25);
}
.form-control::-webkit-input-placeholder {
color: #999;
}
.form-control::-moz-placeholder {
color: #999;
}
.form-control:-ms-input-placeholder {
color: #999;
}
.form-control::-ms-input-placeholder {
color: #999;
}
.form-control::placeholder {
color: #999;
}
</style>
</head>
<body>
<div class="content">
<h1 class="text-3xl font-semibold text-center mb-8 text-gradient">OpenAI API余额查询(批量)</h1>
<p style="color:red;text-align:center;">* 本站不保存 KEY 信息,查询后请自行保存</p>
<p style="color:red;text-align:center;">* 2023-07-26 OpenAI官方subscription、usage接口已无法使用,请去<a style="color: orange;" target="_blank" href="https://platform.openai.com/account/usage">官网</a>查吧。</p>
<form>
<label for="api-key-input" class="w-28 mr-4 text-gradient">API密钥:</label>
<textarea id="api-key-input" class="form-control" placeholder="每行一个密钥,必须包含 sk-"></textarea>
<div style="width:200px; margin-right: 20px;">
<select id="api-url-select">
<option value="https://api.openai.com">【OpenAI 官方查询接口】</option>
<option value="https://api.askgptai.tech">【CloudFlare 反代接口】</option>
<option value="https://vercel.askopenai.tech">【Vercel 反代接口】</option>
<option value="custom">自定义反代接口: https://...</option>
</select>
<!-- 自定义API链接输入框 -->
<input type="text" id="custom-url-input" placeholder="输入自定义API HOST" />
</div>
<button type="button" style="display:inline-block; width: 100px;" onclick="sendRequest()">查询</button>
<div id="loader" class="loader ml-4" style="display:none;"></div>
</form>
<table id="result-table">
<thead>
<tr>
<th style="width: 35%">API KEY</th>
<th style="width: 15%">本月总额度(USD)</th>
<th style="width: 15%">本月已使用(USD)</th>
<th style="width: 15%">本月剩余额度(USD)</th>
<th style="width: 10%">开始日期</th>
<th style="width: 10%">结束日期</th>
</tr>
</thead>
<tbody></tbody>
</table>
<footer style="text-align: center; margin-top: 20px;">
<a href="en.html" style="color: white;text-decoration: none;" target="_self">[ English ]</a>
<a href="https://github.com/whc23mj/openai-billing-checker" style="color: white;text-decoration: none;" target="_blank">[ Github ]</a><br>
<p style="color:orange;text-align:center;">不在 OpenAI 支持列表中的国家或地区,可访问以下镜像站</p>
<a href="https://openai.mom" style="color: white;text-decoration: none;" target="_blank">[ ChatGPT Mirrors ]</a><br>
<a href="https://www.buymeacoffee.com/whc23mj" target="_blank"><img src="assets/black-btn-bmc.png" alt="Buy Me A Coffee" style="height: 40px !important; padding-top:10px;" ></a>
<a href="https://discord.gg/tqDtEN9YGR" target="_blank"><img src="assets/black-btn-discord.png" alt="Discord" style="height: 40px !important; padding-top:10px;" ></a>
</footer>
</div>
<script>
// 定义查询过的API-KEY列表
let queriedApiKeys = [];
async function checkBilling(apiKey) {
// 计算起始日期和结束日期
const now = new Date();
const startDate = new Date(now - 90 * 24 * 60 * 60 * 1000);
const endDate = new Date(now.getTime() + 24 * 60 * 60 * 1000);
// 设置API请求URL和请求头
const urlSubscription = 'https://api.openai.com/v1/dashboard/billing/subscription'; // 查是否订阅
const urlBalance = 'https://api.openai.com/dashboard/billing/credit_grants'; // 查普通账单
const urlUsage = `https://api.openai.com/v1/dashboard/billing/usage?start_date=${formatDate(startDate)}&end_date=${formatDate(endDate)}`; // 查使用量
const headers = {
"Authorization": "Bearer " + apiKey,
"Content-Type": "application/json"
};
try {
// 获取API限额
let response = await fetch(urlSubscription, {headers});
if (!response.ok) {
console.log("您的账户已被封禁,请登录OpenAI进行查看。");
return;
}
const subscriptionData = await response.json();
const totalAmount = subscriptionData.hard_limit_usd;
// 获取已使用量
response = await fetch(urlUsage, {headers});
const usageData = await response.json();
const totalUsage = usageData.total_usage / 100;
// 计算剩余额度
const remaining = totalAmount - totalUsage;
// 输出总用量、总额及余额信息
console.log(`Total Amount: ${totalAmount.toFixed(2)}`);
console.log(`Used: ${totalUsage.toFixed(2)}`);
console.log(`Remaining: ${remaining.toFixed(2)}`);
return [totalAmount, totalUsage, remaining, startDateString, endDateString];
} catch (error) {
console.error(error);
return [null, null, null];
}
}
function formatDate(date) {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
return `${year}-${month}-${day}`;
}
function sendRequest() {
let apiKeyInput = document.getElementById("api-key-input");
let apiUrlSelect = document.getElementById("api-url-select");
let customUrlInput = document.getElementById("custom-url-input");
if (apiKeyInput.value.trim() === "") {
alert("请填写API KEY");
return;
}
// 添加以下一行代码,清空之前的结果信息
document.getElementById("result-table").getElementsByTagName('tbody')[0].innerHTML = "";
let apiUrl = "";
if (apiUrlSelect.value === "custom") {
if (customUrlInput.value.trim() === "") {
alert("请设置API链接");
return;
} else {
apiUrl = customUrlInput.value.trim();
console.log(`ApiUrl: https://${apiUrl}`);
}
} else {
apiUrl = apiUrlSelect.value;
console.log(`ApiUrl: ${apiUrl}`);
}
let apiKeys = apiKeyInput.value.trim().split("\n");
let tableBody = document.querySelector("#result-table tbody");
for (let i = 0; i < apiKeys.length; i++) {
let apiKey = apiKeys[i].trim();
// 判断是否已经查询过
if (queriedApiKeys.includes(apiKey)) {
console.log(`API KEY ${apiKey} 已查询过,跳过此次查询`);
continue;
}
queriedApiKeys.push(apiKey);
showLoader();
checkBilling(apiKey).then((data) => {
// update table with data
let row = document.createElement("tr");
// 隐藏API KEY部分信息
const hiddenApiKey = apiKey.substring(0, 7) + "*******************" + apiKey.substring(47);
// API KEY
let apiKeyCell = document.createElement("td");
apiKeyCell.textContent = hiddenApiKey;
row.appendChild(apiKeyCell);
if (data[0] === null) {
// ERROR MESSAGE
let errorMessageCell = document.createElement("td");
errorMessageCell.colSpan = "3";
errorMessageCell.classList.add("status-error");
errorMessageCell.textContent = "不正确或已失效的API-KEY";
row.appendChild(errorMessageCell);
} else {
// TOTAL GRANTED
let totalGrantedCell = document.createElement("td");
totalGrantedCell.textContent = data[0].toFixed(2);
row.appendChild(totalGrantedCell);
// TOTAL USED
let totalUsedCell = document.createElement("td");
totalUsedCell.textContent = data[1].toFixed(2);
row.appendChild(totalUsedCell);
// TOTAL AVAILABLE
let totalAvailableCell = document.createElement("td");
totalAvailableCell.textContent = data[2].toFixed(2);
row.appendChild(totalAvailableCell);
// START DATE
let startDateCell = document.createElement("td");
startDateCell.textContent = data[3];
row.appendChild(startDateCell);
// END DATE
let endDateCell = document.createElement("td");
endDateCell.textContent = data[4];
row.appendChild(endDateCell);
}
tableBody.appendChild(row);
// 添加以下代码,查询完成后删除已查询的API-KEY
if (i === apiKeys.length - 1) {
queriedApiKeys = [];
}
hideLoader();
}).catch((error) => {
console.error(error);
// update table with error message
let row = document.createElement("tr");
let apiKeyCell = document.createElement("td");
// 隐藏API KEY部分信息
const hiddenApiKey = apiKey.substring(0, 7) + "*******************" + apiKey.substring(47);
apiKeyCell.textContent = hiddenApiKey;
row.appendChild(apiKeyCell);
let errorMessageCell = document.createElement("td");
errorMessageCell.colSpan = "3";
errorMessageCell.style.width = "90px";
errorMessageCell.classList.add("status-error");
errorMessageCell.textContent =
"账户已被封禁,请登录OpenAI进行查看。";
row.appendChild(errorMessageCell);
tableBody.appendChild(row);
// 添加以下代码,查询完成后删除已查询的API-KEY
if (i === apiKeys.length - 1) {
queriedApiKeys = [];
}
hideLoader();
});
}
}
let apiUrlSelect = document.getElementById("api-url-select");
let customUrlInput = document.getElementById("custom-url-input");
apiUrlSelect.addEventListener("change", function () {
if (apiUrlSelect.value === "custom") {
customUrlInput.style.display = "inline-block";
customUrlInput.style.marginTop = "5px";
} else {
customUrlInput.style.display = "none";
}
});
</script>
<script>
function showLoader() {
document.getElementById("loader").style.display = "inline-block";
}
function hideLoader() {
document.getElementById("loader").style.display = "none";
}
function displayResult(data) {
if (data.status === "success") {
document.getElementById("result").innerHTML = `
<div class="grid grid-cols-3 gap-4">
<div class="grid-item bg-gray-100 p-6 rounded-lg text-center">
<p class="text-2xl font-semibold">${data.total_granted}</p>
<p class="text-gray-600">总额度</p>
</div>
<div class="grid-item bg-gray-100 p-6 rounded-lg text-center">
<p class="text-2xl font-semibold">${parseFloat(data.total_used).toFixed(3)}</p>
<p class="text-gray-600">已使用额度</p>
</div>
<div class="grid-item bg-gray-100 p-6 rounded-lg text-center">
<p class="text-2xl font-semibold">${parseFloat(data.total_available).toFixed(3)}</p>
<p class="text-gray-600">剩余可用额度</p>
</div>
<div class="grid-item bg-gray-100 p-6 rounded-lg text-center">
<p class="text-2xl font-semibold">${data.total_startDate}</p>
<p class="text-gray-600">开始日期</p>
</div>
<div class="grid-item bg-gray-100 p-6 rounded-lg text-center">
<p class="text-2xl font-semibold">${data.total_endstartDate}</p>
<p class="text-gray-600">结束日期</p>
</div>
</div>
`;
} else {
alert("您的账户已被封禁,请登录OpenAI进行查看。");
}
}
function checkOpenAICredit() {
var apiKey = document.getElementById("api-key-input").value;
if (!apiKey.startsWith("sk-")) {
alert("无效的API密钥,请检查您的API密钥是否正确。");
return;
}
showLoader();
checkBilling(apiKey)
.then(data => {
displayResult({
status: "success",
total_granted: data[0],
total_used: data[1],
total_available: data[2]
});
hideLoader();
})
.catch(error => {
alert("您的账户已被封禁,请登录OpenAI进行查看。");
hideLoader();
});
}
async function checkBilling(apiKey) {
// 计算起始日期和结束日期
const now = new Date();
startDate = new Date(now - 90 * 24 * 60 * 60 * 1000);
const endDate = new Date(now.getTime() + 24 * 60 * 60 * 1000);
// 设置API请求URL和请求头
const urlSubscription = 'https://api.openai.com/v1/dashboard/billing/subscription'; // 查是否订阅
const urlBalance = 'https://api.openai.com/dashboard/billing/credit_grants'; // 查普通账单
urlUsage = `https://api.openai.com/v1/dashboard/billing/usage?start_date=${formatDate(startDate)}&end_date=${formatDate(endDate)}`; // 查使用量
const headers = {
"Authorization": "Bearer " + apiKey,
"Content-Type": "application/json"
};
try {
// 获取API限额
let response = await fetch(urlSubscription, {headers});
if (!response.ok) {
console.log("您的账户已被封禁,请登录OpenAI进行查看。");
return;
}
const subscriptionData = await response.json();
const totalAmount = subscriptionData.hard_limit_usd;
const is_subsrcibed = subscriptionData.has_payment_method;
// 获取已使用量
response = await fetch(urlUsage, {headers});
usageData = await response.json();
totalUsage = usageData.total_usage / 100;
// 如果用户绑卡,额度每月会刷新
if(is_subsrcibed) {
// 获取当前月的第一天日期
const day = now.getDate(); // 本月过去的天数
startDate = new Date(now - (day - 1) * 24 * 60 * 60 * 1000); // 本月第一天
urlUsage = `https://api.openai.com/v1/dashboard/billing/usage?start_date=${formatDate(startDate)}&end_date=${formatDate(endDate)}`; // 查使用量
response = await fetch(urlUsage, {headers});
usageData = await response.json();
totalUsage = usageData.total_usage / 100;
}
// 计算剩余额度
const remaining = totalAmount - totalUsage;
// 计算开始时间和结束时间
const endDateString = new Date(subscriptionData.access_until * 1000).toLocaleDateString();
const startDateString = new Date(startDate).toLocaleDateString();
// 输出总用量、总额及余额信息
console.log(`Total Amount: ${totalAmount.toFixed(2)}`);
console.log(`Used: ${totalUsage.toFixed(2)}`);
console.log(`Remaining: ${remaining.toFixed(2)}`);
console.log(`Plan: ${subscriptionData.plan.title}`);
//console.log(`--------------------`);
return [totalAmount, totalUsage, remaining, startDateString, endDateString];
} catch (error) {
console.error(error);
return [null, null, null];
}
}
function formatDate(date) {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
return `${year}-${month}-${day}`;
}
</script>
</body>
</html>