Spaces:
Running
Running
File size: 83,293 Bytes
1de7911 |
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 |
const API_BASE = '/api';
const REFRESH_INTERVAL = 3000;
let autoRefreshTimer = null;
// Admin Password Management
let adminPassword = null;
const ADMIN_STORAGE_KEY = 'zencoder_admin_pass';
// State
let currentState = {
page: 1,
size: 10,
category: 'normal',
total: 0,
selectedIds: new Set(),
items: []
};
// --- Admin Password Management ---
function savePassword(password) {
try {
localStorage.setItem(ADMIN_STORAGE_KEY, password);
console.log('Password saved to localStorage');
return true;
} catch (e) {
console.error('Failed to save password to localStorage:', e);
return false;
}
}
function getSavedPassword() {
try {
const saved = localStorage.getItem(ADMIN_STORAGE_KEY);
if (saved) {
console.log('Found saved password in localStorage');
}
return saved;
} catch (e) {
console.error('Failed to get password from localStorage:', e);
return null;
}
}
function clearSavedPassword() {
try {
localStorage.removeItem(ADMIN_STORAGE_KEY);
} catch (e) {
console.error('Failed to clear password from localStorage:', e);
}
}
async function verifyAdminPassword(password) {
try {
// 尝试调用一个需要管理密码的API来验证
const response = await fetch(`${API_BASE}/accounts?page=1&size=1`, {
headers: {
'X-Admin-Password': password
}
});
return response.ok;
} catch (e) {
return false;
}
}
function showAdminLogin() {
document.getElementById('adminPasswordModal').classList.remove('hidden');
document.getElementById('mainApp').classList.add('hidden');
document.getElementById('adminPassword').focus();
}
function hideAdminLogin() {
document.getElementById('adminPasswordModal').classList.add('hidden');
document.getElementById('mainApp').classList.remove('hidden');
document.getElementById('mainApp').classList.add('flex');
}
async function handleAdminLogin(password, remember = false) {
console.log('Attempting login, remember:', remember);
const isValid = await verifyAdminPassword(password);
if (isValid) {
adminPassword = password;
if (remember) {
const saved = savePassword(password);
if (!saved) {
console.warn('Failed to save password to localStorage');
}
} else {
// 如果没有勾选记住,清除之前保存的密码
clearSavedPassword();
}
hideAdminLogin();
document.getElementById('passwordError').classList.add('hidden');
// 开始加载数据
initializeApp();
return true;
} else {
document.getElementById('passwordError').classList.remove('hidden');
return false;
}
}
function logout() {
adminPassword = null;
clearSavedPassword();
// 停止自动刷新
if (autoRefreshTimer) {
clearInterval(autoRefreshTimer);
autoRefreshTimer = null;
}
// 显示登录界面
showAdminLogin();
}
async function initAdminAuth() {
// 检查是否有保存的密码
const savedPassword = getSavedPassword();
if (savedPassword) {
console.log('Found saved password, attempting auto-login...');
// 直接设置密码,不验证(因为验证可能由于网络问题失败)
adminPassword = savedPassword;
// 尝试验证密码
try {
const isValid = await verifyAdminPassword(savedPassword);
if (isValid) {
console.log('Saved password validated successfully');
hideAdminLogin();
initializeApp();
return;
} else {
console.log('Saved password validation failed');
// 密码无效,清除并显示登录界面
adminPassword = null;
clearSavedPassword();
}
} catch (e) {
console.log('Password validation error, keeping saved password:', e);
// 网络错误时仍然保留密码并尝试使用
hideAdminLogin();
initializeApp();
return;
}
}
// 显示登录界面
showAdminLogin();
}
function toggleAdminPasswordVisibility() {
const input = document.getElementById('adminPassword');
const eyeIcon = document.getElementById('adminEyeIcon');
if (input.type === 'password') {
input.type = 'text';
eyeIcon.innerHTML = '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21" />';
} else {
input.type = 'password';
eyeIcon.innerHTML = '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" /><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />';
}
}
// Admin Password Form Handler
document.addEventListener('DOMContentLoaded', function() {
const adminForm = document.getElementById('adminPasswordForm');
if (adminForm) {
// 检查并恢复记住密码的勾选状态
const hasSavedPassword = getSavedPassword() !== null;
if (hasSavedPassword) {
const rememberCheckbox = document.getElementById('rememberPassword');
if (rememberCheckbox) {
rememberCheckbox.checked = true;
}
}
adminForm.addEventListener('submit', async (e) => {
e.preventDefault();
const password = document.getElementById('adminPassword').value.trim();
const remember = document.getElementById('rememberPassword').checked;
const btn = document.getElementById('adminLoginBtn');
const btnText = document.getElementById('adminBtnText');
const btnLoading = document.getElementById('adminBtnLoading');
if (!password) {
document.getElementById('passwordError').textContent = '请输入管理密码';
document.getElementById('passwordError').classList.remove('hidden');
return;
}
btn.disabled = true;
btnText.textContent = '验证中...';
btnLoading.classList.remove('hidden');
const success = await handleAdminLogin(password, remember);
btn.disabled = false;
btnText.textContent = '验证';
btnLoading.classList.add('hidden');
if (success) {
document.getElementById('adminPassword').value = '';
}
});
}
});
function getAuthHeaders() {
const headers = {};
if (adminPassword) {
headers['X-Admin-Password'] = adminPassword;
}
return headers;
}
// --- Theme Management ---
function initTheme() {
const isDark = localStorage.theme === 'dark' ||
(!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches);
if (isDark) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
updateThemeIcons(isDark);
}
function toggleTheme() {
const isDark = document.documentElement.classList.toggle('dark');
localStorage.theme = isDark ? 'dark' : 'light';
updateThemeIcons(isDark);
}
function updateThemeIcons(isDark) {
const sun = document.getElementById('sunIcon');
const moon = document.getElementById('moonIcon');
if (isDark) {
sun.classList.remove('hidden');
moon.classList.add('hidden');
} else {
sun.classList.add('hidden');
moon.classList.remove('hidden');
}
}
document.getElementById('themeToggle').addEventListener('click', toggleTheme);
// --- Password Visibility ---
function togglePasswordVisibility() {
const input = document.getElementById('client_secret');
const eyeIcon = document.getElementById('eyeIcon');
if (input.type === 'password') {
input.type = 'text';
eyeIcon.innerHTML = '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13.875 18.825A10.05 10.05 0 0112 19c-4.478 0-8.268-2.943-9.543-7a9.97 9.97 0 011.563-3.029m5.858.908a3 3 0 114.243 4.243M9.878 9.878l4.242 4.242M9.88 9.88l-3.29-3.29m7.532 7.532l3.29 3.29M3 3l3.59 3.59m0 0A9.953 9.953 0 0112 5c4.478 0 8.268 2.943 9.543 7a10.025 10.025 0 01-4.132 5.411m0 0L21 21" />';
} else {
input.type = 'password';
eyeIcon.innerHTML = '<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" /><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2.458 12C3.732 7.943 7.523 5 12 5c4.478 0 8.268 2.943 9.542 7-1.274 4.057-5.064 7-9.542 7-4.477 0-8.268-2.943-9.542-7z" />';
}
}
// --- Data Logic ---
const PLAN_LIMITS = { Free: 30, Starter: 280, Core: 750, Advanced: 1900, Max: 4200 };
function getStatusConfig(acc) {
switch (acc.status) {
case 'banned':
return { text: '已封禁', class: 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400', dot: 'bg-red-500' };
case 'error':
return { text: '异常', class: 'bg-yellow-100 text-yellow-700 dark:bg-yellow-900/30 dark:text-yellow-400', dot: 'bg-yellow-500' };
case 'cooling':
return { text: '冷却中', class: 'bg-orange-100 text-orange-700 dark:bg-orange-900/30 dark:text-orange-400', dot: 'bg-orange-500' };
case 'disabled':
return { text: '已禁用', class: 'bg-gray-100 text-gray-700 dark:bg-gray-900/30 dark:text-gray-400', dot: 'bg-gray-500' };
default:
return { text: '正常', class: 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400', dot: 'bg-green-500' };
}
}
function getTokenStatusConfig(record) {
switch (record.status) {
case 'banned':
return { text: '已封禁', class: 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400', dot: 'bg-red-500' };
case 'expired':
return { text: '已过期', class: 'bg-orange-100 text-orange-700 dark:bg-orange-900/30 dark:text-orange-400', dot: 'bg-orange-500' };
case 'disabled':
return { text: '已禁用', class: 'bg-gray-100 text-gray-700 dark:bg-gray-900/30 dark:text-gray-400', dot: 'bg-gray-500' };
default:
return { text: '正常', class: 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400', dot: 'bg-green-500' };
}
}
function formatDate(dateStr) {
if (!dateStr || dateStr.startsWith('0001')) return '-';
const d = new Date(dateStr);
return d.toLocaleDateString('zh-CN');
}
function formatLastUsed(dateStr) {
if (!dateStr || dateStr.startsWith('0001')) return '从未';
const date = new Date(dateStr);
const now = new Date();
const diff = now - date;
// 转换为秒、分钟、小时、天
const seconds = Math.floor(diff / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (days > 0) {
return `${days}天前`;
} else if (hours > 0) {
return `${hours}小时前`;
} else if (minutes > 0) {
return `${minutes}分钟前`;
} else if (seconds > 0) {
return `${seconds}秒前`;
} else {
return '刚刚';
}
}
// 格式化积分刷新时间显示
function formatCreditRefresh(creditRefreshTimeStr) {
if (!creditRefreshTimeStr || creditRefreshTimeStr.startsWith('0001')) {
return { text: '未知', class: 'text-gray-400', detail: '' };
}
const refreshTime = new Date(creditRefreshTimeStr);
const now = new Date();
const diffMs = refreshTime - now;
if (diffMs < 0) {
// 已过期,需要刷新
const daysPast = Math.floor(-diffMs / (1000 * 60 * 60 * 24));
const hoursPast = Math.floor(-diffMs / (1000 * 60 * 60));
if (daysPast > 0) {
return {
text: '需要刷新',
class: 'text-red-600 dark:text-red-400',
detail: `${daysPast}天前过期`
};
} else if (hoursPast > 0) {
return {
text: '需要刷新',
class: 'text-red-600 dark:text-red-400',
detail: `${hoursPast}小时前过期`
};
} else {
return {
text: '需要刷新',
class: 'text-red-600 dark:text-red-400',
detail: '刚过期'
};
}
} else if (diffMs < 1000 * 60 * 60) {
// 1小时内刷新
const minutes = Math.floor(diffMs / (1000 * 60));
return {
text: `${minutes}分钟后`,
class: 'text-orange-600 dark:text-orange-400',
detail: '即将刷新'
};
} else if (diffMs < 1000 * 60 * 60 * 24) {
// 24小时内刷新
const hours = Math.floor(diffMs / (1000 * 60 * 60));
return {
text: `${hours}小时后`,
class: 'text-yellow-600 dark:text-yellow-400',
detail: '今日刷新'
};
} else {
// 超过1天
const days = Math.floor(diffMs / (1000 * 60 * 60 * 24));
return {
text: `${days}天后`,
class: 'text-green-600 dark:text-green-400',
detail: refreshTime.toLocaleDateString('zh-CN') + ' ' + refreshTime.toLocaleTimeString('zh-CN', {hour: '2-digit', minute: '2-digit'})
};
}
}
async function loadAccounts(isAutoRefresh = false) {
try {
const params = new URLSearchParams({
page: currentState.page,
size: currentState.size,
status: currentState.category // map category to status param
});
const resp = await fetch(`${API_BASE}/accounts?${params}`, {
headers: getAuthHeaders()
});
if (!resp.ok) throw new Error('Failed to fetch');
const data = await resp.json();
// Handle both old and new API response formats temporarily if needed, but we know it's new
const items = data.items || [];
const total = data.total || 0;
currentState.items = items;
currentState.total = total;
// Retain selection if items still exist
const newSet = new Set();
items.forEach(item => {
if (currentState.selectedIds.has(item.id)) {
newSet.add(item.id);
}
});
currentState.selectedIds = newSet;
renderAccounts(items);
updatePaginationUI();
updateTabsUI();
updateBatchUI();
if (data.stats) {
updateStatsUI(data.stats);
}
} catch (e) {
if (!isAutoRefresh) console.error("Failed to load accounts", e);
}
}
function updateStatsUI(stats) {
if (!stats) return;
document.getElementById('stat-total-accounts').textContent = stats.total_accounts;
document.getElementById('stat-active-accounts').textContent = stats.active_accounts;
document.getElementById('stat-cooling-accounts').textContent = stats.cooling_accounts || 0;
document.getElementById('stat-banned-accounts').textContent = stats.banned_accounts;
document.getElementById('stat-error-accounts').textContent = stats.error_accounts;
document.getElementById('stat-disabled-accounts').textContent = stats.disabled_accounts || 0;
document.getElementById('stat-today-usage').textContent = stats.today_usage.toFixed(2);
document.getElementById('stat-total-usage').textContent = stats.total_usage.toFixed(2);
}
function renderAccounts(accounts) {
const tbody = document.getElementById('accountList');
const emptyState = document.getElementById('emptyState');
const tableContainer = document.getElementById('tableContainer');
const paginationContainer = document.getElementById('paginationContainer');
const selectAll = document.getElementById('selectAll');
if (accounts.length === 0) {
tbody.innerHTML = '';
tableContainer.classList.add('hidden');
paginationContainer.classList.add('hidden');
emptyState.classList.remove('hidden');
emptyState.classList.add('flex');
selectAll.checked = false;
selectAll.disabled = true;
return;
}
tableContainer.classList.remove('hidden');
paginationContainer.classList.remove('hidden');
emptyState.classList.add('hidden');
emptyState.classList.remove('flex');
selectAll.disabled = false;
selectAll.checked = accounts.length > 0 && accounts.every(a => currentState.selectedIds.has(a.id));
const html = accounts.map(acc => {
const status = getStatusConfig(acc);
const limit = PLAN_LIMITS[acc.plan_type] || 30;
const subDate = formatDate(acc.subscription_start_date);
const emailOrId = acc.email ? acc.email : acc.client_id;
const shortId = acc.client_id.length > 12 ? acc.client_id.substring(0, 12) + '...' : acc.client_id;
const usagePercent = Math.min((acc.daily_used / limit) * 100, 100);
const isSelected = currentState.selectedIds.has(acc.id);
const lastUsedText = formatLastUsed(acc.last_used);
// 格式化Token过期时间
const formatTokenExpiry = (tokenExpiryStr) => {
if (!tokenExpiryStr || tokenExpiryStr.startsWith('0001')) {
return { text: '未知', class: 'text-gray-400', detail: '' };
}
const expiryDate = new Date(tokenExpiryStr);
const now = new Date();
const diffMs = expiryDate - now;
if (diffMs < 0) {
// 已过期
const daysPast = Math.floor(-diffMs / (1000 * 60 * 60 * 24));
return {
text: '已过期',
class: 'text-red-600 dark:text-red-400',
detail: `${daysPast}天前过期`
};
} else if (diffMs < 1000 * 60 * 60) {
// 1小时内过期
const minutes = Math.floor(diffMs / (1000 * 60));
return {
text: `${minutes}分钟后`,
class: 'text-red-500 dark:text-red-400',
detail: '即将过期'
};
} else if (diffMs < 1000 * 60 * 60 * 24) {
// 24小时内过期
const hours = Math.floor(diffMs / (1000 * 60 * 60));
return {
text: `${hours}小时后`,
class: 'text-orange-600 dark:text-orange-400',
detail: '今日过期'
};
} else {
// 超过1天
const days = Math.floor(diffMs / (1000 * 60 * 60 * 24));
return {
text: `${days}天后`,
class: 'text-green-600 dark:text-green-400',
detail: expiryDate.toLocaleDateString('zh-CN')
};
}
};
const tokenExpiry = formatTokenExpiry(acc.token_expiry);
return `
<tr class="hover:bg-gray-50 dark:hover:bg-gray-800/50 transition-colors ${isSelected ? 'bg-blue-50 dark:bg-blue-900/10' : ''}">
<td class="px-6 py-4 whitespace-nowrap text-center">
<input type="checkbox" onchange="toggleSelect(${acc.id})" ${isSelected ? 'checked' : ''} class="rounded border-gray-300 dark:border-gray-600 text-primary focus:ring-primary h-4 w-4 mx-auto">
</td>
<td class="px-6 py-4 whitespace-nowrap text-center">
<div class="text-sm font-medium text-gray-900 dark:text-white">${acc.id}</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="flex items-center justify-center text-center">
<div>
<div class="text-sm font-medium text-gray-900 dark:text-white">${emailOrId}</div>
<div class="text-xs text-gray-500 dark:text-gray-400 font-mono mt-0.5" title="${acc.client_id}">ID: ${shortId}</div>
</div>
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-center">
<div class="text-sm text-gray-900 dark:text-white font-medium">${acc.plan_type}</div>
<div class="text-xs text-gray-500 dark:text-gray-400 mt-0.5">自: ${subDate}</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="w-full max-w-[140px] mx-auto">
<div class="flex justify-end gap-1 text-xs mb-1">
<span class="text-gray-900 dark:text-white font-medium">${acc.daily_used.toFixed(2)}</span>
<span class="text-gray-500">/ ${limit}</span>
</div>
<div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-1.5 overflow-hidden">
<div class="bg-primary h-1.5 rounded-full" style="width: ${usagePercent}%"></div>
</div>
<div class="text-[10px] text-gray-400 mt-1">总计: ${acc.total_used.toFixed(2)}</div>
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-center">
<div class="text-sm">
<span class="${lastUsedText === '从未' ? 'text-gray-400' : 'text-gray-600 dark:text-gray-300'}">${lastUsedText}</span>
${acc.last_used && !acc.last_used.startsWith('0001') ?
`<div class="text-[10px] text-gray-400 mt-0.5">${new Date(acc.last_used).toLocaleTimeString('zh-CN', {hour: '2-digit', minute: '2-digit'})}</div>`
: ''}
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-center">
<div class="text-sm">
<span class="${tokenExpiry.class} font-medium">${tokenExpiry.text}</span>
${tokenExpiry.detail ? `<div class="text-[10px] text-gray-400 mt-0.5">${tokenExpiry.detail}</div>` : ''}
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-center">
<div class="text-sm">
<span class="${formatCreditRefresh(acc.credit_refresh_time).class} font-medium">${formatCreditRefresh(acc.credit_refresh_time).text}</span>
${formatCreditRefresh(acc.credit_refresh_time).detail ? `<div class="text-[10px] text-gray-400 mt-0.5">${formatCreditRefresh(acc.credit_refresh_time).detail}</div>` : ''}
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap text-center">
<span class="px-2.5 py-0.5 inline-flex items-center text-xs font-medium rounded-full ${status.class}">
<span class="w-1.5 h-1.5 rounded-full ${status.dot} mr-1.5"></span>
${status.text}
</span>
${acc.status === 'cooling' && acc.cooling_until && !acc.cooling_until.startsWith('0001') ?
(() => {
const coolingDate = new Date(acc.cooling_until);
const now = new Date();
const diffMs = coolingDate - now;
if (diffMs > 0) {
const hours = Math.floor(diffMs / (1000 * 60 * 60));
const minutes = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60));
let timeText = '';
if (hours > 0) {
timeText = `${hours}小时${minutes}分钟后解除`;
} else {
timeText = `${minutes}分钟后解除`;
}
return `<div class="text-[10px] text-orange-500 dark:text-orange-400 mt-1" title="${coolingDate.toLocaleString('zh-CN')}">
<svg xmlns="http://www.w3.org/2000/svg" class="inline h-3 w-3 mr-0.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
</svg>
${timeText}
</div>`;
} else {
return `<div class="text-[10px] text-orange-500 dark:text-orange-400 mt-1">即将解除</div>`;
}
})()
: acc.ban_reason ? `<div class="text-[10px] text-red-500 dark:text-red-400 mt-1 max-w-[120px] truncate mx-auto" title="${acc.ban_reason}">${acc.ban_reason}</div>` : ''}
</td>
<td class="px-6 py-4 whitespace-nowrap text-center text-sm font-medium">
<div class="flex justify-center gap-3">
<button onclick="toggleAccount(${acc.id})" class="text-primary hover:text-primary-hover transition-colors" title="${acc.is_active ? '禁用' : '启用'}">
${acc.is_active
? '<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 9v6m4-6v6m7-3a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>'
: '<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M14.752 11.168l-3.197-2.132A1 1 0 0010 9.87v4.263a1 1 0 001.555.832l3.197-2.132a1 1 0 000-1.664z" /><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg>'
}
</button>
<button onclick="deleteAccount(${acc.id})" class="text-red-500 hover:text-red-700 transition-colors" title="删除">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</button>
</div>
</td>
</tr>`;
}).join('');
// Optimization: Only update if content changed
if (tbody.innerHTML !== html) {
tbody.innerHTML = html;
}
}
// --- Interactions ---
function switchCategory(cat) {
currentState.category = cat;
currentState.page = 1;
currentState.selectedIds.clear();
loadAccounts();
}
function updateTabsUI() {
['normal', 'banned', 'cooling', 'disabled', 'error'].forEach(cat => {
const btn = document.getElementById(`tab-${cat}`);
if (currentState.category === cat) {
btn.className = "px-3 py-1.5 text-xs font-medium rounded-md transition-all bg-white dark:bg-gray-600 text-gray-900 dark:text-white shadow-sm";
} else {
btn.className = "px-3 py-1.5 text-xs font-medium rounded-md transition-all text-gray-500 hover:text-gray-900 dark:text-gray-400 dark:hover:text-white";
}
});
}
function changePage(delta) {
const newPage = currentState.page + delta;
if (newPage > 0 && newPage <= Math.ceil(currentState.total / currentState.size)) {
currentState.page = newPage;
loadAccounts();
}
}
function updatePaginationUI() {
const totalPages = Math.ceil(currentState.total / currentState.size);
document.getElementById('pageStart').textContent = currentState.total === 0 ? 0 : (currentState.page - 1) * currentState.size + 1;
document.getElementById('pageEnd').textContent = Math.min(currentState.page * currentState.size, currentState.total);
document.getElementById('totalItems').textContent = currentState.total;
document.getElementById('prevPage').disabled = currentState.page <= 1;
document.getElementById('nextPage').disabled = currentState.page >= totalPages;
}
function toggleSelectAll() {
const selectAll = document.getElementById('selectAll');
if (selectAll.checked) {
currentState.items.forEach(item => currentState.selectedIds.add(item.id));
} else {
currentState.selectedIds.clear();
}
renderAccounts(currentState.items); // re-render to show selection state
updateBatchUI();
}
function toggleSelect(id) {
if (currentState.selectedIds.has(id)) {
currentState.selectedIds.delete(id);
} else {
currentState.selectedIds.add(id);
}
renderAccounts(currentState.items);
updateBatchUI();
}
function updateBatchUI() {
const batchActions = document.getElementById('batchActions');
const countSpan = document.getElementById('selectedCount');
const count = currentState.selectedIds.size;
// 始终显示批量操作区域
batchActions.classList.remove('hidden');
batchActions.classList.add('flex');
// 根据当前tab和选中状态显示不同的按钮
const buttonsHtml = getBatchButtonsHtml(currentState.category, count);
// 更新按钮区域内容
if (count > 0) {
countSpan.textContent = `${count} 选中`;
countSpan.classList.remove('hidden');
} else {
countSpan.classList.add('hidden');
}
// 更新按钮容器
const buttonsContainer = document.getElementById('batchButtonsContainer');
if (buttonsContainer) {
buttonsContainer.innerHTML = buttonsHtml;
}
}
function getBatchButtonsHtml(category, selectedCount) {
// 根据是否有选中数据,决定使用哪个函数
const moveHandler = selectedCount > 0 ? 'batchMove' : 'oneClickMove';
const deleteHandler = selectedCount > 0 ? 'batchDelete(false)' : 'batchDelete(true)';
// 统一的按钮布局,只隐藏当前tab对应的按钮
return `
<button onclick="${moveHandler}('normal')" class="px-3 py-1.5 bg-green-600 text-white text-xs font-medium rounded-md hover:bg-green-700 transition-colors ${category === 'normal' ? 'hidden' : ''}">
移至正常
</button>
<button onclick="${moveHandler}('cooling')" class="px-3 py-1.5 bg-orange-600 text-white text-xs font-medium rounded-md hover:bg-orange-700 transition-colors ${category === 'cooling' ? 'hidden' : ''}">
移至冷却
</button>
<button onclick="${moveHandler}('disabled')" class="px-3 py-1.5 bg-gray-600 text-white text-xs font-medium rounded-md hover:bg-gray-700 transition-colors ${category === 'disabled' ? 'hidden' : ''}">
移至禁用
</button>
<button onclick="${moveHandler}('banned')" class="px-3 py-1.5 bg-red-600 text-white text-xs font-medium rounded-md hover:bg-red-700 transition-colors ${category === 'banned' ? 'hidden' : ''}">
移至封禁
</button>
<button onclick="${moveHandler}('error')" class="px-3 py-1.5 bg-yellow-600 text-white text-xs font-medium rounded-md hover:bg-yellow-700 transition-colors ${category === 'error' ? 'hidden' : ''}">
移至异常
</button>
<div class="border-l border-gray-300 dark:border-gray-600 mx-2 h-6"></div>
<button onclick="${deleteHandler}" class="px-3 py-1.5 bg-red-700 text-white text-xs font-medium rounded-md hover:bg-red-800 transition-colors flex items-center gap-1">
<svg xmlns="http://www.w3.org/2000/svg" class="h-3.5 w-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
${selectedCount > 0 ? '删除选中' : '删除全部'}
</button>
`;
}
async function oneClickMove(targetCategory) {
// 一键移动当前分类的所有账号到目标分类
if (currentState.category === targetCategory) {
alert('当前已在目标分类中');
return;
}
const confirmMsg = `确定要将当前分类"${getCategoryName(currentState.category)}"的所有账号移至"${getCategoryName(targetCategory)}"吗?`;
if (!confirm(confirmMsg)) return;
try {
const resp = await fetch(`${API_BASE}/accounts/batch/move-all`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders()
},
body: JSON.stringify({
from_status: currentState.category,
to_status: targetCategory
})
});
if (!resp.ok) throw new Error('One-click move failed');
const result = await resp.json();
alert(`成功移动 ${result.moved_count || 0} 个账号`);
// 刷新当前页面
loadAccounts();
} catch (e) {
alert('一键操作失败: ' + e.message);
}
}
function getCategoryName(category) {
const names = {
'normal': '正常',
'cooling': '冷却',
'banned': '封禁',
'disabled': '禁用',
'error': '异常'
};
return names[category] || category;
}
async function batchMove(category) {
if (currentState.selectedIds.size === 0) return;
const ids = Array.from(currentState.selectedIds);
try {
const resp = await fetch(`${API_BASE}/accounts/batch/category`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders()
},
body: JSON.stringify({ ids, status: category }) // send as status
});
if (!resp.ok) throw new Error('Batch update failed');
currentState.selectedIds.clear();
loadAccounts();
} catch (e) {
alert('批量操作失败');
}
}
// 批量删除功能
async function batchDelete(deleteAll = false) {
let confirmMsg;
let requestData;
if (deleteAll) {
// 删除当前分类的所有账号
confirmMsg = `确定要删除当前分类"${getCategoryName(currentState.category)}"的所有账号吗?此操作不可逆转!`;
requestData = {
delete_all: true,
status: currentState.category
};
} else {
// 删除选中的账号
if (currentState.selectedIds.size === 0) {
alert('请先选择要删除的账号');
return;
}
confirmMsg = `确定要删除选中的 ${currentState.selectedIds.size} 个账号吗?此操作不可逆转!`;
requestData = {
ids: Array.from(currentState.selectedIds)
};
}
if (!confirm(confirmMsg)) return;
try {
const resp = await fetch(`${API_BASE}/accounts/batch/delete`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders()
},
body: JSON.stringify(requestData)
});
if (!resp.ok) {
const err = await resp.json();
throw new Error(err.error || 'Delete failed');
}
const result = await resp.json();
alert(`成功删除 ${result.deleted_count || 0} 个账号`);
// 清空选择并刷新页面
currentState.selectedIds.clear();
loadAccounts();
} catch (e) {
alert('批量删除失败: ' + e.message);
}
}
// 批量刷新Token功能
async function batchRefreshToken(refreshAll = false) {
let confirmMsg;
let requestData;
if (refreshAll) {
confirmMsg = "确定要刷新所有正常账号的Token吗?此操作可能需要较长时间。";
requestData = { all: true };
} else {
if (currentState.selectedIds.size === 0) {
alert('请先选择要刷新Token的账号');
return;
}
confirmMsg = `确定要刷新选中的 ${currentState.selectedIds.size} 个账号的Token吗?`;
requestData = { ids: Array.from(currentState.selectedIds) };
}
if (!confirm(confirmMsg)) return;
try {
// 显示进度弹窗
showRefreshProgressModal();
addRefreshProgressLog('开始批量刷新Token...', 'info');
const resp = await fetch(`${API_BASE}/accounts/batch/refresh-token`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders()
},
body: JSON.stringify(requestData)
});
if (!resp.ok) {
const err = await resp.json();
throw new Error(err.error || 'Unknown error');
}
// 处理流式响应
const reader = resp.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let successCount = 0;
let failCount = 0;
let total = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop(); // 保留不完整的行
for (const line of lines) {
if (!line.trim() || !line.startsWith('data: ')) continue;
try {
const jsonStr = line.substring(6); // 移除 "data: " 前缀
const event = JSON.parse(jsonStr);
switch (event.type) {
case 'start':
total = event.total;
updateRefreshProgress(0, total, '开始刷新');
addRefreshProgressLog(`准备刷新 ${total} 个账号的Token`, 'info');
break;
case 'success':
successCount++;
updateRefreshProgress(successCount + failCount, total, '刷新中');
addRefreshProgressLog(`✓ [${event.index}/${total}] 刷新成功: ${event.account_id}${event.email ? ` (${event.email})` : ''}`, 'success');
break;
case 'error':
failCount++;
updateRefreshProgress(successCount + failCount, total, '刷新中');
addRefreshProgressLog(`✗ [${event.index}/${total}] ${event.message} (${event.account_id})`, 'error');
break;
case 'complete':
updateRefreshProgress(total, total, '完成');
addRefreshProgressLog(`批量刷新完成!成功 ${event.success} 个,失败 ${event.fail} 个`, 'info');
showRefreshProgressSummary(event.success, event.fail);
break;
}
} catch (e) {
console.error('解析事件失败:', e, line);
}
}
}
return true;
} catch (e) {
addRefreshProgressLog(`错误: ${e.message}`, 'error');
document.getElementById('refreshProgressCloseBtn').classList.remove('hidden');
return false;
}
}
// 刷新进度弹窗管理
function showRefreshProgressModal() {
document.getElementById('refreshProgressModal').classList.remove('hidden');
document.getElementById('refreshProgressLog').innerHTML = '';
document.getElementById('refreshProgressBar').style.width = '0%';
document.getElementById('refreshProgressText').textContent = '准备中...';
document.getElementById('refreshProgressCount').textContent = '0/0';
document.getElementById('refreshProgressSummary').classList.add('hidden');
document.getElementById('refreshProgressCloseBtn').classList.add('hidden');
}
function closeRefreshProgressModal() {
document.getElementById('refreshProgressModal').classList.add('hidden');
loadAccounts(); // 刷新账号列表
currentState.selectedIds.clear(); // 清空选择
updateBatchUI(); // 更新批量操作UI
}
function addRefreshProgressLog(message, type = 'info') {
const log = document.getElementById('refreshProgressLog');
const colors = {
info: 'text-gray-600 dark:text-gray-400',
success: 'text-green-600 dark:text-green-400',
error: 'text-red-600 dark:text-red-400',
warning: 'text-yellow-600 dark:text-yellow-400'
};
const entry = document.createElement('div');
entry.className = colors[type] || colors.info;
entry.textContent = message;
log.appendChild(entry);
// 自动滚动到底部
log.scrollTop = log.scrollHeight;
}
function updateRefreshProgress(current, total, text) {
const percentage = total > 0 ? (current / total) * 100 : 0;
document.getElementById('refreshProgressBar').style.width = `${percentage}%`;
document.getElementById('refreshProgressText').textContent = text;
document.getElementById('refreshProgressCount').textContent = `${current}/${total}`;
}
function showRefreshProgressSummary(success, fail) {
document.getElementById('refreshSummarySuccess').textContent = success;
document.getElementById('refreshSummaryFail').textContent = fail;
document.getElementById('refreshProgressSummary').classList.remove('hidden');
document.getElementById('refreshProgressCloseBtn').classList.remove('hidden');
}
async function addAccount(data) {
const btn = document.getElementById('submitBtn');
const btnText = document.getElementById('btnText');
const btnLoading = document.getElementById('btnLoading');
btn.disabled = true;
btnLoading.classList.remove('hidden');
try {
// 生成模式使用流式传输
if (data.generate_mode) {
btnText.textContent = '生成中...';
const resp = await fetch(`${API_BASE}/accounts`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders()
},
body: JSON.stringify(data)
});
if (!resp.ok) {
const err = await resp.json();
throw new Error(err.error || 'Unknown error');
}
// 处理流式响应
const reader = resp.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let successCount = 0;
let failCount = 0;
let total = 0;
let firstSuccessAccount = null; // 记录第一个成功的账号信息
let isProgressShown = false;
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop(); // 保留不完整的行
for (const line of lines) {
if (!line.trim() || !line.startsWith('data: ')) continue;
try {
const jsonStr = line.substring(6); // 移除 "data: " 前缀
const event = JSON.parse(jsonStr);
switch (event.type) {
case 'start':
total = event.total;
// 如果是单个账号生成,不显示进度窗口
if (total > 1) {
showProgressModal();
updateProgress(0, total, '开始生成');
addProgressLog('开始批量生成凭证...', 'info');
addProgressLog(`准备生成 ${total} 个凭证`, 'info');
isProgressShown = true;
}
break;
case 'success':
successCount++;
// 记录第一个成功的账号信息(单个生成时使用)
if (!firstSuccessAccount && event.email) {
firstSuccessAccount = {
email: event.email,
plan: event.plan,
token_expiry: event.token_expiry,
subscription_start_date: event.subscription_start_date
};
}
if (isProgressShown) {
updateProgress(successCount + failCount, total, '生成中');
const action = event.action === 'created' ? '创建' : '更新';
addProgressLog(`✓ [${event.index}/${total}] ${action}成功: ${event.email} (${event.plan})`, 'success');
}
break;
case 'error':
failCount++;
if (isProgressShown) {
updateProgress(successCount + failCount, total, '生成中');
const clientInfo = event.client_id ? ` (${event.client_id})` : '';
addProgressLog(`✗ [${event.index}/${total}] ${event.message}${clientInfo}`, 'error');
}
break;
case 'complete':
if (isProgressShown) {
// 批量生成完成
updateProgress(total, total, '完成');
addProgressLog(`批量生成完成!成功 ${event.success} 个,失败 ${event.fail} 个`, 'info');
showProgressSummary(event.success, event.fail);
} else {
// 单个账号生成完成
if (firstSuccessAccount) {
// 显示账号信息弹窗
showAccountInfoModal(firstSuccessAccount);
} else if (failCount > 0) {
// 生成失败
showToast('账号生成失败', 'error');
}
}
break;
}
} catch (e) {
console.error('解析事件失败:', e, line);
}
}
}
return true;
} else {
// 凭证模式使用普通请求
btnText.textContent = '添加中...';
const resp = await fetch(`${API_BASE}/accounts`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders()
},
body: JSON.stringify(data)
});
if (!resp.ok) {
const err = await resp.json();
throw new Error(err.error || 'Unknown error');
}
loadAccounts();
return true;
}
} catch (e) {
if (data.generate_mode) {
showToast(`生成失败: ${e.message}`, 'error');
} else {
alert('操作失败: ' + e.message);
}
return false;
} finally {
btn.disabled = false;
btnText.textContent = currentAddMode === 'credential' ? '添加账号' : '批量生成';
btnLoading.classList.add('hidden');
}
}
async function deleteAccount(id) {
if (!confirm('确定要删除此账号吗?')) return;
try {
await fetch(`${API_BASE}/accounts/${id}`, {
method: 'DELETE',
headers: getAuthHeaders()
});
loadAccounts();
} catch (e) {
alert('删除失败');
}
}
async function toggleAccount(id) {
try {
await fetch(`${API_BASE}/accounts/${id}/toggle`, {
method: 'POST',
headers: getAuthHeaders()
});
loadAccounts();
} catch (e) {
alert('操作失败');
}
}
// --- Progress Modal Management ---
function showProgressModal() {
document.getElementById('progressModal').classList.remove('hidden');
document.getElementById('progressLog').innerHTML = '';
document.getElementById('progressBar').style.width = '0%';
document.getElementById('progressText').textContent = '准备中...';
document.getElementById('progressCount').textContent = '0/0';
document.getElementById('progressSummary').classList.add('hidden');
document.getElementById('progressCloseBtn').classList.add('hidden');
}
function closeProgressModal() {
document.getElementById('progressModal').classList.add('hidden');
loadAccounts(); // 刷新账号列表
}
function addProgressLog(message, type = 'info') {
const log = document.getElementById('progressLog');
const colors = {
info: 'text-gray-600 dark:text-gray-400',
success: 'text-green-600 dark:text-green-400',
error: 'text-red-600 dark:text-red-400',
warning: 'text-yellow-600 dark:text-yellow-400'
};
const entry = document.createElement('div');
entry.className = colors[type] || colors.info;
entry.textContent = message;
log.appendChild(entry);
// 自动滚动到底部
log.scrollTop = log.scrollHeight;
}
function updateProgress(current, total, text) {
const percentage = total > 0 ? (current / total) * 100 : 0;
document.getElementById('progressBar').style.width = `${percentage}%`;
document.getElementById('progressText').textContent = text;
document.getElementById('progressCount').textContent = `${current}/${total}`;
}
function showProgressSummary(success, fail) {
document.getElementById('summarySuccess').textContent = success;
document.getElementById('summaryFail').textContent = fail;
document.getElementById('progressSummary').classList.remove('hidden');
document.getElementById('progressCloseBtn').classList.remove('hidden');
}
// --- Add Mode Management ---
let currentAddMode = 'credential'; // 'credential' or 'generate'
function switchAddMode(mode) {
currentAddMode = mode;
const credentialBtn = document.getElementById('mode-credential');
const generateBtn = document.getElementById('mode-generate');
const credentialFields = document.getElementById('credentialFields');
const generateFields = document.getElementById('generateFields');
const submitBtn = document.getElementById('btnText');
if (mode === 'credential') {
credentialBtn.classList.add('bg-white', 'dark:bg-gray-600', 'text-gray-900', 'dark:text-white', 'shadow-sm');
credentialBtn.classList.remove('text-gray-500', 'hover:text-gray-900', 'dark:text-gray-400', 'dark:hover:text-white');
generateBtn.classList.remove('bg-white', 'dark:bg-gray-600', 'text-gray-900', 'dark:text-white', 'shadow-sm');
generateBtn.classList.add('text-gray-500', 'hover:text-gray-900', 'dark:text-gray-400', 'dark:hover:text-white');
credentialFields.classList.remove('hidden');
generateFields.classList.add('hidden');
submitBtn.textContent = '添加账号';
} else {
generateBtn.classList.add('bg-white', 'dark:bg-gray-600', 'text-gray-900', 'dark:text-white', 'shadow-sm');
generateBtn.classList.remove('text-gray-500', 'hover:text-gray-900', 'dark:text-gray-400', 'dark:hover:text-white');
credentialBtn.classList.remove('bg-white', 'dark:bg-gray-600', 'text-gray-900', 'dark:text-white', 'shadow-sm');
credentialBtn.classList.add('text-gray-500', 'hover:text-gray-900', 'dark:text-gray-400', 'dark:hover:text-white');
credentialFields.classList.add('hidden');
generateFields.classList.remove('hidden');
submitBtn.textContent = '批量生成';
}
}
// Form Handler
document.getElementById('addForm').addEventListener('submit', async (e) => {
e.preventDefault();
let data;
if (currentAddMode === 'credential') {
const accessToken = document.getElementById('credential_access_token').value.trim();
const refreshToken = document.getElementById('credential_refresh_token').value.trim();
const proxy = document.getElementById('proxy').value.trim();
if (!accessToken && !refreshToken) {
alert('请填写 Access Token 或 Refresh Token(至少一个)');
return;
}
data = {
proxy: proxy,
generate_mode: false
};
// 提交用户填写的所有token字段,后端会根据优先级处理
if (accessToken) {
data.access_token = accessToken;
}
if (refreshToken) {
data.refresh_token = refreshToken;
}
} else {
const masterAccessToken = document.getElementById('generate_access_token').value.trim();
const masterRefreshToken = document.getElementById('generate_refresh_token').value.trim();
const proxy = document.getElementById('proxy').value.trim();
if (!masterAccessToken && !masterRefreshToken) {
alert('请填写 Master Access Token 或 Master Refresh Token(至少一个)');
return;
}
data = {
proxy: proxy,
generate_mode: true
};
// 提交用户填写的所有token字段,后端会根据优先级处理
if (masterAccessToken) {
data.access_token = masterAccessToken;
}
if (masterRefreshToken) {
data.refresh_token = masterRefreshToken;
}
}
if (await addAccount(data)) {
e.target.reset();
if (currentAddMode === 'credential') {
document.getElementById('credential_refresh_token').focus();
} else {
document.getElementById('generate_refresh_token').focus();
}
}
});
// Initialization function for after admin login
function initializeApp() {
loadAccounts();
// Auto Refresh
if (autoRefreshTimer) {
clearInterval(autoRefreshTimer);
}
autoRefreshTimer = setInterval(() => {
loadAccounts(true);
}, REFRESH_INTERVAL);
}
// --- Token Management ---
let currentMainView = 'pool'; // 'pool' or 'token'
let tokenRecords = [];
let generationTasks = [];
function switchMainView(view) {
currentMainView = view;
const poolView = document.getElementById('poolView');
const tokenView = document.getElementById('tokenView');
const poolBtn = document.getElementById('view-pool');
const tokenBtn = document.getElementById('view-token');
if (view === 'pool') {
poolView.classList.remove('hidden');
tokenView.classList.add('hidden');
poolBtn.classList.add('bg-white', 'dark:bg-gray-600', 'text-gray-900', 'dark:text-white', 'shadow-sm');
poolBtn.classList.remove('text-gray-500', 'hover:text-gray-900', 'dark:text-gray-400', 'dark:hover:text-white');
tokenBtn.classList.remove('bg-white', 'dark:bg-gray-600', 'text-gray-900', 'dark:text-white', 'shadow-sm');
tokenBtn.classList.add('text-gray-500', 'hover:text-gray-900', 'dark:text-gray-400', 'dark:hover:text-white');
loadAccounts();
} else {
poolView.classList.add('hidden');
tokenView.classList.remove('hidden');
tokenBtn.classList.add('bg-white', 'dark:bg-gray-600', 'text-gray-900', 'dark:text-white', 'shadow-sm');
tokenBtn.classList.remove('text-gray-500', 'hover:text-gray-900', 'dark:text-gray-400', 'dark:hover:text-white');
poolBtn.classList.remove('bg-white', 'dark:bg-gray-600', 'text-gray-900', 'dark:text-white', 'shadow-sm');
poolBtn.classList.add('text-gray-500', 'hover:text-gray-900', 'dark:text-gray-400', 'dark:hover:text-white');
loadTokenData();
}
}
async function loadTokenData() {
await Promise.all([
loadTokenRecords(),
loadGenerationTasks(),
loadPoolStatus()
]);
}
async function loadTokenRecords() {
try {
const resp = await fetch(`${API_BASE}/tokens`, {
headers: getAuthHeaders()
});
if (!resp.ok) throw new Error('Failed to fetch token records');
const data = await resp.json();
tokenRecords = data.items || [];
renderTokenRecords(tokenRecords);
} catch (e) {
console.error("Failed to load token records", e);
}
}
async function loadGenerationTasks() {
try {
const resp = await fetch(`${API_BASE}/tokens/tasks`, {
headers: getAuthHeaders()
});
if (!resp.ok) throw new Error('Failed to fetch generation tasks');
const data = await resp.json();
generationTasks = data.items || [];
renderGenerationTasks(generationTasks);
} catch (e) {
console.error("Failed to load generation tasks", e);
}
}
async function loadPoolStatus() {
try {
const resp = await fetch(`${API_BASE}/tokens/pool-status`, {
headers: getAuthHeaders()
});
if (!resp.ok) throw new Error('Failed to fetch pool status');
const data = await resp.json();
updateTokenStatsUI(data);
} catch (e) {
console.error("Failed to load pool status", e);
}
}
function updateTokenStatsUI(stats) {
document.getElementById('token-stat-active').textContent = stats.active_tokens || 0;
document.getElementById('token-stat-normal').textContent = stats.normal_accounts || 0;
document.getElementById('token-stat-running').textContent = stats.running_tasks || 0;
}
function renderTokenRecords(records) {
const tbody = document.getElementById('tokenList');
const emptyState = document.getElementById('tokenEmptyState');
if (records.length === 0) {
tbody.innerHTML = '';
emptyState.classList.remove('hidden');
emptyState.classList.add('flex');
return;
}
emptyState.classList.add('hidden');
emptyState.classList.remove('flex');
const html = records.map(record => {
// 格式化订阅日期
const subDate = record.subscription_start_date && !record.subscription_start_date.startsWith('0001')
? new Date(record.subscription_start_date).toLocaleDateString('zh-CN')
: '-';
// 格式化Token过期时间
const tokenExpiryDate = record.token_expiry && !record.token_expiry.startsWith('0001')
? new Date(record.token_expiry)
: null;
let tokenStatusClass, tokenStatusText;
if (!tokenExpiryDate) {
tokenStatusClass = 'bg-gray-100 text-gray-700 dark:bg-gray-900/30 dark:text-gray-400';
tokenStatusText = '未知';
} else {
const now = new Date();
const hoursUntilExpiry = (tokenExpiryDate - now) / (1000 * 60 * 60);
if (hoursUntilExpiry < 0) {
tokenStatusClass = 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400';
tokenStatusText = '已过期';
} else if (hoursUntilExpiry < 1) {
tokenStatusClass = 'bg-orange-100 text-orange-700 dark:bg-orange-900/30 dark:text-orange-400';
tokenStatusText = `${Math.floor(hoursUntilExpiry * 60)}分钟后过期`;
} else if (hoursUntilExpiry < 24) {
tokenStatusClass = 'bg-yellow-100 text-yellow-700 dark:bg-yellow-900/30 dark:text-yellow-400';
tokenStatusText = `${Math.floor(hoursUntilExpiry)}小时后过期`;
} else {
tokenStatusClass = 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400';
tokenStatusText = `${Math.floor(hoursUntilExpiry / 24)}天后过期`;
}
}
return `
<tr class="hover:bg-gray-50 dark:hover:bg-gray-800/50 transition-colors">
<td class="px-6 py-4">
<div>
<div class="text-sm font-medium text-gray-900 dark:text-white">${record.email || '未知邮箱'}</div>
<div class="text-xs text-gray-500 dark:text-gray-400 mt-0.5">${record.description || `令牌 #${record.id}`}</div>
</div>
</td>
<td class="px-6 py-4 text-center">
<div>
<div class="text-sm font-medium ${record.plan_type === 'Starter' ? 'text-blue-600 dark:text-blue-400' : record.plan_type === 'Core' ? 'text-purple-600 dark:text-purple-400' : record.plan_type === 'Advanced' ? 'text-orange-600 dark:text-orange-400' : record.plan_type === 'Max' ? 'text-red-600 dark:text-red-400' : 'text-gray-600 dark:text-gray-400'}">${record.plan_type || 'Free'}</div>
</div>
</td>
<td class="px-6 py-4 text-center">
<span class="text-sm text-gray-600 dark:text-gray-400">${subDate}</span>
</td>
<td class="px-6 py-4 text-center">
<div class="space-y-1">
<div>
<span class="px-2 py-0.5 inline-flex items-center text-xs font-medium rounded-full ${getTokenStatusConfig(record).class}">
<span class="w-1.5 h-1.5 rounded-full ${getTokenStatusConfig(record).dot} mr-1.5"></span>
${getTokenStatusConfig(record).text}
</span>
</div>
<div>
<span class="px-2 py-0.5 inline-flex text-xs rounded-full ${tokenStatusClass}">
${tokenStatusText}
</span>
</div>
</div>
</td>
<td class="px-6 py-4 text-center">
<div class="text-sm">
<span class="text-gray-900 dark:text-white font-medium">${record.generated_count || 0}</span>
<span class="text-gray-500">/</span>
<span class="text-gray-600 dark:text-gray-400">${record.threshold}</span>
<div class="text-xs text-gray-500 dark:text-gray-400 mt-0.5">批次: ${record.generate_batch}</div>
</div>
</td>
<td class="px-6 py-4 text-center">
<div class="text-sm">
<span class="text-green-600 dark:text-green-400">${record.total_success || 0}</span>
<span class="text-gray-500">/</span>
<span class="text-red-600 dark:text-red-400">${record.total_fail || 0}</span>
</div>
</td>
<td class="px-6 py-4 text-center">
<button onclick="quickToggleAutoGenerate(${record.id}, ${!record.auto_generate})"
class="${record.auto_generate ? 'text-green-600' : 'text-gray-400'} hover:opacity-70 transition-opacity">
${record.auto_generate
? '<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mx-auto" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" /></svg>'
: '<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mx-auto" viewBox="0 0 20 20" fill="currentColor"><path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" /></svg>'}
</button>
</td>
<td class="px-6 py-4 text-center">
<div class="flex justify-center gap-2">
<button onclick='showTokenConfigModal(${JSON.stringify(record).replace(/'/g, "\\'")})'
class="text-primary hover:text-primary-hover transition-colors" title="配置">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z" />
</svg>
</button>
${record.is_active ? `
<button onclick="triggerGeneration(${record.id})"
class="text-blue-600 hover:text-blue-700 transition-colors" title="手动触发生成">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z" />
</svg>
</button>` : ''}
${record.has_refresh_token ? `
<button onclick="refreshToken(${record.id})"
class="text-green-600 hover:text-green-700 transition-colors" title="刷新令牌">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
</svg>
</button>` : ''}
<button onclick="deleteTokenRecord(${record.id})"
class="text-red-500 hover:text-red-700 transition-colors" title="删除">
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
</svg>
</button>
</div>
</td>
</tr>`;
}).join('');
tbody.innerHTML = html;
}
// 添加刷新令牌函数
async function refreshToken(tokenId) {
try {
const resp = await fetch(`${API_BASE}/tokens/${tokenId}/refresh`, {
method: 'POST',
headers: getAuthHeaders()
});
if (!resp.ok) {
const error = await resp.json();
throw new Error(error.error || 'Failed to refresh token');
}
const result = await resp.json();
showToast(result.message || 'Token刷新成功', 'success');
loadTokenRecords();
} catch (e) {
showToast('Token刷新失败: ' + e.message, 'error');
}
}
function renderGenerationTasks(tasks) {
const tbody = document.getElementById('taskList');
if (tasks.length === 0) {
tbody.innerHTML = '<tr><td colspan="6" class="px-6 py-8 text-center text-sm text-gray-500 dark:text-gray-400">暂无生成任务记录</td></tr>';
return;
}
const html = tasks.map(task => {
const startTime = task.started_at && !task.started_at.startsWith('0001')
? new Date(task.started_at).toLocaleString('zh-CN')
: '-';
const completeTime = task.completed_at && !task.completed_at.startsWith('0001')
? new Date(task.completed_at).toLocaleString('zh-CN')
: '-';
let statusClass, statusText;
switch (task.status) {
case 'running':
statusClass = 'bg-blue-100 text-blue-700 dark:bg-blue-900/30 dark:text-blue-400';
statusText = '运行中';
break;
case 'completed':
statusClass = 'bg-green-100 text-green-700 dark:bg-green-900/30 dark:text-green-400';
statusText = '已完成';
break;
case 'failed':
statusClass = 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400';
statusText = '失败';
break;
default:
statusClass = 'bg-gray-100 text-gray-700 dark:bg-gray-900/30 dark:text-gray-400';
statusText = '待处理';
}
return `
<tr class="hover:bg-gray-50 dark:hover:bg-gray-800/50 transition-colors">
<td class="px-6 py-3 text-sm text-gray-900 dark:text-white">#${task.id}</td>
<td class="px-6 py-3 text-sm text-center text-gray-900 dark:text-white">${task.batch_size}</td>
<td class="px-6 py-3 text-sm text-center">
<span class="text-green-600 dark:text-green-400">${task.success_count}</span>
<span class="text-gray-500">/</span>
<span class="text-red-600 dark:text-red-400">${task.fail_count}</span>
</td>
<td class="px-6 py-3 text-center">
<span class="px-2 py-0.5 inline-flex text-xs font-medium rounded-full ${statusClass}">
${statusText}
</span>
</td>
<td class="px-6 py-3 text-sm text-center text-gray-600 dark:text-gray-400">${startTime}</td>
<td class="px-6 py-3 text-sm text-center text-gray-600 dark:text-gray-400">${completeTime}</td>
</tr>`;
}).join('');
tbody.innerHTML = html;
}
async function quickToggleAutoGenerate(tokenId, enable) {
try {
const resp = await fetch(`${API_BASE}/tokens/${tokenId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders()
},
body: JSON.stringify({ auto_generate: enable })
});
if (!resp.ok) throw new Error('Failed to update token');
loadTokenRecords();
} catch (e) {
alert('更新失败: ' + e.message);
}
}
async function quickToggleActive(tokenId, enable) {
try {
const resp = await fetch(`${API_BASE}/tokens/${tokenId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders()
},
body: JSON.stringify({ is_active: enable })
});
if (!resp.ok) throw new Error('Failed to update token');
loadTokenRecords();
} catch (e) {
alert('更新失败: ' + e.message);
}
}
async function deleteTokenRecord(tokenId) {
if (!confirm('确定要删除此令牌记录吗?')) return;
try {
const resp = await fetch(`${API_BASE}/tokens/${tokenId}`, {
method: 'DELETE',
headers: getAuthHeaders()
});
if (!resp.ok) throw new Error('Failed to delete token');
const result = await resp.json();
alert(result.message || '删除成功');
loadTokenRecords();
} catch (e) {
alert('删除失败: ' + e.message);
}
}
async function triggerGeneration(tokenId) {
if (!confirm('确定要手动触发生成任务吗?')) return;
try {
const resp = await fetch(`${API_BASE}/tokens/${tokenId}/trigger`, {
method: 'POST',
headers: getAuthHeaders()
});
if (!resp.ok) throw new Error('Failed to trigger generation');
alert('生成任务已触发,请稍后查看结果');
setTimeout(() => {
loadTokenData();
}, 2000);
} catch (e) {
alert('触发失败: ' + e.message);
}
}
let currentEditingToken = null;
function showTokenConfigModal(record) {
currentEditingToken = record;
document.getElementById('tokenConfigModal').classList.remove('hidden');
// 填充表单数据
document.getElementById('configTokenId').value = record.id;
document.getElementById('configDescription').value = record.description || '';
document.getElementById('configThreshold').value = record.threshold || 10;
document.getElementById('configBatch').value = record.generate_batch || 30;
// 设置开关状态
setConfigSwitch('configAutoGenerate', record.auto_generate);
setConfigSwitch('configIsActive', record.is_active);
}
function closeTokenConfigModal() {
document.getElementById('tokenConfigModal').classList.add('hidden');
currentEditingToken = null;
}
function setConfigSwitch(switchId, isOn) {
const switchBtn = document.getElementById(switchId);
const indicator = switchBtn.querySelector('span');
if (isOn) {
switchBtn.classList.remove('bg-gray-200', 'dark:bg-gray-600');
switchBtn.classList.add('bg-primary');
indicator.classList.remove('translate-x-1');
indicator.classList.add('translate-x-6');
switchBtn.dataset.checked = 'true';
} else {
switchBtn.classList.add('bg-gray-200', 'dark:bg-gray-600');
switchBtn.classList.remove('bg-primary');
indicator.classList.add('translate-x-1');
indicator.classList.remove('translate-x-6');
switchBtn.dataset.checked = 'false';
}
}
function toggleConfigSwitch(switchId) {
const switchBtn = document.getElementById(switchId);
const isChecked = switchBtn.dataset.checked === 'true';
setConfigSwitch(switchId, !isChecked);
}
// Token config form handler
document.getElementById('tokenConfigForm').addEventListener('submit', async (e) => {
e.preventDefault();
const tokenId = document.getElementById('configTokenId').value;
const config = {
description: document.getElementById('configDescription').value,
threshold: parseInt(document.getElementById('configThreshold').value),
generate_batch: parseInt(document.getElementById('configBatch').value),
auto_generate: document.getElementById('configAutoGenerate').dataset.checked === 'true',
is_active: document.getElementById('configIsActive').dataset.checked === 'true'
};
try {
const resp = await fetch(`${API_BASE}/tokens/${tokenId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders()
},
body: JSON.stringify(config)
});
if (!resp.ok) throw new Error('Failed to update token config');
closeTokenConfigModal();
loadTokenRecords();
} catch (e) {
alert('更新配置失败: ' + e.message);
}
});
async function refreshTokenData() {
await loadTokenData();
}
// OAuth RT获取功能
function startOAuthForRT() {
// 打开新窗口进行OAuth认证
const width = 600;
const height = 700;
const left = (window.screen.width - width) / 2;
const top = (window.screen.height - height) / 2;
const authWindow = window.open(
'/api/oauth/start-rt',
'ZenCoderOAuth',
`width=${width},height=${height},left=${left},top=${top},toolbar=no,menubar=no,scrollbars=yes,resizable=yes`
);
// 监听OAuth完成消息
window.addEventListener('message', function handleOAuthMessage(event) {
// 验证消息来源
if (event.origin !== window.location.origin) return;
if (event.data.type === 'oauth-rt-complete') {
// 关闭认证窗口
if (authWindow && !authWindow.closed) {
authWindow.close();
}
// 移除事件监听器
window.removeEventListener('message', handleOAuthMessage);
if (event.data.success && (event.data.accessToken || event.data.refreshToken)) {
// 自动填充到对应的输入框
const currentMode = currentAddMode;
if (currentMode === 'credential') {
// 优先填充access_token
if (event.data.accessToken) {
document.getElementById('credential_access_token').value = event.data.accessToken;
}
// 同时填充refresh_token
if (event.data.refreshToken) {
document.getElementById('credential_refresh_token').value = event.data.refreshToken;
}
// 显示成功提示
showToast('Token 获取成功!', 'success');
} else {
// 生成模式
if (event.data.accessToken) {
document.getElementById('generate_access_token').value = event.data.accessToken;
}
if (event.data.refreshToken) {
document.getElementById('generate_refresh_token').value = event.data.refreshToken;
}
showToast('Master Token 获取成功!', 'success');
}
} else {
showToast(event.data.error || 'OAuth认证失败', 'error');
}
}
});
}
// Toast提示功能
function showToast(message, type = 'info') {
// 创建toast元素
const toast = document.createElement('div');
toast.className = `fixed top-4 right-4 px-6 py-3 rounded-lg shadow-lg transition-all duration-300 transform translate-x-96 z-50`;
// 根据类型设置样式
const styles = {
success: 'bg-green-600 text-white',
error: 'bg-red-600 text-white',
warning: 'bg-yellow-500 text-white',
info: 'bg-blue-600 text-white'
};
toast.className += ` ${styles[type] || styles.info}`;
toast.innerHTML = `
<div class="flex items-center gap-2">
<span class="text-sm font-medium">${message}</span>
</div>
`;
document.body.appendChild(toast);
// 动画显示
setTimeout(() => {
toast.classList.remove('translate-x-96');
toast.classList.add('translate-x-0');
}, 10);
// 3秒后自动消失
setTimeout(() => {
toast.classList.remove('translate-x-0');
toast.classList.add('translate-x-96');
setTimeout(() => {
document.body.removeChild(toast);
}, 300);
}, 3000);
}
// --- Account Info Modal Management ---
function showAccountInfoModal(accountInfo) {
document.getElementById('accountInfoModal').classList.remove('hidden');
// 填充账号信息
document.getElementById('accountInfoEmail').textContent = accountInfo.email || '未知';
document.getElementById('accountInfoPlan').textContent = accountInfo.plan || 'Free';
// 格式化Token过期时间
let tokenExpiryText = '未知';
if (accountInfo.token_expiry && !accountInfo.token_expiry.startsWith('0001')) {
const expiryDate = new Date(accountInfo.token_expiry);
const now = new Date();
const diffMs = expiryDate - now;
if (diffMs < 0) {
tokenExpiryText = '已过期';
} else if (diffMs < 1000 * 60 * 60 * 24) {
// 24小时内过期
const hours = Math.floor(diffMs / (1000 * 60 * 60));
tokenExpiryText = `${hours}小时后过期`;
} else {
// 显示具体日期
tokenExpiryText = expiryDate.toLocaleDateString('zh-CN') + ' ' + expiryDate.toLocaleTimeString('zh-CN', {hour: '2-digit', minute: '2-digit'});
}
}
document.getElementById('accountInfoTokenExpiry').textContent = tokenExpiryText;
// 格式化订阅开始时间
let subscriptionStartText = '未知';
if (accountInfo.subscription_start_date && !accountInfo.subscription_start_date.startsWith('0001')) {
const startDate = new Date(accountInfo.subscription_start_date);
subscriptionStartText = startDate.toLocaleDateString('zh-CN') + ' ' + startDate.toLocaleTimeString('zh-CN', {hour: '2-digit', minute: '2-digit'});
}
document.getElementById('accountInfoSubscriptionStart').textContent = subscriptionStartText;
}
function closeAccountInfoModal() {
document.getElementById('accountInfoModal').classList.add('hidden');
// 刷新账号列表
loadAccounts();
}
// Page Initialization
window.addEventListener('load', function() {
console.log('Page loaded, initializing...');
initTheme();
initAdminAuth();
});
|