域名管家:利用 Cloudflare 免费搭建带 Telegram 通知的域名监控系统

如果你手里囤了不少域名,最怕的就是忘记续费导致域名被抢注。买昂贵的服务器搭建监控系统又显得大材小用。本文将手把手教你如何利用 Cloudflare Workers 零成本搭建一个高颜值的域名监控系统,并且支持 Telegram 自动到期提醒。

🌟 为什么选择这个方案?

  1. 完全免费:利用 Cloudflare 强大的免费额度(每天 10 万次请求),零服务器成本。
  2. 自动查询:内置 WHOIS 接口,输入域名一键自动获取注册厂商、注册时间和到期时间。
  3. 防漏续费:支持配置 Telegram 机器人,域名到期前自动推送到你的手机。
  4. 颜值与体验:高颜值的毛玻璃 UI 设计,支持电脑/手机端自适应,带有可视化进度条。

🛠️ 第一步:准备必要数据

在开始搭建前,请先明确或收集以下信息:

  • Cloudflare 账号:用于运行脚本和数据库。
  • Telegram 机器人 Token(可选):找 @BotFather 创建机器人获取,用于接收通知。
  • 管理员 ID(可选):找 @userinfobot 获取你的数字 ID,用于接收通知。
  • WhoisJSON API 密钥(可选):在 WhoisJSON 免费注册获取,用于自动查询域名信息。

📦 第二步:创建 KV 数据库

KV 数据库相当于这个系统的“记忆”,用来永久保存你的域名列表和配置信息。

  1. 登录 Cloudflare 控制台
  2. 在左侧导航栏找到并点击 Workers & Pages -> KV
  3. 点击右侧的 创建命名空间 按钮。
  4. 在“命名空间名称”输入框中,严格填写 DOMAIN_MONITOR(注意全部大写)。
  5. 点击 添加 完成创建。

💻 第三步:创建并部署 Worker 脚本

  1. 在左侧导航栏点击 Workers & Pages -> 概述
  2. 点击 创建应用程序,然后点击 创建 Worker
  3. 给你的系统取个名字(例如 my-domain-monitor),点击右下角的 部署 按钮。
  4. 部署成功后,点击 编辑代码 按钮进入代码编辑器。
  5. 清空左侧原本自带的所有代码。
  6. 将完整的域名监控系统代码粘贴进去,然后点击右上角的 保存并部署
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
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
/*
* 域名监控系统 - Cloudflare Workers
* 使用KV存储域名信息,支持Telegram通知
* 功能:域名到期监控、自动通知、域名管理
*/

// ================================
// 配置常量区域
// ================================

// iconfont阿里巴巴图标库
const ICONFONT_CSS = '//at.alicdn.com/t/c/font_4973034_1qunj5fctpb.css';
const ICONFONT_JS = '//at.alicdn.com/t/c/font_4973034_1qunj5fctpb.js';

// 网站图标和背景图片
const DEFAULT_LOGO = 'https://tc.agsy.hidns.vip/1767788978899_bn2booz.png'; // 默认logo,外置变量为LOGO_URL
const DEFAULT_BACKGROUND = 'https://tc.agsy.hidns.vip/1766481242820_ixsazgt.jpg'; // 默认背景,外置变量为BACKGROUND_URL
const DEFAULT_MOBILE_BACKGROUND = 'https://tc.agsy.hidns.vip/1767802054245_2g7llb5.jpg'; // 默认移动端背景,外置变量为MOBILE_BACKGROUND_URL

// 登录账号设置 (新增)
const DEFAULT_USERNAME = 'admin'; // 默认账号,外置变量为USERNAME

// 登录密码设置
const DEFAULT_TOKEN = ''; // 默认密码,留空则使用'domain',外置变量为TOKEN

// Telegram通知配置
const DEFAULT_TG_TOKEN = ''; // Telegram机器人Token,外置变量为TG_TOKEN
const DEFAULT_TG_ID = ''; // Telegram聊天ID,外置变量为TG_ID

// 网站标题配置
const DEFAULT_SITE_NAME = ''; // 默认网站标题,外置变量为SITE_NAME

// WhoisJSON API配置
const DEFAULT_WHOISJSON_API_KEY = ''; // WhoisJSON API密钥,外置变量为WHOISJSON_API_KEY

// ================================
// 工具函数区域
// ================================

// === 优化后的加密工具函数 (支持后台变量) ===
async function generateHash(text) {
// 优先读取后台配置的 AUTH_SALT,如果没有配置,才使用默认的乱码
const salt = (typeof AUTH_SALT !== 'undefined' && AUTH_SALT) ? AUTH_SALT : "MySecretSalt_2026_@#$";

const msgBuffer = new TextEncoder().encode(text + salt);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}

// 格式化日期函数
function formatDate(dateString) {
const date = new Date(dateString);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}

// JSON响应工具函数
function jsonResponse(data, status = 200) {
return new Response(JSON.stringify(data), {
status,
headers: { 'Content-Type': 'application/json' }
});
}

// WhoisJSON API查询函数
async function queryDomainWhois(domain) {
try {
// 获取API密钥,优先使用环境变量,否则使用默认值
const apiKey = typeof WHOISJSON_API_KEY !== 'undefined' ? WHOISJSON_API_KEY : DEFAULT_WHOISJSON_API_KEY;

if (!apiKey) {
throw new Error('WhoisJSON API密钥未配置');
}

// 调用WhoisJSON API
const response = await fetch(`https://whoisjson.com/api/v1/whois?domain=${encodeURIComponent(domain)}`, {
method: 'GET',
headers: {
'Authorization': `Token=${apiKey}`,
'Content-Type': 'application/json'
}
});

if (!response.ok) {
throw new Error(`API请求失败: ${response.status} ${response.statusText}`);
}

const data = await response.json();

// 解析并格式化返回数据
return {
success: true,
domain: data.name || domain,
registered: data.registered || false,
registrationDate: data.created ? formatDate(data.created) : null,
expiryDate: data.expires ? formatDate(data.expires) : null,
lastUpdated: data.changed ? formatDate(data.changed) : null,
registrar: data.registrar ? data.registrar.name : null,
registrarUrl: data.registrar ? data.registrar.url : null,
nameservers: data.nameserver || [],
status: data.status || [],
dnssec: data.dnssec || null,
raw: data // 保留原始数据以备后用
};
} catch (error) {
return {
success: false,
error: error.message,
domain: domain
};
}
}

// 检查KV是否已配置
function isKVConfigured() {
return typeof DOMAIN_MONITOR !== 'undefined';
}

// ================================
// HTML模板区域
// ================================

// 登录页面模板
const getLoginHTML = (env) => `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${env.SITE_TITLE || DEFAULT_SITE_NAME}</title>
<!-- 添加网站图标(favicon) -->
<link rel="icon" href="${typeof LOGO_URL !== 'undefined' ? LOGO_URL : DEFAULT_LOGO}" type="image/png">
<link rel="shortcut icon" href="${typeof LOGO_URL !== 'undefined' ? LOGO_URL : DEFAULT_LOGO}" type="image/png">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- 添加阿里巴巴iconfont图标库支持 -->
<link rel="stylesheet" href="${ICONFONT_CSS}">
<!-- 确保图标正确加载 -->
<script src="${ICONFONT_JS}"></script>
<style>
body {
margin: 0;
padding: 0;
height: 100vh;
background-image: url('${typeof BACKGROUND_URL !== 'undefined' ? BACKGROUND_URL : DEFAULT_BACKGROUND}');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
display: flex;
align-items: center;
justify-content: center;
position: relative;
overflow: hidden;
}

/* 登录界面移动端背景图片适配 */
@media (max-width: 768px) {
body {
background-image: url('${typeof MOBILE_BACKGROUND_URL !== 'undefined' && MOBILE_BACKGROUND_URL ? MOBILE_BACKGROUND_URL : (DEFAULT_MOBILE_BACKGROUND ? DEFAULT_MOBILE_BACKGROUND : (typeof BACKGROUND_URL !== 'undefined' ? BACKGROUND_URL : DEFAULT_BACKGROUND))}');
background-attachment: scroll;
background-position: center;
}
}

body::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.4); /* 这里调整登录界面背景图的黑色蒙版不透明度 */
z-index: 1;
}

.github-corner {
position: fixed;
top: 0;
right: 0;
width: 0;
height: 0;
border-style: solid;
border-width: 0 100px 100px 0;
border-color: transparent rgba(53, 83, 248, 0.9) transparent transparent;
color: white;
text-decoration: none;
z-index: 1000;
transition: all 0.3s ease;
overflow: visible; /* 确保图标不会被裁剪 */
}
.github-corner:hover {
border-color: transparent rgba(99, 122, 250, 0.8) transparent transparent;
}
.github-corner i {
position: absolute;
top: 18px;
right: -82.5px;
font-size: 40px;
transform: rotate(45deg); /* 顺时针旋转45度 */
line-height: 1;
display: inline-block; /* 确保图标有确定的尺寸 */
width: 40px; /* 设置宽度与字体大小相同 */
height: 40px; /* 设置高度与字体大小相同 */
text-align: center; /* 文本居中 */
}
.login-container {
background-color: rgba(255, 255, 255, 0.15);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
border-radius: 16px;
padding: 35px;
width: 90%;
max-width: 400px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
border: 1px solid rgba(255, 255, 255, 0.18);
position: relative;
z-index: 10;
}
.login-title {
text-align: center;
color: #ffffff;
margin-bottom: 25px;
font-weight: 600;
display: flex;
align-items: center;
justify-content: center; /* 保持居中 */
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
margin-left: 0; /* 保留无左边距 */
margin-right: auto;
width: 100%;
padding-left: 0; /* 完全移除左内边距 */
margin-right: 8px; /* 添加右边距以平衡 */
}
.login-logo {
height: 64px;
width: 64px;
margin-right: 0px; /* 控制logo和标题文字之间的间距 */
filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.3));
vertical-align: middle;
}
.form-control {
background-color: rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.3);
padding: 14px;
height: auto;
color: #fff;
font-size: 1.05rem;
border-radius: 10px;
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
transition: all 0.3s ease;
}
.form-control::placeholder {
color: rgba(255, 255, 255, 0.8);
}
.form-control:focus {
background-color: rgba(255, 255, 255, 0.25);
border-color: rgba(255, 255, 255, 0.5);
box-shadow: 0 0 0 0.25rem rgba(255, 255, 255, 0.2);
color: #fff;
}
.btn-login {
background-color: rgba(53, 83, 248, 0.9); // 登录按钮的颜色
border: none;
color: white;
padding: 12px;
width: 100%;
font-weight: 500;
font-size: 1.05rem;
border-radius: 10px;
margin-top: 10px;
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
transition: all 0.3s ease;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
}
.btn-login:hover {
background-color: rgba(99, 122, 250, 0.8); // 登录按钮的悬停颜色
transform: translateY(-2px);
box-shadow: 0 6px 15px rgba(0, 0, 0, 0.3);
}
.error-message {
color: #ffcccc;
margin-top: 15px;
text-align: center;
display: none;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}
</style>
</head>
<body>

<div class="login-container">
<div style="display: flex; flex-direction: column; align-items: center; width: 100%;">
<h2 class="login-title">
<img src="${typeof LOGO_URL !== 'undefined' ? LOGO_URL : DEFAULT_LOGO}" alt="Logo" class="login-logo">
<span>${env.SITE_TITLE || DEFAULT_SITE_NAME}</span>
</h2>
<form id="loginForm" style="width: 100%;">
<div class="mb-3">
<input type="text" class="form-control" id="username" placeholder="请输入账号" required style="margin-bottom: 10px;">
<input type="password" class="form-control" id="password" placeholder="请输入访问密码" required>
</div>
<button type="submit" class="btn btn-login"><i class="iconfont icon-mima" style="margin-right: 5px;"></i>登录</button>
<div id="errorMessage" class="error-message">密码错误,请重试</div>
</form>
</div>
</div>

<script>
document.getElementById('loginForm').addEventListener('submit', function(e) {
e.preventDefault();
const username = document.getElementById('username').value; // 获取账号
const password = document.getElementById('password').value;

// 使用POST请求验证账号和密码
fetch('/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
// 同时发送 username 和 password
body: JSON.stringify({ username: username, password: password })
})
.then(response => {
if (response.ok) {
// 密码正确,跳转到dashboard页面
window.location.href = '/dashboard';
} else {
// 密码错误,显示错误信息
document.getElementById('errorMessage').style.display = 'block';
}
})
.catch(error => {
document.getElementById('errorMessage').style.display = 'block';
});
});
</script>
</body>
</html>
`;

// 主界面模板
const getHTMLContent = (title) => `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>${title}</title>
<!-- 添加网站图标(favicon) -->
<link rel="icon" href="${typeof LOGO_URL !== 'undefined' ? LOGO_URL : DEFAULT_LOGO}" type="image/png">
<link rel="shortcut icon" href="${typeof LOGO_URL !== 'undefined' ? LOGO_URL : DEFAULT_LOGO}" type="image/png">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- 添加阿里巴巴iconfont图标库支持 -->
<link rel="stylesheet" href="${ICONFONT_CSS}">
<!-- 确保图标正确加载 -->
<script src="${ICONFONT_JS}"></script>
<!-- 添加登出脚本 -->
<script>
// 添加登出功能
function logout() {
window.location.href = '/logout';
}
</script>
<style>
:root {
--primary-color: #4e54c8;
--secondary-color: #6c757d;
--success-color:rgb(0, 255, 60);
--danger-color:rgb(255, 0, 25);
--warning-color:rgb(255, 230, 0);
--info-color: #17a2b8;
--light-color: #f8f9fa;
--dark-color: #343a40;
--domain-note-spacing: 2px; /* 域名和备注标签之间的间距 */
--domain-line-height: 1.15; /* 域名换行后的行高 */
}

body {
font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
background-image: url('${typeof BACKGROUND_URL !== 'undefined' ? BACKGROUND_URL : DEFAULT_BACKGROUND}');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
padding-top: 20px;
position: relative;
min-height: 100vh;
}

/* 移动端背景图片优化 */
@media (max-width: 768px) {
body {
background-attachment: scroll;
background-size: cover;
background-position: center top;
min-height: 100vh;
/* 移动端使用专门的背景图片,如果没有则回退到桌面端背景图片 */
background-image: url('${typeof MOBILE_BACKGROUND_URL !== 'undefined' && MOBILE_BACKGROUND_URL ? MOBILE_BACKGROUND_URL : (DEFAULT_MOBILE_BACKGROUND ? DEFAULT_MOBILE_BACKGROUND : (typeof BACKGROUND_URL !== 'undefined' ? BACKGROUND_URL : DEFAULT_BACKGROUND))}');
}

/* 使用伪元素固定背景,避免缩放问题 */
body::after {
content: '';
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
background-image: url('${typeof MOBILE_BACKGROUND_URL !== 'undefined' && MOBILE_BACKGROUND_URL ? MOBILE_BACKGROUND_URL : (DEFAULT_MOBILE_BACKGROUND ? DEFAULT_MOBILE_BACKGROUND : (typeof BACKGROUND_URL !== 'undefined' ? BACKGROUND_URL : DEFAULT_BACKGROUND))}');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
z-index: -2;
pointer-events: none;
}
}

body::before {
content: '';
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.55); /* 这里调整登录后界面背景图的黑色蒙版不透明度 */
z-index: -1;
}

.navbar {
background-color: rgba(255, 255, 255, 0.15);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
border: 1px solid rgba(255, 255, 255, 0.18);
margin-bottom: 24px;
padding: 14px 20px;
border-radius: 16px;
display: flex;
justify-content: space-between;
align-items: center;
position: relative;
z-index: 2;
}

.navbar-brand {
display: flex;
align-items: center;
font-weight: 600;
color: #ffffff;
font-size: 1.7rem;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
gap: 2px; /* 使用gap属性统一控制子元素之间的间距 */
}

.navbar-brand i {
font-size: 1.8rem;
color: white;
margin: 0; /* 移除所有margin */
}

.logo-link {
display: flex;
align-items: center;
margin-right: 0px;
text-decoration: none;
}

.logo-img {
height: 64px;
width: 64px;
object-fit: contain;
transition: transform 0.3s ease;
}

.logo-img:hover {
transform: scale(1.1);
cursor: pointer;
filter: brightness(1.2);
}

@keyframes pulse {
0% { transform: scale(1); }
50% { transform: scale(1.1); }
100% { transform: scale(1); }
}

.logo-img.refreshing {
animation: pulse 0.8s ease-in-out;
}

.navbar-actions {
margin-left: auto;
display: flex;
align-items: center;
}

.btn-logout {
margin-left: 10px;
background-color: transparent;
border: 1px solid #dc3545;
color: #dc3545;
padding: 5px 15px;
border-radius: 5px;
font-size: 0.9rem;
cursor: pointer;
transition: all 0.2s;
}

.btn-logout:hover {
background-color: #dc3545;
color: white;
}

.container {
max-width: 1200px;
position: relative;
z-index: 1;
}

.page-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 16px;
padding: 12px 20px;
background-color: rgba(255, 255, 255, 0.15);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
border: 1px solid rgba(255, 255, 255, 0.18);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
position: relative;
z-index: 2;
}

.page-title {
font-size: 1.3rem;
font-weight: 600;
color: #ffffff;
margin: 0;
display: flex;
align-items: center;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}

.page-title i {
margin-right: 8px;
font-size: 1.2rem;
}

.btn-action-group {
display: flex;
flex-direction: row;
align-items: center;
gap: 10px;
margin-left: auto;
}

.btn-primary {
background-color: var(--primary-color);
border-color: var(--primary-color);
}

.btn-primary:hover {
background-color: #3f44ae;
border-color: #3f44ae;
}

.card {
border: 2px solid rgba(255, 255, 255, 0.3);
border-radius: 16px;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
margin-bottom: 8px; /* 进一步减小卡片间的垂直间距 */
transition: all 0.3s;
overflow: hidden !important; /* 确保内容不会溢出 */
position: relative;
z-index: 1; /* 设置卡片的基础z-index */
width: 100%;
background-color: rgba(255, 255, 255, 0.15);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}

/* ===== 卡片头部样式 - 开始 ===== */
/* 卡片头部容器 */
.card-header {
background-color: rgba(255, 255, 255, 0.1);
border-bottom: 1px solid rgba(255, 255, 255, 0.18);
padding: 15px 0; /* 移除左右内边距,改为在各元素上单独控制 */
padding-top: 12px;
padding-bottom: 12px;
position: relative;
display: flex;
align-items: center;
justify-content: space-between;
overflow: hidden; /* 防止内容溢出 */
border-top-left-radius: 16px;
border-top-right-radius: 16px;
gap: 0; /* 移除间距,改为在各元素上单独控制 */
min-height: 84px; /* 最小高度 */
height: auto; /* 自动调整高度以适应内容 */
max-height: 140px; /* 最大高度限制 */
box-sizing: border-box; /* 确保padding不会增加元素高度 */
}

/* 状态指示圆点 */
.status-dot {
display: inline-block;
width: 10px;
height: 10px;
border-radius: 50%;
margin-right: 10px; /* 与域名文字的间距 */
margin-left: 20px; /* 与卡片左边的间距 */
vertical-align: middle;
flex-shrink: 0;
}

.status-dot.expired {
background-color: var(--danger-color);
box-shadow: 0 0 5px var(--danger-color);
}

.status-dot.warning {
background-color: var(--warning-color);
box-shadow: 0 0 5px var(--warning-color);
}

.status-dot.safe {
background-color: var(--success-color);
box-shadow: 0 0 5px var(--success-color);
}

/* 域名标题区域 */
.domain-header {
display: flex;
flex-direction: column;
justify-content: center;
flex: 1;
min-width: 0; /* 解决flex子项目溢出问题 */
max-width: calc(100% - 2px); /* 限制最大宽度,为右侧状态标签留出空间 */
overflow: hidden; /* 确保内容不会溢出 */
padding-left: 5px; /* 与小圆点的间距 */
padding-right: 2px; /* 与右侧状态标签的间距 */
transition: all 0.3s ease; /* 添加过渡效果 */
min-height: 60px; /* 最小高度,可根据内容自动增加 */
height: auto; /* 自动调整高度以适应内容 */
max-height: 120px; /* 设置最大高度限制 */
}

.domain-header h5 {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis; /* 添加省略号 */
color: #ffffff;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
font-size: 1.25rem; /* 设置域名字体大小 */
font-weight: 600; /* 加粗字体 */
transition: white-space 0.3s ease; /* 添加过渡效果 */
margin: 0; /* 重置所有margin */
line-height: 1.5;
}

/* 展开状态下的域名显示 */
.domain-card.expanded .domain-header h5 {
white-space: normal; /* 允许换行 */
word-wrap: break-word; /* 确保长单词也能换行 */
word-break: break-all; /* 在任何字符间换行 */
}

/* 域名容器样式 */
.domain-name-container {
display: flex;
flex-direction: column;
}

/* 域名标题样式 */
.domain-title {
display: inline-block;
margin-bottom: 0; /* 确保没有额外的底部边距 */
}

/* 备注标签样式 */
.domain-header .domain-meta {
font-size: 0.75rem;
color: rgba(255, 255, 255, 0.8);
line-height: 1.2;
}

/* 展开状态下的域名显示 */
.domain-card.expanded .domain-title {
display: block; /* 确保域名可以正常换行 */
}

/* 域名文字样式 */
.domain-text {
display: inline;
}

/* 展开状态下域名文字样式 */
.domain-card.expanded .domain-title .domain-text {
display: inline-block;
line-height: var(--domain-line-height); /* 控制行高,即行间距 */
}

/* 确保展开状态下域名文字能够正确换行 */
.domain-card.expanded .domain-title {
word-break: break-all;
word-wrap: break-word;
}

/* 间隔元素样式 */
.spacer {
display: block;
width: 100%;
/* 高度由内联样式通过CSS变量控制 */
flex-shrink: 0; /* 防止被压缩 */
}

/* 自定义备注样式 - 多种颜色标签风格 */
.domain-meta .text-info, .domain-meta [class*="tag-"], .note-preview {
background-color: #3B82F6; /* 默认蓝色 */
color: white !important;
font-weight: 500;
padding: 3px 12px;
border-radius: 16px;
display: inline-block;
font-size: 0.8rem;
box-shadow: 0 1px 3px rgba(59, 130, 246, 0.3);
letter-spacing: 0.2px;
}

/* 自定义备注颜色类 - 只修改颜色,保留原有样式 */
.text-info.tag-blue { background-color: #3B82F6 !important; box-shadow: 0 1px 3px rgba(59, 130, 246, 0.3) !important; }
.text-info.tag-green { background-color: #10B981 !important; box-shadow: 0 1px 3px rgba(16, 185, 129, 0.3) !important; }
.text-info.tag-red { background-color: #EF4444 !important; box-shadow: 0 1px 3px rgba(239, 68, 68, 0.3) !important; }
.text-info.tag-yellow { background-color: #F59E0B !important; box-shadow: 0 1px 3px rgba(245, 158, 11, 0.3) !important; }
.text-info.tag-purple { background-color: #8B5CF6 !important; box-shadow: 0 1px 3px rgba(139, 92, 246, 0.3) !important; }
.text-info.tag-pink { background-color: #EC4899 !important; box-shadow: 0 1px 3px rgba(236, 72, 153, 0.3) !important; }
.text-info.tag-indigo { background-color: #6366F1 !important; box-shadow: 0 1px 3px rgba(99, 102, 241, 0.3) !important; }
.text-info.tag-gray { background-color: #6B7280 !important; box-shadow: 0 1px 3px rgba(107, 114, 128, 0.3) !important; }

/* 分类容器样式 */
.domain-group-container {
margin-bottom: 4px; /* 分类之间的间距 */
}

/* 主容器底部间距统一 */
.container {
margin-bottom: 60px; /* 统一设置与页脚的间距 */
}

/* 空状态容器间距 - 桌面端 */
.empty-state-container {
margin-top: 2px !important; /* 与分类标题和卡片的间距保持一致 */
margin-bottom: 0 !important; /* 确保空状态容器底部间距与其他状态一致 */
}

/* 确保分类标题与卡片左对齐 */
.col-12.px-1-5 {
padding-left: 0.375rem !important; /* 与卡片列相同的左内边距 */
}

/* 分类标题样式 */
.category-header {
padding: 8px 0; /* 移除左右内边距 */
margin-bottom: 2px; /* 分类标题和卡片的间距 */
margin-left: 10px; /* 增加左边距,使文字与卡片对齐 */
display: block; /* 改为块级元素,确保宽度占满 */
min-width: 120px;
}

.category-title {
margin: 0;
padding: 0;
color: white;
font-size: 1.3rem;
font-weight: 600;
text-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
display: flex;
align-items: center;
}

/* 毛玻璃风格的统计徽章 */
.count-badge {
background: rgba(255, 255, 255, 0.15);
border: 1px solid rgba(255, 255, 255, 0.2);
backdrop-filter: blur(10px);
color: rgba(255, 255, 255, 0.9);
padding: 0.25rem 0.5rem;
border-radius: 0.375rem;
font-size: 0.875rem;
font-weight: 500;
margin-left: 0.5rem;
}

/* 状态区域 */
.domain-status {
display: flex;
align-items: center;
justify-content: flex-end;
flex-shrink: 0; /* 防止被压缩 */
min-width: 100px; /* 增加最小宽度,确保状态标签有足够空间 */
padding-right: 20px; /* 下拉按钮与卡片右边的间距 */
margin-left: 10px; /* 与域名区域的间距 */
}

.domain-status .badge {
margin-right: 10px; /* 与下拉箭头的间距 */
white-space: nowrap; /* 确保标签文本不换行 */
padding: 5px 10px;
border-radius: 20px;
font-weight: 500;
color: white;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.badge .iconfont {
margin-right: 3px;
font-size: 0.9rem;
vertical-align: middle;
color: white;
}

/* 下拉按钮 */
.toggle-details {
padding: 0;
margin-left: 0; /* 与状态标签的间距 */
color: white;
background: none;
border: none;
box-shadow: none;
position: relative;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
line-height: 1;
text-decoration: none !important;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.toggle-details:hover {
color: rgba(255, 255, 255, 0.8);
}

/* 图标容器 */
.toggle-icon-container {
position: relative;
width: 16px;
height: 16px;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden; /* 防止内容溢出 */
line-height: 1;
}

/* 箭头图标 */
.toggle-icon {
font-size: 16px;
transition: transform 0.3s ease;
margin-right: 0 !important; /* 覆盖默认的margin-right */
display: block;
line-height: 1;
}
/* ===== 卡片头部样式 - 结束 ===== */

.card-body .d-flex {
margin-right: 0;
padding-right: 0;
overflow: visible !important;
}

/* 移除单独的百分比值样式,改为直接在SVG中使用text元素 */

.card-header,
.card-body {
padding-left: 0; /* 移除左内边距 */
padding-right: 0; /* 移除右内边距 */
position: relative;
}

/* 卡片头部相关样式已移至上方统一管理区域 */

/* 骨架屏样式 */
.skeleton-card {
background-color: rgba(255, 255, 255, 0.1);
border-radius: 12px;
overflow: hidden;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
animation: skeleton-pulse 1.5s infinite ease-in-out;
}

.skeleton-header {
background-color: rgba(255, 255, 255, 0.15);
padding: 16px;
border-top-left-radius: 12px;
border-top-right-radius: 12px;
}

.skeleton-text-lg {
height: 24px;
background-color: rgba(255, 255, 255, 0.2);
border-radius: 4px;
margin-bottom: 8px;
width: 70%;
}

.skeleton-text-sm {
height: 16px;
background-color: rgba(255, 255, 255, 0.2);
border-radius: 4px;
width: 50%;
}

.skeleton-text {
height: 16px;
background-color: rgba(255, 255, 255, 0.2);
border-radius: 4px;
margin-bottom: 8px;
width: 90%;
}

.skeleton-progress {
height: 24px;
background-color: rgba(255, 255, 255, 0.2);
border-radius: 12px;
margin-top: 16px;
}

@keyframes skeleton-pulse {
0% {
opacity: 0.6;
}
50% {
opacity: 0.8;
}
100% {
opacity: 0.6;
}
}

/* 自定义测试成功消息的颜色 */
.telegram-test-success {
color:rgb(19, 221, 144) !important; /* 紫色 */
font-weight: 500;
}

/* 状态区域样式已移至上方统一管理区域 */

.progress-circle-container {
display: flex;
justify-content: flex-end;
align-items: flex-start; /* 改为顶部对齐,确保右上角固定 */
padding-right: 10px;
padding-top: 5px; /* 添加顶部间距保持位置 */
box-sizing: border-box;
overflow: visible;
position: absolute;
right: 0;
top: 0; /* 改为顶部对齐 */
transform: none; /* 移除垂直居中变换 */
z-index: 10; /* 提高z-index值确保在文本上方 */
min-width: 90px; /* 再次增加最小宽度适应更大的圆圈 */
}

.progress-circle {
position: relative;
width: 85px; /* 再次增加宽度 */
height: 85px; /* 再次增加高度 */
margin: 0;
box-sizing: border-box;
overflow: visible;
z-index: 6; /* 降低z-index值 */
}

.progress-circle-bg {
width: 100%;
height: 100%;
border-radius: 50%;
border: 6px solid #f5f5f5;
box-sizing: border-box;
position: relative;
display: flex;
justify-content: center;
align-items: center;
z-index: 7; /* 降低z-index值 */
}

.progress-circle-value {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
font-size: 0.95rem;
font-weight: bold;
}

.progress-ring {
position: relative;
transform: rotate(-90deg); /* 从12点钟方向开始 */
z-index: 8; /* 降低z-index值 */
}

/* 添加样式使SVG中的文本不旋转 */
.progress-ring text {
transform: rotate(90deg); /* 抵消父元素的旋转 */
fill: #ffffff; /* 改为白色 */
font-size: 14px;
font-weight: bold;
font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
dominant-baseline: central;
text-anchor: middle;
paint-order: stroke;
stroke: rgba(0, 0, 0, 0.3); /* 改为半透明黑色描边 */
stroke-width: 1px;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); /* 添加文字阴影 */
}

.progress-ring-circle {
transition: stroke-dashoffset 0.35s;
transform-origin: center;
z-index: 9; /* 降低z-index值 */
}

/* 进度条百分比文字样式 */
.progress-percent-text {
font-size: 18px; /* 再次增加字体大小以适应更大的圆圈 */
font-weight: bold;
color: #ffffff;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.card-body {
padding: 12px 15px;
padding-left: 20px; /* 恢复卡片内容的左内边距 */
padding-right: 20px; /* 恢复卡片内容的右内边距 */
overflow: visible !important;
}

/* 折叠区域样式 */
.collapse {
margin: 0;
padding: 0;
}

.domain-card {
transition: all 0.3s;
}

.domain-card:hover {
transform: translateY(-5px);
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
}

/* 域名列样式 */
.domain-column {
display: flex;
flex-direction: column;
}

/* 完全移除底部边框,仅在展开状态下显示 */
.domain-card .card::after {
content: none; /* 默认不显示任何内容 */
}

/* 展开状态下显示底部边框 */
.domain-card.expanded .card {
border-bottom: 1px solid rgba(255, 255, 255, 0.18);
border-bottom-left-radius: 16px;
border-bottom-right-radius: 16px;
}

/* 域名列样式 */
.domain-column {
display: flex;
flex-direction: column;
}

/* 状态指示圆点样式已移至上方统一管理区域 */

/* 域名卡片容器样式 */
.domain-card-container {
margin-bottom: 12px; /* 统一设置卡片间距 */
position: relative;
border-radius: 16px; /* 确保容器也有圆角 */
overflow: hidden; /* 防止内容溢出 */
}

/* Badge样式已移至上方统一管理区域 */

/* 下拉按钮相关样式已移至上方统一管理区域 */

/* 当展开时旋转箭头 */
.toggle-details:not(.collapsed) .toggle-icon {
transform: rotate(90deg);
}

/* 折叠内容样式 */
.details-content {
padding-top: 10px;
}

.domain-tag {
display: inline-block;
padding: 3px 8px;
margin-left: 8px;
border-radius: 20px;
background-color: #f8f9fa;
color: #666;
font-size: 0.75rem;
font-weight: normal;
vertical-align: middle;
}

.domain-info {
display: flex;
flex-wrap: wrap;
gap: 10px;
color: rgba(255, 255, 255, 0.8);
font-size: 0.75rem;
}

.domain-actions {
display: flex;
gap: 5px;
justify-content: flex-end;
}

.domain-actions .btn {
padding: 5px 10px;
font-size: 0.85rem;
}

.badge {
padding: 5px 10px;
border-radius: 20px;
font-weight: 500;
}

.btn-action {
padding: 5px 10px;
border-radius: 6px;
font-size: 0.8rem;
font-weight: 500;
transition: all 0.2s;
}

.btn-action:hover {
/* 移除浮动效果,只保留颜色变化 */
}

.btn-outline-primary {
color: var(--primary-color);
border-color: var(--primary-color);
}

.btn-outline-primary:hover {
background-color: var(--primary-color);
color: white;
}

.btn-outline-success {
color: var(--success-color);
border-color: var(--success-color);
}

.btn-outline-success:hover {
background-color: var(--success-color);
color: white;
}

.btn-outline-danger {
color: var(--danger-color);
border-color: var(--danger-color);
}

.btn-outline-danger:hover {
background-color: var(--danger-color);
color: white;
}

.btn-outline-info {
color: var(--info-color);
border-color: var(--info-color);
}

.btn-outline-info:hover {
background-color: var(--info-color);
color: white;
}

/* 视图按钮样式 */
.view-option {
color: rgb(223, 223, 223) !important; /* 未选状态使用灰色文字 */
background-color: rgba(204, 204, 204, 0.8) !important; /* 未选状态的背景色 */
border-color:rgba(109, 109, 109, 0.3) !important; /* 修改边框颜色 */
}

.view-option.btn-info {
background-color:rgb(255, 255, 255) !important; /* 修改选中状态背景色为蓝色 */
color: rgb(31, 34, 39) !important; /* 选中状态使用白色文字 */
}

.view-option .view-text {
font-weight: 500;
}

/* 添加新域名按钮自定义样式 */
.add-domain-btn {
background-color:rgb(42, 175, 86) !important; /* 绿色 */
border-color:rgba(33, 148, 72, 0.8) !important;
}

.add-domain-btn:hover {
background-color:rgb(24, 216, 120) !important; /* 深绿色 */
border-color:rgba(38, 190, 114, 0.8) !important;
}

/* 分类管理按钮样式,与添加域名按钮保持一致 */
.category-manage-btn {
background-color:rgb(42, 175, 86) !important; /* 绿色 */
border-color:rgba(33, 148, 72, 0.8) !important;
}

.category-manage-btn:hover {
background-color:rgb(24, 216, 120) !important; /* 深绿色 */
border-color:rgba(38, 190, 114, 0.8) !important;
}

/* 排序按钮自定义样式 */
.sort-btn {
background-color: rgb(0, 123, 255) !important; /* 蓝色 */
border-color: rgba(0, 111, 230, 0.8) !important;
}

.sort-btn:hover {
background-color: rgb(0, 162, 255) !important; /* 蓝色 */
border-color: rgba(23, 137, 202, 0.8) !important;
}



/* 排序选项的勾选图标样式 */
.sort-check {
visibility: hidden;
margin-right: 5px;
/* 使用与文字相同的颜色 */
color: inherit;
}

.sort-option.active .sort-check {
visibility: visible;
}

/* 排序选项选中状态样式 - 只显示勾符号,不使用背景色 */
.sort-option.active {
background-color: transparent !important;
color: white !important;
}

/* 确保所有排序选项文字左对齐 */
.dropdown-item {
display: flex;
align-items: center;
}

/* 添加iconfont图标的通用样式 */
.iconfont {
font-size: 1rem;
vertical-align: middle;
margin-right: 4px;
}

/* 按钮中的图标特殊样式 */
.btn-action .iconfont {
font-size: 0.9rem;
}

/* WHOIS查询按钮样式 */
.whois-query-btn {
color: white !important;
background-color: #0d6efd !important;
border-color: #0d6efd !important;
}

.whois-query-btn:hover {
color: white !important;
background-color: #0b5ed7 !important;
border-color: #0a58ca !important;
}

.whois-query-btn:focus,
.whois-query-btn:active {
color: white !important;
background-color: #0a58ca !important;
border-color: #0a53be !important;
box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25) !important;
}

.whois-query-btn:disabled {
color: white !important;
background-color: #6c757d !important;
border-color: #6c757d !important;
}

/* 确保WHOIS查询按钮中的图标也是白色 */
.whois-query-btn .iconfont {
color: white !important;
}

.whois-query-btn:hover .iconfont,
.whois-query-btn:focus .iconfont,
.whois-query-btn:active .iconfont,
.whois-query-btn:disabled .iconfont {
color: white !important;
}

/* WHOIS清除按钮样式 */
.whois-clear-btn {
color: white !important;
background-color: #dc3545 !important;
border-color: #dc3545 !important;
}

.whois-clear-btn:hover {
color: white !important;
background-color: #c82333 !important;
border-color: #bd2130 !important;
}

.whois-clear-btn:focus,
.whois-clear-btn:active {
color: white !important;
background-color: #bd2130 !important;
border-color: #b21f2d !important;
box-shadow: 0 0 0 0.25rem rgba(220, 53, 69, 0.25) !important;
}

.whois-clear-btn:disabled {
color: white !important;
background-color: #dc3545 !important;
border-color: #dc3545 !important;
opacity: 0.65;
}

/* 确保WHOIS清除按钮中的图标也是白色 */
.whois-clear-btn .iconfont {
color: white !important;
}

.whois-clear-btn:hover .iconfont,
.whois-clear-btn:focus .iconfont,
.whois-clear-btn:active .iconfont,
.whois-clear-btn:disabled .iconfont {
color: white !important;
}

/* 表单和模态框中的图标统一样式 */
.modal-body .iconfont {
color: #555;
}

/* 大号图标样式 */
.iconfont-lg {
font-size: 1.5rem;
}

/* 不同颜色的图标 */
.icon-primary { color: var(--primary-color); }
.icon-success { color: var(--success-color); }
.icon-danger { color: var(--danger-color); }
.icon-warning { color: var(--warning-color); }
.icon-info { color: var(--info-color); }

.domain-actions {
display: flex;
justify-content: space-between;
margin-top: 10px;
gap: 5px;
margin-bottom: 8px;
}

.domain-actions .btn {
flex-grow: 1;
text-align: center;
padding: 8px 0;
border-radius: 6px;
font-weight: 500;
font-size: 0.85rem;
}

/* 纯图标按钮样式 - 用于替换文字按钮 */
.btn-icon-only {
width: 40px;
height: 40px;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
flex-grow: 1;
max-width: 40px;
}

.btn-icon-only .iconfont {
margin: 0;
font-size: 1.2rem;
}

/* 纯图标链接样式 */
.link-icon-only {
width: 40px;
height: 40px;
padding: 0;
display: flex;
align-items: center;
justify-content: center;
border-radius: 50%;
flex-grow: 1;
max-width: 40px;
}

.link-icon-only .iconfont {
margin: 0;
font-size: 1.2rem;
}

/* 域名操作按钮行样式 */
.domain-actions {
display: flex;
justify-content: space-between;
gap: 3px;
margin-top: 12px;
margin-bottom: 8px;
flex-wrap: nowrap;
width: 100%;
}

.domain-actions .btn,
.domain-actions a.btn {
flex: 1;
padding: 6px 2px;
font-size: 0.7rem;
white-space: nowrap;
text-align: center;
overflow: hidden;
text-overflow: ellipsis;
color: white;
border: none;
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
transition: all 0.3s ease;
}

.domain-actions .btn:hover,
.domain-actions a.btn:hover {
/* 移除浮动效果,只保留颜色变化 */
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
color: white;
}

.domain-actions .iconfont {
margin-right: 2px;
font-size: 0.85rem;
display: inline-block !important;
vertical-align: middle;
color: white;
}

/* 按钮颜色 */
.btn-primary, .btn-outline-primary {
background-color: rgba(47, 103, 167, 0.9);
border-color: rgba(37, 86, 141, 0.3);
}

/* 在这里添加悬停样式 */
.btn-primary:hover {
background-color: rgba(0, 119, 255, 0.7);
border-color: rgba(0, 119, 255, 0.3);
}

.btn-success, .btn-outline-success {
background-color: rgba(40, 167, 69, 0.7);
border-color: rgba(40, 167, 69, 0.3);
}

.btn-success:hover {
background-color: rgba(69, 211, 102, 0.8);
border-color: rgba(47, 196, 81, 0.3);
}

/* 自定义状态标签颜色 */
.bg-success {
background-color: rgba(42, 165, 93, 0.8) !important;
}

.btn-info, .btn-outline-info {
background-color: rgba(23, 162, 184, 0.7);
border-color: rgba(23, 162, 184, 0.3);
}

.btn-warning, .btn-outline-warning {
background-color: rgba(255, 193, 7, 0.7);
border-color: rgba(255, 193, 7, 0.3);
}

.btn-danger, .btn-outline-danger {
background-color: rgba(220, 53, 69, 0.7);
border-color: rgba(220, 53, 69, 0.3);
}

.btn-secondary, .btn-outline-secondary {
background-color: rgba(108, 117, 125, 0.7);
border-color: rgba(108, 117, 125, 0.3);
}

/* 续期链接样式已整合到按钮行中 */

.test-notify-btn {
width: 100%;
border-radius: 6px;
padding: 8px 0;
font-weight: 500;
font-size: 0.85rem;
background-color: white;
border: 1px solid #17a2b8;
color: #17a2b8;
}

.test-notify-btn:hover {
background-color: #17a2b8;
color: white;
}

.card-text {
margin-bottom: 6px;
color: rgba(255, 255, 255, 0.9);
font-size: 0.85rem;
display: flex;
align-items: center;
padding-left: 2px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

.card-text strong {
color: #ffffff;
font-weight: 600;
font-size: 0.85rem;
margin-right: 4px;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.card-text .iconfont {
margin-right: 6px;
color: rgba(255, 255, 255, 0.9);
font-size: 0.85rem;
width: 16px;
text-align: center;
display: inline-flex;
justify-content: center;
}

.modal-content {
border-radius: 16px;
border: 1px solid rgba(255, 255, 255, 0.18);
box-shadow: 0 10px 30px rgba(0,0,0,0.3);
background-color: rgba(255, 255, 255, 0.15);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
overflow: hidden; /* 确保内部元素不会超出圆角边界 */
}

.modal-header {
border-bottom: 1px solid rgba(255, 255, 255, 0.18);
background-color: rgba(255, 255, 255, 0.1);
padding: 15px 20px;
color: white;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
border-top-left-radius: 16px;
border-top-right-radius: 16px;
}

.modal-footer {
border-top: 1px solid rgba(255, 255, 255, 0.18);
padding: 15px 20px;
border-bottom-left-radius: 16px;
border-bottom-right-radius: 16px;
background-color: rgba(255, 255, 255, 0.05);
}

/* 确保下拉菜单显示在最上层 */
.dropdown-menu {
z-index: 1050 !important;
background-color: rgba(60, 65, 70, 0.75) !important; /* 更浅的背景色 */
backdrop-filter: blur(15px) !important;
-webkit-backdrop-filter: blur(15px) !important;
border: 1px solid rgba(255, 255, 255, 0.25) !important;
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.35) !important;
padding: 8px !important;
border-radius: 10px !important;
}

.dropdown-item {
font-size: 0.85rem;
padding: 0.5rem 1rem;
color: white !important;
border-radius: 6px;
margin-bottom: 2px;
font-weight: 500;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.dropdown-item:hover {
background-color: rgba(255, 255, 255, 0.25) !important;
color: white !important;
}

.dropdown-divider {
border-top: 1px solid rgba(255, 255, 255, 0.25) !important;
margin: 6px 0;
}

/* 自定义间距类 */
.px-1-5 {
padding-left: 0.375rem !important;
padding-right: 0.375rem !important;
}

.form-label {
font-weight: 500;
color: white;
display: flex;
align-items: center;
gap: 5px;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.form-label .iconfont,
.modal-body h6 .iconfont,
.modal-body .form-text .iconfont {
color: rgba(255, 255, 255, 0.9);
margin-right: 0;
font-size: 0.95rem;
}

.modal-body {
color: white;
}

.modal-body .form-text {
color: rgba(255, 255, 255, 0.8);
}

.form-control {
border-radius: 10px;
border: 1px solid rgba(255, 255, 255, 0.3);
padding: 10px 15px;
background-color: rgba(255, 255, 255, 0.2);
color: white;
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
}

.form-control:focus {
border-color: rgba(255, 255, 255, 0.5);
box-shadow: 0 0 0 0.2rem rgba(255, 255, 255, 0.25);
background-color: rgba(255, 255, 255, 0.25);
color: white;
}

.form-control::placeholder {
color: rgba(255, 255, 255, 0.7);
}

/* 自动填充高亮样式 */
.form-control.auto-filled,
.form-select.auto-filled {
background-color: rgba(74, 222, 128, 0.15) !important;
border-color: rgba(74, 222, 128, 0.6) !important;
box-shadow: 0 0 0 0.2rem rgba(74, 222, 128, 0.25) !important;
animation: autoFillGlow 2s ease-in-out;
}

@keyframes autoFillGlow {
0% {
background-color: rgba(74, 222, 128, 0.3);
box-shadow: 0 0 0 0.3rem rgba(74, 222, 128, 0.4);
}
100% {
background-color: rgba(74, 222, 128, 0.15);
box-shadow: 0 0 0 0.2rem rgba(74, 222, 128, 0.25);
}
}

.form-select {
background-color: rgba(255, 255, 255, 0.2);
border: 1px solid rgba(255, 255, 255, 0.3);
color: white;
border-radius: 10px;
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
}

.form-select:focus {
border-color: rgba(255, 255, 255, 0.5);
box-shadow: 0 0 0 0.2rem rgba(255, 255, 255, 0.25);
background-color: rgba(255, 255, 255, 0.25);
}

/* 添加select下拉选项的样式 */
.form-select option {
background-color: #333;
color: white;
padding: 8px;
}

.alert {
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.05);
}

@media (max-width: 768px) {
.domain-card {
flex-direction: column;
}

.domain-card .card-header {
width: 100%;
border-right: none;
border-bottom: 1px solid rgba(255, 255, 255, 0.18);
}

.domain-card .btn {
padding: 0.2rem 0.3rem;
font-size: 0.65rem;
}

.domain-actions {
flex-wrap: nowrap;
gap: 2px;
margin-top: 8px;
margin-bottom: 5px;
}

.domain-actions .btn,
.domain-actions a.btn {
padding: 6px 8px; /* 优化:增大点击范围 */
font-size: 0.75rem; /* 优化:稍微调大字体 */
margin-bottom: 4px; /* 优化:增加按钮间距 */
}

.domain-actions .iconfont {
margin-right: 1px;
font-size: 0.75rem;
}

.page-header {
flex-direction: column;
align-items: flex-start;
gap: 10px;
}

.btn-action-group {
width: 100%;
display: flex;
justify-content: flex-end;
}

/* 移动端导航栏按钮只显示图标,隐藏文字 */
.btn-action span {
display: none !important;
}

/* 移动端WHOIS按钮只显示图标,但保持和输入框的整体性 */
.whois-query-btn span,
.whois-clear-btn span {
display: none !important;
}

.whois-query-btn,
.whois-clear-btn {
display: flex !important;
align-items: center !important;
justify-content: center !important;
padding: 0.375rem 0.75rem !important;
min-width: 44px !important;
}

.whois-query-btn .iconfont,
.whois-clear-btn .iconfont {
font-size: 1.1rem !important;
line-height: 1 !important;
margin: 0 !important;
}

.view-text {
display: none !important;
}

/* 移动端空状态容器间距调整 - 只影响空状态提示框 */
.empty-state-container {
margin-top: 2px !important; /* 与分类标题和卡片的间距保持一致 */
margin-bottom: 0 !important; /* 确保空状态容器底部间距与其他状态一致 */
}

/* 移动端空状态提示框内容居中优化 */
.empty-state-container .py-3 {
height: 140px !important;
padding: 0.5rem !important;
}

/* 调整按钮间距和大小 */
.btn-action {
min-width: 40px;
padding: 8px 10px;
display: flex;
align-items: center;
justify-content: center;
}

/* 确保按钮内的图标居中 */
.btn-action .iconfont {
margin: 0 !important;
line-height: 1;
}


}
</style>
</head>
<body>
<div class="container">
<!-- 导航栏 -->
<nav class="navbar">
<span class="navbar-brand">
<a href="javascript:void(0);" class="logo-link" id="refreshLogo" title="点击刷新页面">
<img src="${typeof LOGO_URL !== 'undefined' ? LOGO_URL : DEFAULT_LOGO}" alt="Logo" class="logo-img">
</a>
<i class="iconfont icon-domain iconfont-lg"></i>
<span style="color: white; text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);">${title}</span>
</span>
<div class="navbar-actions">
<button class="btn btn-secondary me-3" data-bs-toggle="modal" data-bs-target="#settingsModal">
<i class="iconfont icon-gear" style="color: white;"></i> <span style="color: white;">系统设置</span>
</button>
<button class="btn btn-info me-3" onclick="exportData()" title="下载备份">
<i class="iconfont icon-download" style="color: white;"></i> <span style="color: white;">备份</span>
</button>
<button class="btn btn-danger" onclick="logout()">
<i class="iconfont icon-sign-out-alt" style="color: white;"></i> <span style="color: white;">退出</span>
</button>
</div>
</nav>

<!-- 页面标题和操作按钮 -->
<div class="page-header">
<h1 class="page-title"><i class="iconfont icon-list-ul"></i> 域名列表 <span class="count-badge" id="totalDomainCount">(0)</span></h1>
<div class="btn-action-group">
<div class="btn-group me-2">
<button class="btn btn-outline-info btn-action view-option" data-view="collapse-all" type="button" style="transition: background-color 0.2s, color 0.2s;">
<i class="iconfont icon-quanjusuoxiao"></i> <span class="view-text">折叠</span>
</button>
<button class="btn btn-outline-info btn-action view-option" data-view="expand-all" type="button" style="transition: background-color 0.2s, color 0.2s;">
<i class="iconfont icon-quanjufangda"></i> <span class="view-text">展开</span>
</button>
</div>
<button class="btn btn-success btn-action category-manage-btn" data-bs-toggle="modal" data-bs-target="#categoryManageModal">
<i class="iconfont icon-fenlei" style="color: white;"></i> <span style="color: white;">分类管理</span>
</button>
<button class="btn btn-success btn-action add-domain-btn" data-bs-toggle="modal" data-bs-target="#addDomainModal">
<i class="iconfont icon-jia" style="color: white;"></i> <span style="color: white;">添加域名</span>
</button>
<div class="dropdown">
<button class="btn btn-danger btn-action sort-btn" type="button" id="sortDropdown" data-bs-toggle="dropdown" aria-expanded="false">
<i class="iconfont icon-paixu" style="color: white;"></i> <span style="color: white;">域名排序</span>
</button>
<ul class="dropdown-menu dropdown-menu-end" aria-labelledby="sortDropdown">
<li><a class="dropdown-item sort-option" data-sort="suffix" data-order="asc" href="#"><i class="iconfont icon-gou1 sort-check"></i> 按域名后缀升序</a></li>
<li><a class="dropdown-item sort-option" data-sort="suffix" data-order="desc" href="#"><i class="iconfont icon-gou1 sort-check"></i> 按域名后缀降序</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item sort-option" data-sort="name" data-order="asc" href="#"><i class="iconfont icon-gou1 sort-check"></i> 按域名升序</a></li>
<li><a class="dropdown-item sort-option" data-sort="name" data-order="desc" href="#"><i class="iconfont icon-gou1 sort-check"></i> 按域名降序</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item sort-option" data-sort="daysLeft" data-order="asc" href="#"><i class="iconfont icon-gou1 sort-check"></i> 按剩余天数升序</a></li>
<li><a class="dropdown-item sort-option" data-sort="daysLeft" data-order="desc" href="#"><i class="iconfont icon-gou1 sort-check"></i> 按剩余天数降序</a></li>
<li><hr class="dropdown-divider"></li>
<li><a class="dropdown-item sort-option" data-sort="customNote" data-order="asc" href="#"><i class="iconfont icon-gou1 sort-check"></i> 按备注升序</a></li>
<li><a class="dropdown-item sort-option" data-sort="customNote" data-order="desc" href="#"><i class="iconfont icon-gou1 sort-check"></i> 按备注降序</a></li>
</ul>
</div>
</div>
</div>

<div class="row g-1" id="domainListContainer">
<!-- 域名卡片将通过JavaScript动态生成 -->
<div class="col-md-6 col-lg-4 domain-column px-1-5" id="column-1"></div>
<div class="col-md-6 col-lg-4 domain-column px-1-5" id="column-2"></div>
<div class="col-md-6 col-lg-4 domain-column px-1-5" id="column-3"></div>
</div>
</div>

<!-- 添加域名模态框 -->
<div class="modal fade" id="addDomainModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">添加新域名</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form id="addDomainForm">
<input type="hidden" id="domainId" value="">
<div class="mb-3">
<label for="domainName" class="form-label"><i class="iconfont icon-earth-full"></i> 域名 <span style="color: red;">*</span></label>
<div class="input-group">
<input type="text" class="form-control" id="domainName" placeholder="请输入域名,如example.com" required>
<button class="btn btn-outline-primary whois-query-btn" type="button" id="whoisQueryBtn">
<i class="iconfont icon-magnifying-glass"></i><span> 查询</span>
</button>
<button class="btn btn-outline-danger whois-clear-btn" type="button" id="whoisClearBtn" title="清除自动填充的信息">
<i class="iconfont icon-qingchu"></i><span> 清除</span>
</button>
</div>
<div class="form-text">输入域名后点击"查询"可自动填充注册信息</div>
<div id="whoisQueryStatus" class="mt-2" style="display: none;"></div>
</div>
<div class="mb-3">
<label for="registrar" class="form-label"><i class="iconfont icon-house-chimney"></i> 注册厂商</label>
<input type="text" class="form-control" id="registrar" placeholder="请输入注册厂商名称,如阿里云、腾讯云等">
</div>
<div class="mb-3">
<label for="domainCategory" class="form-label"><i class="iconfont icon-fenlei"></i> 分类</label>
<select class="form-select" id="domainCategory">
<option value="">选择分类</option>
<!-- 分类选项将通过JavaScript动态生成 -->
</select>
<small class="form-text">不选择将放入默认分类</small>
</div>
<!-- 添加自定义备注字段 -->
<div class="mb-3">
<label for="customNote" class="form-label"><i class="iconfont icon-tags"></i> 自定义备注</label>
<div class="input-group">
<input type="text" class="form-control" id="customNote" placeholder="添加备注信息">
<select class="form-select" id="noteColor" style="max-width: 120px;">
<option value="tag-blue" selected>蓝色</option>
<option value="tag-green">绿色</option>
<option value="tag-red">红色</option>
<option value="tag-yellow">黄色</option>
<option value="tag-purple">紫色</option>
<option value="tag-pink">粉色</option>
<option value="tag-indigo">靛蓝</option>
<option value="tag-gray">灰色</option>
</select>
</div>
<div class="form-text d-flex align-items-center justify-content-between">
<span>将显示在卡片头部域名下方(可选)</span>
<div id="notePreview" style="display: none;" class="text-info note-preview">预览</div>
</div>
</div>
<div class="mb-3">
<label for="registrationDate" class="form-label"><i class="iconfont icon-calendar-days"></i> 注册时间 <span style="color: red;">*</span></label>
<input type="date" class="form-control" id="registrationDate" required>
<div class="form-text">域名首次注册的时间</div>
</div>

<!-- 续期周期设置 -->
<div class="mb-3">
<label for="renewCycle" class="form-label"><i class="iconfont icon-repeat"></i> 续期周期 <span style="color: red;">*</span></label>
<div class="input-group">
<input type="number" class="form-control" id="renewCycleValue" value="1" min="1" max="100">
<select class="form-select" id="renewCycleUnit">
<option value="year" selected>年</option>
<option value="month">月</option>
<option value="day">日</option>
</select>
</div>
<div class="form-text">域名的常规续期周期,用于计算进度条和到期日期</div>
</div>

<div class="mb-3">
<label for="expiryDate" class="form-label"><i class="iconfont icon-calendar-days"></i> 到期日期 <span style="color: red;">*</span></label>
<input type="date" class="form-control" id="expiryDate" required>
<div class="form-text text-info">根据注册时间和续期周期自动计算,可手动调整</div>
</div>

<!-- 价格设置 -->
<div class="mb-3">
<label for="price" class="form-label"><i class="iconfont icon-licai"></i> 价格</label>
<div class="input-group">
<select class="form-select" id="priceCurrency" style="max-width: 80px;">
<option value="¥" selected>¥</option>
<option value="$">$</option>
<option value="€">€</option>
<option value="£">£</option>
<option value="₽">₽</option>
</select>
<input type="number" class="form-control" id="priceValue" value="" min="0" step="0.01" placeholder="输入价格">
<select class="form-select" id="priceUnit" style="max-width: 110px;">
<option value="year" selected>年</option>
<option value="month">月</option>
<option value="day">日</option>
</select>
</div>
<div class="form-text">域名的价格,支持多国货币</div>
</div>

<!-- 添加续费链接字段 -->
<div class="mb-3">
<label for="renewLink" class="form-label"><i class="iconfont icon-link"></i> 续费链接</label>
<input type="url" class="form-control" id="renewLink" placeholder="https://example.com/renew">
<div class="form-text">域名续费的直达链接</div>
</div>

<!-- 上次续期时间设置 -->
<div class="mb-3" id="lastRenewedContainer" style="display: none;">
<div class="d-flex align-items-center">
<label class="form-label mb-0 me-3">上次续期时间:</label>
<span id="lastRenewedDisplay" class="text-white me-2"></span>
<button type="button" class="btn btn-sm btn-danger" id="clearLastRenewed"><span style="color: white;">清除</span></button>
</div>
<input type="hidden" id="lastRenewed" value="">
<div class="form-text">清除后将不再显示上次续期信息,请慎重操作!!</div>
</div>

<!-- 通知设置 -->
<hr>
<h6 class="mb-3" style="display: flex; align-items: center; gap: 5px;"><i class="iconfont icon-paper-plane" style="color: white;"></i> 通知设置</h6>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="useGlobalSettings" checked>
<label class="form-check-label" for="useGlobalSettings">使用全局通知设置</label>
</div>
<div id="domainNotifySettings" style="display: none;">
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="notifyEnabled" checked>
<label class="form-check-label" for="notifyEnabled">启用到期通知</label>
</div>
<div class="mb-3">
<label for="domainNotifyDays" class="form-label"><i class="iconfont icon-lingdang"></i> 提前通知天数</label>
<input type="number" class="form-control" id="domainNotifyDays" min="1" max="90" value="30">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><span style="color: white;"><i class="iconfont icon-xmark"></i> 取消</span></button>
<button type="button" class="btn btn-primary" id="saveDomainBtn"><span style="color: white;"><i class="iconfont icon-save-3-fill"></i> 保存</span></button>
</div>
</div>
</div>
</div>

<!-- 分类管理模态框 -->
<div class="modal fade" id="categoryManageModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">分类管理</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form id="categoryForm">

<div class="mb-3">
<label for="categoryName" class="form-label"><i class="iconfont icon-shapes"></i> 分类名称 <span style="color: red;">*</span></label>
<input type="text" class="form-control" id="categoryName" placeholder="例如: 生产环境" maxlength="50" required>
</div>
<div class="mb-3">
<label for="categoryDescription" class="form-label"><i class="iconfont icon-bianji"></i> 描述</label>
<input type="text" class="form-control" id="categoryDescription" placeholder="分类描述" maxlength="100">
</div>
<div class="mb-3">
<button type="button" class="btn btn-primary" id="addCategoryBtn">
<i class="iconfont icon-jia" style="color: white;"></i> <span style="color: white;">添加分类</span>
</button>
</div>

<hr class="my-4">

<h6 class="mb-3" style="display: flex; align-items: center; gap: 5px;"><i class="iconfont icon-list-ul" style="color: white;"></i> 现有分类</h6>
<div id="categoryList">
<!-- 分类列表将通过JavaScript动态生成 -->
<div class="text-center p-3 text-muted">
<i class="iconfont icon-loading"></i> 加载中...
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><span style="color: white;"><i class="iconfont icon-xmark"></i> 关闭</span></button>
</div>
</div>
</div>
</div>

<!-- 设置模态框 -->
<div class="modal fade" id="settingsModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">系统设置</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form id="settingsForm">
<h6 class="mb-3" style="display: flex; align-items: center; gap: 5px;"><i class="iconfont icon-telegram" style="color: white;"></i> Telegram通知设置</h6>
<div class="mb-3 form-check">
<input type="checkbox" class="form-check-input" id="telegramEnabled">
<label class="form-check-label" for="telegramEnabled">启用Telegram通知</label>
</div>
<div id="telegramSettings" style="display: none;">
<div class="mb-3">
<label for="telegramToken" class="form-label"><i class="iconfont icon-key"></i> 机器人Token</label>
<input type="text" class="form-control" id="telegramToken" placeholder="如已在环境变量中配置则可留空">
<div class="form-text">在Telegram中找到@BotFather创建机器人并获取Token</div>
</div>
<div class="mb-3">
<label for="telegramChatId" class="form-label"><i class="iconfont icon-robot-2-fill"></i> 聊天ID</label>
<input type="text" class="form-control" id="telegramChatId" placeholder="如已在环境变量中配置则可留空">
<div class="form-text">可以使用@userinfobot获取个人ID,或将机器人添加到群组后获取群组ID</div>
</div>
<div class="mb-3">
<label for="notifyDays" class="form-label"><i class="iconfont icon-lingdang"></i> 提前通知天数</label>
<input type="number" class="form-control" id="notifyDays" min="1" max="90" value="30">
<div class="form-text">域名到期前多少天开始发送通知</div>
</div>
<div class="mb-3">
<button type="button" class="btn btn-info" id="testTelegramBtn"><i class="iconfont icon-paper-plane" style="color: white;"></i> <span style="color: white;">测试Telegram通知</span></button>
<span id="testResult" class="ms-2"></span>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><span style="color: white;"><i class="iconfont icon-xmark"></i> 取消</span></button>
<button type="button" class="btn btn-primary" id="saveSettingsBtn"><span style="color: white;"><i class="iconfont icon-save-3-fill"></i> 保存设置</span></button>
</div>
</div>
</div>
</div>

<!-- 确认删除模态框 -->
<div class="modal fade" id="deleteDomainModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">确认删除</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>确定要删除域名 <span id="deleteModalDomainName"></span> 吗?此操作不可撤销。</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><span style="color: white;"><i class="iconfont icon-xmark"></i> 取消</span></button>
<button type="button" class="btn btn-danger" id="confirmDeleteBtn"><span style="color: white;"><i class="iconfont icon-shanchu"></i> 删除</span></button>
</div>
</div>
</div>
</div>

<!-- 续期模态框 -->
<div class="modal fade" id="renewDomainModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">域名续期</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>为域名 <span id="renewModalDomainName"></span> 续期</p>
<div class="mb-3">
<label for="renewPeriod" class="form-label"><i class="iconfont icon-repeat"></i> 续期周期</label>
<div class="input-group">
<input type="number" class="form-control" id="renewPeriodValue" min="1" max="100" value="1">
<select class="form-select" id="renewPeriodUnit">
<option value="year" selected>年</option>
<option value="month">月</option>
<option value="day">日</option>
</select>
</div>
</div>
<div class="mb-3">
<label for="newExpiryDate" class="form-label"><i class="iconfont icon-calendar-days"></i> 新到期日期</label>
<input type="date" class="form-control" id="newExpiryDate" readonly>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><i class="iconfont icon-xmark"></i> 取消</button>
<button type="button" class="btn btn-success" id="confirmRenewBtn"><i class="iconfont icon-arrows-rotate"></i> 确认续期</button>
</div>
</div>
</div>
</div>

<!-- 确认删除分类模态框 -->
<div class="modal fade" id="deleteCategoryModal" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">删除分类</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<p>确定要删除分类 <strong id="deleteCategoryModalName"></strong> 吗?</p>
<p class="text-warning">该分类下的域名将自动移动到默认分类中。此操作不可撤销。</p>

<div class="form-check mt-3">
<input class="form-check-input" type="checkbox" id="confirmDeleteCheckbox">
<label class="form-check-label" for="confirmDeleteCheckbox">
我确认要删除此分类
</label>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal"><span style="color: white;"><i class="iconfont icon-xmark"></i> 取消</span></button>
<button type="button" class="btn btn-danger" id="confirmDeleteCategoryBtn" disabled><span style="color: white;"><i class="iconfont icon-shanchu"></i> 删除</span></button>
</div>
</div>
</div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- 已在头部引入iconfont图标库,此处无需重复引入 -->
<script>
// 全局变量
let domains = [];
let currentDomainId = null;
let currentCategoryId = null;
let telegramConfig = {};
let currentSortField = 'suffix'; // 默认排序字段改为域名后缀
let currentSortOrder = 'asc'; // 默认排序顺序
let viewMode = 'auto-collapse'; // 默认查看模式:auto-collapse (自动折叠), expand-all (全部展开), collapse-all (全部折叠)

// 将天数转换为年月日格式
function formatDaysToYMD(days) {
if (days <= 0) return '';

const years = Math.floor(days / 365);
const remainingDaysAfterYears = days % 365;
const months = Math.floor(remainingDaysAfterYears / 30);
const remainingDays = remainingDaysAfterYears % 30;

let result = '';

if (years > 0) {
result += years + '年';
}

if (months > 0) {
result += months + '个月';
}

if (remainingDays > 0) {
result += remainingDays + '天';
}

return result;
}

// 格式化日期函数
function formatDate(dateString) {
const date = new Date(dateString);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return year + '-' + month + '-' + day;
}

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', () => {
// 设置事件监听器
setupEventListeners();

// 确保DOM元素已完全加载
setTimeout(() => {
// 使用Promise.all并行加载数据
Promise.all([loadDomains(), loadCategories(), loadTelegramConfig()])
.catch(error => showAlert('danger', '数据加载失败: ' + error.message));
}, 300);

// 设置初始视图模式为全部折叠
setTimeout(() => {
const collapseAllButton = document.querySelector('.view-option[data-view="collapse-all"]');
if (collapseAllButton) {
collapseAllButton.classList.remove('btn-outline-info');
collapseAllButton.classList.add('btn-info');
}
}, 500); // 延迟执行确保DOM已经加载完成
});

// 全局变量用于存储当前查询的控制器
let currentWhoisController = null;

// 设置事件监听器
function setupEventListeners() {
// Logo点击刷新页面
document.getElementById('refreshLogo').addEventListener('click', function() {
// 添加刷新动画效果
const logoImg = this.querySelector('.logo-img');
logoImg.classList.add('refreshing');

// 延迟刷新页面,让用户看到动画效果
setTimeout(() => {
window.location.reload();
}, 300);
});

// 保存域名按钮
document.getElementById('saveDomainBtn').addEventListener('click', saveDomain);

// 确认删除按钮
document.getElementById('confirmDeleteBtn').addEventListener('click', deleteDomain);

// 分类管理相关事件
const addCategoryBtn = document.getElementById('addCategoryBtn');
if (addCategoryBtn) {
addCategoryBtn.addEventListener('click', addCategory);
}

// 分类管理模态框显示时加载分类列表
const categoryModal = document.getElementById('categoryManageModal');
if (categoryModal) {
categoryModal.addEventListener('shown.bs.modal', function() {
loadCategories();
});
}

// 确认续期按钮
document.getElementById('confirmRenewBtn').addEventListener('click', renewDomain);

// 确认删除分类按钮
document.getElementById('confirmDeleteCategoryBtn').addEventListener('click', confirmDeleteCategory);

// 删除分类确认勾选框
document.getElementById('confirmDeleteCheckbox').addEventListener('change', function() {
const deleteBtn = document.getElementById('confirmDeleteCategoryBtn');
deleteBtn.disabled = !this.checked;
});

// 添加域名按钮点击事件 - 清空表单
document.querySelector('.add-domain-btn').addEventListener('click', function() {
resetForm(); // 重置表单,确保显示空白表单
});

// 监听模态框关闭事件,取消正在进行的查询
document.getElementById('addDomainModal').addEventListener('hidden.bs.modal', function() {
if (currentWhoisController) {
currentWhoisController.abort();
currentWhoisController = null;
}
// 清除自动填充的高亮样式
document.getElementById('registrar').classList.remove('auto-filled');
document.getElementById('registrationDate').classList.remove('auto-filled');
document.getElementById('expiryDate').classList.remove('auto-filled');
document.getElementById('renewCycleValue').classList.remove('auto-filled');
document.getElementById('renewCycleUnit').classList.remove('auto-filled');
});

// 添加模态框焦点管理 - 让Bootstrap自己处理aria-hidden
const modals = ['addDomainModal', 'categoryManageModal', 'settingsModal', 'deleteDomainModal', 'renewDomainModal', 'deleteCategoryModal'];
modals.forEach(modalId => {
const modalElement = document.getElementById(modalId);
if (modalElement) {
// 模态框显示时确保焦点陷阱正确工作
modalElement.addEventListener('shown.bs.modal', function() {
// 将焦点设置到模态框的第一个可聚焦元素
const firstFocusable = this.querySelector('input:not([disabled]):not([aria-hidden="true"]), button:not([disabled]):not([aria-hidden="true"]), select:not([disabled]):not([aria-hidden="true"]), textarea:not([disabled]):not([aria-hidden="true"]), [tabindex]:not([tabindex="-1"]):not([aria-hidden="true"])');
if (firstFocusable) {
setTimeout(() => {
firstFocusable.focus();
}, 150);
}
});

// 确保模态框内的元素在隐藏时不会获得焦点
modalElement.addEventListener('hide.bs.modal', function() {
// 移除可能残留的焦点
if (document.activeElement && this.contains(document.activeElement)) {
document.activeElement.blur();
}
});
}
});

// 清除上次续期时间按钮
document.getElementById('clearLastRenewed').addEventListener('click', function() {
document.getElementById('lastRenewed').value = '';
document.getElementById('lastRenewedDisplay').textContent = '已清除';
document.getElementById('lastRenewedDisplay').classList.add('text-danger');

// 清除上次续期时间后,重新根据注册时间和续期周期计算到期日期
calculateExpiryDate();
});

// WHOIS自动查询按钮
document.getElementById('whoisQueryBtn').addEventListener('click', async function() {
const domainInput = document.getElementById('domainName');
const domain = domainInput.value.trim();

if (!domain) {
showWhoisStatus('请先输入域名', 'danger');
return;
}

// 验证域名格式 - 更宽松的域名格式验证
const domainRegex = /^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.[a-zA-Z]{2,}$/;
if (!domainRegex.test(domain)) {
showWhoisStatus('仅支持自动查询一级域名', 'danger');
return;
}

// 验证是否为一级域名(只能有一个点)
const dotCount = domain.split('.').length - 1;

if (dotCount !== 1) {
if (dotCount === 0) {
showWhoisStatus('请输入完整的域名(如:example.com)', 'danger');
} else {
showWhoisStatus('只能查询一级域名,不支持二级域名查询(检测到' + dotCount + '个点)', 'danger');
}
return;
}

// 取消之前的查询
if (currentWhoisController) {
currentWhoisController.abort();
}

// 创建新的查询控制器
currentWhoisController = new AbortController();

await performWhoisQuery(domain, currentWhoisController);
});

// WHOIS清除按钮
document.getElementById('whoisClearBtn').addEventListener('click', function() {
// 清除域名输入框
document.getElementById('domainName').value = '';

// 清除自动填充的字段
document.getElementById('registrar').value = '';
document.getElementById('registrationDate').value = '';
document.getElementById('expiryDate').value = '';
document.getElementById('renewCycleValue').value = '1';
document.getElementById('renewCycleUnit').value = 'year';

// 清除高亮样式
document.getElementById('registrar').classList.remove('auto-filled');
document.getElementById('registrationDate').classList.remove('auto-filled');
document.getElementById('expiryDate').classList.remove('auto-filled');
document.getElementById('renewCycleValue').classList.remove('auto-filled');
document.getElementById('renewCycleUnit').classList.remove('auto-filled');

// 清除查询状态信息
const statusDiv = document.getElementById('whoisQueryStatus');
if (statusDiv) {
statusDiv.style.display = 'none';
statusDiv.innerHTML = '';
}

// 显示清除成功提示
showWhoisStatus('已清除自动填充的域名信息', 'info');
});

// 续期值或单位变化时更新新到期日期
document.getElementById('renewPeriodValue').addEventListener('input', updateNewExpiryDate);
document.getElementById('renewPeriodUnit').addEventListener('change', updateNewExpiryDate);

// 注册时间和续期周期变化时自动计算到期日期
document.getElementById('registrationDate').addEventListener('change', calculateExpiryDate);
document.getElementById('renewCycleValue').addEventListener('input', calculateExpiryDate);
document.getElementById('renewCycleUnit').addEventListener('change', calculateExpiryDate);

// Telegram启用状态变化
document.getElementById('telegramEnabled').addEventListener('change', function() {
document.getElementById('telegramSettings').style.display = this.checked ? 'block' : 'none';
});

// 保存设置按钮
document.getElementById('saveSettingsBtn').addEventListener('click', saveSettings);

// 测试Telegram按钮
document.getElementById('testTelegramBtn').addEventListener('click', testTelegram);

// 域名通知设置 - 全局/自定义切换
document.getElementById('useGlobalSettings').addEventListener('change', function() {
document.getElementById('domainNotifySettings').style.display = this.checked ? 'none' : 'block';
});

// 窗口大小变化监听器 - 用于移动端排序修复
let resizeTimer;
let lastWidth = window.innerWidth;

window.addEventListener('resize', function() {
// 防抖处理,避免频繁重新渲染
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function() {
const currentWidth = window.innerWidth;

// 只有在宽度发生显著变化时才重新渲染(避免移动端滑动时地址栏显示/隐藏导致的高度变化)
const widthChanged = Math.abs(currentWidth - lastWidth) > 50;

if (widthChanged) {
// 重新渲染域名列表以适应新的屏幕尺寸
renderDomainList();
lastWidth = currentWidth;
}
}, 300); // 增加延迟时间,减少触发频率
});

// 根据注册时间和续期周期自动计算到期日期
function calculateExpiryDate() {
const registrationDate = document.getElementById('registrationDate').value;
const lastRenewed = document.getElementById('lastRenewed').value;
const renewCycleValue = parseInt(document.getElementById('renewCycleValue').value) || 1;
const renewCycleUnit = document.getElementById('renewCycleUnit').value;

if (!registrationDate) {
// 如果没有注册时间,清空到期日期
document.getElementById('expiryDate').value = '';
return;
}

// 如果有上次续期时间,则从上次续期时间开始计算;否则从注册时间开始计算
const baseDate = lastRenewed ? new Date(lastRenewed) : new Date(registrationDate);
const expiryDate = new Date(baseDate);

// 根据续期周期单位计算到期日期
switch(renewCycleUnit) {
case 'year':
expiryDate.setFullYear(baseDate.getFullYear() + renewCycleValue);
break;
case 'month':
expiryDate.setMonth(baseDate.getMonth() + renewCycleValue);
break;
case 'day':
expiryDate.setDate(baseDate.getDate() + renewCycleValue);
break;
}

// 格式化日期为 YYYY-MM-DD 格式
const formattedDate = expiryDate.toISOString().split('T')[0];
document.getElementById('expiryDate').value = formattedDate;
}

// 监听备注文本和颜色变化
document.getElementById('customNote').addEventListener('input', updateNotePreview);
document.getElementById('noteColor').addEventListener('change', updateNotePreview);

// 当模态框显示时初始化预览
document.getElementById('addDomainModal').addEventListener('shown.bs.modal', function() {
updateNotePreview();
});

// 排序选项点击事件
document.querySelectorAll('.sort-option').forEach(option => {
option.addEventListener('click', async function(e) {
e.preventDefault();
currentSortField = this.dataset.sort;
currentSortOrder = this.dataset.order;
await renderDomainList();

// 不再更新排序按钮文本,只保留"域名排序"
// 但仍然需要更新勾选状态

// 更新勾选状态
document.querySelectorAll('.sort-option').forEach(opt => {
opt.classList.remove('active');
});
this.classList.add('active');
});
});

// 视图模式选项点击事件
document.querySelectorAll('.view-option').forEach(option => {
option.addEventListener('click', function(e) {
const newViewMode = this.dataset.view;

// 设置视图模式
viewMode = newViewMode;

// 直接获取所有卡片详情元素
const allDetails = document.querySelectorAll('.domain-card .collapse');

// 根据模式直接进行操作
if (newViewMode === 'expand-all') {
// 直接展开所有卡片
allDetails.forEach(detail => {
// 手动添加show类,强制显示
detail.classList.add('show');
detail.style.height = 'auto'; // 确保内容显示
detail.style.overflow = 'visible';

// 获取父级卡片
const domainCard = detail.closest('.domain-card');
if (domainCard) {
// 在父级卡片中寻找toggle按钮
const btn = domainCard.querySelector('.toggle-details');
if (btn) {
btn.classList.remove('collapsed');
btn.setAttribute('aria-expanded', 'true');
}
// 添加展开状态类,使域名可以换行显示
domainCard.classList.add('expanded');
}
});

// 高亮"全部展开"按钮
document.querySelectorAll('.view-option').forEach(btn => {
if (btn.dataset.view === 'expand-all') {
btn.classList.remove('btn-outline-info');
btn.classList.add('btn-info');
} else {
btn.classList.add('btn-outline-info');
btn.classList.remove('btn-info');
}
});
} else if (newViewMode === 'collapse-all' || newViewMode === 'auto-collapse') {
// 直接折叠所有卡片
allDetails.forEach(detail => {
// 手动移除show类,强制隐藏
detail.classList.remove('show');
detail.style.height = '0px'; // 强制隐藏高度
detail.style.overflow = 'hidden';

// 获取父级卡片
const domainCard = detail.closest('.domain-card');
if (domainCard) {
// 在父级卡片中寻找toggle按钮
const btn = domainCard.querySelector('.toggle-details');
if (btn) {
btn.classList.add('collapsed');
btn.setAttribute('aria-expanded', 'false');
}
// 移除展开状态类,恢复省略号显示
domainCard.classList.remove('expanded');
}
});

// 高亮"全部折叠"按钮
document.querySelectorAll('.view-option').forEach(btn => {
if (btn.dataset.view === 'collapse-all') {
btn.classList.remove('btn-outline-info');
btn.classList.add('btn-info');
} else {
btn.classList.add('btn-outline-info');
btn.classList.remove('btn-info');
}
});
}
});
});

// 添加点击空白处折叠卡片的功能
document.addEventListener('click', function(e) {
// 检查是否处于"折叠"模式
const collapseButton = document.querySelector('.view-option[data-view="collapse-all"]');
if (collapseButton && collapseButton.classList.contains('btn-info')) {
// 确保点击的是空白处,而不是卡片内部或其他功能按钮
if (
!e.target.closest('.domain-card') &&
!e.target.closest('.btn') &&
!e.target.closest('.modal') &&
!e.target.closest('.navbar') &&
!e.target.closest('.page-header') &&
!e.target.closest('.dropdown-menu')
) {
// 获取所有展开的卡片
const expandedCards = document.querySelectorAll('.domain-card .collapse.show');

// 折叠所有展开的卡片
expandedCards.forEach(detail => {
// 使用Bootstrap的collapse方法实现平滑的折叠动画
const bsCollapse = bootstrap.Collapse.getInstance(detail);
if (bsCollapse) {
bsCollapse.hide();
}

// 获取父级卡片
const domainCard = detail.closest('.domain-card');
if (domainCard) {
// 监听折叠完成事件,移除展开状态类
detail.addEventListener('hidden.bs.collapse', function() {
// 移除展开状态类,恢复省略号显示
domainCard.classList.remove('expanded');
}, {once: true}); // 只执行一次

// 在父级卡片中寻找toggle按钮并更新状态
const btn = domainCard.querySelector('.toggle-details');
if (btn) {
btn.classList.add('collapsed');
btn.setAttribute('aria-expanded', 'false');
}
}
});
}
}
});

// 初始加载时设置默认排序选项
const defaultSortOption = document.querySelector('.sort-option[data-sort="' + currentSortField + '"][data-order="' + currentSortOrder + '"]');
if (defaultSortOption) {
// 不再更新排序按钮文本,保持"域名排序"

// 设置默认选项为激活状态
defaultSortOption.classList.add('active');
} else {
// 如果找不到匹配的选项,默认选择按后缀升序
const suffixAscOption = document.querySelector('.sort-option[data-sort="suffix"][data-order="asc"]');
if (suffixAscOption) {
suffixAscOption.classList.add('active');
currentSortField = 'suffix';
currentSortOrder = 'asc';
}
}

// 表头排序点击事件
document.addEventListener('click', async function(e) {
if (e.target.tagName === 'TH') {
const field = e.target.dataset.sort;
if (field) {
if (currentSortField === field) {
// 切换排序顺序
currentSortOrder = currentSortOrder === 'asc' ? 'desc' : 'asc';
} else {
currentSortField = field;
currentSortOrder = 'asc';
}
await renderDomainList();
}
}
});
}

// 自定义备注颜色预览函数
function updateNotePreview() {
const noteText = document.getElementById('customNote').value.trim();
const noteColor = document.getElementById('noteColor').value;
const notePreview = document.getElementById('notePreview');

if (noteText) {
// 更新预览文字和显示状态
notePreview.textContent = noteText;
notePreview.style.display = 'inline-block';

// 移除所有颜色类但保留基本样式
notePreview.className = 'text-info note-preview';
// 添加选中的颜色类
notePreview.classList.add(noteColor);

// 使用内联样式强制设置颜色
const colorMap = {
'tag-blue': '#3B82F6',
'tag-green': '#10B981',
'tag-red': '#EF4444',
'tag-yellow': '#F59E0B',
'tag-purple': '#8B5CF6',
'tag-pink': '#EC4899',
'tag-indigo': '#6366F1',
'tag-gray': '#6B7280'
};
notePreview.style.backgroundColor = colorMap[noteColor] || '#3B82F6';
} else {
notePreview.style.display = 'none';
}
}

// 加载所有域名
async function loadDomains() {
// 先尝试显示加载状态,但不阻止后续操作
try {
showDomainLoadingState();
} catch (loadingError) {
// 继续执行,不要因为显示加载状态失败而中断
}

try {
const response = await fetch('/api/domains');

if (!response.ok) {
throw new Error('获取域名列表失败: ' + response.status);
}

domains = await response.json();

// 确保DOM元素已加载后再渲染
setTimeout(async () => {
await renderDomainList();
}, 100);

return domains; // 返回加载的域名数据
} catch (error) {
showAlert('danger', '加载域名列表失败: ' + error.message);
throw error;
}
}

// 显示域名加载中的状态
function showDomainLoadingState() {
// 使用document.querySelector作为备选方法
const domainListContainer = document.getElementById('domainListContainer') || document.querySelector('#domainListContainer');

if (!domainListContainer) {
return;
}

try {
// 创建骨架屏
domainListContainer.innerHTML =
'<div class="col-md-6 col-lg-4 domain-column px-1-5">' +
generateSkeletonCard() +
generateSkeletonCard() +
'</div>' +
'<div class="col-md-6 col-lg-4 domain-column px-1-5">' +
generateSkeletonCard() +
generateSkeletonCard() +
'</div>' +
'<div class="col-md-6 col-lg-4 domain-column px-1-5">' +
generateSkeletonCard() +
generateSkeletonCard() +
'</div>';
} catch (error) {
// 忽略骨架屏设置失败
}
}

// 生成骨架屏卡片
function generateSkeletonCard() {
return '<div class="domain-card skeleton-card mb-3">' +
'<div class="domain-header skeleton-header">' +
'<div class="skeleton-text-lg"></div>' +
'<div class="skeleton-text-sm"></div>' +
'</div>' +
'<div class="domain-body">' +
'<div class="skeleton-text"></div>' +
'<div class="skeleton-text"></div>' +
'<div class="skeleton-progress"></div>' +
'</div>' +
'</div>';
}

// 加载Telegram配置
async function loadTelegramConfig() {
try {
const response = await fetch('/api/telegram/config');
if (!response.ok) throw new Error('获取Telegram配置失败');

telegramConfig = await response.json();

// 更新表单
document.getElementById('telegramEnabled').checked = telegramConfig.enabled;
document.getElementById('telegramSettings').style.display = telegramConfig.enabled ? 'block' : 'none';
document.getElementById('notifyDays').value = telegramConfig.notifyDays || 30;

// 处理聊天ID的显示
if (telegramConfig.chatIdFromEnv) {
// 如果聊天ID来自环境变量,显示固定文本
document.getElementById('telegramChatId').value = '';
document.getElementById('telegramChatId').placeholder = '已通过环境变量配置';
document.getElementById('telegramChatId').disabled = false; // 允许用户编辑
} else {
// 显示用户设置的聊天ID
document.getElementById('telegramChatId').value = telegramConfig.chatId || '';
document.getElementById('telegramChatId').placeholder = '如已在环境变量中配置则可留空';
document.getElementById('telegramChatId').disabled = false;
}

// 处理Token的显示
if (telegramConfig.tokenFromEnv) {
// 如果Token来自环境变量,显示固定文本
document.getElementById('telegramToken').value = '';
document.getElementById('telegramToken').placeholder = '已通过环境变量配置';
document.getElementById('telegramToken').disabled = false; // 允许用户编辑
} else {
// 显示用户设置的Token
document.getElementById('telegramToken').value = telegramConfig.botToken || '';
document.getElementById('telegramToken').placeholder = '如已在环境变量中配置则可留空';
document.getElementById('telegramToken').disabled = false;
}
} catch (error) {
// 忽略Telegram配置加载失败
}
}

// 保存设置
async function saveSettings() {
const enabled = document.getElementById('telegramEnabled').checked;
// 获取表单值,即使是空字符串也保留
const botToken = document.getElementById('telegramToken').value;
const chatId = document.getElementById('telegramChatId').value;
const notifyDays = parseInt(document.getElementById('notifyDays').value) || 30;

try {
const response = await fetch('/api/telegram/config', {
headers: { 'Content-Type': 'application/json' },
method: 'POST',
body: JSON.stringify({
enabled,
botToken,
chatId,
notifyDays
})
});

if (!response.ok) {
try {
const error = await response.json();
throw new Error(error.error || '保存设置失败');
} catch (jsonError) {
// 如果响应不是JSON格式,直接使用状态文本
throw new Error('保存设置失败: ' + response.statusText);
}
}

telegramConfig = await response.json();
showAlert('success', '设置保存成功');

// 关闭模态框
bootstrap.Modal.getInstance(document.getElementById('settingsModal')).hide();
} catch (error) {
showAlert('danger', error.message);
}
}

// 测试Telegram通知
async function testTelegram() {
const testResult = document.getElementById('testResult');
testResult.textContent = '发送中...';
testResult.className = 'ms-2 text-info';

try {
const response = await fetch('/api/telegram/test', {
method: 'POST'
});

if (!response.ok) {
const error = await response.json();
throw new Error(error.error || '测试失败');
}

const result = await response.json();
testResult.textContent = '测试成功!请检查Telegram是否收到消息';
testResult.className = 'ms-2 telegram-test-success';
} catch (error) {
testResult.textContent = '测试失败: ' + error.message;
testResult.className = 'ms-2 text-danger';
}
}

// 渲染域名列表
async function renderDomainList() {
// 更新总域名数量统计
const totalDomainCountElement = document.getElementById('totalDomainCount');
if (totalDomainCountElement) {
totalDomainCountElement.textContent = '(' + domains.length + ')';
}

// 获取domainListContainer
const domainListContainer = document.getElementById('domainListContainer');
if (!domainListContainer) {
return;
}

if (domains.length === 0) {
// 显示无域名记录提示
domainListContainer.innerHTML = '<div class="col-12"><div class="alert alert-info">暂无域名记录,请点击右上角按钮添加域名。</div></div>';
return;
}

// 清空容器
domainListContainer.innerHTML = '';

// 获取全局通知设置
const globalNotifyDays = telegramConfig.notifyDays || 30;

// 计算每个域名的剩余天数
domains.forEach(domain => {
const expiryDate = new Date(domain.expiryDate);
const today = new Date();
domain.daysLeft = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));
});

// 按照指定字段和顺序排序
sortDomains(domains, currentSortField, currentSortOrder);

// 获取分类数据
let categoryList = [];
try {
const response = await fetch('/api/categories');
if (response.ok) {
categoryList = await response.json();
// 同步到全局变量
categories = categoryList;
}
} catch (error) {
// 如果获取分类失败,创建默认分类
categoryList = [{
id: 'default',
name: '默认分类',
description: '未指定分类的域名',
order: 0,
isDefault: true
}];
// 同步到全局变量
categories = categoryList;
}

// 按分类分组域名
const domainGroups = {};

// 初始化所有分类组
categoryList.forEach(category => {
domainGroups[category.id] = {
id: category.id, // 添加ID字段
name: category.name,
description: category.description,
domains: [],
order: category.order,
isDefault: category.isDefault
};
});

// 将域名分配到不同的分组
domains.forEach(domain => {
// 确保向后兼容:如果域名没有categoryId,根据是否有注册商信息决定分类
if (!domain.categoryId) {
if (domain.registrar && domain.registrar.trim() !== '') {
// 如果有注册商但没有分类,先保持原有的注册商分组逻辑,但标记为需要迁移
domain.categoryId = 'default';
} else {
// 没有注册商信息的域名放入默认分类
domain.categoryId = 'default';
}
}

// 将域名添加到对应分类,如果分类不存在则放入默认分类
const categoryId = domain.categoryId || 'default';
if (domainGroups[categoryId]) {
domainGroups[categoryId].domains.push(domain);
} else {
domainGroups['default'].domains.push(domain);
}
});

// 处理域名分组
const renderGroup = (categoryData) => {
// 如果该分组没有域名,跳过(但默认分类有域名时才显示)
if (categoryData.domains.length === 0 && categoryData.isDefault) return;

const groupName = categoryData.name;
const groupDomains = categoryData.domains;

// 创建分类容器,用于包含标题和卡片
const groupContainer = document.createElement('div');
groupContainer.className = 'domain-group-container'; // 移除mb-4类,使用CSS中定义的margin
domainListContainer.appendChild(groupContainer);

// 创建分类标题行
const categoryRow = document.createElement('div');
categoryRow.className = 'row'; // 移除额外的margin类
categoryRow.innerHTML =
'<div class="col-12 px-1-5">' + /* 添加与卡片列相同的内边距类 */
'<div class="category-header">' +
'<h5 class="category-title">' + groupName + ' <span class="count-badge">(' + groupDomains.length + ')</span></h5>' +
'</div>' +
'</div>';
groupContainer.appendChild(categoryRow);

// 创建域名卡片行容器
const domainsRow = document.createElement('div');
domainsRow.className = 'row g-2';
groupContainer.appendChild(domainsRow);

// 创建三列布局
const column1 = document.createElement('div');
const column2 = document.createElement('div');
const column3 = document.createElement('div');
column1.className = 'col-md-6 col-lg-4 domain-column px-1-5';
column2.className = 'col-md-6 col-lg-4 domain-column px-1-5';
column3.className = 'col-md-6 col-lg-4 domain-column px-1-5';
domainsRow.appendChild(column1);
domainsRow.appendChild(column2);
domainsRow.appendChild(column3);

// 如果分类下没有域名,显示提示信息
if (groupDomains.length === 0) {
const emptyMessage = document.createElement('div');
emptyMessage.className = 'col-12 empty-state-container';
emptyMessage.innerHTML =
'<div class="text-center py-3 rounded" style="background: rgba(255, 255, 255, 0.15); border: 1px solid rgba(255, 255, 255, 0.3); color: rgba(255, 255, 255, 0.9); display: table; width: 100%; height: 160px;">' +
'<div style="display: table-cell; vertical-align: middle; width: 100%;">' +
'<i class="iconfont icon-folder-open" style="font-size: 32px; opacity: 0.6; display: block; margin-bottom: 10px;"></i>' +
'<p class="mb-1 small">该分类下暂无域名</p>' +
'<small class="opacity-75" style="display: block; margin-bottom: 15px;">在此分类下添加域名</small>' +
'<button class="btn btn-primary btn-sm add-domain-to-category" data-category-id="' + categoryData.id + '" data-category-name="' + categoryData.name + '">' +
'<i class="iconfont icon-jia" style="color: white;"></i> <span style="color: white;">添加域名</span>' +
'</button>' +
'</div>' +
'</div>';
domainsRow.appendChild(emptyMessage);
return; // 跳过域名卡片创建
}

// 为每个域名创建卡片,并按列分配
groupDomains.forEach((domain, index) => {
// 检测屏幕尺寸,决定分配策略
let columnIndex;
const isMobile = window.innerWidth < 768; // Bootstrap的md断点

if (isMobile) {
// 移动端:所有域名都放在第一列,保持排序顺序
columnIndex = 0;
} else {
// PC端:按列分配,轮询分配到各列
columnIndex = index % 3;
}

const targetColumn = columnIndex === 0 ? column1 : (columnIndex === 1 ? column2 : column3);
// 创建卡片容器
const domainCard = document.createElement('div');
domainCard.className = 'mb-2'; // 简化类名,不再需要列类

const daysLeft = domain.daysLeft;

// 确保通知设置存在
if (!domain.notifySettings) {
domain.notifySettings = { useGlobalSettings: true, enabled: true, notifyDays: 30 };
}

// 获取该域名的通知设置
const notifySettings = domain.notifySettings;
const notifyDays = notifySettings.useGlobalSettings ? globalNotifyDays : notifySettings.notifyDays;

// 状态标签逻辑
let statusClass = 'safe';
let statusText = '<i class="iconfont icon-circle-check"></i> 正常';
let statusBadge = 'success';

// 进度条颜色逻辑(先初始化,后面根据百分比设置)
let progressColor = 'rgba(0, 255, 76, 0.9)'; // 默认绿色

// 设置状态标签
if (daysLeft <= 0) {
statusClass = 'expired';
statusText = '<i class="iconfont icon-triangle-exclamation"></i> 已过期';
statusBadge = 'danger';
} else if (daysLeft <= 30) { // 修改为固定30天,按需求调整
statusClass = 'warning';
statusText = '<i class="iconfont icon-bullhorn"></i> 即将到期';
statusBadge = 'warning';
}

// 计算域名有效期的百分比进度
let progressPercent = 0;

// 获取域名的续期周期设置,如果没有则使用默认值(1年)
let cycleDays = 365; // 默认为1年

if (domain.renewCycle) {
// 根据续期周期单位计算天数
switch(domain.renewCycle.unit) {
case 'year':
cycleDays = domain.renewCycle.value * 365;
break;
case 'month':
// 更精确地计算月份的实际天数
if (domain.renewCycle.value === 1) {
const currentDate = new Date(domain.expiryDate);
const nextMonth = new Date(currentDate);
nextMonth.setMonth(nextMonth.getMonth() + 1);
cycleDays = Math.round((nextMonth - currentDate) / (1000 * 60 * 60 * 24));
} else {
const currentDate = new Date(domain.expiryDate);
const futureDate = new Date(currentDate);
futureDate.setMonth(futureDate.getMonth() + domain.renewCycle.value);
cycleDays = Math.round((futureDate - currentDate) / (1000 * 60 * 60 * 24));
}
break;
case 'day':
cycleDays = domain.renewCycle.value;
break;
default:
cycleDays = 365;
}
}

// 简化进度条计算逻辑
if (daysLeft <= 0) {
// 已过期域名,但如果有lastRenewed字段,说明已续期
if (domain.lastRenewed) {
const today = new Date();
const expiryDate = new Date(domain.expiryDate);
const newDaysLeft = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));

if (newDaysLeft >= cycleDays) {
progressPercent = 100;
} else {
// 使用精确计算,四舍五入后取整数位
progressPercent = Math.round((newDaysLeft / cycleDays) * 100);
}
} else {
progressPercent = 0;
}
} else {
// 未过期域名
if (daysLeft >= cycleDays) {
progressPercent = 100;
} else {
// 使用精确计算,四舍五入后取整数位
progressPercent = Math.round((daysLeft / cycleDays) * 100);
}
}

// 确保进度百分比在0-100范围内
if (progressPercent < 0) progressPercent = 0;
if (progressPercent > 100) progressPercent = 100;

// 根据百分比设置进度条颜色
if (progressPercent < 10) {
progressColor = 'rgba(231, 18, 64, 0.9)'; // 小于10%显示红色
} else if (progressPercent < 30) {
progressColor = 'rgba(255, 208, 0, 0.9)'; // 10%-30%显示黄色
} else {
progressColor = 'rgba(0, 255, 76, 0.9)'; // 大于30%显示绿色
}

// 创建圆环进度条样式
let progressCircleHtml = '';

// 使用SVG实现圆环进度条
const radius = 36; // 再次增加圆环半径
const circumference = 2 * Math.PI * radius; // 圆环周长
const offset = circumference - (progressPercent / 100) * circumference; // 计算偏移量

// 创建SVG圆环进度条,增加SVG尺寸
const svgSize = 85; // 再次增加SVG容器大小
const svgCenter = svgSize / 2; // 居中

// SVG圆环和百分比分开处理
const percentText = progressPercent + '%';

if (daysLeft <= 0) {
// 已过期域名显示简化的进度条,但保留0%文本
progressCircleHtml =
'<div style="position:relative; width:' + svgSize + 'px; height:' + svgSize + 'px;">' +
'<svg class="progress-ring" width="' + svgSize + '" height="' + svgSize + '" viewBox="0 0 ' + svgSize + ' ' + svgSize + '">' +
'<circle class="progress-ring-circle-bg" stroke="#f5f5f5" stroke-width="6" fill="transparent" r="' + radius + '" cx="' + svgCenter + '" cy="' + svgCenter + '"/>' +
'</svg>' +
'<div style="position:absolute; top:0; left:0; right:0; bottom:0; display:flex; align-items:center; justify-content:center; z-index:9999;">' +
'<span class="progress-percent-text">0%</span>' +
'</div>' +
'</div>';
} else {
// 正常域名显示完整进度条
progressCircleHtml =
'<div style="position:relative; width:' + svgSize + 'px; height:' + svgSize + 'px;">' +
'<svg class="progress-ring" width="' + svgSize + '" height="' + svgSize + '" viewBox="0 0 ' + svgSize + ' ' + svgSize + '">' +
'<circle class="progress-ring-circle-bg" stroke="#f5f5f5" stroke-width="6" fill="transparent" r="' + radius + '" cx="' + svgCenter + '" cy="' + svgCenter + '"/>' +
'<circle class="progress-ring-circle" stroke="' + progressColor + '" stroke-width="6" fill="transparent" ' +
'stroke-dasharray="' + circumference + ' ' + circumference + '" ' +
'style="stroke-dashoffset:' + offset + 'px;" ' +
'r="' + radius + '" cx="' + svgCenter + '" cy="' + svgCenter + '"/>' +
'</svg>' +
'<div style="position:absolute; top:0; left:0; right:0; bottom:0; display:flex; align-items:center; justify-content:center; z-index:9999;">' +
'<span class="progress-percent-text">' + percentText + '</span>' +
'</div>' +
'</div>';
}

// 准备通知信息和上次续期信息
let infoHtml = '';

// 添加通知信息
if (notifySettings.enabled) {
const effectiveNotifyDays = notifySettings.useGlobalSettings ? globalNotifyDays : notifySettings.notifyDays;
const notifyLabel = notifySettings.useGlobalSettings ? '全局通知: ' : '自定义通知: ';
infoHtml += '<small class="text-white d-inline-block me-3">' +
notifyLabel + effectiveNotifyDays + '天' +
'</small>';
} else {
infoHtml += '<small class="text-white d-inline-block me-3">通知已禁用</small>';
}

// 添加上次续期信息
if (domain.lastRenewed) {
infoHtml += '<small class="text-white d-inline-block">上次续期: ' + formatDate(domain.lastRenewed) + '</small>';
}

// 生成价格信息HTML
let priceHtml = '';
if (domain.price && domain.price.value !== null && domain.price.value !== undefined && domain.price.value !== '') {
priceHtml = ' <span class="text-white-50">(' + domain.price.currency + domain.price.value +
'/' + (domain.price.unit === 'year' ? '年' : domain.price.unit === 'month' ? '月' : '日') +
')</span>';
}

const cardHtml = '<div class="card domain-card ' + statusClass + ' mb-2">' +
'<div class="card-header">' +
'<span class="status-dot ' + statusClass + '"></span>' +
'<div class="domain-header">' +
(domain.customNote && domain.customNote.trim() !== '' ?
// 有备注时的布局 - 标签在域名下方
'<div class="domain-name-container" style="display: flex; flex-direction: column; justify-content: flex-start; height: 100%;">' +
'<h5 class="mb-0 domain-title" style="word-break: break-all;"><span class="domain-text" style="line-height: var(--domain-line-height);">' + domain.name + '</span></h5>' +
'<div class="spacer" style="height: var(--domain-note-spacing);"></div>' +
'<div class="domain-meta">' +
'<span class="text-info ' + (domain.noteColor || 'tag-blue') + '" style="background-color: ' +
(domain.noteColor === 'tag-blue' ? '#3B82F6' :
domain.noteColor === 'tag-green' ? '#10B981' :
domain.noteColor === 'tag-red' ? '#EF4444' :
domain.noteColor === 'tag-yellow' ? '#F59E0B' :
domain.noteColor === 'tag-purple' ? '#8B5CF6' :
domain.noteColor === 'tag-pink' ? '#EC4899' :
domain.noteColor === 'tag-indigo' ? '#6366F1' :
domain.noteColor === 'tag-gray' ? '#6B7280' : '#3B82F6') +
' !important">' + domain.customNote + '</span>' +
'</div>' +
'</div>'
:
// 无备注时的布局 - 保持与有备注布局相同的结构,只是没有备注标签
'<div class="domain-name-container" style="display: flex; flex-direction: column; justify-content: flex-start; height: 100%;">' +
'<h5 class="mb-0 domain-title" style="word-break: break-all;"><span class="domain-text" style="line-height: var(--domain-line-height);">' + domain.name + '</span></h5>' +
'<div class="spacer" style="height: var(--domain-note-spacing);"></div>' +
'<div class="domain-meta"></div>' +
'</div>'
) +
'</div>' +
'<div class="domain-status">' +
'<span class="badge bg-' + statusBadge + '">' + statusText + '</span>' +
'<button class="btn btn-sm btn-link toggle-details collapsed" data-bs-toggle="collapse" data-bs-target="#details-' + domain.id + '" aria-expanded="false" aria-controls="details-' + domain.id + '">' +
'<span class="toggle-icon-container">' +
'<i class="iconfont icon-angle-down toggle-icon"></i>' +
'</span>' +
'</button>' +
'</div>' +
'</div>' +
'<div class="collapse" id="details-' + domain.id + '">' +
'<div class="card-body pb-2">' +
'<div class="d-flex justify-content-between align-items-start mb-2" style="position: relative;">' +
'<div class="flex-grow-1" style="padding-right: 95px; min-width: 0;">' +
(domain.registrar ? '<p class="card-text mb-1" style="overflow: hidden; text-overflow: ellipsis; white-space: nowrap; width: 100%; display: block;"><i class="iconfont icon-house-chimney"></i><strong>注册厂商:</strong> ' + domain.registrar + '</p>' : '') +
(domain.registrationDate ? '<p class="card-text mb-1 text-nowrap" style="overflow: hidden; text-overflow: ellipsis;"><i class="iconfont icon-calendar-days"></i><strong>注册时间:</strong>' + formatDate(domain.registrationDate) + '</p>' : '') +
'<p class="card-text mb-1 text-nowrap" style="overflow: hidden; text-overflow: ellipsis;"><i class="iconfont icon-rili"></i><strong>到期日期:</strong>' + formatDate(domain.expiryDate) + '</p>' +
'<p class="card-text mb-1 text-nowrap" style="overflow: hidden; text-overflow: ellipsis;"><i class="iconfont icon-repeat"></i><strong>续期周期:</strong>' +
(domain.renewCycle ? domain.renewCycle.value + ' ' +
(domain.renewCycle.unit === 'year' ? '年' :
domain.renewCycle.unit === 'month' ? '月' : '天') : '1 年') +
priceHtml + '</p>' +
'<p class="card-text mb-0 text-nowrap" style="overflow: hidden; text-overflow: ellipsis;"><i class="iconfont icon-hourglass-start"></i><strong>剩余天数:</strong>' + (daysLeft > 0 ? daysLeft + ' 天 <span class="text-white-50">(' + formatDaysToYMD(daysLeft) + ')</span>' : '已过期') + '</p>' +
'</div>' +
'<div class="progress-circle-container">' +
'<div class="progress-circle">' +
progressCircleHtml +
'</div>' +
'</div>' +
'</div>' +
(infoHtml ? '<div class="domain-info mb-2">' + infoHtml + '</div>' : '') +
'<div class="domain-actions">' +
'<button class="btn btn-sm btn-primary edit-domain" data-id="' + domain.id + '" title="编辑域名"><i class="iconfont icon-pencil"></i> 编辑</button>' +
'<button class="btn btn-sm btn-success renew-domain" data-id="' + domain.id + '" data-name="' + domain.name + '" data-expiry="' + domain.expiryDate + '" title="续期域名"><i class="iconfont icon-arrows-rotate"></i> 续期</button>' +
'<button class="btn btn-sm btn-info test-domain-notify" data-id="' + domain.id + '" title="测试通知"><i class="iconfont icon-paper-plane"></i> 测试</button>' +
(domain.renewLink ?
'<a href="' + domain.renewLink + '" target="_blank" class="btn btn-sm btn-warning" title="前往续期页面"><i class="iconfont icon-link"></i> 链接</a>' :
'<button class="btn btn-sm btn-secondary" disabled title="未设置续期链接"><i class="iconfont icon-link"></i> 链接</button>') +
'<button class="btn btn-sm btn-danger delete-domain" data-id="' + domain.id + '" data-name="' + domain.name + '" title="删除域名"><i class="iconfont icon-shanchu"></i> 删除</button>' +
'</div>' +
'</div>' +
'</div>' +
'</div>';
domainCard.innerHTML = cardHtml;

// 将卡片添加到对应的列中
targetColumn.appendChild(domainCard);
});
};

// 按分类顺序渲染域名分组
const sortedCategories = Object.values(domainGroups).sort((a, b) => a.order - b.order);

// 首先渲染默认分类(只有在有域名的情况下)
const defaultCategory = sortedCategories.find(cat => cat.isDefault);
if (defaultCategory && defaultCategory.domains.length > 0) {
renderGroup(defaultCategory);
}

// 然后渲染其他分类(包括空分类)
sortedCategories.forEach(category => {
if (!category.isDefault) {
renderGroup(category);
}
});

// 添加事件监听器
document.querySelectorAll('.edit-domain').forEach(button => {
button.addEventListener('click', () => editDomain(button.dataset.id));
});

document.querySelectorAll('.delete-domain').forEach(button => {
button.addEventListener('click', () => showDeleteModal(button.dataset.id, button.dataset.name));
});

// 分类下添加域名按钮事件
document.querySelectorAll('.add-domain-to-category').forEach(button => {
button.addEventListener('click', async () => {
const categoryId = button.getAttribute('data-category-id');
await openAddDomainModal(categoryId);
});
});

document.querySelectorAll('.renew-domain').forEach(button => {
button.addEventListener('click', () => showRenewModal(button.dataset.id, button.dataset.name, button.dataset.expiry));
});

// 添加测试通知按钮的事件监听器
document.querySelectorAll('.test-domain-notify').forEach(button => {
button.addEventListener('click', () => testDomainNotification(button.dataset.id));
});

// 添加下拉按钮的事件监听器
document.querySelectorAll('.toggle-details').forEach(button => {
// 监听Bootstrap折叠/展开事件,更新域名显示样式
const collapseTarget = document.querySelector(button.getAttribute('data-bs-target'));
if (collapseTarget) {
// 监听展开事件
collapseTarget.addEventListener('shown.bs.collapse', function() {
// 找到包含此折叠内容的卡片
const domainCard = this.closest('.domain-card');
if (domainCard) {
// 添加expanded类,使域名可以换行显示
domainCard.classList.add('expanded');
}
});

// 监听折叠事件
collapseTarget.addEventListener('hidden.bs.collapse', function() {
// 找到包含此折叠内容的卡片
const domainCard = this.closest('.domain-card');
if (domainCard) {
// 移除expanded类,使域名显示省略号
domainCard.classList.remove('expanded');
}
});
}

// 原有的点击事件处理
button.addEventListener('click', function(e) {
// 如果当前是全部折叠模式,点击切换按钮会切换到自动折叠模式
// 但如果是全部展开模式,则保持该模式
if (viewMode === 'collapse-all') {
// 先阻止默认的bootstrap折叠/展开行为
e.preventDefault();
e.stopPropagation();

// 切换到自动折叠模式
viewMode = 'auto-collapse';

// 获取当前按钮对应的卡片详情
const collapseTarget = document.querySelector(button.getAttribute('data-bs-target'));

// 折叠所有其他卡片
document.querySelectorAll('.collapse.show').forEach(detail => {
if (detail !== collapseTarget) {
bootstrap.Collapse.getInstance(detail)?.hide();
}
});

// 切换当前卡片的状态
const collapseInstance = bootstrap.Collapse.getInstance(collapseTarget);
if (collapseInstance) {
if (collapseTarget.classList.contains('show')) {
collapseInstance.hide();
} else {
collapseInstance.show();
}
}
} else if (viewMode === 'expand-all') {
// 在全部展开模式下,只处理折叠操作,不改变viewMode
const collapseTarget = document.querySelector(button.getAttribute('data-bs-target'));
const collapseInstance = bootstrap.Collapse.getInstance(collapseTarget);

// 只有当用户尝试折叠卡片时才处理
if (collapseTarget.classList.contains('show')) {
// 先阻止默认的bootstrap折叠行为
e.preventDefault();
e.stopPropagation();

// 手动折叠当前卡片
collapseInstance?.hide();
}
// 如果是尝试展开一个已折叠的卡片,使用默认行为
}
// 在自动折叠模式下,使用默认的bootstrap行为
});
});
}

// 保存域名
async function saveDomain() {
const domainId = document.getElementById('domainId').value;
const name = document.getElementById('domainName').value.trim();
const expiryDate = document.getElementById('expiryDate').value;
const registrationDate = document.getElementById('registrationDate').value;
const registrar = document.getElementById('registrar').value;
const categoryId = document.getElementById('domainCategory').value || 'default';
const customNote = document.getElementById('customNote').value;
const noteColor = document.getElementById('noteColor').value;
const renewLink = document.getElementById('renewLink').value;

// 获取续期周期设置
const renewCycleValue = parseInt(document.getElementById('renewCycleValue').value) || 1;
const renewCycleUnit = document.getElementById('renewCycleUnit').value;

// 获取价格设置
const priceValue = document.getElementById('priceValue').value ? parseFloat(document.getElementById('priceValue').value) : null;
const priceCurrency = document.getElementById('priceCurrency').value;
const priceUnit = document.getElementById('priceUnit').value;

// 获取上次续期时间,如果用户清除了则设为null
const lastRenewed = document.getElementById('lastRenewed').value || null;

// 获取通知设置
const useGlobalSettings = document.getElementById('useGlobalSettings').checked;
const notifyEnabled = document.getElementById('notifyEnabled').checked;
const notifyDays = parseInt(document.getElementById('domainNotifyDays').value) || 30;

if (!name || !registrationDate || !expiryDate) {
showAlert('danger', '域名、注册时间和到期日期为必填项');
return;
}

// 确保通知设置字段存在且正确
const notifySettings = {
useGlobalSettings: useGlobalSettings,
enabled: notifyEnabled,
notifyDays: notifyDays
};

// 构建价格对象
const priceObj = priceValue !== null ? {
value: priceValue,
currency: priceCurrency,
unit: priceUnit
} : null;

const domainData = {
name,
expiryDate,
registrationDate,
registrar,
categoryId,
customNote,
noteColor,
renewLink,
lastRenewed,
renewCycle: {
value: renewCycleValue,
unit: renewCycleUnit
},
price: priceObj,
notifySettings: notifySettings
};

try {
let response;
if (domainId) {
// 更新现有域名
domainData.id = domainId;
response = await fetch('/api/domains/' + domainId, {
headers: { 'Content-Type': 'application/json' },
method: 'PUT',
body: JSON.stringify(domainData)
});
} else {
// 添加新域名
response = await fetch('/api/domains', {
headers: { 'Content-Type': 'application/json' },
method: 'POST',
body: JSON.stringify(domainData)
});
}

if (!response.ok) throw new Error('保存域名失败');

// 关闭模态框并重新加载域名列表
bootstrap.Modal.getInstance(document.getElementById('addDomainModal')).hide();
resetForm();
await loadDomains();
showAlert('success', domainId ? '域名更新成功' : '域名添加成功');
} catch (error) {
showAlert('danger', '保存域名失败: ' + error.message);
}
}

// 打开添加域名模态框并预选分类
async function openAddDomainModal(categoryId) {
// 重置表单
resetForm();

// 设置模态框标题为添加模式
document.querySelector('#addDomainModal .modal-title').textContent = '添加域名';

// 确保分类数据已加载
let currentCategories = categories;
if (!currentCategories || currentCategories.length === 0) {
try {
const response = await fetch('/api/categories');
if (response.ok) {
currentCategories = await response.json();
categories = currentCategories; // 同步到全局变量
}
} catch (error) {
// 加载分类失败时静默处理
}
}

// 立即更新分类选择框,传入分类数据和预选分类
updateCategorySelect(currentCategories, categoryId);

// 显示模态框
const modal = new bootstrap.Modal(document.getElementById('addDomainModal'));
modal.show();

// 预选分类(在模态框显示后再次设置确保正确)
modal._element.addEventListener('shown.bs.modal', function() {
updateCategorySelect(currentCategories, categoryId);
}, { once: true });
}

// 编辑域名
function editDomain(id) {
const domain = domains.find(d => d.id === id);
if (!domain) return;

document.getElementById('domainId').value = domain.id;
document.getElementById('domainName').value = domain.name;
document.getElementById('expiryDate').value = domain.expiryDate;
document.getElementById('registrationDate').value = domain.registrationDate !== undefined ? domain.registrationDate : '';
document.getElementById('registrar').value = domain.registrar !== undefined ? domain.registrar : '';
// 设置分类选择
updateCategorySelect(categories, domain.categoryId || 'default');
document.getElementById('customNote').value = domain.customNote !== undefined ? domain.customNote : '';
// 设置标签颜色(如果有)
if (domain.noteColor) {
document.getElementById('noteColor').value = domain.noteColor;
} else {
document.getElementById('noteColor').value = 'tag-blue'; // 默认蓝色
}
document.getElementById('renewLink').value = domain.renewLink !== undefined ? domain.renewLink : '';

// 设置续期周期
if (domain.renewCycle) {
document.getElementById('renewCycleValue').value = domain.renewCycle.value || 1;
document.getElementById('renewCycleUnit').value = domain.renewCycle.unit || 'year';
} else {
document.getElementById('renewCycleValue').value = 1;
document.getElementById('renewCycleUnit').value = 'year';
}

// 设置价格
if (domain.price) {
document.getElementById('priceValue').value = domain.price.value;
document.getElementById('priceCurrency').value = domain.price.currency || '¥';
document.getElementById('priceUnit').value = domain.price.unit || 'year';
} else {
document.getElementById('priceValue').value = '';
document.getElementById('priceCurrency').value = '¥';
document.getElementById('priceUnit').value = 'year';
}

// 显示上次续期时间(如果有)
const lastRenewedContainer = document.getElementById('lastRenewedContainer');
const lastRenewedDisplay = document.getElementById('lastRenewedDisplay');
const lastRenewed = document.getElementById('lastRenewed');

if (domain.lastRenewed) {
lastRenewedContainer.style.display = 'block';
lastRenewedDisplay.textContent = formatDate(domain.lastRenewed);
lastRenewed.value = domain.lastRenewed;
} else {
lastRenewedContainer.style.display = 'none';
lastRenewedDisplay.textContent = '';
lastRenewed.value = '';
}

// 设置通知选项
const notifySettings = domain.notifySettings || { useGlobalSettings: true, enabled: true, notifyDays: 30 };
document.getElementById('useGlobalSettings').checked = notifySettings.useGlobalSettings;
document.getElementById('notifyEnabled').checked = notifySettings.enabled;
document.getElementById('domainNotifyDays').value = notifySettings.notifyDays || 30;
document.getElementById('domainNotifySettings').style.display = notifySettings.useGlobalSettings ? 'none' : 'block';

document.querySelector('#addDomainModal .modal-title').textContent = '编辑域名';
const modal = new bootstrap.Modal(document.getElementById('addDomainModal'));
modal.show();

// 在编辑模式下的设置
setTimeout(() => {
// 等待模态框完全显示后执行
document.getElementById('expiryDate').removeAttribute('readonly');
const expiryLabel = document.querySelector('label[for="expiryDate"]');
if (expiryLabel) {
expiryLabel.innerHTML = '<i class="iconfont icon-calendar-days"></i> 到期日期 <span style="color: red;">*</span>';
const helpText = expiryLabel.nextElementSibling && expiryLabel.nextElementSibling.nextElementSibling;
if (helpText) {
helpText.textContent = '根据注册时间和续期周期自动计算,可手动调整';
helpText.className = 'form-text text-info';
}
}
updateNotePreview(); // 更新备注预览
}, 100);
}

// 显示删除确认模态框
function showDeleteModal(id, name) {
currentDomainId = id;
document.getElementById('deleteModalDomainName').textContent = name;
const modal = new bootstrap.Modal(document.getElementById('deleteDomainModal'));
modal.show();
}

// 删除域名
async function deleteDomain() {
if (!currentDomainId) return;

try {
const response = await fetch('/api/domains/' + currentDomainId, {
method: 'DELETE'
});

if (!response.ok) throw new Error('删除域名失败');

// 关闭模态框并重新加载域名列表
bootstrap.Modal.getInstance(document.getElementById('deleteDomainModal')).hide();
currentDomainId = null;
await loadDomains();
showAlert('success', '域名删除成功');
} catch (error) {
showAlert('danger', '删除域名失败: ' + error.message);
}
}

// 显示续期模态框
function showRenewModal(id, name, expiryDate) {
currentDomainId = id;
document.getElementById('renewModalDomainName').textContent = name;

// 获取域名的续期周期设置
const domain = domains.find(d => d.id === id);
if (domain && domain.renewCycle) {
document.getElementById('renewPeriodValue').value = domain.renewCycle.value;
document.getElementById('renewPeriodUnit').value = domain.renewCycle.unit;
} else {
document.getElementById('renewPeriodValue').value = 1;
document.getElementById('renewPeriodUnit').value = 'year';
}

// 计算新的到期日期
updateNewExpiryDate();

const modal = new bootstrap.Modal(document.getElementById('renewDomainModal'));
modal.show();
}

// 更新新到期日期
function updateNewExpiryDate() {
const domain = domains.find(d => d.id === currentDomainId);
if (!domain) return;

const renewValue = parseInt(document.getElementById('renewPeriodValue').value) || 1;
const renewUnit = document.getElementById('renewPeriodUnit').value;

// 无论域名是否过期,都从原先的到期日期开始计算
const expiryDate = new Date(domain.expiryDate);
const newExpiryDate = new Date(expiryDate);

// 根据选择的单位添加时间
switch(renewUnit) {
case 'year':
newExpiryDate.setFullYear(expiryDate.getFullYear() + renewValue);
break;
case 'month':
newExpiryDate.setMonth(expiryDate.getMonth() + renewValue);
break;
case 'day':
newExpiryDate.setDate(expiryDate.getDate() + renewValue);
break;
}

document.getElementById('newExpiryDate').value = newExpiryDate.toISOString().split('T')[0];
}

// 续期域名
async function renewDomain() {
if (!currentDomainId) return;

const renewValue = parseInt(document.getElementById('renewPeriodValue').value) || 1;
const renewUnit = document.getElementById('renewPeriodUnit').value;
const newExpiryDate = document.getElementById('newExpiryDate').value;

try {
const response = await fetch('/api/domains/' + currentDomainId + '/renew', {
headers: { 'Content-Type': 'application/json' },
method: 'POST',
body: JSON.stringify({
value: renewValue,
unit: renewUnit,
newExpiryDate
})
});

if (!response.ok) throw new Error('域名续期失败');

// 关闭模态框并重新加载域名列表
bootstrap.Modal.getInstance(document.getElementById('renewDomainModal')).hide();
currentDomainId = null;
await loadDomains();
showAlert('success', '域名续期成功');
} catch (error) {
showAlert('danger', '域名续期失败: ' + error.message);
}
}

// 重置表单
function resetForm() {
document.getElementById('domainId').value = '';
document.getElementById('domainName').value = '';
document.getElementById('expiryDate').value = '';
document.getElementById('registrationDate').value = '';
document.getElementById('registrar').value = '';
document.getElementById('customNote').value = '';
document.getElementById('noteColor').value = 'tag-blue'; // 重置为默认蓝色
document.getElementById('renewLink').value = '';

// 重置续期周期设置
document.getElementById('renewCycleValue').value = '1';
document.getElementById('renewCycleUnit').value = 'year';

// 重置价格设置
document.getElementById('priceValue').value = '';
document.getElementById('priceCurrency').value = '¥';
document.getElementById('priceUnit').value = 'year';

// 重置上次续期时间
document.getElementById('lastRenewed').value = '';
document.getElementById('lastRenewedContainer').style.display = 'none';
document.getElementById('lastRenewedDisplay').textContent = '';
document.getElementById('lastRenewedDisplay').classList.remove('text-danger');

// 重置通知设置
document.getElementById('useGlobalSettings').checked = true;
document.getElementById('notifyEnabled').checked = true;
document.getElementById('domainNotifyDays').value = '30';
document.getElementById('domainNotifySettings').style.display = 'none';

// 重置到期日期字段状态(添加新域名时保持可编辑)
document.getElementById('expiryDate').removeAttribute('readonly');
const expiryLabel = document.querySelector('label[for="expiryDate"]');
if (expiryLabel) {
expiryLabel.innerHTML = '<i class="iconfont icon-calendar-days"></i> 到期日期 <span style="color: red;">*</span>';
const helpText = expiryLabel.nextElementSibling && expiryLabel.nextElementSibling.nextElementSibling;
if (helpText) {
helpText.textContent = '根据注册时间和续期周期自动计算,可手动调整';
helpText.className = 'form-text text-info';
}
}

// 重置WHOIS查询状态
const whoisStatus = document.getElementById('whoisQueryStatus');
if (whoisStatus) {
whoisStatus.style.display = 'none';
whoisStatus.innerHTML = '';
}

document.querySelector('#addDomainModal .modal-title').textContent = '添加新域名';
}

// ================================
// 分类管理功能
// ================================

// 全局变量存储分类数据
let categories = [];

// 加载分类数据
async function loadCategories() {
try {
const response = await fetch('/api/categories');
if (response.ok) {
categories = await response.json();
updateCategorySelect();
// 只有在分类管理模态框存在时才渲染分类列表
if (document.getElementById('categoryList')) {
renderCategoryList();
}
}
} catch (error) {
// 加载分类失败时静默处理
}
}

// 更新分类选择下拉框
function updateCategorySelect(categoryData = null, selectedCategoryId = null) {
const categorySelect = document.getElementById('domainCategory');
if (!categorySelect) return;

// 使用传入的数据或全局数据
const dataToUse = categoryData || categories;

// 清空现有选项
categorySelect.innerHTML = '';

// 检查分类数据是否存在
if (!dataToUse || !Array.isArray(dataToUse)) {
return;
}

// 首先添加默认分类
const defaultCategory = dataToUse.find(cat => cat.isDefault);
if (defaultCategory) {
const option = document.createElement('option');
option.value = defaultCategory.id;
option.textContent = defaultCategory.name;
// 如果没有指定选择的分类,或者指定的就是默认分类,则选中默认分类
option.selected = !selectedCategoryId || selectedCategoryId === defaultCategory.id;
categorySelect.appendChild(option);
}

// 然后添加用户自定义分类
dataToUse.filter(cat => !cat.isDefault).forEach(category => {
const option = document.createElement('option');
option.value = category.id;
option.textContent = category.name;
// 如果指定的分类是当前分类,则选中
option.selected = selectedCategoryId === category.id;
categorySelect.appendChild(option);
});
}

// 渲染分类列表
function renderCategoryList() {
const categoryList = document.getElementById('categoryList');
if (!categoryList) return;

if (categories.length === 0) {
categoryList.innerHTML = '<div class="text-center p-3 text-muted">暂无分类</div>';
return;
}

categoryList.innerHTML = '';

// 筛选出非默认分类
const userCategories = categories.filter(cat => !cat.isDefault);

if (userCategories.length === 0) {
categoryList.innerHTML = '<div class="text-center p-3 text-muted">暂无自定义分类</div>';
return;
}

userCategories.forEach((category, index) => {
const categoryItem = document.createElement('div');
categoryItem.className = 'mb-3 p-3 rounded category-item';
categoryItem.style.cssText = 'background: rgba(255, 255, 255, 0.1); border: 1px solid rgba(255, 255, 255, 0.2); backdrop-filter: blur(10px);';
categoryItem.innerHTML =
'<div class="d-flex justify-content-between align-items-start">' +
'<div class="flex-grow-1">' +
'<h6 class="mb-1 fw-bold text-white">' + category.name + '</h6>' +
'<small class="text-light opacity-75">' + (category.description || '无描述') + '</small>' +
'</div>' +
'<div class="d-flex gap-2 ms-3">' +
'<button type="button" class="btn btn-outline-light move-category-up" data-id="' + category.id + '" ' + (index === 0 ? 'disabled' : '') + ' title="上移" style="width: 32px; height: 32px; padding: 0; display: flex; align-items: center; justify-content: center; border-radius: 6px; line-height: 1;">' +
'<i class="iconfont icon-shangjiantou1" style="color: white; font-size: 14px; display: block; line-height: 1; margin: 0; vertical-align: middle;"></i>' +
'</button>' +
'<button type="button" class="btn btn-outline-light move-category-down" data-id="' + category.id + '" ' + (index === userCategories.length - 1 ? 'disabled' : '') + ' title="下移" style="width: 32px; height: 32px; padding: 0; display: flex; align-items: center; justify-content: center; border-radius: 6px; line-height: 1;">' +
'<i class="iconfont icon-xiajiantou1" style="color: white; font-size: 14px; display: block; line-height: 1; margin: 0; vertical-align: middle;"></i>' +
'</button>' +
'<button type="button" class="btn btn-primary edit-category" data-id="' + category.id + '" title="编辑" style="width: 32px; height: 32px; padding: 0; display: flex; align-items: center; justify-content: center; border-radius: 6px; line-height: 1;">' +
'<i class="iconfont icon-pencil" style="color: white; font-size: 14px; display: block; line-height: 1; margin: 0; vertical-align: middle;"></i>' +
'</button>' +
'<button type="button" class="btn btn-outline-danger delete-category" data-id="' + category.id + '" title="删除" style="width: 32px; height: 32px; padding: 0; display: flex; align-items: center; justify-content: center; border-radius: 6px; line-height: 1;">' +
'<i class="iconfont icon-shanchu" style="color: white; font-size: 14px; display: block; line-height: 1; margin: 0; vertical-align: middle;"></i>' +
'</button>' +
'</div>' +
'</div>';
categoryList.appendChild(categoryItem);
});

// 添加事件监听器
bindCategoryEvents();
}

// 绑定分类管理事件
function bindCategoryEvents() {
// 编辑分类
document.querySelectorAll('.edit-category').forEach(btn => {
btn.addEventListener('click', function() {
const categoryId = this.dataset.id;
editCategory(categoryId);
});
});

// 上移分类
document.querySelectorAll('.move-category-up').forEach(btn => {
btn.addEventListener('click', function() {
const categoryId = this.dataset.id;
moveCategoryOrder(categoryId, 'up');
});
});

// 下移分类
document.querySelectorAll('.move-category-down').forEach(btn => {
btn.addEventListener('click', function() {
const categoryId = this.dataset.id;
moveCategoryOrder(categoryId, 'down');
});
});

// 删除分类
document.querySelectorAll('.delete-category').forEach(btn => {
btn.addEventListener('click', function() {
const categoryId = this.dataset.id;
deleteCategory(categoryId);
});
});
}

// 添加分类
async function addCategory() {
const nameInput = document.getElementById('categoryName');
const descInput = document.getElementById('categoryDescription');

if (!nameInput || !descInput) {
return;
}

const name = nameInput.value.trim();
const description = descInput.value.trim();

if (!name) {
showAlert('danger', '分类名称不能为空');
return;
}

try {
const response = await fetch('/api/categories', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, description })
});

if (!response.ok) {
const error = await response.json();
throw new Error(error.error || '添加分类失败');
}

// 清空输入框
if (nameInput) nameInput.value = '';
if (descInput) descInput.value = '';

// 重新加载分类列表和域名列表
await loadCategories();
await loadDomains(); // 刷新域名列表以更新分类选择框
showAlert('success', '分类添加成功');
} catch (error) {
showAlert('danger', error.message);
}
}

// 编辑分类
function editCategory(categoryId) {
const category = categories.find(cat => cat.id === categoryId);
if (!category) return;

// 找到对应的分类项目
const categoryItems = document.querySelectorAll('#categoryList .category-item');
let targetItem = null;

categoryItems.forEach(item => {
if (item.querySelector('[data-id="' + categoryId + '"]')) {
targetItem = item;
}
});

if (!targetItem) return;

// 保存原始内容
const originalContent = targetItem.innerHTML;

// 替换为编辑表单
targetItem.innerHTML =
'<div class="p-3 rounded" style="background: rgba(255, 255, 255, 0.1); border: 1px solid rgba(255, 255, 255, 0.2);">' +
'<div class="mb-3">' +
'<label class="form-label text-white small">分类名称</label>' +
'<input type="text" class="form-control form-control-sm" id="editName_' + categoryId + '" value="' + category.name.replace(/"/g, '&quot;') + '" maxlength="50">' +
'</div>' +
'<div class="mb-3">' +
'<label class="form-label text-white small">描述</label>' +
'<input type="text" class="form-control form-control-sm" id="editDesc_' + categoryId + '" value="' + (category.description || '').replace(/"/g, '&quot;') + '" maxlength="100">' +
'</div>' +
'<div class="d-flex gap-2">' +
'<button type="button" class="btn btn-success btn-sm save-edit" data-id="' + categoryId + '">' +
'<i class="iconfont icon-check" style="color: white;"></i> <span style="color: white;">保存</span>' +
'</button>' +
'<button type="button" class="btn btn-secondary btn-sm cancel-edit" data-id="' + categoryId + '">' +
'<i class="iconfont icon-xmark" style="color: white;"></i> <span style="color: white;">取消</span>' +
'</button>' +
'</div>' +
'</div>';

// 添加保存按钮事件
targetItem.querySelector('.save-edit').addEventListener('click', function() {
const nameInput = document.getElementById('editName_' + categoryId);
const descInput = document.getElementById('editDesc_' + categoryId);

const newName = nameInput.value.trim();
const newDesc = descInput.value.trim();

if (!newName) {
showAlert('danger', '分类名称不能为空');
nameInput.focus();
return;
}

updateCategory(categoryId, newName, newDesc);
});

// 添加取消按钮事件
targetItem.querySelector('.cancel-edit').addEventListener('click', function() {
targetItem.innerHTML = originalContent;
bindCategoryEvents(); // 重新绑定事件
});

// 聚焦到名称输入框
document.getElementById('editName_' + categoryId).focus();
}

// 更新分类
async function updateCategory(categoryId, name, description) {
try {
const response = await fetch('/api/categories/' + categoryId, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, description })
});

if (!response.ok) {
const error = await response.json();
throw new Error(error.error || '更新分类失败');
}

await loadCategories();
await loadDomains(); // 同时刷新域名列表,因为分类名称变化会影响显示
showAlert('success', '分类更新成功');
} catch (error) {
showAlert('danger', error.message);
}
}

// 显示删除分类确认模态框
function deleteCategory(categoryId) {
const category = categories.find(cat => cat.id === categoryId);
if (!category) return;

currentCategoryId = categoryId;
document.getElementById('deleteCategoryModalName').textContent = category.name;

// 重置勾选框状态
const checkbox = document.getElementById('confirmDeleteCheckbox');
const deleteBtn = document.getElementById('confirmDeleteCategoryBtn');
checkbox.checked = false;
deleteBtn.disabled = true;

const modal = new bootstrap.Modal(document.getElementById('deleteCategoryModal'));
modal.show();
}

// 确认删除分类
async function confirmDeleteCategory() {
if (!currentCategoryId) return;

try {
const response = await fetch('/api/categories/' + currentCategoryId, {
method: 'DELETE'
});

if (!response.ok) {
const error = await response.json();
throw new Error(error.error || '删除分类失败');
}

// 关闭模态框
bootstrap.Modal.getInstance(document.getElementById('deleteCategoryModal')).hide();
currentCategoryId = null;

await loadCategories();
await loadDomains(); // 重新加载域名以更新显示
showAlert('success', '分类删除成功');
} catch (error) {
showAlert('danger', error.message);
}
}

// 移动分类顺序
async function moveCategoryOrder(categoryId, direction) {
try {
const response = await fetch('/api/categories/' + categoryId + '/move', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ direction })
});

if (!response.ok) {
const error = await response.json();
throw new Error(error.error || '移动分类失败');
}

await loadCategories();
await loadDomains(); // 同时刷新域名列表,因为分类顺序变化会影响显示
showAlert('success', '分类顺序更新成功');
} catch (error) {
showAlert('danger', error.message);
}
}

// 显示WHOIS查询状态
function showWhoisStatus(message, type = 'info') {
const statusDiv = document.getElementById('whoisQueryStatus');
statusDiv.style.display = 'block';
statusDiv.className = 'alert alert-' + type + ' py-2';
statusDiv.innerHTML = '<i class="iconfont icon-' + (type === 'info' ? 'loading' : type === 'success' ? 'check-circle' : 'exclamation-circle') + '"></i> ' + message;

// 3秒后自动隐藏非错误消息
if (type !== 'danger') {
setTimeout(() => {
statusDiv.style.display = 'none';
}, 3000);
}
}

// 执行WHOIS查询并填充表单
async function performWhoisQuery(domain, controller) {
const queryBtn = document.getElementById('whoisQueryBtn');
const originalText = queryBtn.innerHTML;

try {
// 显示查询中状态
queryBtn.disabled = true;
queryBtn.innerHTML = '<i class="spinner-border spinner-border-sm me-2"></i>查询中...';
showWhoisStatus('正在查询域名信息,请稍候...', 'info');

// 调用后端API,支持取消
const response = await fetch('/api/whois', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ domain: domain }),
signal: controller.signal
});

const result = await response.json();

if (!response.ok) {
throw new Error(result.error || 'API请求失败');
}

if (result.success) {
// 查询成功,填充表单数据
const fillResult = fillFormWithWhoisData(result);
if (fillResult) {
showWhoisStatus('域名信息查询成功,已自动填充相关字段', 'success');
}
} else {
// 查询失败
showWhoisStatus('查询失败: ' + (result.error || '未知错误'), 'danger');
}

} catch (error) {
// 如果是用户取消的请求,不显示错误信息
if (error.name === 'AbortError') {
showWhoisStatus('查询已取消', 'info');
} else {
showWhoisStatus('查询失败: ' + error.message, 'danger');
}
} finally {
// 恢复按钮状态
queryBtn.disabled = false;
queryBtn.innerHTML = originalText;

// 清除控制器引用
if (currentWhoisController === controller) {
currentWhoisController = null;
}
}
}

// 使用WHOIS数据填充表单
function fillFormWithWhoisData(whoisData) {
let filledFields = []; // 记录成功填充的字段
let missingFields = []; // 记录缺失的字段

// 清除之前的高亮样式
document.getElementById('registrar').classList.remove('auto-filled');
document.getElementById('registrationDate').classList.remove('auto-filled');
document.getElementById('expiryDate').classList.remove('auto-filled');
document.getElementById('renewCycleValue').classList.remove('auto-filled');
document.getElementById('renewCycleUnit').classList.remove('auto-filled');

// 填充注册商
if (whoisData.registrar) {
const registrarField = document.getElementById('registrar');
registrarField.value = whoisData.registrar;
registrarField.classList.add('auto-filled');
filledFields.push('注册商');
} else {
missingFields.push('注册商');
}

// 填充注册日期
if (whoisData.registrationDate) {
const registrationDateField = document.getElementById('registrationDate');
registrationDateField.value = whoisData.registrationDate;
registrationDateField.classList.add('auto-filled');
filledFields.push('注册日期');
} else {
missingFields.push('注册日期');
}

// 填充到期日期
if (whoisData.expiryDate) {
const expiryDateField = document.getElementById('expiryDate');
expiryDateField.value = whoisData.expiryDate;
expiryDateField.classList.add('auto-filled');
filledFields.push('到期日期');
} else {
missingFields.push('到期日期');
}

// 自动计算续期周期(仅当有注册日期和到期日期时)
if (whoisData.registrationDate && whoisData.expiryDate) {
calculateRenewCycle(whoisData.registrationDate, whoisData.expiryDate);
// 为自动计算的续期周期添加高亮效果
document.getElementById('renewCycleValue').classList.add('auto-filled');
document.getElementById('renewCycleUnit').classList.add('auto-filled');
filledFields.push('续期周期');
}

// 根据填充结果显示相应提示
if (filledFields.length > 0) {
let message = '已自动填充: ' + filledFields.join('、');
if (missingFields.length > 0) {
message += ';未查询到: ' + missingFields.join('、');
}
showWhoisStatus(message, 'success');
return true;
} else {
showWhoisStatus('未能获取到任何有效的域名信息', 'warning');
return false;
}
}

// 根据注册时间和到期时间计算续期周期
function calculateRenewCycle(registrationDate, expiryDate) {
try {
const regDate = new Date(registrationDate);
const expDate = new Date(expiryDate);

// 计算时间差(毫秒)
const timeDiff = expDate.getTime() - regDate.getTime();

// 转换为天数
const daysDiff = Math.round(timeDiff / (1000 * 60 * 60 * 24));

// 根据天数推算续期周期
if (daysDiff >= 360 && daysDiff <= 370) {
// 1年 (365天左右,考虑闰年)
document.getElementById('renewCycleValue').value = '1';
document.getElementById('renewCycleUnit').value = 'year';
} else if (daysDiff >= 720 && daysDiff <= 740) {
// 2年
document.getElementById('renewCycleValue').value = '2';
document.getElementById('renewCycleUnit').value = 'year';
} else if (daysDiff >= 1080 && daysDiff <= 1110) {
// 3年
document.getElementById('renewCycleValue').value = '3';
document.getElementById('renewCycleUnit').value = 'year';
} else if (daysDiff >= 1800 && daysDiff <= 1830) {
// 5年
document.getElementById('renewCycleValue').value = '5';
document.getElementById('renewCycleUnit').value = 'year';
} else if (daysDiff >= 3600 && daysDiff <= 3670) {
// 10年
document.getElementById('renewCycleValue').value = '10';
document.getElementById('renewCycleUnit').value = 'year';
} else if (daysDiff >= 28 && daysDiff <= 31) {
// 1个月
document.getElementById('renewCycleValue').value = '1';
document.getElementById('renewCycleUnit').value = 'month';
} else if (daysDiff >= 85 && daysDiff <= 95) {
// 3个月
document.getElementById('renewCycleValue').value = '3';
document.getElementById('renewCycleUnit').value = 'month';
} else if (daysDiff >= 175 && daysDiff <= 185) {
// 6个月
document.getElementById('renewCycleValue').value = '6';
document.getElementById('renewCycleUnit').value = 'month';
} else {
// 其他情况,计算最接近的年数
const years = Math.round(daysDiff / 365);
if (years >= 1) {
document.getElementById('renewCycleValue').value = years.toString();
document.getElementById('renewCycleUnit').value = 'year';
} else {
// 小于1年的情况,按月计算
const months = Math.round(daysDiff / 30);
document.getElementById('renewCycleValue').value = Math.max(1, months).toString();
document.getElementById('renewCycleUnit').value = 'month';
}
}
} catch (error) {
// 出错时使用默认值
document.getElementById('renewCycleValue').value = '1';
document.getElementById('renewCycleUnit').value = 'year';
}
}

// 显示提示信息
function showAlert(type, message) {
const alertDiv = document.createElement('div');
alertDiv.className = 'alert alert-' + type + ' alert-dismissible fade show position-fixed top-0 start-50 translate-middle-x mt-3';
alertDiv.style.zIndex = '9999';
alertDiv.style.boxShadow = '0 4px 12px rgba(0,0,0,0.15)';
alertDiv.style.borderRadius = '8px';
alertDiv.style.minWidth = '300px';
alertDiv.style.maxWidth = '80%';

// 根据消息类型选择合适的图标
let iconClass = '';
switch(type) {
case 'success':
iconClass = 'icon-success';
break;
case 'danger':
iconClass = 'icon-error';
break;
case 'warning':
iconClass = 'icon-warning';
break;
case 'info':
iconClass = 'icon-info';
break;
}

alertDiv.innerHTML = '<i class="iconfont ' + iconClass + '"></i> ' + message +
'<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>';
document.body.appendChild(alertDiv);

// 3秒后自动消失
setTimeout(() => {
alertDiv.classList.remove('show');
setTimeout(() => alertDiv.remove(), 300);
}, 3000);
}



// 添加测试单个域名通知的函数
async function testDomainNotification(domainId) {
try {
const response = await fetch('/api/domains/' + domainId + '/test-notify', {
method: 'POST'
});

if (!response.ok) {
const error = await response.json();
throw new Error(error.error || '测试失败');
}

const result = await response.json();
showAlert('success', '通知测试成功!请检查Telegram是否收到消息');
} catch (error) {
showAlert('danger', '测试通知失败: ' + error.message);
}
}

// 按照指定字段和顺序排序域名
function sortDomains(domains, field, order) {
domains.sort((a, b) => {
let valueA, valueB;

// 根据字段提取排序值
switch (field) {
case 'name':
valueA = a.name.toLowerCase();
valueB = b.name.toLowerCase();
break;
case 'suffix':
// 获取域名后缀并反转字符串用于排序(从后往前排序)
const getSuffixForSort = (domain) => {
return domain.toLowerCase().split('').reverse().join('');
};
valueA = getSuffixForSort(a.name);
valueB = getSuffixForSort(b.name);
break;
case 'customNote':
valueA = (a.customNote || '').toLowerCase();
valueB = (b.customNote || '').toLowerCase();
break;
case 'expiryDate':
valueA = new Date(a.expiryDate).getTime();
valueB = new Date(b.expiryDate).getTime();
break;
case 'daysLeft':
valueA = a.daysLeft;
valueB = b.daysLeft;
break;
case 'notifyDays':
const notifySettingsA = a.notifySettings || { useGlobalSettings: true, notifyDays: 30 };
const notifySettingsB = b.notifySettings || { useGlobalSettings: true, notifyDays: 30 };
valueA = notifySettingsA.useGlobalSettings ? (telegramConfig.notifyDays || 30) : notifySettingsA.notifyDays;
valueB = notifySettingsB.useGlobalSettings ? (telegramConfig.notifyDays || 30) : notifySettingsB.notifyDays;
break;
default:
valueA = a.daysLeft;
valueB = b.daysLeft;
}

// 根据排序顺序返回比较结果
if (order === 'asc') {
return valueA > valueB ? 1 : valueA < valueB ? -1 : 0;
} else {
return valueA < valueB ? 1 : valueA > valueB ? -1 : 0;
}
});
}
</script>
</body>
</html>
`;

// ================================
// 主请求处理函数
// ================================

// 处理请求
async function handleRequest(request) {
const url = new URL(request.url);
const path = url.pathname;

// 配置检测API(在KV检查之前处理)
if (path === '/api/check-setup' && request.method === 'GET') {
return await checkSetupStatus();
}

// 检查是否已配置KV空间
if (!isKVConfigured()) {
if (path === '/setup-complete') {
return Response.redirect(url.origin, 302);
}
return new Response(getSetupHTML(), {
headers: { 'Content-Type': 'text/html;charset=UTF-8' },
});
}

// 获取标题
let siteTitle = '域名到期监控';
if (typeof SITE_NAME !== 'undefined' && SITE_NAME) {
siteTitle = SITE_NAME;
} else if (DEFAULT_SITE_NAME) {
siteTitle = DEFAULT_SITE_NAME;
}

// 1. 获取正确的账号 (新增)
let correctUsername = 'admin';
if (typeof USERNAME !== 'undefined' && USERNAME) {
correctUsername = USERNAME;
} else if (DEFAULT_USERNAME) {
correctUsername = DEFAULT_USERNAME;
}

// 2. 获取正确的密码
let correctPassword = 'domain';
if (typeof TOKEN !== 'undefined' && TOKEN) {
correctPassword = TOKEN;
} else if (DEFAULT_TOKEN) {
correctPassword = DEFAULT_TOKEN;
}

// 3. 检查是否已经登录 (修改版:比对哈希值)
const cookieHeader = request.headers.get('Cookie') || '';
// 计算正确账号密码的哈希指纹
const correctAuthHash = await generateHash(`${correctUsername}:${correctPassword}`);
// 检查 Cookie 中是否包含这个指纹
const isAuthenticated = cookieHeader.includes(`auth=${correctAuthHash}`);

// 处理登录POST请求
if (path === '/login' && request.method === 'POST') {
try {
const requestData = await request.json();
const submittedUsername = requestData.username; // 获取前端传来的账号
const submittedPassword = requestData.password;

// 验证账号 AND 密码
if (submittedUsername === correctUsername && submittedPassword === correctPassword) {
// 1. 计算要存入 Cookie 的哈希值
const authHash = await generateHash(`${submittedUsername}:${submittedPassword}`);

// 2. 写入 Cookie (存的是哈希,不再是明文)
return new Response(JSON.stringify({ success: true }), {
status: 200,
headers: {
'Content-Type': 'application/json',
// 这里把原来的账号密码变量换成了 authHash
'Set-Cookie': `auth=${authHash}; Path=/; HttpOnly; SameSite=Strict; Max-Age=86400`,
},
});
} else {
// === 新增:防爆破延迟 ===
// 强制等待 2 秒(2000毫秒),让暴力破解变慢
await new Promise(resolve => setTimeout(resolve, 2000));

// 账号或密码错误
return new Response(JSON.stringify({ success: false, error: '账号或密码错误' }), {
status: 401,
headers: {
'Content-Type': 'application/json',
},
});
}
} catch (error) {
return new Response(JSON.stringify({ success: false, error: '请求格式错误' }), {
status: 400,
headers: {
'Content-Type': 'application/json',
},
});
}
}

// 处理dashboard页面请求
if (path === '/dashboard') {
if (isAuthenticated) {
// 已登录,显示主页面
const htmlContent = getHTMLContent(siteTitle);
const response = new Response(htmlContent, {
headers: {
'Content-Type': 'text/html;charset=UTF-8',
},
});

return response;
} else {
// 未登录,重定向到登录页面
return Response.redirect(url.origin, 302);
}
}

// 登出功能
if (path === '/logout') {
return new Response('登出成功', {
status: 302,
headers: {
'Location': '/',
'Set-Cookie': 'auth=; Path=/; HttpOnly; SameSite=Strict; Max-Age=0', // 清除cookie
},
});
}

// 根路径或任何其他路径(除了/api和/dashboard)都显示登录页面
if (path === '/' || (!path.startsWith('/api/') && path !== '/dashboard')) {
// 如果已登录,重定向到dashboard
if (isAuthenticated) {
return Response.redirect(url.origin + '/dashboard', 302);
}

// 修复:这里不能传env,要手动构建一个对象传进去
const loginHtml = getLoginHTML({ SITE_TITLE: siteTitle });
return new Response(loginHtml, {
headers: {
'Content-Type': 'text/html;charset=UTF-8',
},
});
}

// API 路由处理
if (path.startsWith('/api/')) {
// 检查是否已登录
if (!isAuthenticated) {
return jsonResponse({ error: '未授权访问', success: false }, 401);
}

return await handleApiRequest(request);
}

// 如果都不匹配,返回登录页面
const loginHtml = getLoginHTML({ SITE_TITLE: siteTitle });
return new Response(loginHtml, {
headers: {
'Content-Type': 'text/html;charset=UTF-8',
},
});
}

// ================================
// API处理函数区域
// ================================

// 处理API请求
async function handleApiRequest(request) {
const url = new URL(request.url);
const path = url.pathname;

// 获取所有域名
if (path === '/api/domains' && request.method === 'GET') {
try {
const domains = await getDomains();
return jsonResponse(domains);
} catch (error) {
return jsonResponse({ error: '获取域名列表失败' }, 500);
}
}

// 添加新域名
if (path === '/api/domains' && request.method === 'POST') {
try {
const domainData = await request.json();
const domain = await addDomain(domainData);
return jsonResponse(domain, 201);
} catch (error) {
return jsonResponse({ error: '添加域名失败' }, 400);
}
}

// 更新域名
if (path.match(/^\/api\/domains\/[^\/]+$/) && request.method === 'PUT') {
const id = path.split('/').pop();
try {
const domainData = await request.json();
const domain = await updateDomain(id, domainData);
return jsonResponse(domain);
} catch (error) {
return jsonResponse({ error: '更新域名失败' }, 400);
}
}

// 删除域名
if (path.match(/^\/api\/domains\/[^\/]+$/) && request.method === 'DELETE') {
const id = path.split('/').pop();
try {
await deleteDomain(id);
return jsonResponse({ success: true });
} catch (error) {
return jsonResponse({ error: '删除域名失败' }, 400);
}
}

// 域名续期
if (path.match(/^\/api\/domains\/[^\/]+\/renew$/) && request.method === 'POST') {
const id = path.split('/')[3];
try {
const renewData = await request.json();
const domain = await renewDomain(id, renewData);
return jsonResponse(domain);
} catch (error) {
return jsonResponse({ error: '域名续期失败' }, 400);
}
}

// 获取Telegram配置
if (path === '/api/telegram/config' && request.method === 'GET') {
try {
const config = await getTelegramConfig();
return jsonResponse(config);
} catch (error) {
return jsonResponse({ error: '获取Telegram配置失败' }, 500);
}
}

// 保存Telegram配置
if (path === '/api/telegram/config' && request.method === 'POST') {
try {
const configData = await request.json();
const config = await saveTelegramConfig(configData);
return jsonResponse(config);
} catch (error) {
return jsonResponse({ error: '保存Telegram配置失败: ' + error.message }, 400);
}
}

// 测试Telegram通知
if (path === '/api/telegram/test' && request.method === 'POST') {
try {
const result = await testTelegramNotification();
return jsonResponse(result);
} catch (error) {
return jsonResponse({ error: '测试Telegram通知失败: ' + error.message }, 400);
}
}

// 测试单个域名的通知
if (path.match(/^\/api\/domains\/[^\/]+\/test-notify$/) && request.method === 'POST') {
const id = path.split('/')[3];
try {
const result = await testSingleDomainNotification(id);
return jsonResponse(result);
} catch (error) {
return jsonResponse({ error: '测试通知失败: ' + error.message }, 400);
}
}

// ================================
// 分类管理API
// ================================

// 获取所有分类
if (path === '/api/categories' && request.method === 'GET') {
try {
const categories = await getCategories();
return jsonResponse(categories);
} catch (error) {
return jsonResponse({ error: '获取分类列表失败' }, 500);
}
}

// 添加新分类
if (path === '/api/categories' && request.method === 'POST') {
try {
const categoryData = await request.json();
const category = await addCategory(categoryData);
return jsonResponse(category, 201);
} catch (error) {
return jsonResponse({ error: error.message || '添加分类失败' }, 400);
}
}

// 更新分类
if (path.match(/^\/api\/categories\/[^\/]+$/) && request.method === 'PUT') {
const id = path.split('/').pop();
try {
const categoryData = await request.json();
const category = await updateCategory(id, categoryData);
return jsonResponse(category);
} catch (error) {
return jsonResponse({ error: error.message || '更新分类失败' }, 400);
}
}

// 删除分类
if (path.match(/^\/api\/categories\/[^\/]+$/) && request.method === 'DELETE') {
const id = path.split('/').pop();
try {
await deleteCategory(id);
return jsonResponse({ success: true });
} catch (error) {
return jsonResponse({ error: error.message || '删除分类失败' }, 400);
}
}

// 分类排序(上移/下移)
if (path.match(/^\/api\/categories\/[^\/]+\/move$/) && request.method === 'POST') {
const id = path.split('/')[3];
try {
const { direction } = await request.json();
await moveCategoryOrder(id, direction);
return jsonResponse({ success: true });
} catch (error) {
return jsonResponse({ error: error.message || '移动分类失败' }, 400);
}
}

// WHOIS域名查询
if (path === '/api/whois' && request.method === 'POST') {
try {
const { domain } = await request.json();
if (!domain) {
return jsonResponse({ error: '域名参数不能为空' }, 400);
}

// 验证域名格式
const domainRegex = /^[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?)*$/;
if (!domainRegex.test(domain)) {
return jsonResponse({ error: '仅支持自动查询一级域名' }, 400);
}

// 验证是否为一级域名(只能有一个点)
const dotCount = domain.split('.').length - 1;
if (dotCount !== 1) {
if (dotCount === 0) {
return jsonResponse({ error: '请输入完整的域名(如:example.com)' }, 400);
} else {
return jsonResponse({ error: '只能查询一级域名,不支持二级域名查询' }, 400);
}
}

const result = await queryDomainWhois(domain);
return jsonResponse(result);
} catch (error) {
return jsonResponse({ error: 'WHOIS查询失败: ' + error.message }, 400);
}
}

// 404 - 路由不存在
return jsonResponse({ error: '未找到请求的资源' }, 404);
}

// ================================
// 配置检测函数区域
// ================================

// 检查KV绑定状态
async function checkKVBinding() {
try {
if (typeof DOMAIN_MONITOR === 'undefined' || !DOMAIN_MONITOR) {
return {
isValid: false,
error: 'DOMAIN_MONITOR KV namespace is not bound',
message: 'KV存储空间未绑定'
};
}

// 尝试访问KV存储
await DOMAIN_MONITOR.get('test');
return {
isValid: true,
message: 'KV存储空间已正确绑定'
};
} catch (error) {
return {
isValid: false,
error: error.message,
message: 'KV存储空间访问失败'
};
}
}

// 检查完整的配置状态
async function checkSetupStatus() {
try {
const kvStatus = await checkKVBinding();

if (!kvStatus.isValid) {
return new Response(JSON.stringify({
success: false,
message: kvStatus.message,
details: kvStatus.error,
nextStep: 'bindKV'
}), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
}

// 获取正确的密码配置
let correctPassword = 'domain';
if (typeof TOKEN !== 'undefined' && TOKEN) {
correctPassword = TOKEN;
} else if (DEFAULT_TOKEN) {
correctPassword = DEFAULT_TOKEN;
}

// 检查是否需要认证(域名监控系统默认需要认证)
const authRequired = true;

const result = {
success: true,
message: '配置检查完成',
kvBound: true,
authRequired: authRequired,
hasAuth: authRequired && !!correctPassword,
nextStep: authRequired ? 'login' : 'dashboard'
};

return new Response(JSON.stringify(result), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
} catch (error) {
return new Response(JSON.stringify({
success: false,
message: '配置检查失败',
details: error.message,
nextStep: 'retry'
}), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}
}

// ================================
// 数据操作函数区域
// ================================

// 获取所有域名
async function getDomains() {
const domainsStr = await DOMAIN_MONITOR.get('domains') || '[]';
const domains = JSON.parse(domainsStr);

// 数据迁移:为没有categoryId的域名添加默认分类
let needUpdate = false;
domains.forEach(domain => {
if (!domain.categoryId) {
domain.categoryId = 'default';
needUpdate = true;
}
});

// 如果有数据需要更新,保存到KV
if (needUpdate) {
await DOMAIN_MONITOR.put('domains', JSON.stringify(domains));
}

return domains;
}

// 添加新域名
async function addDomain(domainData) {
const domains = await getDomains();

// 验证域名数据
if (!domainData.name || !domainData.registrationDate || !domainData.expiryDate) {
throw new Error('域名、注册时间和到期日期为必填项');
}

// 生成唯一ID
domainData.id = crypto.randomUUID();

// 添加创建时间
domainData.createdAt = new Date().toISOString();

// 处理通知设置
if (!domainData.notifySettings) {
// 添加默认通知设置
domainData.notifySettings = {
useGlobalSettings: true,
notifyDays: 30,
enabled: true
};
}

// 确保有lastRenewed字段
if (!domainData.lastRenewed) {
domainData.lastRenewed = null;
}

// 添加到列表
domains.push(domainData);

// 保存到KV
await DOMAIN_MONITOR.put('domains', JSON.stringify(domains));

return domainData;
}

// 更新域名
async function updateDomain(id, domainData) {
const domains = await getDomains();

// 查找域名索引
const index = domains.findIndex(d => d.id === id);
if (index === -1) {
throw new Error('域名不存在');
}

// 验证域名数据
if (!domainData.name || !domainData.registrationDate || !domainData.expiryDate) {
throw new Error('域名、注册时间和到期日期为必填项');
}

// 确保通知设置正确
let notifySettings;
if (domainData.notifySettings) {
// 使用提交的通知设置
notifySettings = domainData.notifySettings;
} else if (domains[index].notifySettings) {
// 使用现有的通知设置
notifySettings = domains[index].notifySettings;
} else {
// 创建默认通知设置
notifySettings = {
useGlobalSettings: true,
notifyDays: 30,
enabled: true
};
}

// 更新域名 - 确保正确处理空值
domains[index] = {
...domains[index],
name: domainData.name,
expiryDate: domainData.expiryDate,
registrationDate: domainData.registrationDate !== undefined ? domainData.registrationDate : domains[index].registrationDate,
registrar: domainData.registrar !== undefined ? domainData.registrar : domains[index].registrar,
categoryId: domainData.categoryId !== undefined ? domainData.categoryId : domains[index].categoryId, // 添加分类ID处理
customNote: domainData.customNote !== undefined ? domainData.customNote : domains[index].customNote, // 正确处理空字符串
noteColor: domainData.noteColor !== undefined ? domainData.noteColor : domains[index].noteColor, // 添加备注颜色处理
renewLink: domainData.renewLink !== undefined ? domainData.renewLink : domains[index].renewLink, // 正确处理空字符串
renewCycle: domainData.renewCycle || domains[index].renewCycle,
price: domainData.price !== undefined ? domainData.price : domains[index].price, // 添加价格信息,保留现有价格如果未提供
lastRenewed: domainData.lastRenewed !== undefined ? domainData.lastRenewed : domains[index].lastRenewed, // 根据用户选择更新续期时间
notifySettings: notifySettings,
updatedAt: new Date().toISOString()
};

// 保存到KV
await DOMAIN_MONITOR.put('domains', JSON.stringify(domains));

return domains[index];
}

// 删除域名
async function deleteDomain(id) {
const domains = await getDomains();

// 过滤掉要删除的域名
const newDomains = domains.filter(d => d.id !== id);

// 如果长度相同,说明没有找到要删除的域名
if (newDomains.length === domains.length) {
throw new Error('域名不存在');
}

// 保存到KV
await DOMAIN_MONITOR.put('domains', JSON.stringify(newDomains));

return true;
}

// 域名续期
async function renewDomain(id, renewData) {
const domains = await getDomains();

// 查找域名索引
const index = domains.findIndex(d => d.id === id);
if (index === -1) {
throw new Error('域名不存在');
}

const now = new Date();

// 更新域名信息中的续期数据
if (!domains[index].renewCycle) {
domains[index].renewCycle = {
value: renewData.value || 1,
unit: renewData.unit || 'year'
};
}

// 如果域名是已过期状态,标记为从当前时间开始的全新计算
if (new Date(domains[index].expiryDate) < new Date()) {
domains[index].renewedFromExpired = true;
domains[index].renewStartDate = now.toISOString(); // 记录续期开始时间(当前时间)
}

// 更新到期日期和续期记录
domains[index] = {
...domains[index],
expiryDate: renewData.newExpiryDate,
updatedAt: now.toISOString(),
lastRenewed: now.toISOString(), // 记录本次续期时间
lastRenewPeriod: {
value: renewData.value,
unit: renewData.unit
} // 记录本次续期周期,用于进度条计算
};

// 保存到KV
await DOMAIN_MONITOR.put('domains', JSON.stringify(domains));

return domains[index];
}

// 获取Telegram配置
async function getTelegramConfig() {
const configStr = await DOMAIN_MONITOR.get('telegram_config') || '{}';
const config = JSON.parse(configStr);

// 检查是否使用环境变量
// 当环境变量存在且配置中的值为undefined、null或空字符串时,视为使用环境变量
const tokenFromEnv = typeof TG_TOKEN !== 'undefined' && (
config.botToken === undefined ||
config.botToken === null ||
config.botToken === ''
);

const chatIdFromEnv = typeof TG_ID !== 'undefined' && (
config.chatId === undefined ||
config.chatId === null ||
config.chatId === ''
);

// 检查是否使用代码中定义的变量
const tokenFromCode = !tokenFromEnv && DEFAULT_TG_TOKEN !== '' && (
config.botToken === undefined ||
config.botToken === null ||
config.botToken === ''
);

const chatIdFromCode = !chatIdFromEnv && DEFAULT_TG_ID !== '' && (
config.chatId === undefined ||
config.chatId === null ||
config.chatId === ''
);

// 返回完整的配置信息,包括token和chatId
return {
enabled: !!config.enabled,
chatId: chatIdFromEnv || chatIdFromCode ? '' : (config.chatId || ''),
botToken: tokenFromEnv || tokenFromCode ? '' : (config.botToken || ''), // 如果有环境变量或代码变量,则返回空字符串
chatIdFromEnv: chatIdFromEnv || chatIdFromCode, // 环境变量或代码中有设置都显示为已配置
tokenFromEnv: tokenFromEnv || tokenFromCode, // 环境变量或代码中有设置都显示为已配置
hasToken: tokenFromEnv || tokenFromCode || (config.botToken !== undefined && config.botToken !== null && config.botToken !== ''),
notifyDays: config.notifyDays || 30,
};
}

// 保存Telegram配置
async function saveTelegramConfig(configData) {
// 验证必要的配置 - 只有当启用Telegram通知且环境变量中也没有配置时才需要验证
if (configData.enabled) {
// 检查是否可以使用环境变量或用户输入的值
// 注意:空字符串("")被视为有效的清除操作,不应该抛出错误
const hasTokenSource = (configData.botToken !== undefined && configData.botToken !== null) ||
typeof TG_TOKEN !== 'undefined' ||
DEFAULT_TG_TOKEN !== '';
const hasChatIdSource = (configData.chatId !== undefined && configData.chatId !== null) ||
typeof TG_ID !== 'undefined' ||
DEFAULT_TG_ID !== '';

if (!hasTokenSource) {
throw new Error('启用Telegram通知需要提供机器人Token或在环境变量中配置');
}
if (!hasChatIdSource) {
throw new Error('启用Telegram通知需要提供聊天ID或在环境变量中配置');
}
}

// 保存配置到KV - 即使值为空也保存,表示用户有意清除值
const config = {
enabled: !!configData.enabled,
botToken: configData.botToken, // 可能为空字符串,表示用户清除了值
chatId: configData.chatId, // 可能为空字符串,表示用户清除了值
notifyDays: configData.notifyDays || 30,
};

await DOMAIN_MONITOR.put('telegram_config', JSON.stringify(config));

// 检查是否使用环境变量
// 当环境变量存在且配置中的值为undefined、null或空字符串时,视为使用环境变量
const tokenFromEnv = typeof TG_TOKEN !== 'undefined' && (
config.botToken === undefined ||
config.botToken === null ||
config.botToken === ''
);

const chatIdFromEnv = typeof TG_ID !== 'undefined' && (
config.chatId === undefined ||
config.chatId === null ||
config.chatId === ''
);

// 检查是否使用代码中定义的变量
const tokenFromCode = !tokenFromEnv && DEFAULT_TG_TOKEN !== '' && (
config.botToken === undefined ||
config.botToken === null ||
config.botToken === ''
);

const chatIdFromCode = !chatIdFromEnv && DEFAULT_TG_ID !== '' && (
config.chatId === undefined ||
config.chatId === null ||
config.chatId === ''
);

// 返回完整的配置信息,包括token和chatId
return {
enabled: config.enabled,
chatId: chatIdFromEnv || chatIdFromCode ? '' : (config.chatId || ''),
botToken: tokenFromEnv || tokenFromCode ? '' : (config.botToken || ''), // 如果有环境变量或代码变量,则返回空字符串
chatIdFromEnv: chatIdFromEnv || chatIdFromCode, // 环境变量或代码中有设置都显示为已配置
tokenFromEnv: tokenFromEnv || tokenFromCode, // 环境变量或代码中有设置都显示为已配置
hasToken: tokenFromEnv || tokenFromCode || !!config.botToken,
notifyDays: config.notifyDays,
};
}

// 测试Telegram通知
async function testTelegramNotification() {
const config = await getTelegramConfigWithToken();

if (!config.enabled) {
throw new Error('Telegram通知未启用');
}

if (!config.botToken && typeof TG_TOKEN === 'undefined' && DEFAULT_TG_TOKEN === '') {
throw new Error('未配置Telegram机器人Token');
}

if (!config.chatId && typeof TG_ID === 'undefined' && DEFAULT_TG_ID === '') {
throw new Error('未配置Telegram聊天ID');
}

const message = '这是一条来自域名监控系统的测试通知,如果您收到此消息,表示Telegram通知配置成功!';

const result = await sendTelegramMessage(config, message);
return { success: true, message: '测试通知已发送' };
}

// 获取完整的Telegram配置(包括token)
async function getTelegramConfigWithToken() {
const configStr = await DOMAIN_MONITOR.get('telegram_config') || '{}';
const config = JSON.parse(configStr);

// 如果KV中没有token或chatId,或者是空字符串,但环境变量中有值,则使用环境变量中的值
if (typeof TG_TOKEN !== 'undefined' && (
config.botToken === undefined ||
config.botToken === null ||
config.botToken === ''
)) {
config.botToken = TG_TOKEN;
}

// 同样处理chatId
if (typeof TG_ID !== 'undefined' && (
config.chatId === undefined ||
config.chatId === null ||
config.chatId === ''
)) {
config.chatId = TG_ID;
}

// 如果环境变量中没有,但代码中有,则使用代码中的值
else if (DEFAULT_TG_TOKEN !== '' && (
config.botToken === undefined ||
config.botToken === null ||
config.botToken === ''
)) {
config.botToken = DEFAULT_TG_TOKEN;
}

// 如果环境变量中没有,但代码中有,则使用代码中的值
else if (DEFAULT_TG_ID !== '' && (
config.chatId === undefined ||
config.chatId === null ||
config.chatId === ''
)) {
config.chatId = DEFAULT_TG_ID;
}

return {
enabled: !!config.enabled,
botToken: config.botToken || '',
chatId: config.chatId || '',
notifyDays: config.notifyDays || 30,
};
}

// ================================
// 通知功能区域
// ================================
// 分类管理函数区域
// ================================

// 获取所有分类
async function getCategories() {
const categoriesStr = await DOMAIN_MONITOR.get('categories') || '[]';
let categories = JSON.parse(categoriesStr);

// 确保默认分类存在且在最前面
const defaultCategory = {
id: 'default',
name: '默认分类',
description: '未指定分类的域名',
order: 0,
isDefault: true
};

// 检查是否已存在默认分类
const hasDefault = categories.some(cat => cat.id === 'default');
if (!hasDefault) {
categories.unshift(defaultCategory);
} else {
// 确保默认分类的属性正确
const defaultIndex = categories.findIndex(cat => cat.id === 'default');
categories[defaultIndex] = { ...categories[defaultIndex], ...defaultCategory };
}

// 按order排序
categories.sort((a, b) => a.order - b.order);

return categories;
}

// 添加新分类
async function addCategory(categoryData) {
const { name, description } = categoryData;

if (!name || name.trim() === '') {
throw new Error('分类名称不能为空');
}

const categories = await getCategories();

// 检查分类名是否已存在
if (categories.some(cat => cat.name === name.trim())) {
throw new Error('分类名称已存在');
}

// 生成新ID
const id = 'cat_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);

// 计算新的order值(最大值+1)
const maxOrder = Math.max(...categories.map(cat => cat.order));

const newCategory = {
id,
name: name.trim(),
description: description?.trim() || '',
order: maxOrder + 1,
isDefault: false,
createdAt: new Date().toISOString()
};

categories.push(newCategory);
await DOMAIN_MONITOR.put('categories', JSON.stringify(categories));

return newCategory;
}

// 更新分类
async function updateCategory(id, categoryData) {
const { name, description } = categoryData;

if (id === 'default') {
throw new Error('不能编辑默认分类');
}

if (!name || name.trim() === '') {
throw new Error('分类名称不能为空');
}

const categories = await getCategories();
const categoryIndex = categories.findIndex(cat => cat.id === id);

if (categoryIndex === -1) {
throw new Error('分类不存在');
}

// 检查分类名是否与其他分类重复
const existingCategory = categories.find(cat => cat.name === name.trim() && cat.id !== id);
if (existingCategory) {
throw new Error('分类名称已存在');
}

// 更新分类信息
categories[categoryIndex] = {
...categories[categoryIndex],
name: name.trim(),
description: description?.trim() || '',
updatedAt: new Date().toISOString()
};

await DOMAIN_MONITOR.put('categories', JSON.stringify(categories));

return categories[categoryIndex];
}

// 删除分类
async function deleteCategory(id) {
if (id === 'default') {
throw new Error('不能删除默认分类');
}

const categories = await getCategories();
const categoryIndex = categories.findIndex(cat => cat.id === id);

if (categoryIndex === -1) {
throw new Error('分类不存在');
}

// 检查该分类下是否有域名
const domains = await getDomains();
const domainsInCategory = domains.filter(domain => domain.categoryId === id);

if (domainsInCategory.length > 0) {
// 将该分类下的域名移动到默认分类
for (const domain of domainsInCategory) {
domain.categoryId = 'default';
}
await DOMAIN_MONITOR.put('domains', JSON.stringify(domains));
}

// 删除分类
categories.splice(categoryIndex, 1);
await DOMAIN_MONITOR.put('categories', JSON.stringify(categories));

return true;
}

// 移动分类顺序
async function moveCategoryOrder(id, direction) {
if (id === 'default') {
throw new Error('不能移动默认分类');
}

const categories = await getCategories();
const categoryIndex = categories.findIndex(cat => cat.id === id);

if (categoryIndex === -1) {
throw new Error('分类不存在');
}

// 筛选出非默认分类进行排序操作
const nonDefaultCategories = categories.filter(cat => !cat.isDefault);
const nonDefaultIndex = nonDefaultCategories.findIndex(cat => cat.id === id);

if (direction === 'up' && nonDefaultIndex > 0) {
// 上移:与前一个交换order
const temp = nonDefaultCategories[nonDefaultIndex].order;
nonDefaultCategories[nonDefaultIndex].order = nonDefaultCategories[nonDefaultIndex - 1].order;
nonDefaultCategories[nonDefaultIndex - 1].order = temp;
} else if (direction === 'down' && nonDefaultIndex < nonDefaultCategories.length - 1) {
// 下移:与后一个交换order
const temp = nonDefaultCategories[nonDefaultIndex].order;
nonDefaultCategories[nonDefaultIndex].order = nonDefaultCategories[nonDefaultIndex + 1].order;
nonDefaultCategories[nonDefaultIndex + 1].order = temp;
}

await DOMAIN_MONITOR.put('categories', JSON.stringify(categories));

return true;
}

// ================================

// 发送Telegram消息
async function sendTelegramMessage(config, message) {
// 优先使用配置中的值,如果没有则使用环境变量或代码中的值
let botToken = config.botToken;
let chatId = config.chatId;

// 如果配置中没有值,检查环境变量
if (!botToken) {
if (typeof TG_TOKEN !== 'undefined') {
botToken = TG_TOKEN;
} else if (DEFAULT_TG_TOKEN !== '') {
botToken = DEFAULT_TG_TOKEN;
}
}

if (!chatId) {
if (typeof TG_ID !== 'undefined') {
chatId = TG_ID;
} else if (DEFAULT_TG_ID !== '') {
chatId = DEFAULT_TG_ID;
}
}

if (!botToken) {
throw new Error('未配置Telegram机器人Token');
}

if (!chatId) {
throw new Error('未配置Telegram聊天ID');
}

const url = 'https://api.telegram.org/bot' + botToken + '/sendMessage';

const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
chat_id: chatId,
text: message,
parse_mode: 'HTML',
}),
});

if (!response.ok) {
const error = await response.json();
throw new Error('发送Telegram消息失败: ' + (error.description || '未知错误'));
}

return await response.json();
}

// 设置定时任务,检查即将到期的域名并发送通知
async function checkExpiringDomains() {
const domains = await getDomains();
const today = new Date();

// 获取Telegram配置
const telegramConfig = await getTelegramConfigWithToken();
const globalNotifyDays = telegramConfig.enabled ? telegramConfig.notifyDays : 30;

// 筛选出即将到期和已过期的域名
const domainsToNotify = domains.filter(domain => {
const expiryDate = new Date(domain.expiryDate);
const daysLeft = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));

// 获取该域名的通知设置
const notifySettings = domain.notifySettings || { useGlobalSettings: true, enabled: true, notifyDays: 30 };

// 如果使用全局设置,则使用全局通知天数,否则使用域名自己的设置
const notifyDays = notifySettings.useGlobalSettings ? globalNotifyDays : notifySettings.notifyDays;

// === 优化开始:设置关键通知时间点 ===
const alertDays = [30, 15, 7, 3, 1, 0]; // 仅在这些剩余天数时提醒

// 如果已过期(小于0),或者剩余天数正好在关键点上,且小于等于用户设定的阈值
const shouldNotify = daysLeft <= 0 || (alertDays.includes(daysLeft) && daysLeft <= notifyDays);

return notifySettings.enabled && shouldNotify;
// === 优化结束 ===
});

// 将域名分为已过期和即将到期两组
const expiredDomains = domainsToNotify.filter(domain => {
const expiryDate = new Date(domain.expiryDate);
const daysLeft = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));
return daysLeft <= 0;
});

const expiringDomains = domainsToNotify.filter(domain => {
const expiryDate = new Date(domain.expiryDate);
const daysLeft = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));
return daysLeft > 0;
});

// 如果有即将到期或已过期的域名,发送通知
if (expiringDomains.length > 0 || expiredDomains.length > 0) {

// 如果启用了Telegram通知,则发送通知
if (telegramConfig.enabled &&
((telegramConfig.botToken || typeof TG_TOKEN !== 'undefined') &&
(telegramConfig.chatId || typeof TG_ID !== 'undefined'))) {
try {
// 发送合并的域名通知
await sendCombinedDomainsNotification(telegramConfig, expiringDomains, expiredDomains);
} catch (error) {
// 静默处理Telegram通知发送失败
}
}
}
}

// 发送域名通知(即将到期或已过期)
async function sendExpiringDomainsNotification(config, domains, isExpired) {
if (domains.length === 0) return;

// 构建消息内容
let title = isExpired ?
'🚫 <b>域名已过期提醒</b> 🚫' :
'🚨 <b>域名到期提醒</b> 🚨';

// 根据不同通知类型使用不同长度的等号分隔线
// 域名到期提醒使用19个字符,域名已过期提醒使用21个字符
const separator = isExpired ?
'=====================' :
'===================';
// 域名之间的短横线分隔符统一使用40个字符
const domainSeparator = '----------------------------------------';

let message = title + '\n' + separator + '\n\n';

domains.forEach((domain, index) => {
const expiryDate = new Date(domain.expiryDate);
const today = new Date();
const daysLeft = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));

if (index > 0) {
message += '\n' + domainSeparator + '\n\n';
}

message += '🌍 <b>域名:</b> ' + domain.name + '\n';
if (domain.registrar) {
message += '🏬 <b>注册厂商:</b> ' + domain.registrar + '\n';
}
message += '⏳ <b>剩余时间:</b> ' + daysLeft + ' 天\n';
message += '📅 <b>到期日期:</b> ' + formatDate(domain.expiryDate) + '\n';

if (domain.renewLink) {
message += '⚠️ <b>点击续期:</b> ' + domain.renewLink + '\n';
} else {
message += '⚠️ <b>点击续期:</b> 未设置续期链接\n';
}
});

// 发送消息
return await sendTelegramMessage(config, message);
}

// 发送合并的域名通知(即将到期和已过期)
async function sendCombinedDomainsNotification(config, expiringDomains, expiredDomains) {
if (expiringDomains.length === 0 && expiredDomains.length === 0) return;

let message = '';

// 处理即将到期的域名
if (expiringDomains.length > 0) {
const title = '🚨 <b>域名到期提醒</b> 🚨';
const separator = '===================';

message += title + '\n' + separator + '\n\n';

expiringDomains.forEach((domain, index) => {
const expiryDate = new Date(domain.expiryDate);
const today = new Date();
const daysLeft = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));

if (index > 0) {
message += '\n';
}

message += '🌍 域名: ' + domain.name + '\n';
if (domain.registrar) {
message += '🏬 注册厂商: ' + domain.registrar + '\n';
}
message += '⏳ 剩余时间: ' + daysLeft + ' 天\n';
message += '📅 到期日期: ' + formatDate(domain.expiryDate) + '\n';

if (domain.renewLink) {
message += '⚠️ 点击续期: ' + domain.renewLink + '\n';
} else {
message += '⚠️ 点击续期: 未设置续期链接\n';
}
});
}

// 如果两种类型的域名都存在,添加分隔线
if (expiringDomains.length > 0 && expiredDomains.length > 0) {
message += '\n━━━━━━━━━━━━━━━━\n\n';
}

// 处理已过期的域名
if (expiredDomains.length > 0) {
const title = '🚫 <b>域名已过期提醒</b> 🚫';
const separator = '=====================';

message += title + '\n' + separator + '\n\n';

expiredDomains.forEach((domain, index) => {
const expiryDate = new Date(domain.expiryDate);
const today = new Date();
const daysLeft = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));

if (index > 0) {
message += '\n';
}

message += '🌍 域名: ' + domain.name + '\n';
if (domain.registrar) {
message += '🏬 注册厂商: ' + domain.registrar + '\n';
}
message += '⏳ 剩余时间: ' + daysLeft + ' 天\n';
message += '📅 到期日期: ' + formatDate(domain.expiryDate) + '\n';

if (domain.renewLink) {
message += '⚠️ 点击续期: ' + domain.renewLink + '\n';
} else {
message += '⚠️ 点击续期: 未设置续期链接\n';
}
});
}

// 发送消息
return await sendTelegramMessage(config, message);
}

// 添加测试单个域名通知的后端函数
async function testSingleDomainNotification(id) {
// 获取域名信息
const domains = await getDomains();
const domain = domains.find(d => d.id === id);

if (!domain) {
throw new Error('域名不存在');
}

// 获取Telegram配置
const telegramConfig = await getTelegramConfigWithToken();

if (!telegramConfig.enabled) {
throw new Error('Telegram通知未启用');
}

if (!telegramConfig.botToken && typeof TG_TOKEN === 'undefined') {
throw new Error('未配置Telegram机器人Token');
}

if (!telegramConfig.chatId && typeof TG_ID === 'undefined') {
throw new Error('未配置Telegram聊天ID');
}

// 构建测试消息
const expiryDate = new Date(domain.expiryDate);
const today = new Date();
const daysLeft = Math.ceil((expiryDate - today) / (1000 * 60 * 60 * 24));
const isExpired = daysLeft <= 0;

let title = isExpired ?
'🚫 <b>域名已过期测试通知</b> 🚫' :
'🚨 <b>域名到期测试通知</b> 🚨';

// 根据不同通知类型使用不同长度的分隔线
// 域名到期测试通知使用23个字符,域名已过期测试通知使用25个字符
const separator = isExpired ?
'=========================' :
'=======================';

let message = title + '\n' + separator + '\n\n';
message += '这是一条测试通知,用于预览域名' + (isExpired ? '已过期' : '到期') + '提醒的格式:\n\n';

message += '🌍 <b>域名:</b> ' + domain.name + '\n';
if (domain.registrar) {
message += '🏬 <b>注册厂商:</b> ' + domain.registrar + '\n';
}
message += '⏳ <b>剩余时间:</b> ' + daysLeft + ' 天\n';
message += '📅 <b>到期日期:</b> ' + formatDate(domain.expiryDate) + '\n';

if (domain.renewLink) {
message += '⚠️ <b>点击续期:</b> ' + domain.renewLink + '\n';
} else {
message += '⚠️ <b>点击续期:</b> 未设置续期链接\n';
}

// 发送测试消息
const result = await sendTelegramMessage(telegramConfig, message);
return { success: true, message: '测试通知已发送' };
}

// ================================
// Cloudflare Workers事件处理
// ================================

// 注册fetch事件处理程序
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});

// 注册定时任务,每天检查一次
addEventListener('scheduled', event => {
event.waitUntil(checkExpiringDomains());
});

// ================================
// === 新增:导出数据函数 ===
async function exportData() {
try {
const domains = await getDomains(); // 复用现有的获取数据函数
const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(domains, null, 2));
const downloadAnchorNode = document.createElement('a');
downloadAnchorNode.setAttribute("href", dataStr);
// 文件名包含当前日期
const date = new Date().toISOString().slice(0, 10);
downloadAnchorNode.setAttribute("download", `domains_backup_${date}.json`);
document.body.appendChild(downloadAnchorNode);
downloadAnchorNode.click();
downloadAnchorNode.remove();
showAlert('success', '数据备份下载已开始');
} catch (error) {
showAlert('danger', '备份失败: ' + error.message);
}
}

// 辅助函数区域
// ================================

// 添加页面底部版权信息
function addCopyrightFooter(html) {
// === 修复:定义页脚内容变量 ===
const footerContent = `&copy; ${new Date().getFullYear()} 域名监控系统 | Powered by Cloudflare Workers`;
// ==========================

// 定义页脚内容和样式,只需要修改这里
// 页脚文字大小
const footerFontSize = '14px';
// 页脚图标大小
const footerIconSize = '14px';
// 页脚图标颜色(使用CSS颜色值,如:#4e54c8、blue、rgba(0,0,0,0.7)等)
const footerIconColor = 'white';

const bodyEndIndex = html.lastIndexOf('</body>');

// 如果找到了</body>标签
if (bodyEndIndex !== -1) {
// 在</body>标签前插入页脚和相关脚本
const footer = `
<style>
html {
height: 100%;
}
body {
min-height: 100%;
display: flex;
flex-direction: column;
}
.content-wrapper {
flex: 1 0 auto;
}
#copyright-footer {
flex-shrink: 0;
text-align: center;
padding: 10px;
font-size: ${footerFontSize};
border-top: 1px solid rgba(255, 255, 255, 0.18);
margin-top: auto;
background-color: rgba(0, 0, 0, 0.2);
backdrop-filter: blur(5px);
-webkit-backdrop-filter: blur(5px);
color: white;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}

/* 移动端页脚响应式缩放 */
@media (max-width: 768px) {
#copyright-footer {
font-size: 12px;
padding: 8px 5px;
}
}

@media (max-width: 480px) {
#copyright-footer {
font-size: 10px;
padding: 6px 3px;
}

#copyright-footer .iconfont {
font-size: 10px !important;
}
}

@media (max-width: 320px) {
#copyright-footer {
font-size: 9px;
padding: 5px 2px;
}

#copyright-footer .iconfont {
font-size: 9px !important;
}
}
</style>

<footer id="copyright-footer">
${footerContent}
</footer>

<script>
// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
// 将body内的所有内容(除了页脚)包裹在一个div中
const footer = document.getElementById('copyright-footer');
const contentWrapper = document.createElement('div');
contentWrapper.className = 'content-wrapper';

// 将body中除页脚外的所有元素移到contentWrapper中
while (document.body.firstChild !== footer) {
if (document.body.firstChild) {
contentWrapper.appendChild(document.body.firstChild);
} else {
break;
}
}

// 将contentWrapper插入到body的开头
document.body.insertBefore(contentWrapper, footer);
});
</script>
`;

return html.slice(0, bodyEndIndex) + footer + html.slice(bodyEndIndex);
}

// 如果没找到</body>标签,就直接添加到HTML末尾
const footerHtml = `
<div style="text-align: center; padding: 10px; font-size: ${footerFontSize}; margin-top: 20px; border-top: 1px solid rgba(255, 255, 255, 0.18); background-color: rgba(0, 0, 0, 0.2); backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px); color: white; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">
${footerContent}
</div>
`;

// 在</body>标签前插入页脚
return html.replace('</body>', `${footerHtml}</body>`);
}

// 修改响应处理,添加版权信息
async function addFooterToResponse(response) {
const contentType = response.headers.get('Content-Type') || '';

// 只处理HTML响应
if (contentType.includes('text/html')) {
const html = await response.text();
const modifiedHtml = addCopyrightFooter(html);

return new Response(modifiedHtml, {
status: response.status,
statusText: response.statusText,
headers: response.headers
});
}

return response;
}


// 获取配置向导HTML
function getSetupHTML() {
return `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>域名监控系统 - 初始化配置</title>
<link rel="icon" type="image/svg+xml" href="${DEFAULT_LOGO}">
<link rel="stylesheet" href="${ICONFONT_CSS}">
<script src="${ICONFONT_JS}"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
color: #333;
line-height: 1.6;
}

.setup-container {
background: white;
border-radius: 16px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.15);
padding: 40px;
max-width: 800px;
width: 90%;
margin: 20px;
}

.setup-header {
text-align: center;
margin-bottom: 40px;
}

.setup-header .iconfont {
font-size: 64px;
color: #272830;
margin-bottom: 16px;
display: block;
}

.setup-header h1 {
color: #2c3e50;
font-size: 28px;
margin-bottom: 8px;
}

.setup-header p {
color: #7f8c8d;
font-size: 16px;
}

.step {
margin-bottom: 30px;
padding: 24px;
border: 1px solid #e1e8ed;
border-radius: 12px;
background: #f8fafc;
}

.step-title {
display: flex;
align-items: center;
font-size: 18px;
font-weight: 600;
color: #2c3e50;
margin-bottom: 16px;
}

.step-number {
background: #667eea;
color: white;
width: 28px;
height: 28px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
font-size: 14px;
font-weight: bold;
margin-right: 12px;
}

.step-content {
color: #555;
line-height: 1.7;
}

.code-block {
background: #2c3e50;
color: #ecf0f1;
padding: 16px;
border-radius: 8px;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 14px;
margin: 12px 0;
overflow-x: auto;
}

.config-table {
width: 100%;
border-collapse: collapse;
margin: 16px 0;
}

.config-table th,
.config-table td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #e1e8ed;
}

.config-table th {
background: #f1f3f4;
font-weight: 600;
color: #2c3e50;
}

.config-table code {
background: #f1f3f4;
padding: 2px 6px;
border-radius: 4px;
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
font-size: 13px;
}

.check-button {
width: 100%;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 16px 24px;
border-radius: 12px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
margin-top: 30px;
text-decoration: none;
display: inline-block;
text-align: center;
}

.check-button:hover {
transform: translateY(-2px);
box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
color: white;
text-decoration: none;
}

.check-button:active {
transform: translateY(0);
}

.check-button:disabled {
background: #bdc3c7;
cursor: not-allowed;
transform: none;
box-shadow: none;
}

.status-message {
margin-top: 20px;
padding: 16px;
border-radius: 8px;
font-weight: 500;
display: none;
}

.status-success {
background: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}

.status-error {
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}

.status-loading {
background: #d1ecf1;
color: #0c5460;
border: 1px solid #bee5eb;
}

.loading-spinner {
display: inline-block;
width: 16px;
height: 16px;
border: 2px solid #ffffff;
border-radius: 50%;
border-top-color: transparent;
animation: spin 1s ease-in-out infinite;
margin-right: 8px;
}

@keyframes spin {
to { transform: rotate(360deg); }
}

.iconfont {
font-size: 20px;
margin-right: 8px;
}

@media (max-width: 768px) {
.setup-container {
padding: 24px;
margin: 10px;
}

.setup-header h1 {
font-size: 24px;
}

.code-block {
font-size: 12px;
padding: 12px;
}
}
</style>
</head>
<body>
<div class="setup-container">
<div class="setup-header">
<i class="iconfont icon-jiankong-zichanjiankong"></i>
<h1>欢迎使用域名到期监控系统</h1>
<p>首次使用需要进行简单配置,请按照以下步骤完成初始化</p>
</div>

<div class="step">
<div class="step-title">
<span class="step-number">1</span>
<i class="iconfont icon-database"></i>
绑定 KV 存储空间 (必需)
</div>
<div class="step-content">
<p>在 Cloudflare Workers 控制台中为您的 Worker 绑定 KV 存储空间:</p>
<ol style="margin: 12px 0 12px 20px;">
<li>进入 Cloudflare 控制台 → Workers & Pages</li>
<li>找到您的 Worker 项目,点击进入</li>
<li>转到 "设置" → "变量"</li>
<li>在 "KV 命名空间绑定" 部分点击 "添加绑定"</li>
<li>变量名称填写:<code>DOMAIN_MONITOR</code></li>
<li>选择或创建一个 KV 命名空间</li>
<li>点击 "保存并部署"</li>
</ol>
</div>
</div>

<div class="step">
<div class="step-title">
<span class="step-number">2</span>
<i class="iconfont icon-setting"></i>
配置环境变量 (可选)
</div>
<div class="step-content">
<p>根据需要在 "设置" → "变量" → "环境变量" 中添加以下配置:</p>
<table class="config-table">
<thead>
<tr>
<th>变量名</th>
<th>说明</th>
<th>示例</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>TOKEN</code></td>
<td>登录密码(留空则使用默认密码 "domain")</td>
<td>your_password</td>
</tr>
<tr>
<td><code>SITE_NAME</code></td>
<td>网站标题</td>
<td>我的域名监控</td>
</tr>
<tr>
<td><code>LOGO_URL</code></td>
<td>自定义 Logo 图片 URL</td>
<td>https://example.com/logo.png</td>
</tr>
<tr>
<td><code>BACKGROUND_URL</code></td>
<td>自定义背景图片 URL</td>
<td>https://example.com/bg.jpg</td>
</tr>
<tr>
<td><code>TG_TOKEN</code></td>
<td>Telegram Bot Token(用于到期通知)</td>
<td>1234567890:ABC...</td>
</tr>
<tr>
<td><code>TG_ID</code></td>
<td>Telegram Chat ID</td>
<td>123456789</td>
</tr>
<tr>
<td><code>WHOISJSON_API_KEY</code></td>
<td>WhoisJSON API 密钥(用于域名查询)</td>
<td>your_api_key</td>
</tr>
</tbody>
</table>
<p><strong>注意:</strong>环境变量配置后需要重新部署 Worker 才能生效。</p>
</div>
</div>

<button class="check-button" onclick="checkConfiguration()">
<i class="iconfont icon-check"></i>
检测配置并进入系统
</button>

<div id="statusMessage" class="status-message"></div>
</div>

<script>
async function checkConfiguration() {
const button = document.querySelector('.check-button');
const statusDiv = document.getElementById('statusMessage');

// 设置加载状态
button.disabled = true;
button.innerHTML = '<span class="loading-spinner"></span>检测配置中...';

statusDiv.className = 'status-message status-loading';
statusDiv.style.display = 'block';
statusDiv.textContent = '正在检测配置状态...';

try {
const response = await fetch('/api/check-setup');
const result = await response.json();

if (result.success) {
statusDiv.className = 'status-message status-success';
statusDiv.innerHTML = '<i class="iconfont icon-check"></i>' + result.message + ',即将跳转...';

// 根据配置状态决定跳转目标
setTimeout(() => {
if (result.nextStep === 'dashboard') {
window.location.href = '/dashboard';
} else if (result.nextStep === 'login') {
window.location.href = '/';
} else {
window.location.href = '/';
}
}, 1500);
} else {
statusDiv.className = 'status-message status-error';
let errorMessage = '<i class="iconfont icon-close"></i>' + result.message;
if (result.details) {
errorMessage += '<br><small>详细信息: ' + result.details + '</small>';
}
statusDiv.innerHTML = errorMessage;

// 重置按钮
button.disabled = false;
button.innerHTML = '<i class="iconfont icon-refresh"></i>重新检测';
}
} catch (error) {
statusDiv.className = 'status-message status-error';
statusDiv.innerHTML = '<i class="iconfont icon-close"></i>检测失败: ' + error.message;

// 重置按钮
button.disabled = false;
button.innerHTML = '<i class="iconfont icon-refresh"></i>重新检测';
}
}
</script>
</body>
</html>`;
}


(注意:此时直接访问生成的链接会提示“KV存储空间未绑定”,请继续往下看。)


⚙️ 第四步:绑定 KV 与配置环境变量

代码需要权限才能访问你刚创建的数据库,同时我们需要设置后台的账号密码。

  1. 返回该 Worker 的详情管理页面。
  2. 切换到 设置 (Settings) 选项卡,点击左侧的 变量 (Variables)。

绑定 KV 空间:

  • 向下滚动找到 KV 命名空间绑定 区域。
  • 点击 添加绑定
  • 变量名称 填:DOMAIN_MONITOR
  • KV 命名空间 选择你在第二步创建的那个数据库。

添加环境变量(可选):

  • 向上滚动找到 环境变量 区域,点击 添加变量。根据你的需求添加以下参数:
  • USERNAME:后台登录账号(不填默认是 admin)。
  • TOKEN:后台登录密码(不填默认是 domain)。
  • SITE_NAME:网站左上角和标签页的标题。
  • TG_TOKEN:你准备好的 Telegram 机器人 Token。
  • TG_ID:接收通知的 TG 账号数字 ID。

全部添加完毕后,点击底部的 保存并部署


⏱️ 第五步:设置定时任务(自动检查到期)

只有设置了定时触发器,系统才会每天自动去检查有没有快到期的域名并给你发消息。

  1. 依然在 Worker 详情页,切换到 设置 选项卡,点击左侧的 触发器 (Triggers)。
  2. 向下滚动找到 Cron 触发器,点击 添加 Cron 触发器
  3. 在规则中,推荐选择 每天 执行一次。
  • 如果你熟悉 Cron 表达式,也可以手动输入 0 8 * * *(代表每天 UTC 时间 8 点执行)。
  1. 点击 添加 完成设置。

🚀 第六步:访问并使用

  1. 回到 Worker 的 概述 页面,找到右侧的 *.workers.dev 访问链接。
  2. 点击链接打开你的域名监控系统。
  3. 输入你在环境变量中设置的账号和密码(如果没有设置,默认账号:admin,密码:domain)。
  4. 登录后,点击右上角 添加域名。输入域名后,可以直接点击“查询”按钮自动拉取注册和到期时间。
  5. 点击导航栏的 系统设置,点击 测试Telegram通知,如果手机能收到消息,说明一切大功告成!

💡 核心知识点

  • Cloudflare Workers:提供无服务器的代码运行环境,免去了购买和维护 VPS 的烦恼。
  • KV 存储:Cloudflare 的全球分布式键值数据库,读取速度极快,非常适合存储域名这类结构化数据。
  • Cron Triggers:类似 Linux 里的 crontab,依靠 Cloudflare 的服务器定时唤醒你的代码执行检测逻辑。