| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014 |
- // 使用相对路径,自动适配当前域名
- const API_BASE = '/api';
- let currentPage = 1;
- let pageSize = 10;
- let total = 0;
- let editingId = null;
- let currentStatusFilter = ''; // 当前状态筛选:''(全部)、'activated'(已激活)、'unactivated'(未激活)
- let allLicenses = []; // 存储所有License数据用于统计
- // 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';
- }
-
- // 更新批量操作按钮
- updateBatchButtons();
-
- // 更新全选复选框状态
- 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');
- loadStatistics(); // 重新加载统计信息
- 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()) {
- loadStatistics();
- loadLicenses();
- }
- };
- // 加载统计信息
- async function loadStatistics() {
- try {
- const response = await apiRequest(`${API_BASE}/licenses/statistics`);
- if (!response) return;
-
- const result = await response.json();
- if (result.code === 0 && result.data) {
- const stats = result.data;
- document.getElementById('stat-total').textContent = stats.total || 0;
- document.getElementById('stat-activated').textContent = stats.activated || 0;
- document.getElementById('stat-unactivated').textContent = stats.unactivated || 0;
- document.getElementById('stat-devices').textContent = stats.total_devices || 0;
- document.getElementById('stats-container').style.display = 'grid';
- }
- } catch (error) {
- console.error('加载统计信息失败:', error);
- }
- }
- // 加载 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 {
- // 构建查询URL
- let url = `${API_BASE}/licenses?page=${page}&page_size=${pageSize}`;
- if (currentStatusFilter) {
- url += `&status=${currentStatusFilter}`;
- }
-
- const response = await apiRequest(url);
- 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';
-
- // 更新批量操作按钮显示
- updateBatchButtons();
- } else {
- showToast('加载失败: ' + result.msg, 'error');
- loadingEl.style.display = 'none';
- }
- } catch (error) {
- showToast('请求失败: ' + error.message, 'error');
- loadingEl.style.display = 'none';
- }
- }
- // 状态筛选处理
- function handleStatusFilter() {
- const filterSelect = document.getElementById('status-filter');
- currentStatusFilter = filterSelect.value;
- loadLicenses(1); // 重置到第一页
- }
- // 更新批量操作按钮显示
- function updateBatchButtons() {
- const checkboxes = document.querySelectorAll('.license-checkbox:checked');
- const batchUpdateBtn = document.getElementById('batch-update-btn');
- if (checkboxes.length > 0) {
- batchUpdateBtn.style.display = 'block';
- } else {
- batchUpdateBtn.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>
- <div style="max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;" title="${license.remark || ''}">
- ${license.remark || '<span style="color: #9ca3af;">无</span>'}
- </div>
- </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-remark').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-remark').value = license.remark || '';
- 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 || '[]';
- const remark = document.getElementById('license-remark').value || '';
- // 验证 boundDevices 是否为有效 JSON
- try {
- JSON.parse(boundDevices);
- } catch (e) {
- showToast('已绑定设备必须是有效的 JSON 数组格式', 'error');
- return;
- }
- try {
- let response;
- if (editingId) {
- // 更新
- const updateData = {
- max_devices: maxDevices,
- remark: remark // 总是发送remark字段,即使是空字符串
- };
- 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,
- remark: remark
- })
- });
- }
- if (!response) return;
-
- const result = await response.json();
- if (result.code === 0) {
- showToast(editingId ? '更新成功' : '创建成功', 'success');
- closeModal();
- loadStatistics(); // 重新加载统计信息
- 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');
- loadStatistics(); // 重新加载统计信息
- 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();
- loadStatistics(); // 重新加载统计信息
- 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');
- }
- // 导出CSV
- async function exportToCSV() {
- try {
- // 如果有筛选条件,先获取筛选后的数据,然后前端生成CSV
- if (currentStatusFilter) {
- showToast('正在导出筛选后的数据...', 'info');
-
- const listResponse = await apiRequest(`${API_BASE}/licenses?page=1&page_size=10000&status=${currentStatusFilter}`);
- if (!listResponse) return;
-
- const listResult = await listResponse.json();
- if (listResult.code === 0 && listResult.data.length > 0) {
- const licenses = listResult.data;
- const headers = ['ID', '激活码', '最大设备数', '已绑定设备数', '绑定设备列表', '创建时间', '更新时间'];
- const rows = licenses.map(license => {
- let boundDevices = [];
- try {
- boundDevices = JSON.parse(license.bound_devices || '[]');
- } catch (e) {
- boundDevices = [];
- }
-
- return [
- license.id,
- license.key,
- license.max_devices,
- boundDevices.length,
- boundDevices.join('; '),
- new Date(license.created_at).toLocaleString('zh-CN'),
- new Date(license.updated_at).toLocaleString('zh-CN')
- ];
- });
-
- function escapeCSVField(field) {
- if (field === null || field === undefined) return '';
- const str = String(field);
- if (str.includes(',') || str.includes('"') || str.includes('\n')) {
- return '"' + str.replace(/"/g, '""') + '"';
- }
- return str;
- }
-
- const csvContent = [
- headers.map(escapeCSVField).join(','),
- ...rows.map(row => row.map(escapeCSVField).join(','))
- ].join('\n');
-
- const BOM = '\uFEFF';
- const blob = new Blob([BOM + csvContent], { type: 'text/csv;charset=utf-8;' });
- const link = document.createElement('a');
- const url = URL.createObjectURL(blob);
- link.setAttribute('href', url);
-
- const now = new Date();
- const dateStr = now.toISOString().slice(0, 10).replace(/-/g, '');
- const timeStr = now.toTimeString().slice(0, 8).replace(/:/g, '');
- const statusStr = currentStatusFilter ? `_${currentStatusFilter}` : '';
- link.setAttribute('download', `licenses${statusStr}_${dateStr}_${timeStr}.csv`);
-
- link.style.visibility = 'hidden';
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
-
- showToast(`成功导出 ${licenses.length} 条记录`, 'success');
- } else {
- showToast('没有数据可导出', 'warning');
- }
- } else {
- // 没有筛选条件时,使用后端导出接口
- const token = getAuthToken();
- const url = `${API_BASE}/licenses/export?format=csv`;
-
- // 创建一个隐藏的表单来提交带token的请求
- const form = document.createElement('form');
- form.method = 'GET';
- form.action = url;
- form.style.display = 'none';
-
- // 添加token到请求头(通过fetch下载)
- const response = await fetch(url, {
- headers: getAuthHeaders()
- });
-
- if (!response.ok) {
- showToast('导出失败', 'error');
- return;
- }
-
- const blob = await response.blob();
- const downloadUrl = URL.createObjectURL(blob);
- const link = document.createElement('a');
- link.href = downloadUrl;
-
- const now = new Date();
- const dateStr = now.toISOString().slice(0, 10).replace(/-/g, '');
- const timeStr = now.toTimeString().slice(0, 8).replace(/:/g, '');
- link.setAttribute('download', `licenses_${dateStr}_${timeStr}.csv`);
-
- link.style.visibility = 'hidden';
- document.body.appendChild(link);
- link.click();
- document.body.removeChild(link);
- URL.revokeObjectURL(downloadUrl);
-
- showToast('导出成功', 'success');
- }
- } catch (error) {
- showToast('导出失败: ' + error.message, 'error');
- }
- }
- // 打开批量修改最大设备数弹框
- function openBatchUpdateModal() {
- const checkboxes = document.querySelectorAll('.license-checkbox:checked');
- const count = checkboxes.length;
-
- if (count === 0) {
- showToast('请至少选择一个 License', 'warning');
- return;
- }
-
- document.getElementById('batch-update-count').textContent = count;
- document.getElementById('batch-update-max-devices').value = '2';
- document.getElementById('batchUpdateModal').classList.add('show');
- }
- // 关闭批量修改弹框
- function closeBatchUpdateModal() {
- document.getElementById('batchUpdateModal').classList.remove('show');
- }
- // 批量修改最大设备数提交
- async function handleBatchUpdateSubmit(event) {
- event.preventDefault();
-
- const checkboxes = document.querySelectorAll('.license-checkbox:checked');
- const selectedIds = Array.from(checkboxes).map(cb => parseInt(cb.value));
- const maxDevices = parseInt(document.getElementById('batch-update-max-devices').value);
-
- if (selectedIds.length === 0) {
- showToast('请至少选择一个 License', 'warning');
- return;
- }
-
- if (maxDevices < 1) {
- showToast('最大设备数必须大于0', 'warning');
- return;
- }
-
- const confirmed = await showConfirmDialog(
- `确定要将选中的 ${selectedIds.length} 个 License 的最大设备数修改为 ${maxDevices} 吗?`,
- '确认批量修改',
- '确认',
- 'primary'
- );
-
- if (!confirmed) {
- return;
- }
-
- try {
- // 使用批量更新接口
- const response = await apiRequest(`${API_BASE}/licenses/batch/max-devices`, {
- method: 'PUT',
- body: JSON.stringify({
- ids: selectedIds,
- max_devices: maxDevices
- })
- });
-
- if (!response) return;
-
- const result = await response.json();
-
- if (result.code === 0) {
- showToast(result.msg, 'success');
- closeBatchUpdateModal();
- // 清除所有选中状态
- checkboxes.forEach(cb => cb.checked = false);
- updateSelectedCount();
- loadStatistics(); // 重新加载统计信息
- loadLicenses(currentPage); // 重新加载列表
- } else {
- showToast('批量修改失败: ' + result.msg, 'error');
- }
- } catch (error) {
- showToast('请求失败: ' + error.message, 'error');
- }
- }
- // 点击 Modal 外部关闭
- window.onclick = function(event) {
- const licenseModal = document.getElementById('licenseModal');
- const batchModal = document.getElementById('batchModal');
- const batchUpdateModal = document.getElementById('batchUpdateModal');
- const deviceListModal = document.getElementById('deviceListModal');
- const confirmDialog = document.getElementById('confirmDialog');
-
- if (event.target === licenseModal) {
- closeModal();
- }
- if (event.target === batchModal) {
- closeBatchModal();
- }
- if (event.target === batchUpdateModal) {
- closeBatchUpdateModal();
- }
- if (event.target === deviceListModal) {
- closeDeviceListModal();
- }
- if (event.target === confirmDialog) {
- closeConfirmDialog(false);
- }
- }
|