index.html 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>License 管理平台</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  15. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  16. min-height: 100vh;
  17. padding: 20px;
  18. }
  19. .container {
  20. max-width: 1200px;
  21. margin: 0 auto;
  22. }
  23. .header {
  24. background: white;
  25. padding: 30px;
  26. border-radius: 10px;
  27. box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  28. margin-bottom: 20px;
  29. display: flex;
  30. justify-content: space-between;
  31. align-items: center;
  32. }
  33. .header h1 {
  34. color: #333;
  35. font-size: 28px;
  36. }
  37. .btn {
  38. padding: 10px 20px;
  39. border: none;
  40. border-radius: 5px;
  41. cursor: pointer;
  42. font-size: 14px;
  43. transition: all 0.3s;
  44. font-weight: 500;
  45. }
  46. .btn-primary {
  47. background: #667eea;
  48. color: white;
  49. }
  50. .btn-primary:hover {
  51. background: #5568d3;
  52. transform: translateY(-2px);
  53. box-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
  54. }
  55. .btn-danger {
  56. background: #ef4444;
  57. color: white;
  58. }
  59. .btn-danger:hover {
  60. background: #dc2626;
  61. }
  62. .btn-secondary {
  63. background: #6b7280;
  64. color: white;
  65. }
  66. .btn-secondary:hover {
  67. background: #4b5563;
  68. }
  69. .card {
  70. background: white;
  71. border-radius: 10px;
  72. box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  73. padding: 30px;
  74. margin-bottom: 20px;
  75. }
  76. .table-container {
  77. overflow-x: auto;
  78. }
  79. table {
  80. width: 100%;
  81. border-collapse: collapse;
  82. }
  83. th, td {
  84. padding: 12px;
  85. text-align: left;
  86. border-bottom: 1px solid #e5e7eb;
  87. }
  88. th {
  89. background: #f9fafb;
  90. font-weight: 600;
  91. color: #374151;
  92. }
  93. tr:hover {
  94. background: #f9fafb;
  95. }
  96. .badge {
  97. display: inline-block;
  98. padding: 4px 12px;
  99. border-radius: 12px;
  100. font-size: 12px;
  101. font-weight: 500;
  102. }
  103. .badge-success {
  104. background: #d1fae5;
  105. color: #065f46;
  106. }
  107. .badge-warning {
  108. background: #fef3c7;
  109. color: #92400e;
  110. }
  111. .modal {
  112. display: none;
  113. position: fixed;
  114. z-index: 1000;
  115. left: 0;
  116. top: 0;
  117. width: 100%;
  118. height: 100%;
  119. background: rgba(0, 0, 0, 0.5);
  120. animation: fadeIn 0.3s;
  121. }
  122. .modal.show {
  123. display: flex;
  124. align-items: center;
  125. justify-content: center;
  126. }
  127. @keyframes fadeIn {
  128. from { opacity: 0; }
  129. to { opacity: 1; }
  130. }
  131. .modal-content {
  132. background: white;
  133. border-radius: 10px;
  134. padding: 30px;
  135. width: 90%;
  136. max-width: 500px;
  137. animation: slideDown 0.3s;
  138. }
  139. @keyframes slideDown {
  140. from {
  141. transform: translateY(-50px);
  142. opacity: 0;
  143. }
  144. to {
  145. transform: translateY(0);
  146. opacity: 1;
  147. }
  148. }
  149. .modal-header {
  150. display: flex;
  151. justify-content: space-between;
  152. align-items: center;
  153. margin-bottom: 20px;
  154. }
  155. .modal-header h2 {
  156. color: #333;
  157. font-size: 24px;
  158. }
  159. .close {
  160. font-size: 28px;
  161. font-weight: bold;
  162. color: #999;
  163. cursor: pointer;
  164. border: none;
  165. background: none;
  166. }
  167. .close:hover {
  168. color: #333;
  169. }
  170. .form-group {
  171. margin-bottom: 20px;
  172. }
  173. .form-group label {
  174. display: block;
  175. margin-bottom: 8px;
  176. color: #374151;
  177. font-weight: 500;
  178. }
  179. .form-group input {
  180. width: 100%;
  181. padding: 10px;
  182. border: 1px solid #d1d5db;
  183. border-radius: 5px;
  184. font-size: 14px;
  185. }
  186. .form-group input:focus {
  187. outline: none;
  188. border-color: #667eea;
  189. box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  190. }
  191. .form-actions {
  192. display: flex;
  193. gap: 10px;
  194. justify-content: flex-end;
  195. margin-top: 20px;
  196. }
  197. .pagination {
  198. display: flex;
  199. justify-content: center;
  200. align-items: center;
  201. gap: 10px;
  202. margin-top: 20px;
  203. }
  204. .pagination button {
  205. padding: 8px 16px;
  206. border: 1px solid #d1d5db;
  207. background: white;
  208. border-radius: 5px;
  209. cursor: pointer;
  210. }
  211. .pagination button:hover:not(:disabled) {
  212. background: #f9fafb;
  213. }
  214. .pagination button:disabled {
  215. opacity: 0.5;
  216. cursor: not-allowed;
  217. }
  218. .pagination .page-info {
  219. color: #6b7280;
  220. }
  221. .loading {
  222. text-align: center;
  223. padding: 40px;
  224. color: #6b7280;
  225. }
  226. .empty-state {
  227. text-align: center;
  228. padding: 60px 20px;
  229. color: #6b7280;
  230. }
  231. .empty-state svg {
  232. width: 100px;
  233. height: 100px;
  234. margin-bottom: 20px;
  235. opacity: 0.5;
  236. }
  237. .device-list {
  238. font-size: 12px;
  239. color: #6b7280;
  240. max-width: 200px;
  241. overflow: hidden;
  242. text-overflow: ellipsis;
  243. white-space: nowrap;
  244. }
  245. .actions {
  246. display: flex;
  247. gap: 8px;
  248. }
  249. .btn-sm {
  250. padding: 6px 12px;
  251. font-size: 12px;
  252. }
  253. .license-key-cell {
  254. display: flex;
  255. align-items: center;
  256. gap: 8px;
  257. max-width: 200px;
  258. }
  259. .license-key-text {
  260. flex: 1;
  261. overflow: hidden;
  262. text-overflow: ellipsis;
  263. white-space: nowrap;
  264. font-weight: 600;
  265. }
  266. .copy-btn {
  267. padding: 6px 12px;
  268. font-size: 12px;
  269. background: #667eea;
  270. color: white;
  271. border: none;
  272. border-radius: 5px;
  273. cursor: pointer;
  274. transition: all 0.2s;
  275. flex-shrink: 0;
  276. font-weight: 500;
  277. display: flex;
  278. align-items: center;
  279. gap: 4px;
  280. }
  281. .copy-btn:hover {
  282. background: #5568d3;
  283. transform: translateY(-1px);
  284. box-shadow: 0 2px 4px rgba(102, 126, 234, 0.3);
  285. }
  286. .copy-btn:active {
  287. transform: translateY(0);
  288. }
  289. /* Toast 通知样式 */
  290. .toast-container {
  291. position: fixed;
  292. top: 20px;
  293. right: 20px;
  294. z-index: 10000;
  295. display: flex;
  296. flex-direction: column;
  297. gap: 10px;
  298. }
  299. .toast {
  300. background: white;
  301. padding: 16px 20px;
  302. border-radius: 8px;
  303. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  304. display: flex;
  305. align-items: center;
  306. gap: 12px;
  307. min-width: 300px;
  308. max-width: 400px;
  309. animation: slideInRight 0.3s ease-out;
  310. position: relative;
  311. }
  312. @keyframes slideInRight {
  313. from {
  314. transform: translateX(100%);
  315. opacity: 0;
  316. }
  317. to {
  318. transform: translateX(0);
  319. opacity: 1;
  320. }
  321. }
  322. .toast.success {
  323. border-left: 4px solid #10b981;
  324. }
  325. .toast.error {
  326. border-left: 4px solid #ef4444;
  327. }
  328. .toast.warning {
  329. border-left: 4px solid #f59e0b;
  330. }
  331. .toast.info {
  332. border-left: 4px solid #3b82f6;
  333. }
  334. .toast-icon {
  335. font-size: 20px;
  336. flex-shrink: 0;
  337. }
  338. .toast.success .toast-icon {
  339. color: #10b981;
  340. }
  341. .toast.error .toast-icon {
  342. color: #ef4444;
  343. }
  344. .toast.warning .toast-icon {
  345. color: #f59e0b;
  346. }
  347. .toast.info .toast-icon {
  348. color: #3b82f6;
  349. }
  350. .toast-message {
  351. flex: 1;
  352. color: #374151;
  353. font-size: 14px;
  354. line-height: 1.5;
  355. }
  356. .toast-close {
  357. background: none;
  358. border: none;
  359. font-size: 18px;
  360. color: #9ca3af;
  361. cursor: pointer;
  362. padding: 0;
  363. width: 20px;
  364. height: 20px;
  365. display: flex;
  366. align-items: center;
  367. justify-content: center;
  368. flex-shrink: 0;
  369. }
  370. .toast-close:hover {
  371. color: #374151;
  372. }
  373. /* 确认对话框样式 */
  374. .confirm-dialog {
  375. display: none;
  376. position: fixed;
  377. z-index: 10001;
  378. left: 0;
  379. top: 0;
  380. width: 100%;
  381. height: 100%;
  382. background: rgba(0, 0, 0, 0.5);
  383. align-items: center;
  384. justify-content: center;
  385. animation: fadeIn 0.3s;
  386. }
  387. .confirm-dialog.show {
  388. display: flex;
  389. }
  390. .confirm-content {
  391. background: white;
  392. border-radius: 10px;
  393. padding: 30px;
  394. width: 90%;
  395. max-width: 400px;
  396. box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
  397. animation: slideDown 0.3s;
  398. }
  399. .confirm-icon {
  400. font-size: 48px;
  401. text-align: center;
  402. margin-bottom: 20px;
  403. }
  404. .confirm-title {
  405. font-size: 20px;
  406. font-weight: 600;
  407. color: #374151;
  408. text-align: center;
  409. margin-bottom: 15px;
  410. }
  411. .confirm-message {
  412. color: #6b7280;
  413. text-align: center;
  414. margin-bottom: 25px;
  415. line-height: 1.6;
  416. }
  417. .confirm-actions {
  418. display: flex;
  419. gap: 10px;
  420. justify-content: center;
  421. }
  422. .confirm-actions .btn {
  423. min-width: 100px;
  424. }
  425. </style>
  426. </head>
  427. <body>
  428. <!-- Toast 通知容器 -->
  429. <div class="toast-container" id="toast-container"></div>
  430. <!-- 确认对话框 -->
  431. <div id="confirmDialog" class="confirm-dialog">
  432. <div class="confirm-content">
  433. <div class="confirm-icon">⚠️</div>
  434. <div class="confirm-title" id="confirm-title">确认操作</div>
  435. <div class="confirm-message" id="confirm-message"></div>
  436. <div class="confirm-actions">
  437. <button class="btn btn-secondary" onclick="closeConfirmDialog(false)">取消</button>
  438. <button class="btn btn-danger" onclick="closeConfirmDialog(true)" id="confirm-ok-btn">确定</button>
  439. </div>
  440. </div>
  441. </div>
  442. <div class="container">
  443. <div class="header">
  444. <h1>🔑 License 管理平台</h1>
  445. <div style="display: flex; gap: 10px;">
  446. <button class="btn btn-primary" onclick="openBatchModal()">📦 批量生成</button>
  447. <button class="btn btn-primary" onclick="openCreateModal()">+ 创建 License</button>
  448. </div>
  449. </div>
  450. <div class="card">
  451. <div id="loading" class="loading" style="display: none;">加载中...</div>
  452. <div id="empty-state" class="empty-state" style="display: none;">
  453. <svg fill="none" stroke="currentColor" viewBox="0 0 24 24">
  454. <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path>
  455. </svg>
  456. <p>暂无 License 数据</p>
  457. </div>
  458. <div class="table-container" id="table-container" style="display: none;">
  459. <div style="margin-bottom: 15px; display: flex; justify-content: space-between; align-items: center;">
  460. <div style="display: flex; align-items: center; gap: 10px;">
  461. <input type="checkbox" id="select-all" onchange="toggleSelectAll()" style="width: 18px; height: 18px; cursor: pointer;">
  462. <label for="select-all" style="cursor: pointer; user-select: none;">全选</label>
  463. <span id="selected-count" style="color: #6b7280; font-size: 14px;">已选择 0 项</span>
  464. </div>
  465. <button class="btn btn-danger" id="batch-delete-btn" onclick="batchDeleteLicenses()" style="display: none;">
  466. 批量删除
  467. </button>
  468. </div>
  469. <table>
  470. <thead>
  471. <tr>
  472. <th style="width: 50px;">
  473. <input type="checkbox" id="select-all-header" onchange="toggleSelectAll()" style="width: 18px; height: 18px; cursor: pointer;">
  474. </th>
  475. <th>ID</th>
  476. <th>激活码</th>
  477. <th>最大设备数</th>
  478. <th>已绑定设备</th>
  479. <th>设备激活时间</th>
  480. <th>绑定设备数</th>
  481. <th>创建时间</th>
  482. <th>操作</th>
  483. </tr>
  484. </thead>
  485. <tbody id="license-table-body">
  486. </tbody>
  487. </table>
  488. <div class="pagination" id="pagination"></div>
  489. </div>
  490. </div>
  491. </div>
  492. <!-- 创建/编辑 Modal -->
  493. <div id="licenseModal" class="modal">
  494. <div class="modal-content">
  495. <div class="modal-header">
  496. <h2 id="modal-title">创建 License</h2>
  497. <button class="close" onclick="closeModal()">&times;</button>
  498. </div>
  499. <form id="licenseForm" onsubmit="handleSubmit(event)">
  500. <input type="hidden" id="license-id">
  501. <div class="form-group">
  502. <label for="license-key">激活码 *</label>
  503. <input type="text" id="license-key" required placeholder="例如: VIP-8888">
  504. </div>
  505. <div class="form-group">
  506. <label for="license-max-devices">最大设备数 *</label>
  507. <input type="number" id="license-max-devices" required min="1" value="2">
  508. </div>
  509. <div class="form-group">
  510. <label for="license-bound-devices">已绑定设备 (JSON 数组,可选)</label>
  511. <input type="text" id="license-bound-devices" placeholder='例如: ["device-1", "device-2"]'>
  512. </div>
  513. <div class="form-actions">
  514. <button type="button" class="btn btn-secondary" onclick="closeModal()">取消</button>
  515. <button type="submit" class="btn btn-primary">保存</button>
  516. </div>
  517. </form>
  518. </div>
  519. </div>
  520. <!-- 批量生成 Modal -->
  521. <div id="batchModal" class="modal">
  522. <div class="modal-content">
  523. <div class="modal-header">
  524. <h2>批量生成 License</h2>
  525. <button class="close" onclick="closeBatchModal()">&times;</button>
  526. </div>
  527. <form id="batchForm" onsubmit="handleBatchSubmit(event)">
  528. <div class="form-group">
  529. <label for="batch-prefix">激活码前缀 *</label>
  530. <input type="text" id="batch-prefix" required placeholder="例如: VIP" value="VIP">
  531. <small style="color: #6b7280; font-size: 12px; margin-top: 4px; display: block;">
  532. 生成的激活码格式:前缀-随机32位字符串(如:VIP-A3B9C2D4E5F6G7H8I9J0K1L2M3N4O5P6)
  533. </small>
  534. </div>
  535. <div class="form-group">
  536. <label for="batch-count">生成数量 *</label>
  537. <input type="number" id="batch-count" required min="1" max="1000" value="10">
  538. <small style="color: #6b7280; font-size: 12px; margin-top: 4px; display: block;">
  539. 一次最多可生成 1000 个
  540. </small>
  541. </div>
  542. <div class="form-group">
  543. <label for="batch-max-devices">最大设备数 *</label>
  544. <input type="number" id="batch-max-devices" required min="1" value="2">
  545. </div>
  546. <div class="form-actions">
  547. <button type="button" class="btn btn-secondary" onclick="closeBatchModal()">取消</button>
  548. <button type="submit" class="btn btn-primary">生成</button>
  549. </div>
  550. </form>
  551. </div>
  552. </div>
  553. <script>
  554. const API_BASE = 'http://localhost:8080/api';
  555. let currentPage = 1;
  556. let pageSize = 10;
  557. let total = 0;
  558. let editingId = null;
  559. // Toast 通知函数
  560. function showToast(message, type = 'info', duration = 3000) {
  561. const container = document.getElementById('toast-container');
  562. const toast = document.createElement('div');
  563. toast.className = `toast ${type}`;
  564. const icons = {
  565. success: '✅',
  566. error: '❌',
  567. warning: '⚠️',
  568. info: 'ℹ️'
  569. };
  570. toast.innerHTML = `
  571. <span class="toast-icon">${icons[type] || icons.info}</span>
  572. <span class="toast-message">${message}</span>
  573. <button class="toast-close" onclick="this.parentElement.remove()">&times;</button>
  574. `;
  575. container.appendChild(toast);
  576. // 自动移除
  577. setTimeout(() => {
  578. if (toast.parentElement) {
  579. toast.style.animation = 'slideInRight 0.3s ease-out reverse';
  580. setTimeout(() => {
  581. if (toast.parentElement) {
  582. toast.remove();
  583. }
  584. }, 300);
  585. }
  586. }, duration);
  587. }
  588. // 确认对话框函数
  589. let confirmCallback = null;
  590. function showConfirmDialog(message, title = '确认操作', okText = '确定', okType = 'danger') {
  591. return new Promise((resolve) => {
  592. document.getElementById('confirm-title').textContent = title;
  593. document.getElementById('confirm-message').textContent = message;
  594. const okBtn = document.getElementById('confirm-ok-btn');
  595. okBtn.textContent = okText;
  596. okBtn.className = `btn btn-${okType}`;
  597. confirmCallback = resolve;
  598. document.getElementById('confirmDialog').classList.add('show');
  599. });
  600. }
  601. function closeConfirmDialog(confirmed) {
  602. document.getElementById('confirmDialog').classList.remove('show');
  603. if (confirmCallback) {
  604. confirmCallback(confirmed);
  605. confirmCallback = null;
  606. }
  607. }
  608. // 复制激活码到剪贴板
  609. async function copyLicenseKey(key) {
  610. try {
  611. await navigator.clipboard.writeText(key);
  612. showToast('激活码已复制到剪贴板', 'success', 2000);
  613. } catch (err) {
  614. // 降级方案:使用传统方法
  615. const textArea = document.createElement('textarea');
  616. textArea.value = key;
  617. textArea.style.position = 'fixed';
  618. textArea.style.left = '-999999px';
  619. document.body.appendChild(textArea);
  620. textArea.select();
  621. try {
  622. document.execCommand('copy');
  623. showToast('激活码已复制到剪贴板', 'success', 2000);
  624. } catch (err) {
  625. showToast('复制失败,请手动复制', 'error');
  626. }
  627. document.body.removeChild(textArea);
  628. }
  629. }
  630. // 全选/取消全选
  631. function toggleSelectAll() {
  632. const selectAll = document.getElementById('select-all');
  633. const selectAllHeader = document.getElementById('select-all-header');
  634. const checkboxes = document.querySelectorAll('.license-checkbox');
  635. const isChecked = selectAll.checked || selectAllHeader.checked;
  636. checkboxes.forEach(checkbox => {
  637. checkbox.checked = isChecked;
  638. });
  639. // 同步两个全选复选框
  640. selectAll.checked = isChecked;
  641. selectAllHeader.checked = isChecked;
  642. updateSelectedCount();
  643. }
  644. // 更新选中数量
  645. function updateSelectedCount() {
  646. const checkboxes = document.querySelectorAll('.license-checkbox:checked');
  647. const count = checkboxes.length;
  648. const selectedCountEl = document.getElementById('selected-count');
  649. const batchDeleteBtn = document.getElementById('batch-delete-btn');
  650. selectedCountEl.textContent = `已选择 ${count} 项`;
  651. if (count > 0) {
  652. batchDeleteBtn.style.display = 'block';
  653. } else {
  654. batchDeleteBtn.style.display = 'none';
  655. }
  656. // 更新全选复选框状态
  657. const allCheckboxes = document.querySelectorAll('.license-checkbox');
  658. const allChecked = allCheckboxes.length > 0 && checkboxes.length === allCheckboxes.length;
  659. document.getElementById('select-all').checked = allChecked;
  660. document.getElementById('select-all-header').checked = allChecked;
  661. }
  662. // 批量删除 License
  663. async function batchDeleteLicenses() {
  664. const checkboxes = document.querySelectorAll('.license-checkbox:checked');
  665. const selectedIds = Array.from(checkboxes).map(cb => parseInt(cb.value));
  666. if (selectedIds.length === 0) {
  667. showToast('请至少选择一个 License', 'warning');
  668. return;
  669. }
  670. const confirmed = await showConfirmDialog(
  671. `确定要删除选中的 ${selectedIds.length} 个 License 吗?此操作不可恢复!`,
  672. '确认批量删除',
  673. '删除',
  674. 'danger'
  675. );
  676. if (!confirmed) {
  677. return;
  678. }
  679. try {
  680. const response = await apiRequest(`${API_BASE}/licenses/batch`, {
  681. method: 'DELETE',
  682. body: JSON.stringify({
  683. ids: selectedIds
  684. })
  685. });
  686. if (!response) return;
  687. const result = await response.json();
  688. if (result.code === 0) {
  689. showToast(result.msg, 'success');
  690. loadLicenses(currentPage);
  691. } else {
  692. showToast('批量删除失败: ' + result.msg, 'error');
  693. }
  694. } catch (error) {
  695. showToast('请求失败: ' + error.message, 'error');
  696. }
  697. }
  698. // 获取认证token
  699. function getAuthToken() {
  700. return localStorage.getItem('auth_token');
  701. }
  702. // 检查是否已登录
  703. function checkAuth() {
  704. const token = getAuthToken();
  705. if (!token) {
  706. window.location.href = '/web/login.html';
  707. return false;
  708. }
  709. return true;
  710. }
  711. // 获取API请求头(包含token)
  712. function getAuthHeaders() {
  713. const token = getAuthToken();
  714. return {
  715. 'Content-Type': 'application/json',
  716. 'Authorization': `Bearer ${token}`
  717. };
  718. }
  719. // 处理API错误响应
  720. async function handleApiError(response) {
  721. if (response.status === 401) {
  722. // 未授权,清除token并跳转到登录页
  723. localStorage.removeItem('auth_token');
  724. showToast('登录已过期,请重新登录', 'error');
  725. setTimeout(() => {
  726. window.location.href = '/web/login.html';
  727. }, 1000);
  728. return true;
  729. }
  730. return false;
  731. }
  732. // 统一的API请求函数
  733. async function apiRequest(url, options = {}) {
  734. const headers = getAuthHeaders();
  735. if (options.headers) {
  736. Object.assign(headers, options.headers);
  737. }
  738. const response = await fetch(url, {
  739. ...options,
  740. headers: headers
  741. });
  742. if (await handleApiError(response)) {
  743. return null;
  744. }
  745. return response;
  746. }
  747. // 页面加载时检查登录状态
  748. window.onload = () => {
  749. if (checkAuth()) {
  750. loadLicenses();
  751. }
  752. };
  753. // 加载 License 列表
  754. async function loadLicenses(page = 1) {
  755. currentPage = page;
  756. const loadingEl = document.getElementById('loading');
  757. const tableContainer = document.getElementById('table-container');
  758. const emptyState = document.getElementById('empty-state');
  759. loadingEl.style.display = 'block';
  760. tableContainer.style.display = 'none';
  761. emptyState.style.display = 'none';
  762. try {
  763. const response = await apiRequest(`${API_BASE}/licenses?page=${page}&page_size=${pageSize}`);
  764. if (!response) return;
  765. const result = await response.json();
  766. if (result.code === 0) {
  767. total = result.total;
  768. const licenses = result.data;
  769. if (licenses.length === 0) {
  770. loadingEl.style.display = 'none';
  771. emptyState.style.display = 'block';
  772. return;
  773. }
  774. renderTable(licenses);
  775. renderPagination();
  776. loadingEl.style.display = 'none';
  777. tableContainer.style.display = 'block';
  778. } else {
  779. showToast('加载失败: ' + result.msg, 'error');
  780. loadingEl.style.display = 'none';
  781. }
  782. } catch (error) {
  783. showToast('请求失败: ' + error.message, 'error');
  784. loadingEl.style.display = 'none';
  785. }
  786. }
  787. // 渲染表格
  788. function renderTable(licenses) {
  789. const tbody = document.getElementById('license-table-body');
  790. tbody.innerHTML = licenses.map(license => {
  791. let boundDevices = [];
  792. try {
  793. boundDevices = JSON.parse(license.bound_devices || '[]');
  794. } catch (e) {
  795. boundDevices = [];
  796. }
  797. // 解析设备激活时间
  798. let deviceActivations = {};
  799. try {
  800. const activationsStr = JSON.parse(license.device_activations || '{}');
  801. deviceActivations = activationsStr;
  802. } catch (e) {
  803. deviceActivations = {};
  804. }
  805. // 构建设备激活时间显示
  806. let activationTimesHtml = '无';
  807. if (boundDevices.length > 0) {
  808. activationTimesHtml = boundDevices.map(deviceId => {
  809. const activationTime = deviceActivations[deviceId];
  810. if (activationTime) {
  811. const date = new Date(activationTime);
  812. return `${deviceId}<br><small style="color: #6b7280;">${date.toLocaleString('zh-CN')}</small>`;
  813. }
  814. return `${deviceId}<br><small style="color: #9ca3af;">未记录</small>`;
  815. }).join('<br><br>');
  816. }
  817. const boundCount = boundDevices.length;
  818. const isFull = boundCount >= license.max_devices;
  819. const createdDate = new Date(license.created_at).toLocaleString('zh-CN');
  820. // 限制激活码显示长度为10个字符
  821. const displayKey = license.key.length > 10 ? license.key.substring(0, 10) + '...' : license.key;
  822. return `
  823. <tr>
  824. <td style="text-align: center;">
  825. <input type="checkbox" class="license-checkbox" value="${license.id}" onchange="updateSelectedCount()" style="width: 18px; height: 18px; cursor: pointer;">
  826. </td>
  827. <td>${license.id}</td>
  828. <td>
  829. <div class="license-key-cell" title="${license.key}">
  830. <span class="license-key-text">${displayKey}</span>
  831. <button class="copy-btn" onclick="copyLicenseKey('${license.key}')" title="复制激活码">
  832. <span>复制</span>
  833. </button>
  834. </div>
  835. </td>
  836. <td>${license.max_devices}</td>
  837. <td class="device-list" title="${boundDevices.join(', ') || '无'}">
  838. ${boundDevices.length > 0 ? boundDevices.join(', ') : '无'}
  839. </td>
  840. <td style="font-size: 12px; line-height: 1.6; max-width: 300px;">
  841. ${activationTimesHtml}
  842. </td>
  843. <td>
  844. <span class="badge ${isFull ? 'badge-warning' : 'badge-success'}">
  845. ${boundCount} / ${license.max_devices}
  846. </span>
  847. </td>
  848. <td>${createdDate}</td>
  849. <td>
  850. <div class="actions">
  851. <button class="btn btn-primary btn-sm" onclick="editLicense(${license.id})">编辑</button>
  852. <button class="btn btn-danger btn-sm" onclick="deleteLicense(${license.id}, '${license.key}')">删除</button>
  853. </div>
  854. </td>
  855. </tr>
  856. `;
  857. }).join('');
  858. }
  859. // 渲染分页
  860. function renderPagination() {
  861. const pagination = document.getElementById('pagination');
  862. const totalPages = Math.ceil(total / pageSize);
  863. if (totalPages <= 1) {
  864. pagination.innerHTML = '';
  865. return;
  866. }
  867. pagination.innerHTML = `
  868. <button onclick="loadLicenses(${currentPage - 1})" ${currentPage === 1 ? 'disabled' : ''}>
  869. 上一页
  870. </button>
  871. <span class="page-info">第 ${currentPage} / ${totalPages} 页 (共 ${total} 条)</span>
  872. <button onclick="loadLicenses(${currentPage + 1})" ${currentPage >= totalPages ? 'disabled' : ''}>
  873. 下一页
  874. </button>
  875. `;
  876. }
  877. // 打开创建 Modal
  878. function openCreateModal() {
  879. editingId = null;
  880. document.getElementById('modal-title').textContent = '创建 License';
  881. document.getElementById('license-id').value = '';
  882. document.getElementById('license-key').value = '';
  883. document.getElementById('license-max-devices').value = '2';
  884. document.getElementById('license-bound-devices').value = '';
  885. document.getElementById('license-key').disabled = false;
  886. document.getElementById('licenseModal').classList.add('show');
  887. }
  888. // 编辑 License
  889. async function editLicense(id) {
  890. try {
  891. const response = await apiRequest(`${API_BASE}/licenses/${id}`);
  892. if (!response) return;
  893. const result = await response.json();
  894. if (result.code === 0) {
  895. const license = result.data;
  896. editingId = id;
  897. document.getElementById('modal-title').textContent = '编辑 License';
  898. document.getElementById('license-id').value = id;
  899. document.getElementById('license-key').value = license.key;
  900. document.getElementById('license-max-devices').value = license.max_devices;
  901. document.getElementById('license-bound-devices').value = license.bound_devices || '[]';
  902. document.getElementById('license-key').disabled = false;
  903. document.getElementById('licenseModal').classList.add('show');
  904. } else {
  905. showToast('加载失败: ' + result.msg, 'error');
  906. }
  907. } catch (error) {
  908. showToast('请求失败: ' + error.message, 'error');
  909. }
  910. }
  911. // 关闭 Modal
  912. function closeModal() {
  913. document.getElementById('licenseModal').classList.remove('show');
  914. }
  915. // 提交表单
  916. async function handleSubmit(event) {
  917. event.preventDefault();
  918. const id = document.getElementById('license-id').value;
  919. const key = document.getElementById('license-key').value;
  920. const maxDevices = parseInt(document.getElementById('license-max-devices').value);
  921. const boundDevices = document.getElementById('license-bound-devices').value || '[]';
  922. // 验证 boundDevices 是否为有效 JSON
  923. try {
  924. JSON.parse(boundDevices);
  925. } catch (e) {
  926. showToast('已绑定设备必须是有效的 JSON 数组格式', 'error');
  927. return;
  928. }
  929. try {
  930. let response;
  931. if (editingId) {
  932. // 更新
  933. const updateData = {
  934. max_devices: maxDevices
  935. };
  936. if (boundDevices) {
  937. updateData.bound_devices = boundDevices;
  938. }
  939. response = await apiRequest(`${API_BASE}/licenses/${id}`, {
  940. method: 'PUT',
  941. body: JSON.stringify(updateData)
  942. });
  943. } else {
  944. // 创建
  945. response = await apiRequest(`${API_BASE}/licenses`, {
  946. method: 'POST',
  947. body: JSON.stringify({
  948. key: key,
  949. max_devices: maxDevices,
  950. bound_devices: boundDevices
  951. })
  952. });
  953. }
  954. if (!response) return;
  955. const result = await response.json();
  956. if (result.code === 0) {
  957. showToast(editingId ? '更新成功' : '创建成功', 'success');
  958. closeModal();
  959. loadLicenses(currentPage);
  960. } else {
  961. showToast('操作失败: ' + result.msg, 'error');
  962. }
  963. } catch (error) {
  964. showToast('请求失败: ' + error.message, 'error');
  965. }
  966. }
  967. // 删除 License
  968. async function deleteLicense(id, key) {
  969. const confirmed = await showConfirmDialog(
  970. `确定要删除 License "${key}" 吗?此操作不可恢复!`,
  971. '确认删除',
  972. '删除',
  973. 'danger'
  974. );
  975. if (!confirmed) {
  976. return;
  977. }
  978. try {
  979. const response = await apiRequest(`${API_BASE}/licenses/${id}`, {
  980. method: 'DELETE'
  981. });
  982. if (!response) return;
  983. const result = await response.json();
  984. if (result.code === 0) {
  985. showToast('删除成功', 'success');
  986. loadLicenses(currentPage);
  987. } else {
  988. showToast('删除失败: ' + result.msg, 'error');
  989. }
  990. } catch (error) {
  991. showToast('请求失败: ' + error.message, 'error');
  992. }
  993. }
  994. // 打开批量生成 Modal
  995. function openBatchModal() {
  996. document.getElementById('batch-prefix').value = 'VIP';
  997. document.getElementById('batch-count').value = '10';
  998. document.getElementById('batch-max-devices').value = '2';
  999. document.getElementById('batchModal').classList.add('show');
  1000. }
  1001. // 关闭批量生成 Modal
  1002. function closeBatchModal() {
  1003. document.getElementById('batchModal').classList.remove('show');
  1004. }
  1005. // 批量生成提交
  1006. async function handleBatchSubmit(event) {
  1007. event.preventDefault();
  1008. const prefix = document.getElementById('batch-prefix').value.trim();
  1009. const count = parseInt(document.getElementById('batch-count').value);
  1010. const maxDevices = parseInt(document.getElementById('batch-max-devices').value);
  1011. if (!prefix) {
  1012. showToast('请输入激活码前缀', 'warning');
  1013. return;
  1014. }
  1015. if (count <= 0 || count > 1000) {
  1016. showToast('生成数量必须在 1-1000 之间', 'warning');
  1017. return;
  1018. }
  1019. const confirmed = await showConfirmDialog(
  1020. `确定要批量生成 ${count} 个激活码吗?`,
  1021. '确认批量生成',
  1022. '生成',
  1023. 'primary'
  1024. );
  1025. if (!confirmed) {
  1026. return;
  1027. }
  1028. try {
  1029. const response = await apiRequest(`${API_BASE}/licenses/batch`, {
  1030. method: 'POST',
  1031. body: JSON.stringify({
  1032. prefix: prefix,
  1033. count: count,
  1034. max_devices: maxDevices
  1035. })
  1036. });
  1037. if (!response) return;
  1038. const result = await response.json();
  1039. if (result.code === 0) {
  1040. showToast(result.msg, 'success', 4000);
  1041. closeBatchModal();
  1042. loadLicenses(1); // 重新加载第一页
  1043. } else {
  1044. showToast('批量生成失败: ' + result.msg, 'error');
  1045. }
  1046. } catch (error) {
  1047. showToast('请求失败: ' + error.message, 'error');
  1048. }
  1049. }
  1050. // 点击 Modal 外部关闭
  1051. window.onclick = function(event) {
  1052. const licenseModal = document.getElementById('licenseModal');
  1053. const batchModal = document.getElementById('batchModal');
  1054. const confirmDialog = document.getElementById('confirmDialog');
  1055. if (event.target === licenseModal) {
  1056. closeModal();
  1057. }
  1058. if (event.target === batchModal) {
  1059. closeBatchModal();
  1060. }
  1061. if (event.target === confirmDialog) {
  1062. closeConfirmDialog(false);
  1063. }
  1064. }
  1065. </script>
  1066. </body>
  1067. </html>