File size: 109,938 Bytes
69fec20 |
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 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 |
// =====================================================================
// GCLI2API 控制面板公共JavaScript模块
// =====================================================================
// =====================================================================
// 全局状态管理
// =====================================================================
const AppState = {
// 认证相关
authToken: '',
authInProgress: false,
currentProjectId: '',
// Antigravity认证
antigravityAuthState: null,
antigravityAuthInProgress: false,
// 凭证管理
creds: createCredsManager('normal'),
antigravityCreds: createCredsManager('antigravity'),
// 文件上传
uploadFiles: createUploadManager('normal'),
antigravityUploadFiles: createUploadManager('antigravity'),
// 配置管理
currentConfig: {},
envLockedFields: new Set(),
// 日志管理
logWebSocket: null,
allLogs: [],
filteredLogs: [],
currentLogFilter: 'all',
// 使用统计
usageStatsData: {},
// 冷却倒计时
cooldownTimerInterval: null
};
// =====================================================================
// 凭证管理器工厂
// =====================================================================
function createCredsManager(type) {
const modeParam = type === 'antigravity' ? 'mode=antigravity' : 'mode=geminicli';
return {
type: type,
data: {},
filteredData: {},
currentPage: 1,
pageSize: 20,
selectedFiles: new Set(),
totalCount: 0,
currentStatusFilter: 'all',
currentErrorCodeFilter: 'all',
currentCooldownFilter: 'all',
statsData: { total: 0, normal: 0, disabled: 0 },
// API端点
getEndpoint: (action) => {
const endpoints = {
status: `./creds/status`,
action: `./creds/action`,
batchAction: `./creds/batch-action`,
download: `./creds/download`,
downloadAll: `./creds/download-all`,
detail: `./creds/detail`,
fetchEmail: `./creds/fetch-email`,
refreshAllEmails: `./creds/refresh-all-emails`,
deduplicate: `./creds/deduplicate-by-email`,
verifyProject: `./creds/verify-project`,
quota: `./creds/quota`
};
return endpoints[action] || '';
},
// 获取mode参数
getModeParam: () => modeParam,
// DOM元素ID前缀
getElementId: (suffix) => {
// 普通凭证的ID首字母小写,如 credsLoading
// Antigravity的ID是 antigravity + 首字母大写,如 antigravityCredsLoading
if (type === 'antigravity') {
return 'antigravity' + suffix.charAt(0).toUpperCase() + suffix.slice(1);
}
return suffix.charAt(0).toLowerCase() + suffix.slice(1);
},
// 刷新凭证列表
async refresh() {
const loading = document.getElementById(this.getElementId('CredsLoading'));
const list = document.getElementById(this.getElementId('CredsList'));
try {
loading.style.display = 'block';
list.innerHTML = '';
const offset = (this.currentPage - 1) * this.pageSize;
const errorCodeFilter = this.currentErrorCodeFilter || 'all';
const cooldownFilter = this.currentCooldownFilter || 'all';
const response = await fetch(
`${this.getEndpoint('status')}?offset=${offset}&limit=${this.pageSize}&status_filter=${this.currentStatusFilter}&error_code_filter=${errorCodeFilter}&cooldown_filter=${cooldownFilter}&${this.getModeParam()}`,
{ headers: getAuthHeaders() }
);
const data = await response.json();
if (response.ok) {
this.data = {};
data.items.forEach(item => {
this.data[item.filename] = {
filename: item.filename,
status: {
disabled: item.disabled,
error_codes: item.error_codes || [],
last_success: item.last_success,
},
user_email: item.user_email,
model_cooldowns: item.model_cooldowns || {}
};
});
this.totalCount = data.total;
// 使用后端返回的全局统计数据
if (data.stats) {
this.statsData = data.stats;
} else {
// 兼容旧版本后端
this.calculateStats();
}
this.updateStatsDisplay();
this.filteredData = this.data;
this.renderList();
this.updatePagination();
let msg = `已加载 ${data.total} 个${type === 'antigravity' ? 'Antigravity' : ''}凭证文件`;
if (this.currentStatusFilter !== 'all') {
msg += ` (筛选: ${this.currentStatusFilter === 'enabled' ? '仅启用' : '仅禁用'})`;
}
showStatus(msg, 'success');
} else {
showStatus(`加载失败: ${data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
} finally {
loading.style.display = 'none';
}
},
// 计算统计数据(仅用于兼容旧版本后端)
calculateStats() {
this.statsData = { total: this.totalCount, normal: 0, disabled: 0 };
Object.values(this.data).forEach(credInfo => {
if (credInfo.status.disabled) {
this.statsData.disabled++;
} else {
this.statsData.normal++;
}
});
},
// 更新统计显示
updateStatsDisplay() {
document.getElementById(this.getElementId('StatTotal')).textContent = this.statsData.total;
document.getElementById(this.getElementId('StatNormal')).textContent = this.statsData.normal;
document.getElementById(this.getElementId('StatDisabled')).textContent = this.statsData.disabled;
},
// 渲染凭证列表
renderList() {
const list = document.getElementById(this.getElementId('CredsList'));
list.innerHTML = '';
const entries = Object.entries(this.filteredData);
if (entries.length === 0) {
const msg = this.totalCount === 0 ? '暂无凭证文件' : '当前筛选条件下暂无数据';
list.innerHTML = `<p style="text-align: center; color: #666;">${msg}</p>`;
document.getElementById(this.getElementId('PaginationContainer')).style.display = 'none';
return;
}
entries.forEach(([, credInfo]) => {
list.appendChild(createCredCard(credInfo, this));
});
document.getElementById(this.getElementId('PaginationContainer')).style.display =
this.getTotalPages() > 1 ? 'flex' : 'none';
this.updateBatchControls();
},
// 获取总页数
getTotalPages() {
return Math.ceil(this.totalCount / this.pageSize);
},
// 更新分页信息
updatePagination() {
const totalPages = this.getTotalPages();
const startItem = (this.currentPage - 1) * this.pageSize + 1;
const endItem = Math.min(this.currentPage * this.pageSize, this.totalCount);
document.getElementById(this.getElementId('PaginationInfo')).textContent =
`第 ${this.currentPage} 页,共 ${totalPages} 页 (显示 ${startItem}-${endItem},共 ${this.totalCount} 项)`;
document.getElementById(this.getElementId('PrevPageBtn')).disabled = this.currentPage <= 1;
document.getElementById(this.getElementId('NextPageBtn')).disabled = this.currentPage >= totalPages;
},
// 切换页面
changePage(direction) {
const newPage = this.currentPage + direction;
if (newPage >= 1 && newPage <= this.getTotalPages()) {
this.currentPage = newPage;
this.refresh();
}
},
// 改变每页大小
changePageSize() {
this.pageSize = parseInt(document.getElementById(this.getElementId('PageSizeSelect')).value);
this.currentPage = 1;
this.refresh();
},
// 应用状态筛选
applyStatusFilter() {
this.currentStatusFilter = document.getElementById(this.getElementId('StatusFilter')).value;
const errorCodeFilterEl = document.getElementById(this.getElementId('ErrorCodeFilter'));
const cooldownFilterEl = document.getElementById(this.getElementId('CooldownFilter'));
this.currentErrorCodeFilter = errorCodeFilterEl ? errorCodeFilterEl.value : 'all';
this.currentCooldownFilter = cooldownFilterEl ? cooldownFilterEl.value : 'all';
this.currentPage = 1;
this.refresh();
},
// 更新批量控件
updateBatchControls() {
const selectedCount = this.selectedFiles.size;
document.getElementById(this.getElementId('SelectedCount')).textContent = `已选择 ${selectedCount} 项`;
const batchBtns = ['Enable', 'Disable', 'Delete', 'Verify'].map(action =>
document.getElementById(this.getElementId(`Batch${action}Btn`))
);
batchBtns.forEach(btn => btn && (btn.disabled = selectedCount === 0));
const selectAllCheckbox = document.getElementById(this.getElementId('SelectAllCheckbox'));
if (!selectAllCheckbox) return;
const checkboxes = document.querySelectorAll(`.${this.getElementId('file-checkbox')}`);
const currentPageSelectedCount = Array.from(checkboxes)
.filter(cb => this.selectedFiles.has(cb.getAttribute('data-filename'))).length;
if (currentPageSelectedCount === 0) {
selectAllCheckbox.indeterminate = false;
selectAllCheckbox.checked = false;
} else if (currentPageSelectedCount === checkboxes.length) {
selectAllCheckbox.indeterminate = false;
selectAllCheckbox.checked = true;
} else {
selectAllCheckbox.indeterminate = true;
}
checkboxes.forEach(cb => {
cb.checked = this.selectedFiles.has(cb.getAttribute('data-filename'));
});
},
// 凭证操作
async action(filename, action) {
try {
const response = await fetch(`${this.getEndpoint('action')}?${this.getModeParam()}`, {
method: 'POST',
headers: getAuthHeaders(),
body: JSON.stringify({ filename, action })
});
const data = await response.json();
if (response.ok) {
showStatus(data.message || `操作成功: ${action}`, 'success');
await this.refresh();
} else {
showStatus(`操作失败: ${data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
}
},
// 批量操作
async batchAction(action) {
const selectedFiles = Array.from(this.selectedFiles);
if (selectedFiles.length === 0) {
showStatus('请先选择要操作的文件', 'error');
return;
}
const actionNames = { enable: '启用', disable: '禁用', delete: '删除' };
const confirmMsg = action === 'delete'
? `确定要删除选中的 ${selectedFiles.length} 个文件吗?\n注意:此操作不可恢复!`
: `确定要${actionNames[action]}选中的 ${selectedFiles.length} 个文件吗?`;
if (!confirm(confirmMsg)) return;
try {
showStatus(`正在执行批量${actionNames[action]}操作...`, 'info');
const response = await fetch(`${this.getEndpoint('batchAction')}?${this.getModeParam()}`, {
method: 'POST',
headers: getAuthHeaders(),
body: JSON.stringify({ action, filenames: selectedFiles })
});
const data = await response.json();
if (response.ok) {
const successCount = data.success_count || data.succeeded;
showStatus(`批量操作完成:成功处理 ${successCount}/${selectedFiles.length} 个文件`, 'success');
this.selectedFiles.clear();
this.updateBatchControls();
await this.refresh();
} else {
showStatus(`批量操作失败: ${data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`批量操作网络错误: ${error.message}`, 'error');
}
}
};
}
// =====================================================================
// 文件上传管理器工厂
// =====================================================================
function createUploadManager(type) {
const modeParam = type === 'antigravity' ? 'mode=antigravity' : 'mode=geminicli';
const endpoint = `./auth/upload?${modeParam}`;
return {
type: type,
selectedFiles: [],
getElementId: (suffix) => {
// 普通上传的ID首字母小写,如 fileList
// Antigravity的ID是 antigravity + 首字母大写,如 antigravityFileList
if (type === 'antigravity') {
return 'antigravity' + suffix.charAt(0).toUpperCase() + suffix.slice(1);
}
return suffix.charAt(0).toLowerCase() + suffix.slice(1);
},
handleFileSelect(event) {
this.addFiles(Array.from(event.target.files));
},
addFiles(files) {
files.forEach(file => {
const isValid = file.type === 'application/json' || file.name.endsWith('.json') ||
file.type === 'application/zip' || file.name.endsWith('.zip');
if (isValid) {
if (!this.selectedFiles.find(f => f.name === file.name && f.size === file.size)) {
this.selectedFiles.push(file);
}
} else {
showStatus(`文件 ${file.name} 格式不支持,只支持JSON和ZIP文件`, 'error');
}
});
this.updateFileList();
},
updateFileList() {
const list = document.getElementById(this.getElementId('FileList'));
const section = document.getElementById(this.getElementId('FileListSection'));
if (!list || !section) {
console.warn('File list elements not found:', this.getElementId('FileList'));
return;
}
if (this.selectedFiles.length === 0) {
section.classList.add('hidden');
return;
}
section.classList.remove('hidden');
list.innerHTML = '';
this.selectedFiles.forEach((file, index) => {
const isZip = file.name.endsWith('.zip');
const fileIcon = isZip ? '📦' : '📄';
const fileType = isZip ? ' (ZIP压缩包)' : ' (JSON文件)';
const fileItem = document.createElement('div');
fileItem.className = 'file-item';
fileItem.innerHTML = `
<div>
<span class="file-name">${fileIcon} ${file.name}</span>
<span class="file-size">(${formatFileSize(file.size)}${fileType})</span>
</div>
<button class="remove-btn" onclick="${type === 'antigravity' ? 'removeAntigravityFile' : 'removeFile'}(${index})">删除</button>
`;
list.appendChild(fileItem);
});
},
removeFile(index) {
this.selectedFiles.splice(index, 1);
this.updateFileList();
},
clearFiles() {
this.selectedFiles = [];
this.updateFileList();
},
async upload() {
if (this.selectedFiles.length === 0) {
showStatus('请选择要上传的文件', 'error');
return;
}
const progressSection = document.getElementById(this.getElementId('UploadProgressSection'));
const progressFill = document.getElementById(this.getElementId('ProgressFill'));
const progressText = document.getElementById(this.getElementId('ProgressText'));
progressSection.classList.remove('hidden');
const formData = new FormData();
this.selectedFiles.forEach(file => formData.append('files', file));
if (this.selectedFiles.some(f => f.name.endsWith('.zip'))) {
showStatus('正在上传并解压ZIP文件...', 'info');
}
try {
const xhr = new XMLHttpRequest();
xhr.timeout = 300000; // 5分钟
xhr.upload.onprogress = (event) => {
if (event.lengthComputable) {
const percent = (event.loaded / event.total) * 100;
progressFill.style.width = percent + '%';
progressText.textContent = Math.round(percent) + '%';
}
};
xhr.onload = () => {
if (xhr.status === 200) {
try {
const data = JSON.parse(xhr.responseText);
showStatus(`成功上传 ${data.uploaded_count} 个${type === 'antigravity' ? 'Antigravity' : ''}文件`, 'success');
this.clearFiles();
progressSection.classList.add('hidden');
} catch (e) {
showStatus('上传失败: 服务器响应格式错误', 'error');
}
} else {
try {
const error = JSON.parse(xhr.responseText);
showStatus(`上传失败: ${error.detail || error.error || '未知错误'}`, 'error');
} catch (e) {
showStatus(`上传失败: HTTP ${xhr.status}`, 'error');
}
}
};
xhr.onerror = () => {
showStatus(`上传失败:连接中断 - 可能原因:文件过多(${this.selectedFiles.length}个)或网络不稳定。建议分批上传。`, 'error');
progressSection.classList.add('hidden');
};
xhr.ontimeout = () => {
showStatus('上传失败:请求超时 - 文件处理时间过长,请减少文件数量或检查网络连接', 'error');
progressSection.classList.add('hidden');
};
xhr.open('POST', endpoint);
xhr.setRequestHeader('Authorization', `Bearer ${AppState.authToken}`);
xhr.send(formData);
} catch (error) {
showStatus(`上传失败: ${error.message}`, 'error');
}
}
};
}
// =====================================================================
// 工具函数
// =====================================================================
function showStatus(message, type = 'info') {
const statusSection = document.getElementById('statusSection');
if (statusSection) {
// 清除之前的定时器
if (window._statusTimeout) {
clearTimeout(window._statusTimeout);
}
// 创建新的 toast
statusSection.innerHTML = `<div class="status ${type}">${message}</div>`;
const statusDiv = statusSection.querySelector('.status');
// 强制重绘以触发动画
statusDiv.offsetHeight;
statusDiv.classList.add('show');
// 3秒后淡出并移除
window._statusTimeout = setTimeout(() => {
statusDiv.classList.add('fade-out');
setTimeout(() => {
statusSection.innerHTML = '';
}, 300); // 等待淡出动画完成
}, 3000);
} else {
alert(message);
}
}
function getAuthHeaders() {
return {
'Content-Type': 'application/json',
'Authorization': `Bearer ${AppState.authToken}`
};
}
function formatFileSize(bytes) {
if (bytes < 1024) return bytes + ' B';
if (bytes < 1024 * 1024) return Math.round(bytes / 1024) + ' KB';
return Math.round(bytes / (1024 * 1024)) + ' MB';
}
function formatCooldownTime(remainingSeconds) {
const hours = Math.floor(remainingSeconds / 3600);
const minutes = Math.floor((remainingSeconds % 3600) / 60);
const seconds = remainingSeconds % 60;
if (hours > 0) return `${hours}h ${minutes}m ${seconds}s`;
if (minutes > 0) return `${minutes}m ${seconds}s`;
return `${seconds}s`;
}
// =====================================================================
// 凭证卡片创建(通用)
// =====================================================================
function createCredCard(credInfo, manager) {
const div = document.createElement('div');
const { status, filename } = credInfo;
const managerType = manager.type;
// 卡片样式
div.className = status.disabled ? 'cred-card disabled' : 'cred-card';
// 状态徽章
let statusBadges = '';
statusBadges += status.disabled
? '<span class="status-badge disabled">已禁用</span>'
: '<span class="status-badge enabled">已启用</span>';
if (status.error_codes && status.error_codes.length > 0) {
statusBadges += `<span class="error-codes">错误码: ${status.error_codes.join(', ')}</span>`;
const autoBan = status.error_codes.filter(c => c === 400 || c === 403);
if (autoBan.length > 0 && status.disabled) {
statusBadges += '<span class="status-badge" style="background-color: #e74c3c; color: white;">AUTO_BAN</span>';
}
} else {
statusBadges += '<span class="status-badge" style="background-color: #28a745; color: white;">无错误</span>';
}
// 模型级冷却状态
if (credInfo.model_cooldowns && Object.keys(credInfo.model_cooldowns).length > 0) {
const currentTime = Date.now() / 1000;
const activeCooldowns = Object.entries(credInfo.model_cooldowns)
.filter(([, until]) => until > currentTime)
.map(([model, until]) => {
const remaining = Math.max(0, Math.floor(until - currentTime));
const shortModel = model.replace('gemini-', '').replace('-exp', '')
.replace('2.0-', '2-').replace('1.5-', '1.5-');
return {
model: shortModel,
time: formatCooldownTime(remaining).replace(/s$/, '').replace(/ /g, ''),
fullModel: model
};
});
if (activeCooldowns.length > 0) {
activeCooldowns.slice(0, 2).forEach(item => {
statusBadges += `<span class="cooldown-badge" style="background-color: #17a2b8;" title="模型: ${item.fullModel}">🔧 ${item.model}: ${item.time}</span>`;
});
if (activeCooldowns.length > 2) {
const remaining = activeCooldowns.length - 2;
const remainingModels = activeCooldowns.slice(2).map(i => `${i.fullModel}: ${i.time}`).join('\n');
statusBadges += `<span class="cooldown-badge" style="background-color: #17a2b8;" title="其他模型:\n${remainingModels}">+${remaining}</span>`;
}
}
}
// 路径ID
const pathId = (managerType === 'antigravity' ? 'ag_' : '') + btoa(encodeURIComponent(filename)).replace(/[+/=]/g, '_');
// 操作按钮
const actionButtons = `
${status.disabled
? `<button class="cred-btn enable" data-filename="${filename}" data-action="enable">启用</button>`
: `<button class="cred-btn disable" data-filename="${filename}" data-action="disable">禁用</button>`
}
<button class="cred-btn view" onclick="toggle${managerType === 'antigravity' ? 'Antigravity' : ''}CredDetails('${pathId}')">查看内容</button>
<button class="cred-btn download" onclick="download${managerType === 'antigravity' ? 'Antigravity' : ''}Cred('${filename}')">下载</button>
<button class="cred-btn email" onclick="fetch${managerType === 'antigravity' ? 'Antigravity' : ''}UserEmail('${filename}')">查看账号邮箱</button>
${managerType === 'antigravity' ? `<button class="cred-btn" style="background-color: #17a2b8;" onclick="toggleAntigravityQuotaDetails('${pathId}')" title="查看该凭证的额度信息">查看额度</button>` : ''}
<button class="cred-btn" style="background-color: #ff9800;" onclick="verify${managerType === 'antigravity' ? 'Antigravity' : ''}ProjectId('${filename}')" title="重新获取Project ID,可恢复403错误">检验</button>
<button class="cred-btn delete" data-filename="${filename}" data-action="delete">删除</button>
`;
// 邮箱信息
const emailInfo = credInfo.user_email
? `<div class="cred-email" style="font-size: 12px; color: #666; margin-top: 2px;">${credInfo.user_email}</div>`
: '<div class="cred-email" style="font-size: 12px; color: #999; margin-top: 2px; font-style: italic;">未获取邮箱</div>';
const checkboxClass = manager.getElementId('file-checkbox');
div.innerHTML = `
<div class="cred-header">
<div style="display: flex; align-items: center; gap: 10px;">
<input type="checkbox" class="${checkboxClass}" data-filename="${filename}" onchange="toggle${managerType === 'antigravity' ? 'Antigravity' : ''}FileSelection('${filename}')">
<div>
<div class="cred-filename">${filename}</div>
${emailInfo}
</div>
</div>
<div class="cred-status">${statusBadges}</div>
</div>
<div class="cred-actions">${actionButtons}</div>
<div class="cred-details" id="details-${pathId}">
<div class="cred-content" data-filename="${filename}" data-loaded="false">点击"查看内容"按钮加载文件详情...</div>
</div>
${managerType === 'antigravity' ? `
<div class="cred-quota-details" id="quota-${pathId}" style="display: none;">
<div class="cred-quota-content" data-filename="${filename}" data-loaded="false">
点击"查看额度"按钮加载额度信息...
</div>
</div>
` : ''}
`;
// 添加事件监听
div.querySelectorAll('[data-filename][data-action]').forEach(button => {
button.addEventListener('click', function () {
const fn = this.getAttribute('data-filename');
const action = this.getAttribute('data-action');
if (action === 'delete') {
if (confirm(`确定要删除${managerType === 'antigravity' ? ' Antigravity ' : ''}凭证文件吗?\n${fn}`)) {
manager.action(fn, action);
}
} else {
manager.action(fn, action);
}
});
});
return div;
}
// =====================================================================
// 凭证详情切换
// =====================================================================
async function toggleCredDetails(pathId) {
await toggleCredDetailsCommon(pathId, AppState.creds);
}
async function toggleAntigravityCredDetails(pathId) {
await toggleCredDetailsCommon(pathId, AppState.antigravityCreds);
}
async function toggleCredDetailsCommon(pathId, manager) {
const details = document.getElementById('details-' + pathId);
if (!details) return;
const isShowing = details.classList.toggle('show');
if (isShowing) {
const contentDiv = details.querySelector('.cred-content');
const filename = contentDiv.getAttribute('data-filename');
const loaded = contentDiv.getAttribute('data-loaded');
if (loaded === 'false' && filename) {
contentDiv.textContent = '正在加载文件内容...';
try {
const modeParam = manager.type === 'antigravity' ? 'mode=antigravity' : 'mode=geminicli';
const endpoint = `./creds/detail/${encodeURIComponent(filename)}?${modeParam}`;
const response = await fetch(endpoint, { headers: getAuthHeaders() });
const data = await response.json();
if (response.ok && data.content) {
contentDiv.textContent = JSON.stringify(data.content, null, 2);
contentDiv.setAttribute('data-loaded', 'true');
} else {
contentDiv.textContent = '无法加载文件内容: ' + (data.error || data.detail || '未知错误');
}
} catch (error) {
contentDiv.textContent = '加载文件内容失败: ' + error.message;
}
}
}
}
// =====================================================================
// 登录相关函数
// =====================================================================
async function login() {
const password = document.getElementById('loginPassword').value;
if (!password) {
showStatus('请输入密码', 'error');
return;
}
try {
const response = await fetch('./auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ password })
});
const data = await response.json();
if (response.ok) {
AppState.authToken = data.token;
localStorage.setItem('gcli2api_auth_token', AppState.authToken);
document.getElementById('loginSection').classList.add('hidden');
document.getElementById('mainSection').classList.remove('hidden');
showStatus('登录成功', 'success');
// 显示面板后初始化滑块
requestAnimationFrame(() => initTabSlider());
} else {
showStatus(`登录失败: ${data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
}
}
async function autoLogin() {
const savedToken = localStorage.getItem('gcli2api_auth_token');
if (!savedToken) return false;
AppState.authToken = savedToken;
try {
const response = await fetch('./config/get', {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${AppState.authToken}`
}
});
if (response.ok) {
document.getElementById('loginSection').classList.add('hidden');
document.getElementById('mainSection').classList.remove('hidden');
showStatus('自动登录成功', 'success');
// 显示面板后初始化滑块
requestAnimationFrame(() => initTabSlider());
return true;
} else if (response.status === 401) {
localStorage.removeItem('gcli2api_auth_token');
AppState.authToken = '';
return false;
}
return false;
} catch (error) {
return false;
}
}
function logout() {
localStorage.removeItem('gcli2api_auth_token');
AppState.authToken = '';
document.getElementById('loginSection').classList.remove('hidden');
document.getElementById('mainSection').classList.add('hidden');
showStatus('已退出登录', 'info');
const passwordInput = document.getElementById('loginPassword');
if (passwordInput) passwordInput.value = '';
}
function handlePasswordEnter(event) {
if (event.key === 'Enter') login();
}
// =====================================================================
// 标签页切换
// =====================================================================
// 更新滑块位置
function updateTabSlider(targetTab, animate = true) {
const slider = document.querySelector('.tab-slider');
const tabs = document.querySelector('.tabs');
if (!slider || !tabs || !targetTab) return;
// 获取按钮位置和容器宽度
const tabLeft = targetTab.offsetLeft;
const tabWidth = targetTab.offsetWidth;
const tabsWidth = tabs.scrollWidth;
// 使用 left 和 right 同时控制,确保动画同步
const rightValue = tabsWidth - tabLeft - tabWidth;
if (animate) {
slider.style.left = `${tabLeft}px`;
slider.style.right = `${rightValue}px`;
} else {
// 首次加载时不使用动画
slider.style.transition = 'none';
slider.style.left = `${tabLeft}px`;
slider.style.right = `${rightValue}px`;
// 强制重绘后恢复过渡
slider.offsetHeight;
slider.style.transition = '';
}
}
// 初始化滑块位置
function initTabSlider() {
const activeTab = document.querySelector('.tab.active');
if (activeTab) {
updateTabSlider(activeTab, false);
}
}
// 页面加载和窗口大小变化时初始化滑块
document.addEventListener('DOMContentLoaded', initTabSlider);
window.addEventListener('resize', () => {
const activeTab = document.querySelector('.tab.active');
if (activeTab) updateTabSlider(activeTab, false);
});
function switchTab(tabName) {
// 获取当前活动的内容区域
const currentContent = document.querySelector('.tab-content.active');
const targetContent = document.getElementById(tabName + 'Tab');
// 如果点击的是当前标签页,不做任何操作
if (currentContent === targetContent) return;
// 找到目标标签按钮
const targetTab = event && event.target ? event.target :
document.querySelector(`.tab[onclick*="'${tabName}'"]`);
// 移除所有标签页的active状态
document.querySelectorAll('.tab').forEach(tab => tab.classList.remove('active'));
// 添加当前点击标签的active状态
if (targetTab) {
targetTab.classList.add('active');
// 更新滑块位置(带动画)
updateTabSlider(targetTab, true);
}
// 淡出当前内容
if (currentContent) {
// 设置淡出过渡
currentContent.style.transition = 'opacity 0.18s ease-out, transform 0.18s ease-out';
currentContent.style.opacity = '0';
currentContent.style.transform = 'translateX(-12px)';
setTimeout(() => {
currentContent.classList.remove('active');
currentContent.style.transition = '';
currentContent.style.opacity = '';
currentContent.style.transform = '';
// 淡入新内容
if (targetContent) {
// 先设置初始状态(在添加 active 类之前)
targetContent.style.opacity = '0';
targetContent.style.transform = 'translateX(12px)';
targetContent.style.transition = 'none'; // 暂时禁用过渡
// 添加 active 类使元素可见
targetContent.classList.add('active');
// 使用双重 requestAnimationFrame 确保浏览器完成重绘
requestAnimationFrame(() => {
requestAnimationFrame(() => {
// 启用过渡并应用最终状态
targetContent.style.transition = 'opacity 0.25s ease-out, transform 0.25s ease-out';
targetContent.style.opacity = '1';
targetContent.style.transform = 'translateX(0)';
// 清理内联样式并执行数据加载
setTimeout(() => {
targetContent.style.transition = '';
targetContent.style.opacity = '';
targetContent.style.transform = '';
// 动画完成后触发数据加载
triggerTabDataLoad(tabName);
}, 260);
});
});
}
}, 180);
} else {
// 如果没有当前内容(首次加载),直接显示目标内容
if (targetContent) {
targetContent.classList.add('active');
// 直接触发数据加载
triggerTabDataLoad(tabName);
}
}
}
// 标签页数据加载(从动画中分离出来)
function triggerTabDataLoad(tabName) {
if (tabName === 'manage') AppState.creds.refresh();
if (tabName === 'antigravity-manage') AppState.antigravityCreds.refresh();
if (tabName === 'config') loadConfig();
if (tabName === 'logs') connectWebSocket();
}
// =====================================================================
// OAuth认证相关函数
// =====================================================================
async function startAuth() {
const projectId = document.getElementById('projectId').value.trim();
AppState.currentProjectId = projectId || null;
const btn = document.getElementById('getAuthBtn');
btn.disabled = true;
btn.textContent = '正在获取认证链接...';
try {
const requestBody = projectId ? { project_id: projectId } : {};
showStatus(projectId ? '使用指定的项目ID生成认证链接...' : '将尝试自动检测项目ID,正在生成认证链接...', 'info');
const response = await fetch('./auth/start', {
method: 'POST',
headers: getAuthHeaders(),
body: JSON.stringify(requestBody)
});
const data = await response.json();
if (response.ok) {
document.getElementById('authUrl').href = data.auth_url;
document.getElementById('authUrl').textContent = data.auth_url;
document.getElementById('authUrlSection').classList.remove('hidden');
const msg = data.auto_project_detection
? '认证链接已生成(将在认证完成后自动检测项目ID),请点击链接完成授权'
: `认证链接已生成(项目ID: ${data.detected_project_id}),请点击链接完成授权`;
showStatus(msg, 'info');
AppState.authInProgress = true;
} else {
showStatus(`错误: ${data.error || '获取认证链接失败'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
} finally {
btn.disabled = false;
btn.textContent = '获取认证链接';
}
}
async function getCredentials() {
if (!AppState.authInProgress) {
showStatus('请先获取认证链接并完成授权', 'error');
return;
}
const btn = document.getElementById('getCredsBtn');
btn.disabled = true;
btn.textContent = '等待OAuth回调中...';
try {
showStatus('正在等待OAuth回调,这可能需要一些时间...', 'info');
const requestBody = AppState.currentProjectId ? { project_id: AppState.currentProjectId } : {};
const response = await fetch('./auth/callback', {
method: 'POST',
headers: getAuthHeaders(),
body: JSON.stringify(requestBody)
});
const data = await response.json();
if (response.ok) {
document.getElementById('credentialsContent').textContent = JSON.stringify(data.credentials, null, 2);
const msg = data.auto_detected_project
? `✅ 认证成功!项目ID已自动检测为: ${data.credentials.project_id},文件已保存到: ${data.file_path}`
: `✅ 认证成功!文件已保存到: ${data.file_path}`;
showStatus(msg, 'success');
document.getElementById('credentialsSection').classList.remove('hidden');
AppState.authInProgress = false;
} else if (data.requires_project_selection && data.available_projects) {
let projectOptions = "请选择一个项目:\n\n";
data.available_projects.forEach((project, index) => {
projectOptions += `${index + 1}. ${project.name} (${project.project_id})\n`;
});
projectOptions += `\n请输入序号 (1-${data.available_projects.length}):`;
const selection = prompt(projectOptions);
const projectIndex = parseInt(selection) - 1;
if (projectIndex >= 0 && projectIndex < data.available_projects.length) {
AppState.currentProjectId = data.available_projects[projectIndex].project_id;
btn.textContent = '重新尝试获取认证文件';
showStatus(`使用选择的项目重新尝试...`, 'info');
setTimeout(() => getCredentials(), 1000);
return;
} else {
showStatus('无效的选择,请重新开始认证', 'error');
}
} else if (data.requires_manual_project_id) {
const userProjectId = prompt('无法自动检测项目ID,请手动输入您的Google Cloud项目ID:');
if (userProjectId && userProjectId.trim()) {
AppState.currentProjectId = userProjectId.trim();
btn.textContent = '重新尝试获取认证文件';
showStatus('使用手动输入的项目ID重新尝试...', 'info');
setTimeout(() => getCredentials(), 1000);
return;
} else {
showStatus('需要项目ID才能完成认证,请重新开始并输入正确的项目ID', 'error');
}
} else {
showStatus(`❌ 错误: ${data.error || '获取认证文件失败'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
} finally {
btn.disabled = false;
btn.textContent = '获取认证文件';
}
}
// =====================================================================
// Antigravity 认证相关函数
// =====================================================================
async function startAntigravityAuth() {
const btn = document.getElementById('getAntigravityAuthBtn');
btn.disabled = true;
btn.textContent = '生成认证链接中...';
try {
showStatus('正在生成 Antigravity 认证链接...', 'info');
const response = await fetch('./auth/start', {
method: 'POST',
headers: getAuthHeaders(),
body: JSON.stringify({ mode: 'antigravity' })
});
const data = await response.json();
if (response.ok) {
AppState.antigravityAuthState = data.state;
AppState.antigravityAuthInProgress = true;
const authUrlLink = document.getElementById('antigravityAuthUrl');
authUrlLink.href = data.auth_url;
authUrlLink.textContent = data.auth_url;
document.getElementById('antigravityAuthUrlSection').classList.remove('hidden');
showStatus('✅ Antigravity 认证链接已生成!请点击链接完成授权', 'success');
} else {
showStatus(`❌ 错误: ${data.error || '生成认证链接失败'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
} finally {
btn.disabled = false;
btn.textContent = '获取 Antigravity 认证链接';
}
}
async function getAntigravityCredentials() {
if (!AppState.antigravityAuthInProgress) {
showStatus('请先获取 Antigravity 认证链接并完成授权', 'error');
return;
}
const btn = document.getElementById('getAntigravityCredsBtn');
btn.disabled = true;
btn.textContent = '等待OAuth回调中...';
try {
showStatus('正在等待 Antigravity OAuth回调...', 'info');
const response = await fetch('./auth/callback', {
method: 'POST',
headers: getAuthHeaders(),
body: JSON.stringify({ mode: 'antigravity' })
});
const data = await response.json();
if (response.ok) {
document.getElementById('antigravityCredsContent').textContent = JSON.stringify(data.credentials, null, 2);
document.getElementById('antigravityCredsSection').classList.remove('hidden');
AppState.antigravityAuthInProgress = false;
showStatus(`✅ Antigravity 认证成功!文件已保存到: ${data.file_path}`, 'success');
} else {
showStatus(`❌ 错误: ${data.error || '获取认证文件失败'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
} finally {
btn.disabled = false;
btn.textContent = '获取 Antigravity 凭证';
}
}
function downloadAntigravityCredentials() {
const content = document.getElementById('antigravityCredsContent').textContent;
const blob = new Blob([content], { type: 'application/json' });
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `antigravity-credential-${Date.now()}.json`;
a.click();
window.URL.revokeObjectURL(url);
}
// =====================================================================
// 回调URL处理
// =====================================================================
function toggleProjectIdSection() {
const section = document.getElementById('projectIdSection');
const icon = document.getElementById('projectIdToggleIcon');
if (section.style.display === 'none') {
section.style.display = 'block';
icon.style.transform = 'rotate(90deg)';
icon.textContent = '▼';
} else {
section.style.display = 'none';
icon.style.transform = 'rotate(0deg)';
icon.textContent = '▶';
}
}
function toggleCallbackUrlSection() {
const section = document.getElementById('callbackUrlSection');
const icon = document.getElementById('callbackUrlToggleIcon');
if (section.style.display === 'none') {
section.style.display = 'block';
icon.style.transform = 'rotate(180deg)';
icon.textContent = '▲';
} else {
section.style.display = 'none';
icon.style.transform = 'rotate(0deg)';
icon.textContent = '▼';
}
}
function toggleAntigravityCallbackUrlSection() {
const section = document.getElementById('antigravityCallbackUrlSection');
const icon = document.getElementById('antigravityCallbackUrlToggleIcon');
if (section.style.display === 'none') {
section.style.display = 'block';
icon.style.transform = 'rotate(180deg)';
icon.textContent = '▲';
} else {
section.style.display = 'none';
icon.style.transform = 'rotate(0deg)';
icon.textContent = '▼';
}
}
async function processCallbackUrl() {
const callbackUrl = document.getElementById('callbackUrlInput').value.trim();
if (!callbackUrl) {
showStatus('请输入回调URL', 'error');
return;
}
if (!callbackUrl.startsWith('http://') && !callbackUrl.startsWith('https://')) {
showStatus('请输入有效的URL(以http://或https://开头)', 'error');
return;
}
if (!callbackUrl.includes('code=') || !callbackUrl.includes('state=')) {
showStatus('❌ 这不是有效的回调URL!请确保:\n1. 已完成Google OAuth授权\n2. 复制的是浏览器地址栏的完整URL\n3. URL包含code和state参数', 'error');
return;
}
showStatus('正在从回调URL获取凭证...', 'info');
try {
const projectId = document.getElementById('projectId')?.value.trim() || null;
const response = await fetch('./auth/callback-url', {
method: 'POST',
headers: getAuthHeaders(),
body: JSON.stringify({ callback_url: callbackUrl, project_id: projectId })
});
const result = await response.json();
if (result.credentials) {
showStatus(result.message || '从回调URL获取凭证成功!', 'success');
document.getElementById('credentialsContent').innerHTML = '<pre>' + JSON.stringify(result.credentials, null, 2) + '</pre>';
document.getElementById('credentialsSection').classList.remove('hidden');
} else if (result.requires_manual_project_id) {
showStatus('需要手动指定项目ID,请在高级选项中填入Google Cloud项目ID后重试', 'error');
} else if (result.requires_project_selection) {
let msg = '<br><strong>可用项目:</strong><br>';
result.available_projects.forEach(p => {
msg += `• ${p.name} (ID: ${p.project_id})<br>`;
});
showStatus('检测到多个项目,请在高级选项中指定项目ID:' + msg, 'error');
} else {
showStatus(result.error || '从回调URL获取凭证失败', 'error');
}
document.getElementById('callbackUrlInput').value = '';
} catch (error) {
showStatus(`从回调URL获取凭证失败: ${error.message}`, 'error');
}
}
async function processAntigravityCallbackUrl() {
const callbackUrl = document.getElementById('antigravityCallbackUrlInput').value.trim();
if (!callbackUrl) {
showStatus('请输入回调URL', 'error');
return;
}
if (!callbackUrl.startsWith('http://') && !callbackUrl.startsWith('https://')) {
showStatus('请输入有效的URL(以http://或https://开头)', 'error');
return;
}
if (!callbackUrl.includes('code=') || !callbackUrl.includes('state=')) {
showStatus('❌ 这不是有效的回调URL!请确保包含code和state参数', 'error');
return;
}
showStatus('正在从回调URL获取 Antigravity 凭证...', 'info');
try {
const response = await fetch('./auth/callback-url', {
method: 'POST',
headers: getAuthHeaders(),
body: JSON.stringify({ callback_url: callbackUrl, mode: 'antigravity' })
});
const result = await response.json();
if (result.credentials) {
showStatus(result.message || '从回调URL获取 Antigravity 凭证成功!', 'success');
document.getElementById('antigravityCredsContent').textContent = JSON.stringify(result.credentials, null, 2);
document.getElementById('antigravityCredsSection').classList.remove('hidden');
} else {
showStatus(result.error || '从回调URL获取 Antigravity 凭证失败', 'error');
}
document.getElementById('antigravityCallbackUrlInput').value = '';
} catch (error) {
showStatus(`从回调URL获取 Antigravity 凭证失败: ${error.message}`, 'error');
}
}
// =====================================================================
// 全局兼容函数(供HTML调用)
// =====================================================================
// 普通凭证管理
function refreshCredsStatus() { AppState.creds.refresh(); }
function applyStatusFilter() { AppState.creds.applyStatusFilter(); }
function changePage(direction) { AppState.creds.changePage(direction); }
function changePageSize() { AppState.creds.changePageSize(); }
function toggleFileSelection(filename) {
if (AppState.creds.selectedFiles.has(filename)) {
AppState.creds.selectedFiles.delete(filename);
} else {
AppState.creds.selectedFiles.add(filename);
}
AppState.creds.updateBatchControls();
}
function toggleSelectAll() {
const checkbox = document.getElementById('selectAllCheckbox');
const checkboxes = document.querySelectorAll('.file-checkbox');
if (checkbox.checked) {
checkboxes.forEach(cb => AppState.creds.selectedFiles.add(cb.getAttribute('data-filename')));
} else {
AppState.creds.selectedFiles.clear();
}
checkboxes.forEach(cb => cb.checked = checkbox.checked);
AppState.creds.updateBatchControls();
}
function batchAction(action) { AppState.creds.batchAction(action); }
function downloadCred(filename) {
fetch(`./creds/download/${filename}`, { headers: { 'Authorization': `Bearer ${AppState.authToken}` } })
.then(r => r.ok ? r.blob() : Promise.reject())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
showStatus(`已下载文件: ${filename}`, 'success');
})
.catch(() => showStatus(`下载失败: ${filename}`, 'error'));
}
async function downloadAllCreds() {
try {
const response = await fetch('./creds/download-all', {
headers: { 'Authorization': `Bearer ${AppState.authToken}` }
});
if (response.ok) {
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'credentials.zip';
a.click();
window.URL.revokeObjectURL(url);
showStatus('已下载所有凭证文件', 'success');
}
} catch (error) {
showStatus(`打包下载失败: ${error.message}`, 'error');
}
}
// Antigravity凭证管理
function refreshAntigravityCredsList() { AppState.antigravityCreds.refresh(); }
function applyAntigravityStatusFilter() { AppState.antigravityCreds.applyStatusFilter(); }
function changeAntigravityPage(direction) { AppState.antigravityCreds.changePage(direction); }
function changeAntigravityPageSize() { AppState.antigravityCreds.changePageSize(); }
function toggleAntigravityFileSelection(filename) {
if (AppState.antigravityCreds.selectedFiles.has(filename)) {
AppState.antigravityCreds.selectedFiles.delete(filename);
} else {
AppState.antigravityCreds.selectedFiles.add(filename);
}
AppState.antigravityCreds.updateBatchControls();
}
function toggleSelectAllAntigravity() {
const checkbox = document.getElementById('selectAllAntigravityCheckbox');
const checkboxes = document.querySelectorAll('.antigravityFile-checkbox');
if (checkbox.checked) {
checkboxes.forEach(cb => AppState.antigravityCreds.selectedFiles.add(cb.getAttribute('data-filename')));
} else {
AppState.antigravityCreds.selectedFiles.clear();
}
checkboxes.forEach(cb => cb.checked = checkbox.checked);
AppState.antigravityCreds.updateBatchControls();
}
function batchAntigravityAction(action) { AppState.antigravityCreds.batchAction(action); }
function downloadAntigravityCred(filename) {
fetch(`./creds/download/${filename}?mode=antigravity`, { headers: getAuthHeaders() })
.then(r => r.ok ? r.blob() : Promise.reject())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
showStatus(`✅ 已下载: ${filename}`, 'success');
})
.catch(() => showStatus(`下载失败: ${filename}`, 'error'));
}
function deleteAntigravityCred(filename) {
if (confirm(`确定要删除 ${filename} 吗?`)) {
AppState.antigravityCreds.action(filename, 'delete');
}
}
async function downloadAllAntigravityCreds() {
try {
const response = await fetch('./creds/download-all?mode=antigravity', { headers: getAuthHeaders() });
if (response.ok) {
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `antigravity_credentials_${Date.now()}.zip`;
a.click();
window.URL.revokeObjectURL(url);
showStatus('✅ 所有Antigravity凭证已打包下载', 'success');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
}
}
// 文件上传
function handleFileSelect(event) { AppState.uploadFiles.handleFileSelect(event); }
function removeFile(index) { AppState.uploadFiles.removeFile(index); }
function clearFiles() { AppState.uploadFiles.clearFiles(); }
function uploadFiles() { AppState.uploadFiles.upload(); }
function handleAntigravityFileSelect(event) { AppState.antigravityUploadFiles.handleFileSelect(event); }
function handleAntigravityFileDrop(event) {
event.preventDefault();
event.currentTarget.style.borderColor = '#007bff';
event.currentTarget.style.backgroundColor = '#f8f9fa';
AppState.antigravityUploadFiles.addFiles(Array.from(event.dataTransfer.files));
}
function removeAntigravityFile(index) { AppState.antigravityUploadFiles.removeFile(index); }
function clearAntigravityFiles() { AppState.antigravityUploadFiles.clearFiles(); }
function uploadAntigravityFiles() { AppState.antigravityUploadFiles.upload(); }
// 邮箱相关
// 辅助函数:根据文件名更新卡片中的邮箱显示
function updateEmailDisplay(filename, email, managerType = 'normal') {
// 查找对应的凭证卡片
const containerId = managerType === 'antigravity' ? 'antigravityCredsList' : 'credsList';
const container = document.getElementById(containerId);
if (!container) return false;
// 通过 data-filename 找到对应的复选框,再找到其父卡片
const checkbox = container.querySelector(`input[data-filename="${filename}"]`);
if (!checkbox) return false;
// 找到对应的 cred-card 元素
const card = checkbox.closest('.cred-card');
if (!card) return false;
// 找到邮箱显示元素
const emailDiv = card.querySelector('.cred-email');
if (emailDiv) {
emailDiv.textContent = email;
emailDiv.style.color = '#666';
emailDiv.style.fontStyle = 'normal';
return true;
}
return false;
}
async function fetchUserEmail(filename) {
try {
showStatus('正在获取用户邮箱...', 'info');
const response = await fetch(`./creds/fetch-email/${encodeURIComponent(filename)}`, {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok && data.user_email) {
showStatus(`成功获取邮箱: ${data.user_email}`, 'success');
// 直接更新卡片中的邮箱显示,不刷新整个列表
updateEmailDisplay(filename, data.user_email, 'normal');
} else {
showStatus(data.message || '无法获取用户邮箱', 'error');
}
} catch (error) {
showStatus(`获取邮箱失败: ${error.message}`, 'error');
}
}
async function fetchAntigravityUserEmail(filename) {
try {
showStatus('正在获取用户邮箱...', 'info');
const response = await fetch(`./creds/fetch-email/${encodeURIComponent(filename)}?mode=antigravity`, {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok && data.user_email) {
showStatus(`成功获取邮箱: ${data.user_email}`, 'success');
// 直接更新卡片中的邮箱显示,不刷新整个列表
updateEmailDisplay(filename, data.user_email, 'antigravity');
} else {
showStatus(data.message || '无法获取用户邮箱', 'error');
}
} catch (error) {
showStatus(`获取邮箱失败: ${error.message}`, 'error');
}
}
async function verifyProjectId(filename) {
try {
// 显示加载状态
showStatus('🔍 正在检验Project ID,请稍候...', 'info');
const response = await fetch(`./creds/verify-project/${encodeURIComponent(filename)}`, {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok && data.success) {
// 成功时显示绿色成功消息和Project ID
const successMsg = `✅ 检验成功!\n文件: ${filename}\nProject ID: ${data.project_id}\n\n${data.message}`;
showStatus(successMsg.replace(/\n/g, '<br>'), 'success');
// 弹出成功提示
alert(`✅ 检验成功!\n\n文件: ${filename}\nProject ID: ${data.project_id}\n\n${data.message}`);
await AppState.creds.refresh();
} else {
// 失败时显示红色错误消息
const errorMsg = data.message || '检验失败';
showStatus(`❌ ${errorMsg}`, 'error');
alert(`❌ 检验失败\n\n${errorMsg}`);
}
} catch (error) {
const errorMsg = `检验失败: ${error.message}`;
showStatus(`❌ ${errorMsg}`, 'error');
alert(`❌ ${errorMsg}`);
}
}
async function verifyAntigravityProjectId(filename) {
try {
// 显示加载状态
showStatus('🔍 正在检验Antigravity Project ID,请稍候...', 'info');
const response = await fetch(`./creds/verify-project/${encodeURIComponent(filename)}?mode=antigravity`, {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok && data.success) {
// 成功时显示绿色成功消息和Project ID
const successMsg = `✅ 检验成功!\n文件: ${filename}\nProject ID: ${data.project_id}\n\n${data.message}`;
showStatus(successMsg.replace(/\n/g, '<br>'), 'success');
// 弹出成功提示
alert(`✅ Antigravity检验成功!\n\n文件: ${filename}\nProject ID: ${data.project_id}\n\n${data.message}`);
await AppState.antigravityCreds.refresh();
} else {
// 失败时显示红色错误消息
const errorMsg = data.message || '检验失败';
showStatus(`❌ ${errorMsg}`, 'error');
alert(`❌ 检验失败\n\n${errorMsg}`);
}
} catch (error) {
const errorMsg = `检验失败: ${error.message}`;
showStatus(`❌ ${errorMsg}`, 'error');
alert(`❌ ${errorMsg}`);
}
}
async function toggleAntigravityQuotaDetails(pathId) {
const quotaDetails = document.getElementById('quota-' + pathId);
if (!quotaDetails) return;
// 切换显示状态
const isShowing = quotaDetails.style.display === 'block';
if (isShowing) {
// 收起
quotaDetails.style.display = 'none';
} else {
// 展开
quotaDetails.style.display = 'block';
const contentDiv = quotaDetails.querySelector('.cred-quota-content');
const filename = contentDiv.getAttribute('data-filename');
const loaded = contentDiv.getAttribute('data-loaded');
// 如果还没加载过,则加载数据
if (loaded === 'false' && filename) {
contentDiv.innerHTML = '<div style="text-align: center; padding: 20px; color: #666;">📊 正在加载额度信息...</div>';
try {
const response = await fetch(`./creds/quota/${encodeURIComponent(filename)}?mode=antigravity`, {
method: 'GET',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok && data.success) {
// 成功时渲染美化的额度信息
const models = data.models || {};
if (Object.keys(models).length === 0) {
contentDiv.innerHTML = `
<div style="text-align: center; padding: 20px; color: #999;">
<div style="font-size: 48px; margin-bottom: 10px;">📊</div>
<div>暂无额度信息</div>
</div>
`;
} else {
let quotaHTML = `
<div style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 15px; border-radius: 8px 8px 0 0; margin: -10px -10px 15px -10px;">
<h4 style="margin: 0; font-size: 16px; display: flex; align-items: center; gap: 8px;">
<span style="font-size: 20px;">📊</span>
<span>额度信息详情</span>
</h4>
<div style="font-size: 12px; opacity: 0.9; margin-top: 5px;">文件: ${filename}</div>
</div>
<div style="display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px;">
`;
for (const [modelName, quotaData] of Object.entries(models)) {
// 后端返回的是剩余比例 (0-1),不是绝对数量
const remainingFraction = quotaData.remaining || 0;
const resetTime = quotaData.resetTime || 'N/A';
// 计算已使用百分比(1 - 剩余比例)
const usedPercentage = Math.round((1 - remainingFraction) * 100);
const remainingPercentage = Math.round(remainingFraction * 100);
// 根据使用情况选择颜色
let percentageColor = '#28a745'; // 绿色:使用少
if (usedPercentage >= 90) percentageColor = '#dc3545'; // 红色:使用多
else if (usedPercentage >= 70) percentageColor = '#ffc107'; // 黄色:使用较多
else if (usedPercentage >= 50) percentageColor = '#17a2b8'; // 蓝色:使用中等
quotaHTML += `
<div style="background: white; border-left: 4px solid ${percentageColor}; border-radius: 4px; padding: 8px 10px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px;">
<div style="font-weight: bold; color: #333; font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; margin-right: 8px;" title="${modelName} - 剩余${remainingPercentage}% - ${resetTime}">
${modelName}
</div>
<div style="font-size: 13px; font-weight: bold; color: ${percentageColor}; white-space: nowrap;">
${remainingPercentage}%
</div>
</div>
<div style="width: 100%; height: 8px; background-color: #e9ecef; border-radius: 4px; overflow: hidden; margin-bottom: 4px;">
<div style="width: ${usedPercentage}%; height: 100%; background-color: ${percentageColor}; transition: width 0.3s ease;"></div>
</div>
<div style="font-size: 10px; color: #666; text-align: right;">
${resetTime !== 'N/A' ? '🔄 ' + resetTime : ''}
</div>
</div>
`;
}
quotaHTML += '</div>';
contentDiv.innerHTML = quotaHTML;
}
contentDiv.setAttribute('data-loaded', 'true');
showStatus('✅ 成功加载额度信息', 'success');
} else {
// 失败时显示错误
const errorMsg = data.error || '获取额度信息失败';
contentDiv.innerHTML = `
<div style="text-align: center; padding: 20px; color: #dc3545;">
<div style="font-size: 48px; margin-bottom: 10px;">❌</div>
<div style="font-weight: bold; margin-bottom: 5px;">获取额度信息失败</div>
<div style="font-size: 13px; color: #666;">${errorMsg}</div>
</div>
`;
showStatus(`❌ ${errorMsg}`, 'error');
}
} catch (error) {
contentDiv.innerHTML = `
<div style="text-align: center; padding: 20px; color: #dc3545;">
<div style="font-size: 48px; margin-bottom: 10px;">❌</div>
<div style="font-weight: bold; margin-bottom: 5px;">网络错误</div>
<div style="font-size: 13px; color: #666;">${error.message}</div>
</div>
`;
showStatus(`❌ 获取额度信息失败: ${error.message}`, 'error');
}
}
}
}
async function batchVerifyProjectIds() {
const selectedFiles = Array.from(AppState.creds.selectedFiles);
if (selectedFiles.length === 0) {
showStatus('❌ 请先选择要检验的凭证', 'error');
alert('请先选择要检验的凭证');
return;
}
if (!confirm(`确定要批量检验 ${selectedFiles.length} 个凭证的Project ID吗?\n\n将并行检验以加快速度。`)) {
return;
}
showStatus(`🔍 正在并行检验 ${selectedFiles.length} 个凭证,请稍候...`, 'info');
// 并行执行所有检验请求
const promises = selectedFiles.map(async (filename) => {
try {
const response = await fetch(`./creds/verify-project/${encodeURIComponent(filename)}`, {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok && data.success) {
return { success: true, filename, projectId: data.project_id, message: data.message };
} else {
return { success: false, filename, error: data.message || '失败' };
}
} catch (error) {
return { success: false, filename, error: error.message };
}
});
// 等待所有请求完成
const results = await Promise.all(promises);
// 统计结果
let successCount = 0;
let failCount = 0;
const resultMessages = [];
results.forEach(result => {
if (result.success) {
successCount++;
resultMessages.push(`✅ ${result.filename}: ${result.projectId}`);
} else {
failCount++;
resultMessages.push(`❌ ${result.filename}: ${result.error}`);
}
});
await AppState.creds.refresh();
const summary = `批量检验完成!\n\n成功: ${successCount} 个\n失败: ${failCount} 个\n总计: ${selectedFiles.length} 个\n\n详细结果:\n${resultMessages.join('\n')}`;
if (failCount === 0) {
showStatus(`✅ 全部检验成功!成功检验 ${successCount}/${selectedFiles.length} 个凭证`, 'success');
} else if (successCount === 0) {
showStatus(`❌ 全部检验失败!失败 ${failCount}/${selectedFiles.length} 个凭证`, 'error');
} else {
showStatus(`⚠️ 批量检验完成:成功 ${successCount}/${selectedFiles.length} 个,失败 ${failCount} 个`, 'info');
}
console.log(summary);
alert(summary);
}
async function batchVerifyAntigravityProjectIds() {
const selectedFiles = Array.from(AppState.antigravityCreds.selectedFiles);
if (selectedFiles.length === 0) {
showStatus('❌ 请先选择要检验的Antigravity凭证', 'error');
alert('请先选择要检验的Antigravity凭证');
return;
}
if (!confirm(`确定要批量检验 ${selectedFiles.length} 个Antigravity凭证的Project ID吗?\n\n将并行检验以加快速度。`)) {
return;
}
showStatus(`🔍 正在并行检验 ${selectedFiles.length} 个Antigravity凭证,请稍候...`, 'info');
// 并行执行所有检验请求
const promises = selectedFiles.map(async (filename) => {
try {
const response = await fetch(`./creds/verify-project/${encodeURIComponent(filename)}?mode=antigravity`, {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok && data.success) {
return { success: true, filename, projectId: data.project_id, message: data.message };
} else {
return { success: false, filename, error: data.message || '失败' };
}
} catch (error) {
return { success: false, filename, error: error.message };
}
});
// 等待所有请求完成
const results = await Promise.all(promises);
// 统计结果
let successCount = 0;
let failCount = 0;
const resultMessages = [];
results.forEach(result => {
if (result.success) {
successCount++;
resultMessages.push(`✅ ${result.filename}: ${result.projectId}`);
} else {
failCount++;
resultMessages.push(`❌ ${result.filename}: ${result.error}`);
}
});
await AppState.antigravityCreds.refresh();
const summary = `Antigravity批量检验完成!\n\n成功: ${successCount} 个\n失败: ${failCount} 个\n总计: ${selectedFiles.length} 个\n\n详细结果:\n${resultMessages.join('\n')}`;
if (failCount === 0) {
showStatus(`✅ 全部检验成功!成功检验 ${successCount}/${selectedFiles.length} 个Antigravity凭证`, 'success');
} else if (successCount === 0) {
showStatus(`❌ 全部检验失败!失败 ${failCount}/${selectedFiles.length} 个Antigravity凭证`, 'error');
} else {
showStatus(`⚠️ 批量检验完成:成功 ${successCount}/${selectedFiles.length} 个,失败 ${failCount} 个`, 'info');
}
console.log(summary);
alert(summary);
}
async function refreshAllEmails() {
if (!confirm('确定要刷新所有凭证的用户邮箱吗?这可能需要一些时间。')) return;
try {
showStatus('正在刷新所有用户邮箱...', 'info');
const response = await fetch('./creds/refresh-all-emails', {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok) {
showStatus(`邮箱刷新完成:成功获取 ${data.success_count}/${data.total_count} 个邮箱地址`, 'success');
await AppState.creds.refresh();
} else {
showStatus(data.message || '邮箱刷新失败', 'error');
}
} catch (error) {
showStatus(`邮箱刷新网络错误: ${error.message}`, 'error');
}
}
async function refreshAllAntigravityEmails() {
if (!confirm('确定要刷新所有Antigravity凭证的用户邮箱吗?这可能需要一些时间。')) return;
try {
showStatus('正在刷新所有用户邮箱...', 'info');
const response = await fetch('./creds/refresh-all-emails?mode=antigravity', {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok) {
showStatus(`邮箱刷新完成:成功获取 ${data.success_count}/${data.total_count} 个邮箱地址`, 'success');
await AppState.antigravityCreds.refresh();
} else {
showStatus(data.message || '邮箱刷新失败', 'error');
}
} catch (error) {
showStatus(`邮箱刷新网络错误: ${error.message}`, 'error');
}
}
async function deduplicateByEmail() {
if (!confirm('确定要对凭证进行凭证一键去重吗?\n\n相同邮箱的凭证只保留一个,其他将被删除。\n此操作不可撤销!')) return;
try {
showStatus('正在进行凭证一键去重...', 'info');
const response = await fetch('./creds/deduplicate-by-email', {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok) {
const msg = `去重完成:删除 ${data.deleted_count} 个重复凭证,保留 ${data.kept_count} 个凭证(${data.unique_emails_count} 个唯一邮箱)`;
showStatus(msg, 'success');
await AppState.creds.refresh();
// 显示详细信息
if (data.duplicate_groups && data.duplicate_groups.length > 0) {
let details = '去重详情:\n\n';
data.duplicate_groups.forEach(group => {
details += `邮箱: ${group.email}\n保留: ${group.kept_file}\n删除: ${group.deleted_files.join(', ')}\n\n`;
});
console.log(details);
}
} else {
showStatus(data.message || '去重失败', 'error');
}
} catch (error) {
showStatus(`去重网络错误: ${error.message}`, 'error');
}
}
async function deduplicateAntigravityByEmail() {
if (!confirm('确定要对Antigravity凭证进行凭证一键去重吗?\n\n相同邮箱的凭证只保留一个,其他将被删除。\n此操作不可撤销!')) return;
try {
showStatus('正在进行凭证一键去重...', 'info');
const response = await fetch('./creds/deduplicate-by-email?mode=antigravity', {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok) {
const msg = `去重完成:删除 ${data.deleted_count} 个重复凭证,保留 ${data.kept_count} 个凭证(${data.unique_emails_count} 个唯一邮箱)`;
showStatus(msg, 'success');
await AppState.antigravityCreds.refresh();
// 显示详细信息
if (data.duplicate_groups && data.duplicate_groups.length > 0) {
let details = '去重详情:\n\n';
data.duplicate_groups.forEach(group => {
details += `邮箱: ${group.email}\n保留: ${group.kept_file}\n删除: ${group.deleted_files.join(', ')}\n\n`;
});
console.log(details);
}
} else {
showStatus(data.message || '去重失败', 'error');
}
} catch (error) {
showStatus(`去重网络错误: ${error.message}`, 'error');
}
}
// =====================================================================
// WebSocket日志相关
// =====================================================================
function connectWebSocket() {
if (AppState.logWebSocket && AppState.logWebSocket.readyState === WebSocket.OPEN) {
showStatus('WebSocket已经连接', 'info');
return;
}
try {
const wsPath = new URL('./auth/logs/stream', window.location.href).href;
const wsUrl = wsPath.replace(/^http/, 'ws');
document.getElementById('connectionStatusText').textContent = '连接中...';
document.getElementById('logConnectionStatus').className = 'status info';
AppState.logWebSocket = new WebSocket(wsUrl);
AppState.logWebSocket.onopen = () => {
document.getElementById('connectionStatusText').textContent = '已连接';
document.getElementById('logConnectionStatus').className = 'status success';
showStatus('日志流连接成功', 'success');
clearLogsDisplay();
};
AppState.logWebSocket.onmessage = (event) => {
const logLine = event.data;
if (logLine.trim()) {
AppState.allLogs.push(logLine);
if (AppState.allLogs.length > 1000) {
AppState.allLogs = AppState.allLogs.slice(-1000);
}
filterLogs();
if (document.getElementById('autoScroll').checked) {
const logContainer = document.getElementById('logContainer');
logContainer.scrollTop = logContainer.scrollHeight;
}
}
};
AppState.logWebSocket.onclose = () => {
document.getElementById('connectionStatusText').textContent = '连接断开';
document.getElementById('logConnectionStatus').className = 'status error';
showStatus('日志流连接断开', 'info');
};
AppState.logWebSocket.onerror = (error) => {
document.getElementById('connectionStatusText').textContent = '连接错误';
document.getElementById('logConnectionStatus').className = 'status error';
showStatus('日志流连接错误: ' + error, 'error');
};
} catch (error) {
showStatus('创建WebSocket连接失败: ' + error.message, 'error');
document.getElementById('connectionStatusText').textContent = '连接失败';
document.getElementById('logConnectionStatus').className = 'status error';
}
}
function disconnectWebSocket() {
if (AppState.logWebSocket) {
AppState.logWebSocket.close();
AppState.logWebSocket = null;
document.getElementById('connectionStatusText').textContent = '未连接';
document.getElementById('logConnectionStatus').className = 'status info';
showStatus('日志流连接已断开', 'info');
}
}
function clearLogsDisplay() {
AppState.allLogs = [];
AppState.filteredLogs = [];
document.getElementById('logContent').textContent = '日志已清空,等待新日志...';
}
async function downloadLogs() {
try {
const response = await fetch('./auth/logs/download', { headers: getAuthHeaders() });
if (response.ok) {
const contentDisposition = response.headers.get('Content-Disposition');
let filename = 'gcli2api_logs.txt';
if (contentDisposition) {
const match = contentDisposition.match(/filename=(.+)/);
if (match) filename = match[1];
}
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
a.click();
window.URL.revokeObjectURL(url);
showStatus(`日志文件下载成功: ${filename}`, 'success');
} else {
const data = await response.json();
showStatus(`下载日志失败: ${data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`下载日志时网络错误: ${error.message}`, 'error');
}
}
async function clearLogs() {
try {
const response = await fetch('./auth/logs/clear', {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok) {
clearLogsDisplay();
showStatus(data.message, 'success');
} else {
showStatus(`清空日志失败: ${data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
clearLogsDisplay();
showStatus(`清空日志时网络错误: ${error.message}`, 'error');
}
}
function filterLogs() {
const filter = document.getElementById('logLevelFilter').value;
AppState.currentLogFilter = filter;
if (filter === 'all') {
AppState.filteredLogs = [...AppState.allLogs];
} else {
AppState.filteredLogs = AppState.allLogs.filter(log => log.toUpperCase().includes(filter));
}
displayLogs();
}
function displayLogs() {
const logContent = document.getElementById('logContent');
if (AppState.filteredLogs.length === 0) {
logContent.textContent = AppState.currentLogFilter === 'all' ?
'暂无日志...' : `暂无${AppState.currentLogFilter}级别的日志...`;
} else {
logContent.textContent = AppState.filteredLogs.join('\n');
}
}
// =====================================================================
// 环境变量凭证管理
// =====================================================================
async function checkEnvCredsStatus() {
const loading = document.getElementById('envStatusLoading');
const content = document.getElementById('envStatusContent');
try {
loading.style.display = 'block';
content.classList.add('hidden');
const response = await fetch('./auth/env-creds-status', { headers: getAuthHeaders() });
const data = await response.json();
if (response.ok) {
const envVarsList = document.getElementById('envVarsList');
envVarsList.textContent = Object.keys(data.available_env_vars).length > 0
? Object.keys(data.available_env_vars).join(', ')
: '未找到GCLI_CREDS_*环境变量';
const autoLoadStatus = document.getElementById('autoLoadStatus');
autoLoadStatus.textContent = data.auto_load_enabled ? '✅ 已启用' : '❌ 未启用';
autoLoadStatus.style.color = data.auto_load_enabled ? '#28a745' : '#dc3545';
document.getElementById('envFilesCount').textContent = `${data.existing_env_files_count} 个文件`;
const envFilesList = document.getElementById('envFilesList');
envFilesList.textContent = data.existing_env_files.length > 0
? data.existing_env_files.join(', ')
: '无';
content.classList.remove('hidden');
showStatus('环境变量状态检查完成', 'success');
} else {
showStatus(`获取环境变量状态失败: ${data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
} finally {
loading.style.display = 'none';
}
}
async function loadEnvCredentials() {
try {
showStatus('正在从环境变量导入凭证...', 'info');
const response = await fetch('./auth/load-env-creds', {
method: 'POST',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok) {
if (data.loaded_count > 0) {
showStatus(`✅ 成功导入 ${data.loaded_count}/${data.total_count} 个凭证文件`, 'success');
setTimeout(() => checkEnvCredsStatus(), 1000);
} else {
showStatus(`⚠️ ${data.message}`, 'info');
}
} else {
showStatus(`导入失败: ${data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
}
}
async function clearEnvCredentials() {
if (!confirm('确定要清除所有从环境变量导入的凭证文件吗?\n这将删除所有文件名以 "env-" 开头的认证文件。')) {
return;
}
try {
showStatus('正在清除环境变量凭证文件...', 'info');
const response = await fetch('./auth/env-creds', {
method: 'DELETE',
headers: getAuthHeaders()
});
const data = await response.json();
if (response.ok) {
showStatus(`✅ 成功删除 ${data.deleted_count} 个环境变量凭证文件`, 'success');
setTimeout(() => checkEnvCredsStatus(), 1000);
} else {
showStatus(`清除失败: ${data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
}
}
// =====================================================================
// 配置管理
// =====================================================================
async function loadConfig() {
const loading = document.getElementById('configLoading');
const form = document.getElementById('configForm');
try {
loading.style.display = 'block';
form.classList.add('hidden');
const response = await fetch('./config/get', { headers: getAuthHeaders() });
const data = await response.json();
if (response.ok) {
AppState.currentConfig = data.config;
AppState.envLockedFields = new Set(data.env_locked || []);
populateConfigForm();
form.classList.remove('hidden');
showStatus('配置加载成功', 'success');
} else {
showStatus(`加载配置失败: ${data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
} finally {
loading.style.display = 'none';
}
}
function populateConfigForm() {
const c = AppState.currentConfig;
setConfigField('host', c.host || '0.0.0.0');
setConfigField('port', c.port || 7861);
setConfigField('configApiPassword', c.api_password || '');
setConfigField('configPanelPassword', c.panel_password || '');
setConfigField('configPassword', c.password || 'pwd');
setConfigField('credentialsDir', c.credentials_dir || '');
setConfigField('proxy', c.proxy || '');
setConfigField('codeAssistEndpoint', c.code_assist_endpoint || '');
setConfigField('oauthProxyUrl', c.oauth_proxy_url || '');
setConfigField('googleapisProxyUrl', c.googleapis_proxy_url || '');
setConfigField('resourceManagerApiUrl', c.resource_manager_api_url || '');
setConfigField('serviceUsageApiUrl', c.service_usage_api_url || '');
setConfigField('antigravityApiUrl', c.antigravity_api_url || '');
document.getElementById('autoBanEnabled').checked = Boolean(c.auto_ban_enabled);
setConfigField('autoBanErrorCodes', (c.auto_ban_error_codes || []).join(','));
setConfigField('callsPerRotation', c.calls_per_rotation || 10);
document.getElementById('retry429Enabled').checked = Boolean(c.retry_429_enabled);
setConfigField('retry429MaxRetries', c.retry_429_max_retries || 20);
setConfigField('retry429Interval', c.retry_429_interval || 0.1);
document.getElementById('compatibilityModeEnabled').checked = Boolean(c.compatibility_mode_enabled);
document.getElementById('returnThoughtsToFrontend').checked = Boolean(c.return_thoughts_to_frontend !== false);
document.getElementById('antigravityStream2nostream').checked = Boolean(c.antigravity_stream2nostream !== false);
setConfigField('antiTruncationMaxAttempts', c.anti_truncation_max_attempts || 3);
}
function setConfigField(fieldId, value) {
const field = document.getElementById(fieldId);
if (field) {
field.value = value;
const configKey = fieldId.replace(/([A-Z])/g, '_$1').toLowerCase();
if (AppState.envLockedFields.has(configKey)) {
field.disabled = true;
field.classList.add('env-locked');
} else {
field.disabled = false;
field.classList.remove('env-locked');
}
}
}
async function saveConfig() {
try {
const getValue = (id, def = '') => document.getElementById(id)?.value.trim() || def;
const getInt = (id, def = 0) => parseInt(document.getElementById(id)?.value) || def;
const getFloat = (id, def = 0.0) => parseFloat(document.getElementById(id)?.value) || def;
const getChecked = (id, def = false) => document.getElementById(id)?.checked || def;
const config = {
host: getValue('host', '0.0.0.0'),
port: getInt('port', 7861),
api_password: getValue('configApiPassword'),
panel_password: getValue('configPanelPassword'),
password: getValue('configPassword', 'pwd'),
code_assist_endpoint: getValue('codeAssistEndpoint'),
credentials_dir: getValue('credentialsDir'),
proxy: getValue('proxy'),
oauth_proxy_url: getValue('oauthProxyUrl'),
googleapis_proxy_url: getValue('googleapisProxyUrl'),
resource_manager_api_url: getValue('resourceManagerApiUrl'),
service_usage_api_url: getValue('serviceUsageApiUrl'),
antigravity_api_url: getValue('antigravityApiUrl'),
auto_ban_enabled: getChecked('autoBanEnabled'),
auto_ban_error_codes: getValue('autoBanErrorCodes').split(',')
.map(c => parseInt(c.trim())).filter(c => !isNaN(c)),
calls_per_rotation: getInt('callsPerRotation', 10),
retry_429_enabled: getChecked('retry429Enabled'),
retry_429_max_retries: getInt('retry429MaxRetries', 20),
retry_429_interval: getFloat('retry429Interval', 0.1),
compatibility_mode_enabled: getChecked('compatibilityModeEnabled'),
return_thoughts_to_frontend: getChecked('returnThoughtsToFrontend'),
antigravity_stream2nostream: getChecked('antigravityStream2nostream'),
anti_truncation_max_attempts: getInt('antiTruncationMaxAttempts', 3)
};
const response = await fetch('./config/save', {
method: 'POST',
headers: getAuthHeaders(),
body: JSON.stringify({ config })
});
const data = await response.json();
if (response.ok) {
let message = '配置保存成功';
if (data.hot_updated && data.hot_updated.length > 0) {
message += `,以下配置已立即生效: ${data.hot_updated.join(', ')}`;
}
if (data.restart_required && data.restart_required.length > 0) {
message += `\n⚠️ 重启提醒: ${data.restart_notice}`;
showStatus(message, 'info');
} else {
showStatus(message, 'success');
}
setTimeout(() => loadConfig(), 1000);
} else {
showStatus(`保存配置失败: ${data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
}
}
// 镜像网址配置
const mirrorUrls = {
codeAssistEndpoint: 'https://gcli-api.sukaka.top/cloudcode-pa',
oauthProxyUrl: 'https://gcli-api.sukaka.top/oauth2',
googleapisProxyUrl: 'https://gcli-api.sukaka.top/googleapis',
resourceManagerApiUrl: 'https://gcli-api.sukaka.top/cloudresourcemanager',
serviceUsageApiUrl: 'https://gcli-api.sukaka.top/serviceusage',
antigravityApiUrl: 'https://gcli-api.sukaka.top/daily-cloudcode-pa'
};
const officialUrls = {
codeAssistEndpoint: 'https://cloudcode-pa.googleapis.com',
oauthProxyUrl: 'https://oauth2.googleapis.com',
googleapisProxyUrl: 'https://www.googleapis.com',
resourceManagerApiUrl: 'https://cloudresourcemanager.googleapis.com',
serviceUsageApiUrl: 'https://serviceusage.googleapis.com',
antigravityApiUrl: 'https://daily-cloudcode-pa.sandbox.googleapis.com'
};
function useMirrorUrls() {
if (confirm('确定要将所有端点配置为镜像网址吗?')) {
for (const [fieldId, url] of Object.entries(mirrorUrls)) {
const field = document.getElementById(fieldId);
if (field && !field.disabled) field.value = url;
}
showStatus('✅ 已切换到镜像网址配置,记得点击"保存配置"按钮保存设置', 'success');
}
}
function restoreOfficialUrls() {
if (confirm('确定要将所有端点配置为官方地址吗?')) {
for (const [fieldId, url] of Object.entries(officialUrls)) {
const field = document.getElementById(fieldId);
if (field && !field.disabled) field.value = url;
}
showStatus('✅ 已切换到官方端点配置,记得点击"保存配置"按钮保存设置', 'success');
}
}
// =====================================================================
// 使用统计
// =====================================================================
async function refreshUsageStats() {
const loading = document.getElementById('usageLoading');
const list = document.getElementById('usageList');
try {
loading.style.display = 'block';
list.innerHTML = '';
const [statsResponse, aggregatedResponse] = await Promise.all([
fetch('./usage/stats', { headers: getAuthHeaders() }),
fetch('./usage/aggregated', { headers: getAuthHeaders() })
]);
if (statsResponse.status === 401 || aggregatedResponse.status === 401) {
showStatus('认证失败,请重新登录', 'error');
setTimeout(() => location.reload(), 1500);
return;
}
const statsData = await statsResponse.json();
const aggregatedData = await aggregatedResponse.json();
if (statsResponse.ok && aggregatedResponse.ok) {
AppState.usageStatsData = statsData.success ? statsData.data : statsData;
const aggData = aggregatedData.success ? aggregatedData.data : aggregatedData;
document.getElementById('totalApiCalls').textContent = aggData.total_calls_24h || 0;
document.getElementById('totalFiles').textContent = aggData.total_files || 0;
document.getElementById('avgCallsPerFile').textContent = (aggData.avg_calls_per_file || 0).toFixed(1);
renderUsageList();
showStatus(`已加载 ${aggData.total_files || Object.keys(AppState.usageStatsData).length} 个文件的使用统计`, 'success');
} else {
const errorMsg = statsData.detail || aggregatedData.detail || '加载使用统计失败';
showStatus(`错误: ${errorMsg}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
} finally {
loading.style.display = 'none';
}
}
function renderUsageList() {
const list = document.getElementById('usageList');
list.innerHTML = '';
if (Object.keys(AppState.usageStatsData).length === 0) {
list.innerHTML = '<p style="text-align: center; color: #666;">暂无使用统计数据</p>';
return;
}
for (const [filename, stats] of Object.entries(AppState.usageStatsData)) {
const card = document.createElement('div');
card.className = 'usage-card';
const calls24h = stats.calls_24h || 0;
card.innerHTML = `
<div class="usage-header">
<div class="usage-filename">${filename}</div>
</div>
<div class="usage-info">
<div class="usage-info-item" style="grid-column: 1 / -1;">
<span class="usage-info-label">24小时内调用次数</span>
<span class="usage-info-value" style="font-size: 24px; font-weight: bold; color: #007bff;">${calls24h}</span>
</div>
</div>
<div class="usage-actions">
<button class="usage-btn reset" onclick="resetSingleUsageStats('${filename}')">重置统计</button>
</div>
`;
list.appendChild(card);
}
}
async function resetSingleUsageStats(filename) {
if (!confirm(`确定要重置 ${filename} 的使用统计吗?`)) return;
try {
const response = await fetch('./usage/reset', {
method: 'POST',
headers: getAuthHeaders(),
body: JSON.stringify({ filename })
});
const data = await response.json();
if (response.ok && data.success) {
showStatus(data.message, 'success');
await refreshUsageStats();
} else {
showStatus(`重置失败: ${data.message || data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
}
}
async function resetAllUsageStats() {
if (!confirm('确定要重置所有文件的使用统计吗?此操作不可恢复!')) return;
try {
const response = await fetch('./usage/reset', {
method: 'POST',
headers: getAuthHeaders(),
body: JSON.stringify({})
});
const data = await response.json();
if (response.ok && data.success) {
showStatus(data.message, 'success');
await refreshUsageStats();
} else {
showStatus(`重置失败: ${data.message || data.detail || data.error || '未知错误'}`, 'error');
}
} catch (error) {
showStatus(`网络错误: ${error.message}`, 'error');
}
}
// =====================================================================
// 冷却倒计时自动更新
// =====================================================================
function startCooldownTimer() {
if (AppState.cooldownTimerInterval) {
clearInterval(AppState.cooldownTimerInterval);
}
AppState.cooldownTimerInterval = setInterval(() => {
updateCooldownDisplays();
}, 1000);
}
function stopCooldownTimer() {
if (AppState.cooldownTimerInterval) {
clearInterval(AppState.cooldownTimerInterval);
AppState.cooldownTimerInterval = null;
}
}
function updateCooldownDisplays() {
let needsRefresh = false;
// 检查模型级冷却是否过期
for (const credInfo of Object.values(AppState.creds.data)) {
if (credInfo.model_cooldowns && Object.keys(credInfo.model_cooldowns).length > 0) {
const currentTime = Date.now() / 1000;
const hasExpiredCooldowns = Object.entries(credInfo.model_cooldowns).some(([, until]) => until <= currentTime);
if (hasExpiredCooldowns) {
needsRefresh = true;
break;
}
}
}
if (needsRefresh) {
AppState.creds.renderList();
return;
}
// 更新模型级冷却的显示
document.querySelectorAll('.cooldown-badge').forEach(badge => {
const card = badge.closest('.cred-card');
const filenameEl = card?.querySelector('.cred-filename');
if (!filenameEl) return;
const filename = filenameEl.textContent;
const credInfo = Object.values(AppState.creds.data).find(c => c.filename === filename);
if (credInfo && credInfo.model_cooldowns) {
const currentTime = Date.now() / 1000;
const titleMatch = badge.getAttribute('title')?.match(/模型: (.+)/);
if (titleMatch) {
const model = titleMatch[1];
const cooldownUntil = credInfo.model_cooldowns[model];
if (cooldownUntil) {
const remaining = Math.max(0, Math.floor(cooldownUntil - currentTime));
if (remaining > 0) {
const shortModel = model.replace('gemini-', '').replace('-exp', '')
.replace('2.0-', '2-').replace('1.5-', '1.5-');
const timeDisplay = formatCooldownTime(remaining).replace(/s$/, '').replace(/ /g, '');
badge.innerHTML = `🔧 ${shortModel}: ${timeDisplay}`;
}
}
}
}
});
}
// =====================================================================
// 版本信息管理
// =====================================================================
// 获取并显示版本信息(不检查更新)
async function fetchAndDisplayVersion() {
try {
const response = await fetch('./version/info');
const data = await response.json();
const versionText = document.getElementById('versionText');
if (data.success) {
// 只显示版本号
versionText.textContent = `v${data.version}`;
versionText.title = `完整版本: ${data.full_hash}\n提交信息: ${data.message}\n提交时间: ${data.date}`;
versionText.style.cursor = 'help';
} else {
versionText.textContent = '未知版本';
versionText.title = data.error || '无法获取版本信息';
}
} catch (error) {
console.error('获取版本信息失败:', error);
const versionText = document.getElementById('versionText');
if (versionText) {
versionText.textContent = '版本信息获取失败';
}
}
}
// 检查更新
async function checkForUpdates() {
const checkBtn = document.getElementById('checkUpdateBtn');
if (!checkBtn) return;
const originalText = checkBtn.textContent;
try {
// 显示检查中状态
checkBtn.textContent = '检查中...';
checkBtn.disabled = true;
// 调用API检查更新
const response = await fetch('./version/info?check_update=true');
const data = await response.json();
if (data.success) {
if (data.check_update === false) {
// 检查更新失败
showStatus(`检查更新失败: ${data.update_error || '未知错误'}`, 'error');
} else if (data.has_update === true) {
// 有更新
const updateMsg = `发现新版本!\n当前: v${data.version}\n最新: v${data.latest_version}\n\n更新内容: ${data.latest_message || '无'}`;
showStatus(updateMsg.replace(/\n/g, ' '), 'warning');
// 更新按钮样式
checkBtn.style.backgroundColor = '#ffc107';
checkBtn.textContent = '有新版本';
setTimeout(() => {
checkBtn.style.backgroundColor = '#17a2b8';
checkBtn.textContent = originalText;
}, 5000);
} else if (data.has_update === false) {
// 已是最新
showStatus('已是最新版本!', 'success');
checkBtn.style.backgroundColor = '#28a745';
checkBtn.textContent = '已是最新';
setTimeout(() => {
checkBtn.style.backgroundColor = '#17a2b8';
checkBtn.textContent = originalText;
}, 3000);
} else {
// 无法确定
showStatus('无法确定是否有更新', 'info');
}
} else {
showStatus(`检查更新失败: ${data.error}`, 'error');
}
} catch (error) {
console.error('检查更新失败:', error);
showStatus(`检查更新失败: ${error.message}`, 'error');
} finally {
checkBtn.disabled = false;
if (checkBtn.textContent === '检查中...') {
checkBtn.textContent = originalText;
}
}
}
// =====================================================================
// 页面初始化
// =====================================================================
window.onload = async function () {
const autoLoginSuccess = await autoLogin();
if (!autoLoginSuccess) {
showStatus('请输入密码登录', 'info');
} else {
// 登录成功后获取版本信息
await fetchAndDisplayVersion();
}
startCooldownTimer();
const antigravityAuthBtn = document.getElementById('getAntigravityAuthBtn');
if (antigravityAuthBtn) {
antigravityAuthBtn.addEventListener('click', startAntigravityAuth);
}
};
// 拖拽功能 - 初始化
document.addEventListener('DOMContentLoaded', function () {
const uploadArea = document.getElementById('uploadArea');
if (uploadArea) {
uploadArea.addEventListener('dragover', (event) => {
event.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', (event) => {
event.preventDefault();
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (event) => {
event.preventDefault();
uploadArea.classList.remove('dragover');
AppState.uploadFiles.addFiles(Array.from(event.dataTransfer.files));
});
}
});
|