| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>License 管理平台</title>
- <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;
- padding: 20px;
- }
- .container {
- max-width: 1200px;
- margin: 0 auto;
- }
- .header {
- background: white;
- padding: 30px;
- border-radius: 10px;
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
- margin-bottom: 20px;
- display: flex;
- justify-content: space-between;
- align-items: center;
- }
- .header h1 {
- color: #333;
- font-size: 28px;
- }
- .btn {
- padding: 10px 20px;
- border: none;
- border-radius: 5px;
- cursor: pointer;
- font-size: 14px;
- transition: all 0.3s;
- font-weight: 500;
- }
- .btn-primary {
- background: #667eea;
- color: white;
- }
- .btn-primary:hover {
- background: #5568d3;
- transform: translateY(-2px);
- box-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
- }
- .btn-danger {
- background: #ef4444;
- color: white;
- }
- .btn-danger:hover {
- background: #dc2626;
- }
- .btn-secondary {
- background: #6b7280;
- color: white;
- }
- .btn-secondary:hover {
- background: #4b5563;
- }
- .card {
- background: white;
- border-radius: 10px;
- box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
- padding: 30px;
- margin-bottom: 20px;
- }
- .table-container {
- overflow-x: auto;
- }
- table {
- width: 100%;
- border-collapse: collapse;
- }
- th, td {
- padding: 12px;
- text-align: left;
- border-bottom: 1px solid #e5e7eb;
- }
- th {
- background: #f9fafb;
- font-weight: 600;
- color: #374151;
- }
- tr:hover {
- background: #f9fafb;
- }
- .badge {
- display: inline-block;
- padding: 4px 12px;
- border-radius: 12px;
- font-size: 12px;
- font-weight: 500;
- }
- .badge-success {
- background: #d1fae5;
- color: #065f46;
- }
- .badge-warning {
- background: #fef3c7;
- color: #92400e;
- }
- .modal {
- display: none;
- position: fixed;
- z-index: 1000;
- left: 0;
- top: 0;
- width: 100%;
- height: 100%;
- background: rgba(0, 0, 0, 0.5);
- animation: fadeIn 0.3s;
- }
- .modal.show {
- display: flex;
- align-items: center;
- justify-content: center;
- }
- @keyframes fadeIn {
- from { opacity: 0; }
- to { opacity: 1; }
- }
- .modal-content {
- background: white;
- border-radius: 10px;
- padding: 30px;
- width: 90%;
- max-width: 500px;
- animation: slideDown 0.3s;
- }
- @keyframes slideDown {
- from {
- transform: translateY(-50px);
- opacity: 0;
- }
- to {
- transform: translateY(0);
- opacity: 1;
- }
- }
- .modal-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 20px;
- }
- .modal-header h2 {
- color: #333;
- font-size: 24px;
- }
- .close {
- font-size: 28px;
- font-weight: bold;
- color: #999;
- cursor: pointer;
- border: none;
- background: none;
- }
- .close:hover {
- color: #333;
- }
- .form-group {
- margin-bottom: 20px;
- }
- .form-group label {
- display: block;
- margin-bottom: 8px;
- color: #374151;
- font-weight: 500;
- }
- .form-group input {
- width: 100%;
- padding: 10px;
- border: 1px solid #d1d5db;
- border-radius: 5px;
- font-size: 14px;
- }
- .form-group input:focus {
- outline: none;
- border-color: #667eea;
- box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
- }
- .form-actions {
- display: flex;
- gap: 10px;
- justify-content: flex-end;
- margin-top: 20px;
- }
- .pagination {
- display: flex;
- justify-content: center;
- align-items: center;
- gap: 10px;
- margin-top: 20px;
- }
- .pagination button {
- padding: 8px 16px;
- border: 1px solid #d1d5db;
- background: white;
- border-radius: 5px;
- cursor: pointer;
- }
- .pagination button:hover:not(:disabled) {
- background: #f9fafb;
- }
- .pagination button:disabled {
- opacity: 0.5;
- cursor: not-allowed;
- }
- .pagination .page-info {
- color: #6b7280;
- }
- .loading {
- text-align: center;
- padding: 40px;
- color: #6b7280;
- }
- .empty-state {
- text-align: center;
- padding: 60px 20px;
- color: #6b7280;
- }
- .empty-state svg {
- width: 100px;
- height: 100px;
- margin-bottom: 20px;
- opacity: 0.5;
- }
- .device-list {
- font-size: 12px;
- color: #6b7280;
- max-width: 200px;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .device-detail-cell {
- cursor: pointer;
- color: #667eea;
- font-size: 14px;
- padding: 8px 12px;
- border-radius: 5px;
- transition: all 0.2s;
- max-width: 300px;
- }
- .device-detail-cell:hover {
- background: #f3f4f6;
- color: #5568d3;
- }
- .device-detail-cell .device-count {
- font-weight: 600;
- color: #667eea;
- }
- .device-detail-cell .device-preview {
- font-size: 12px;
- color: #6b7280;
- margin-top: 4px;
- }
- /* 设备列表弹框样式 */
- .device-list-modal .modal-content {
- max-width: 700px;
- }
- .device-list-table {
- width: 100%;
- border-collapse: collapse;
- margin-top: 20px;
- }
- .device-list-table th,
- .device-list-table td {
- padding: 12px;
- text-align: left;
- border-bottom: 1px solid #e5e7eb;
- }
- .device-list-table th {
- background: #f9fafb;
- font-weight: 600;
- color: #374151;
- }
- .device-list-table tr:hover {
- background: #f9fafb;
- }
- .device-list-empty {
- text-align: center;
- padding: 40px;
- color: #6b7280;
- }
- .actions {
- display: flex;
- gap: 8px;
- }
- .btn-sm {
- padding: 6px 12px;
- font-size: 12px;
- }
- .license-key-cell {
- display: flex;
- align-items: center;
- gap: 8px;
- max-width: 200px;
- }
- .license-key-text {
- flex: 1;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- font-weight: 600;
- }
- .copy-btn {
- padding: 6px 12px;
- font-size: 12px;
- background: #667eea;
- color: white;
- border: none;
- border-radius: 5px;
- cursor: pointer;
- transition: all 0.2s;
- flex-shrink: 0;
- font-weight: 500;
- display: flex;
- align-items: center;
- gap: 4px;
- }
- .copy-btn:hover {
- background: #5568d3;
- transform: translateY(-1px);
- box-shadow: 0 2px 4px rgba(102, 126, 234, 0.3);
- }
- .copy-btn:active {
- transform: translateY(0);
- }
- /* Toast 通知样式 */
- .toast-container {
- position: fixed;
- top: 20px;
- right: 20px;
- z-index: 10000;
- display: flex;
- flex-direction: column;
- gap: 10px;
- }
- .toast {
- background: white;
- padding: 16px 20px;
- border-radius: 8px;
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
- display: flex;
- align-items: center;
- gap: 12px;
- min-width: 300px;
- max-width: 400px;
- animation: slideInRight 0.3s ease-out;
- position: relative;
- }
- @keyframes slideInRight {
- from {
- transform: translateX(100%);
- opacity: 0;
- }
- to {
- transform: translateX(0);
- opacity: 1;
- }
- }
- .toast.success {
- border-left: 4px solid #10b981;
- }
- .toast.error {
- border-left: 4px solid #ef4444;
- }
- .toast.warning {
- border-left: 4px solid #f59e0b;
- }
- .toast.info {
- border-left: 4px solid #3b82f6;
- }
- .toast-icon {
- font-size: 20px;
- flex-shrink: 0;
- }
- .toast.success .toast-icon {
- color: #10b981;
- }
- .toast.error .toast-icon {
- color: #ef4444;
- }
- .toast.warning .toast-icon {
- color: #f59e0b;
- }
- .toast.info .toast-icon {
- color: #3b82f6;
- }
- .toast-message {
- flex: 1;
- color: #374151;
- font-size: 14px;
- line-height: 1.5;
- }
- .toast-close {
- background: none;
- border: none;
- font-size: 18px;
- color: #9ca3af;
- cursor: pointer;
- padding: 0;
- width: 20px;
- height: 20px;
- display: flex;
- align-items: center;
- justify-content: center;
- flex-shrink: 0;
- }
- .toast-close:hover {
- color: #374151;
- }
- /* 确认对话框样式 */
- .confirm-dialog {
- display: none;
- position: fixed;
- z-index: 10001;
- left: 0;
- top: 0;
- width: 100%;
- height: 100%;
- background: rgba(0, 0, 0, 0.5);
- align-items: center;
- justify-content: center;
- animation: fadeIn 0.3s;
- }
- .confirm-dialog.show {
- display: flex;
- }
- .confirm-content {
- background: white;
- border-radius: 10px;
- padding: 30px;
- width: 90%;
- max-width: 400px;
- box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
- animation: slideDown 0.3s;
- }
- .confirm-icon {
- font-size: 48px;
- text-align: center;
- margin-bottom: 20px;
- }
- .confirm-title {
- font-size: 20px;
- font-weight: 600;
- color: #374151;
- text-align: center;
- margin-bottom: 15px;
- }
- .confirm-message {
- color: #6b7280;
- text-align: center;
- margin-bottom: 25px;
- line-height: 1.6;
- }
- .confirm-actions {
- display: flex;
- gap: 10px;
- justify-content: center;
- }
- .confirm-actions .btn {
- min-width: 100px;
- }
- </style>
- </head>
- <body>
- <!-- Toast 通知容器 -->
- <div class="toast-container" id="toast-container"></div>
- <!-- 确认对话框 -->
- <div id="confirmDialog" class="confirm-dialog">
- <div class="confirm-content">
- <div class="confirm-icon">⚠️</div>
- <div class="confirm-title" id="confirm-title">确认操作</div>
- <div class="confirm-message" id="confirm-message"></div>
- <div class="confirm-actions">
- <button class="btn btn-secondary" onclick="closeConfirmDialog(false)">取消</button>
- <button class="btn btn-danger" onclick="closeConfirmDialog(true)" id="confirm-ok-btn">确定</button>
- </div>
- </div>
- </div>
- <div class="container">
- <div class="header">
- <h1>🔑 License 管理平台</h1>
- <div style="display: flex; gap: 10px;">
- <button class="btn btn-primary" onclick="openBatchModal()">📦 批量生成</button>
- <button class="btn btn-primary" onclick="openCreateModal()">+ 创建 License</button>
- </div>
- </div>
- <div class="card">
- <div id="loading" class="loading" style="display: none;">加载中...</div>
- <div id="empty-state" class="empty-state" style="display: none;">
- <svg fill="none" stroke="currentColor" viewBox="0 0 24 24">
- <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>
- </svg>
- <p>暂无 License 数据</p>
- </div>
- <div class="table-container" id="table-container" style="display: none;">
- <div style="margin-bottom: 15px; display: flex; justify-content: space-between; align-items: center;">
- <div style="display: flex; align-items: center; gap: 10px;">
- <input type="checkbox" id="select-all" onchange="toggleSelectAll()" style="width: 18px; height: 18px; cursor: pointer;">
- <label for="select-all" style="cursor: pointer; user-select: none;">全选</label>
- <span id="selected-count" style="color: #6b7280; font-size: 14px;">已选择 0 项</span>
- </div>
- <button class="btn btn-danger" id="batch-delete-btn" onclick="batchDeleteLicenses()" style="display: none;">
- 批量删除
- </button>
- </div>
- <table>
- <thead>
- <tr>
- <th style="width: 50px;">
- <input type="checkbox" id="select-all-header" onchange="toggleSelectAll()" style="width: 18px; height: 18px; cursor: pointer;">
- </th>
- <th>ID</th>
- <th>激活码</th>
- <th>设备详情</th>
- <th>绑定设备数</th>
- <th>创建时间</th>
- <th>操作</th>
- </tr>
- </thead>
- <tbody id="license-table-body">
- </tbody>
- </table>
- <div class="pagination" id="pagination"></div>
- </div>
- </div>
- </div>
- <!-- 创建/编辑 Modal -->
- <div id="licenseModal" class="modal">
- <div class="modal-content">
- <div class="modal-header">
- <h2 id="modal-title">创建 License</h2>
- <button class="close" onclick="closeModal()">×</button>
- </div>
- <form id="licenseForm" onsubmit="handleSubmit(event)">
- <input type="hidden" id="license-id">
- <div class="form-group">
- <label for="license-key">激活码 *</label>
- <input type="text" id="license-key" required placeholder="例如: VIP-8888">
- </div>
- <div class="form-group">
- <label for="license-max-devices">最大设备数 *</label>
- <input type="number" id="license-max-devices" required min="1" value="2">
- </div>
- <div class="form-group">
- <label for="license-bound-devices">已绑定设备 (JSON 数组,可选)</label>
- <input type="text" id="license-bound-devices" placeholder='例如: ["device-1", "device-2"]'>
- </div>
- <div class="form-actions">
- <button type="button" class="btn btn-secondary" onclick="closeModal()">取消</button>
- <button type="submit" class="btn btn-primary">保存</button>
- </div>
- </form>
- </div>
- </div>
- <!-- 批量生成 Modal -->
- <div id="batchModal" class="modal">
- <div class="modal-content">
- <div class="modal-header">
- <h2>批量生成 License</h2>
- <button class="close" onclick="closeBatchModal()">×</button>
- </div>
- <form id="batchForm" onsubmit="handleBatchSubmit(event)">
- <div class="form-group">
- <label for="batch-prefix">激活码前缀 *</label>
- <input type="text" id="batch-prefix" required placeholder="例如: VIP" value="VIP">
- <small style="color: #6b7280; font-size: 12px; margin-top: 4px; display: block;">
- 生成的激活码格式:前缀-随机32位字符串(如:VIP-A3B9C2D4E5F6G7H8I9J0K1L2M3N4O5P6)
- </small>
- </div>
- <div class="form-group">
- <label for="batch-count">生成数量 *</label>
- <input type="number" id="batch-count" required min="1" max="1000" value="10">
- <small style="color: #6b7280; font-size: 12px; margin-top: 4px; display: block;">
- 一次最多可生成 1000 个
- </small>
- </div>
- <div class="form-group">
- <label for="batch-max-devices">最大设备数 *</label>
- <input type="number" id="batch-max-devices" required min="1" value="2">
- </div>
- <div class="form-actions">
- <button type="button" class="btn btn-secondary" onclick="closeBatchModal()">取消</button>
- <button type="submit" class="btn btn-primary">生成</button>
- </div>
- </form>
- </div>
- </div>
- <!-- 设备列表 Modal -->
- <div id="deviceListModal" class="modal device-list-modal">
- <div class="modal-content">
- <div class="modal-header">
- <h2 id="device-list-title">设备列表</h2>
- <button class="close" onclick="closeDeviceListModal()">×</button>
- </div>
- <div id="device-list-content">
- <div class="device-list-empty">加载中...</div>
- </div>
- </div>
- </div>
- <script>
- // 使用相对路径,自动适配当前域名
- const API_BASE = '/api';
- let currentPage = 1;
- let pageSize = 10;
- let total = 0;
- let editingId = null;
- // Toast 通知函数
- function showToast(message, type = 'info', duration = 3000) {
- const container = document.getElementById('toast-container');
- const toast = document.createElement('div');
- toast.className = `toast ${type}`;
-
- const icons = {
- success: '✅',
- error: '❌',
- warning: '⚠️',
- info: 'ℹ️'
- };
-
- toast.innerHTML = `
- <span class="toast-icon">${icons[type] || icons.info}</span>
- <span class="toast-message">${message}</span>
- <button class="toast-close" onclick="this.parentElement.remove()">×</button>
- `;
-
- container.appendChild(toast);
-
- // 自动移除
- setTimeout(() => {
- if (toast.parentElement) {
- toast.style.animation = 'slideInRight 0.3s ease-out reverse';
- setTimeout(() => {
- if (toast.parentElement) {
- toast.remove();
- }
- }, 300);
- }
- }, duration);
- }
- // 确认对话框函数
- let confirmCallback = null;
-
- function showConfirmDialog(message, title = '确认操作', okText = '确定', okType = 'danger') {
- return new Promise((resolve) => {
- document.getElementById('confirm-title').textContent = title;
- document.getElementById('confirm-message').textContent = message;
- const okBtn = document.getElementById('confirm-ok-btn');
- okBtn.textContent = okText;
- okBtn.className = `btn btn-${okType}`;
-
- confirmCallback = resolve;
- document.getElementById('confirmDialog').classList.add('show');
- });
- }
-
- function closeConfirmDialog(confirmed) {
- document.getElementById('confirmDialog').classList.remove('show');
- if (confirmCallback) {
- confirmCallback(confirmed);
- confirmCallback = null;
- }
- }
- // 复制激活码到剪贴板
- async function copyLicenseKey(key) {
- try {
- await navigator.clipboard.writeText(key);
- showToast('激活码已复制到剪贴板', 'success', 2000);
- } catch (err) {
- // 降级方案:使用传统方法
- const textArea = document.createElement('textarea');
- textArea.value = key;
- textArea.style.position = 'fixed';
- textArea.style.left = '-999999px';
- document.body.appendChild(textArea);
- textArea.select();
- try {
- document.execCommand('copy');
- showToast('激活码已复制到剪贴板', 'success', 2000);
- } catch (err) {
- showToast('复制失败,请手动复制', 'error');
- }
- document.body.removeChild(textArea);
- }
- }
- // 全选/取消全选
- function toggleSelectAll() {
- const selectAll = document.getElementById('select-all');
- const selectAllHeader = document.getElementById('select-all-header');
- const checkboxes = document.querySelectorAll('.license-checkbox');
-
- // 检查当前是否所有复选框都已选中
- const allChecked = checkboxes.length > 0 &&
- Array.from(checkboxes).every(checkbox => checkbox.checked);
-
- // 如果全部已选中,则取消全选;否则全选
- const isChecked = !allChecked;
-
- checkboxes.forEach(checkbox => {
- checkbox.checked = isChecked;
- });
-
- // 同步两个全选复选框
- selectAll.checked = isChecked;
- selectAllHeader.checked = isChecked;
-
- updateSelectedCount();
- }
- // 更新选中数量
- function updateSelectedCount() {
- const checkboxes = document.querySelectorAll('.license-checkbox:checked');
- const count = checkboxes.length;
- const selectedCountEl = document.getElementById('selected-count');
- const batchDeleteBtn = document.getElementById('batch-delete-btn');
-
- selectedCountEl.textContent = `已选择 ${count} 项`;
-
- if (count > 0) {
- batchDeleteBtn.style.display = 'block';
- } else {
- batchDeleteBtn.style.display = 'none';
- }
-
- // 更新全选复选框状态
- const allCheckboxes = document.querySelectorAll('.license-checkbox');
- const allChecked = allCheckboxes.length > 0 && checkboxes.length === allCheckboxes.length;
- document.getElementById('select-all').checked = allChecked;
- document.getElementById('select-all-header').checked = allChecked;
- }
- // 批量删除 License
- async function batchDeleteLicenses() {
- const checkboxes = document.querySelectorAll('.license-checkbox:checked');
- const selectedIds = Array.from(checkboxes).map(cb => parseInt(cb.value));
-
- if (selectedIds.length === 0) {
- showToast('请至少选择一个 License', 'warning');
- return;
- }
-
- const confirmed = await showConfirmDialog(
- `确定要删除选中的 ${selectedIds.length} 个 License 吗?此操作不可恢复!`,
- '确认批量删除',
- '删除',
- 'danger'
- );
-
- if (!confirmed) {
- return;
- }
-
- try {
- const response = await apiRequest(`${API_BASE}/licenses/batch`, {
- method: 'DELETE',
- body: JSON.stringify({
- ids: selectedIds
- })
- });
- if (!response) return;
-
- const result = await response.json();
-
- if (result.code === 0) {
- showToast(result.msg, 'success');
- loadLicenses(currentPage);
- } else {
- showToast('批量删除失败: ' + result.msg, 'error');
- }
- } catch (error) {
- showToast('请求失败: ' + error.message, 'error');
- }
- }
- // 获取认证token
- function getAuthToken() {
- return localStorage.getItem('auth_token');
- }
- // 检查是否已登录
- function checkAuth() {
- const token = getAuthToken();
- if (!token) {
- window.location.href = '/web/login.html';
- return false;
- }
- return true;
- }
- // 获取API请求头(包含token)
- function getAuthHeaders() {
- const token = getAuthToken();
- return {
- 'Content-Type': 'application/json',
- 'Authorization': `Bearer ${token}`
- };
- }
- // 处理API错误响应
- async function handleApiError(response) {
- if (response.status === 401) {
- // 未授权,清除token并跳转到登录页
- localStorage.removeItem('auth_token');
- showToast('登录已过期,请重新登录', 'error');
- setTimeout(() => {
- window.location.href = '/web/login.html';
- }, 1000);
- return true;
- }
- return false;
- }
- // 统一的API请求函数
- async function apiRequest(url, options = {}) {
- const headers = getAuthHeaders();
- if (options.headers) {
- Object.assign(headers, options.headers);
- }
-
- const response = await fetch(url, {
- ...options,
- headers: headers
- });
-
- if (await handleApiError(response)) {
- return null;
- }
-
- return response;
- }
- // 页面加载时检查登录状态
- window.onload = () => {
- if (checkAuth()) {
- loadLicenses();
- }
- };
- // 加载 License 列表
- async function loadLicenses(page = 1) {
- currentPage = page;
- const loadingEl = document.getElementById('loading');
- const tableContainer = document.getElementById('table-container');
- const emptyState = document.getElementById('empty-state');
- loadingEl.style.display = 'block';
- tableContainer.style.display = 'none';
- emptyState.style.display = 'none';
- try {
- const response = await apiRequest(`${API_BASE}/licenses?page=${page}&page_size=${pageSize}`);
- if (!response) return;
-
- const result = await response.json();
- if (result.code === 0) {
- total = result.total;
- const licenses = result.data;
- if (licenses.length === 0) {
- loadingEl.style.display = 'none';
- emptyState.style.display = 'block';
- return;
- }
- renderTable(licenses);
- renderPagination();
- loadingEl.style.display = 'none';
- tableContainer.style.display = 'block';
- } else {
- showToast('加载失败: ' + result.msg, 'error');
- loadingEl.style.display = 'none';
- }
- } catch (error) {
- showToast('请求失败: ' + error.message, 'error');
- loadingEl.style.display = 'none';
- }
- }
- // 渲染表格
- function renderTable(licenses) {
- const tbody = document.getElementById('license-table-body');
- tbody.innerHTML = licenses.map(license => {
- let boundDevices = [];
- try {
- boundDevices = JSON.parse(license.bound_devices || '[]');
- } catch (e) {
- boundDevices = [];
- }
-
- // 解析设备激活时间
- let deviceActivations = {};
- try {
- const activationsStr = JSON.parse(license.device_activations || '{}');
- deviceActivations = activationsStr;
- } catch (e) {
- deviceActivations = {};
- }
-
- const boundCount = boundDevices.length;
- const isFull = boundCount >= license.max_devices;
- const createdDate = new Date(license.created_at).toLocaleString('zh-CN');
- // 限制激活码显示长度为10个字符
- const displayKey = license.key.length > 10 ? license.key.substring(0, 10) + '...' : license.key;
-
- // 构建设备详情显示
- let deviceDetailHtml = '';
- if (boundDevices.length === 0) {
- deviceDetailHtml = '<span style="color: #9ca3af;">无设备</span>';
- } else {
- // 显示前2个设备作为预览
- const previewDevices = boundDevices.slice(0, 2);
- const previewText = previewDevices.map(deviceId => {
- const activationTime = deviceActivations[deviceId];
- if (activationTime) {
- const date = new Date(activationTime);
- return `${deviceId} (${date.toLocaleString('zh-CN')})`;
- }
- return `${deviceId} (未记录)`;
- }).join('、');
-
- const moreCount = boundDevices.length - 2;
- // 使用 data 属性安全传递 licenseKey
- const escapedKey = license.key.replace(/"/g, '"').replace(/'/g, ''');
- deviceDetailHtml = `
- <div class="device-detail-cell" data-license-id="${license.id}" data-license-key="${escapedKey}" onclick="showDeviceListFromElement(this)">
- <div class="device-count">${boundCount} 个设备</div>
- <div class="device-preview">${previewText}${moreCount > 0 ? ` 等${moreCount}个...` : ''}</div>
- </div>
- `;
- }
-
- return `
- <tr>
- <td style="text-align: center;">
- <input type="checkbox" class="license-checkbox" value="${license.id}" onchange="updateSelectedCount()" style="width: 18px; height: 18px; cursor: pointer;">
- </td>
- <td>${license.id}</td>
- <td>
- <div class="license-key-cell" title="${license.key}">
- <span class="license-key-text">${displayKey}</span>
- <button class="copy-btn" onclick="copyLicenseKey('${license.key}')" title="复制激活码">
- <span>复制</span>
- </button>
- </div>
- </td>
- <td>
- ${deviceDetailHtml}
- </td>
- <td>
- <span class="badge ${isFull ? 'badge-warning' : 'badge-success'}">
- ${boundCount} / ${license.max_devices}
- </span>
- </td>
- <td>${createdDate}</td>
- <td>
- <div class="actions">
- <button class="btn btn-primary btn-sm" onclick="editLicense(${license.id})">编辑</button>
- <button class="btn btn-danger btn-sm" onclick="deleteLicense(${license.id}, '${license.key}')">删除</button>
- </div>
- </td>
- </tr>
- `;
- }).join('');
- }
- // 渲染分页
- function renderPagination() {
- const pagination = document.getElementById('pagination');
- const totalPages = Math.ceil(total / pageSize);
- if (totalPages <= 1) {
- pagination.innerHTML = '';
- return;
- }
- pagination.innerHTML = `
- <button onclick="loadLicenses(${currentPage - 1})" ${currentPage === 1 ? 'disabled' : ''}>
- 上一页
- </button>
- <span class="page-info">第 ${currentPage} / ${totalPages} 页 (共 ${total} 条)</span>
- <button onclick="loadLicenses(${currentPage + 1})" ${currentPage >= totalPages ? 'disabled' : ''}>
- 下一页
- </button>
- `;
- }
- // 打开创建 Modal
- function openCreateModal() {
- editingId = null;
- document.getElementById('modal-title').textContent = '创建 License';
- document.getElementById('license-id').value = '';
- document.getElementById('license-key').value = '';
- document.getElementById('license-max-devices').value = '2';
- document.getElementById('license-bound-devices').value = '';
- document.getElementById('license-key').disabled = false;
- document.getElementById('licenseModal').classList.add('show');
- }
- // 编辑 License
- async function editLicense(id) {
- try {
- const response = await apiRequest(`${API_BASE}/licenses/${id}`);
- if (!response) return;
-
- const result = await response.json();
- if (result.code === 0) {
- const license = result.data;
- editingId = id;
- document.getElementById('modal-title').textContent = '编辑 License';
- document.getElementById('license-id').value = id;
- document.getElementById('license-key').value = license.key;
- document.getElementById('license-max-devices').value = license.max_devices;
- document.getElementById('license-bound-devices').value = license.bound_devices || '[]';
- document.getElementById('license-key').disabled = false;
- document.getElementById('licenseModal').classList.add('show');
- } else {
- showToast('加载失败: ' + result.msg, 'error');
- }
- } catch (error) {
- showToast('请求失败: ' + error.message, 'error');
- }
- }
- // 关闭 Modal
- function closeModal() {
- document.getElementById('licenseModal').classList.remove('show');
- }
- // 提交表单
- async function handleSubmit(event) {
- event.preventDefault();
- const id = document.getElementById('license-id').value;
- const key = document.getElementById('license-key').value;
- const maxDevices = parseInt(document.getElementById('license-max-devices').value);
- const boundDevices = document.getElementById('license-bound-devices').value || '[]';
- // 验证 boundDevices 是否为有效 JSON
- try {
- JSON.parse(boundDevices);
- } catch (e) {
- showToast('已绑定设备必须是有效的 JSON 数组格式', 'error');
- return;
- }
- try {
- let response;
- if (editingId) {
- // 更新
- const updateData = {
- max_devices: maxDevices
- };
- if (boundDevices) {
- updateData.bound_devices = boundDevices;
- }
- response = await apiRequest(`${API_BASE}/licenses/${id}`, {
- method: 'PUT',
- body: JSON.stringify(updateData)
- });
- } else {
- // 创建
- response = await apiRequest(`${API_BASE}/licenses`, {
- method: 'POST',
- body: JSON.stringify({
- key: key,
- max_devices: maxDevices,
- bound_devices: boundDevices
- })
- });
- }
- if (!response) return;
-
- const result = await response.json();
- if (result.code === 0) {
- showToast(editingId ? '更新成功' : '创建成功', 'success');
- closeModal();
- loadLicenses(currentPage);
- } else {
- showToast('操作失败: ' + result.msg, 'error');
- }
- } catch (error) {
- showToast('请求失败: ' + error.message, 'error');
- }
- }
- // 删除 License
- async function deleteLicense(id, key) {
- const confirmed = await showConfirmDialog(
- `确定要删除 License "${key}" 吗?此操作不可恢复!`,
- '确认删除',
- '删除',
- 'danger'
- );
-
- if (!confirmed) {
- return;
- }
- try {
- const response = await apiRequest(`${API_BASE}/licenses/${id}`, {
- method: 'DELETE'
- });
- if (!response) return;
-
- const result = await response.json();
- if (result.code === 0) {
- showToast('删除成功', 'success');
- loadLicenses(currentPage);
- } else {
- showToast('删除失败: ' + result.msg, 'error');
- }
- } catch (error) {
- showToast('请求失败: ' + error.message, 'error');
- }
- }
- // 打开批量生成 Modal
- function openBatchModal() {
- document.getElementById('batch-prefix').value = 'VIP';
- document.getElementById('batch-count').value = '10';
- document.getElementById('batch-max-devices').value = '2';
- document.getElementById('batchModal').classList.add('show');
- }
- // 关闭批量生成 Modal
- function closeBatchModal() {
- document.getElementById('batchModal').classList.remove('show');
- }
- // 批量生成提交
- async function handleBatchSubmit(event) {
- event.preventDefault();
- const prefix = document.getElementById('batch-prefix').value.trim();
- const count = parseInt(document.getElementById('batch-count').value);
- const maxDevices = parseInt(document.getElementById('batch-max-devices').value);
- if (!prefix) {
- showToast('请输入激活码前缀', 'warning');
- return;
- }
- if (count <= 0 || count > 1000) {
- showToast('生成数量必须在 1-1000 之间', 'warning');
- return;
- }
- const confirmed = await showConfirmDialog(
- `确定要批量生成 ${count} 个激活码吗?`,
- '确认批量生成',
- '生成',
- 'primary'
- );
-
- if (!confirmed) {
- return;
- }
- try {
- const response = await apiRequest(`${API_BASE}/licenses/batch`, {
- method: 'POST',
- body: JSON.stringify({
- prefix: prefix,
- count: count,
- max_devices: maxDevices
- })
- });
- if (!response) return;
- const result = await response.json();
- if (result.code === 0) {
- showToast(result.msg, 'success', 4000);
- closeBatchModal();
- loadLicenses(1); // 重新加载第一页
- } else {
- showToast('批量生成失败: ' + result.msg, 'error');
- }
- } catch (error) {
- showToast('请求失败: ' + error.message, 'error');
- }
- }
- // 从元素获取数据并显示设备列表弹框
- function showDeviceListFromElement(element) {
- const licenseId = parseInt(element.getAttribute('data-license-id'));
- const licenseKey = element.getAttribute('data-license-key');
- showDeviceList(licenseId, licenseKey);
- }
- // 显示设备列表弹框
- async function showDeviceList(licenseId, licenseKey) {
- try {
- const response = await apiRequest(`${API_BASE}/licenses/${licenseId}`);
- if (!response) return;
-
- const result = await response.json();
- if (result.code === 0) {
- const license = result.data;
- let boundDevices = [];
- try {
- boundDevices = JSON.parse(license.bound_devices || '[]');
- } catch (e) {
- boundDevices = [];
- }
-
- // 解析设备激活时间
- let deviceActivations = {};
- try {
- const activationsStr = JSON.parse(license.device_activations || '{}');
- deviceActivations = activationsStr;
- } catch (e) {
- deviceActivations = {};
- }
-
- // 解析设备心跳时间
- let deviceHeartbeats = {};
- try {
- const heartbeatsStr = JSON.parse(license.device_heartbeats || '{}');
- deviceHeartbeats = heartbeatsStr;
- } catch (e) {
- deviceHeartbeats = {};
- }
-
- // 设置标题
- document.getElementById('device-list-title').textContent = `设备列表 - ${licenseKey}`;
-
- // 渲染设备列表
- const contentEl = document.getElementById('device-list-content');
- if (boundDevices.length === 0) {
- contentEl.innerHTML = `
- <div class="device-list-empty">
- <p>暂无绑定设备</p>
- </div>
- `;
- } else {
- const tableHtml = `
- <table class="device-list-table">
- <thead>
- <tr>
- <th>序号</th>
- <th>设备ID</th>
- <th>激活时间</th>
- <th>最近心跳时间</th>
- </tr>
- </thead>
- <tbody>
- ${boundDevices.map((deviceId, index) => {
- const activationTime = deviceActivations[deviceId];
- let timeDisplay = '<span style="color: #9ca3af;">未记录</span>';
- if (activationTime) {
- const date = new Date(activationTime);
- timeDisplay = date.toLocaleString('zh-CN');
- }
-
- // 处理心跳时间显示
- const heartbeatTime = deviceHeartbeats[deviceId];
- let heartbeatDisplay = '<span style="color: #9ca3af;">未记录</span>';
- if (heartbeatTime) {
- const heartbeatDate = new Date(heartbeatTime);
- const now = new Date();
- const diff = now - heartbeatDate;
- const seconds = Math.floor(diff / 1000);
- const minutes = Math.floor(seconds / 60);
- const hours = Math.floor(minutes / 60);
- const days = Math.floor(hours / 24);
-
- // 格式化相对时间
- let relativeTime = '';
- let heartbeatColor = '#6b7280'; // 默认灰色
- if (days > 0) {
- relativeTime = `${days}天前`;
- heartbeatColor = '#ef4444'; // 红色 - 很久没心跳
- } else if (hours > 0) {
- relativeTime = `${hours}小时前`;
- heartbeatColor = '#f59e0b'; // 橙色 - 较久
- } else if (minutes > 0) {
- relativeTime = `${minutes}分钟前`;
- heartbeatColor = minutes < 10 ? '#10b981' : '#f59e0b'; // 绿色(10分钟内)或橙色
- } else if (seconds > 0) {
- relativeTime = `${seconds}秒前`;
- heartbeatColor = '#10b981'; // 绿色
- } else {
- relativeTime = '刚刚';
- heartbeatColor = '#10b981'; // 绿色
- }
-
- heartbeatDisplay = `${heartbeatDate.toLocaleString('zh-CN')} <span style="color: ${heartbeatColor}; font-size: 11px; font-weight: 400; margin-left: 6px;">(${relativeTime})</span>`;
- }
-
- return `
- <tr>
- <td>${index + 1}</td>
- <td><strong>${deviceId}</strong></td>
- <td>${timeDisplay}</td>
- <td>${heartbeatDisplay}</td>
- </tr>
- `;
- }).join('')}
- </tbody>
- </table>
- <div style="margin-top: 15px; color: #6b7280; font-size: 14px; text-align: right;">
- 共 ${boundDevices.length} 个设备
- </div>
- `;
- contentEl.innerHTML = tableHtml;
- }
-
- document.getElementById('deviceListModal').classList.add('show');
- } else {
- showToast('加载失败: ' + result.msg, 'error');
- }
- } catch (error) {
- showToast('请求失败: ' + error.message, 'error');
- }
- }
- // 关闭设备列表弹框
- function closeDeviceListModal() {
- document.getElementById('deviceListModal').classList.remove('show');
- }
- // 点击 Modal 外部关闭
- window.onclick = function(event) {
- const licenseModal = document.getElementById('licenseModal');
- const batchModal = document.getElementById('batchModal');
- const deviceListModal = document.getElementById('deviceListModal');
- const confirmDialog = document.getElementById('confirmDialog');
-
- if (event.target === licenseModal) {
- closeModal();
- }
- if (event.target === batchModal) {
- closeBatchModal();
- }
- if (event.target === deviceListModal) {
- closeDeviceListModal();
- }
- if (event.target === confirmDialog) {
- closeConfirmDialog(false);
- }
- }
- </script>
- </body>
- </html>
|