// 全局变量定义 let page = 11; // 初始页码为1,代表从第1条数据开始获取 let pageSize = 20; // 初始每次固定获取10条数据 let total = 0; // 数据总量(从接口获取) let loadedItems = 0; // 已加载的数据条目数量 let isLoading = false; // 防止多次加载 const timeoutDuration = 10000; // 超时时间10秒 const preLoadDistance = 300; // 距离底部300px时提前加载 // 假设 Authorization 值存储在 localStorage 中,key 为 "authToken" const authToken = localStorage.getItem("Authorization"); const currentUserInfo = fetchUsername(); // 获取当前用户登录信息 let currentUserPermissions; // 用于存储用户权限信息 //获取 主内容区域 const license_info_mainElement = document.querySelector('main'); // 主内容区域 //模态框 const license_info_modal = document.getElementById('license-info-modal'); // 模态框容器 const license_info_modalContent = document.querySelector('.license-info-modal-content'); // 模态框内容区域 const license_info_modalDescription = document.getElementById('license-info-modal-description'); // 模态框描述 const license_info_modalPrice = document.getElementById('license-info-modal-price'); // 模态框产品信息 const license_info_modalRating = document.getElementById('license-info-modal-rating'); // 模态框MAC地址 const license_info_closeModal = document.querySelector('.license-info-close'); // 模态框关闭按钮 const license_info_loadingIndicator = document.getElementById('loading-indicator'); // 加载提示元素 //存储 let LicApplicationData = []; // 用于存储从接口获取的数据 // 统一的打开模态框函数 function openModal(modalId) { const modal = document.getElementById(modalId); if (modal) { modal.style.display = "block"; // 显示模态框 } else { console.error('模态框不存在,ID:', modalId); } } // 统一的关闭模态框函数 function closeModal(modalId) { const modal = document.getElementById(modalId); if (modal) { modal.style.display = "none"; // 隐藏模态框 } else { console.error('模态框不存在,ID:', modalId); } } //-----------侧边栏---------------------------- // 获取所有菜单项 const menuItems = document.querySelectorAll('nav ul li a'); // 为每个菜单项添加点击事件监听器 menuItems.forEach(item => { item.addEventListener('click', function() { // 移除其他项的 active 类 menuItems.forEach(i => i.classList.remove('active')); // 为当前点击的项添加 active 类 this.classList.add('active'); }); }); //用户管理- //获取用户管理和 License 信息按钮 const userManagementLink = document.getElementById('user-management-link'); const licenseInfoLink = document.getElementById('license-info-link'); const roleManagementLink = document.getElementById('role-management-link'); // 监听用户管理按钮的点击事件 userManagementLink.addEventListener('click', function(event) { event.preventDefault(); // 阻止默认的跳转行为 removeScrollListeners(); // 移除滚动监听器 // 使用 fetch 来加载 user_management.html 的内容 fetch('../user/user_management.html') .then(response => response.text()) .then(data => { // 将 user_management.html 的内容插入到主内容区域 license_info_mainElement.innerHTML = data; // 动态引入 user.js 文件 const script = document.createElement('script'); script.src = '../user/user.js'; document.body.appendChild(script); }) .catch(error => console.error('加载用户管理页面失败:', error)); }); // 监听 License 信息按钮的点击事件 licenseInfoLink.addEventListener('click', function(event) { event.preventDefault(); // 阻止默认的跳转行为 // 将瀑布流的 License 信息内容恢复到主内容区域 const licenseInfoHtml = `
${firstItem.ApplicationDate} ${firstItem.ApplicationTime}
创建者:${firstItem.Creator}
公司:${firstItem.Company}
集群:${childRowCount} 套 共计:${firstItem.TotalNodes} 节点
许可证状态:${firstItem.LicenseFlage}
oa_request_id:${firstItem.oa_request_id}
${firstItem.ApplicationDate} ${firstItem.ApplicationTime}
公司:${firstItem.Company}
${firstItem.Project}
创建者:${firstItem.Creator}
许可证状态:${firstItem.LicenseFlage}
节点数: ${group.NodeCount}
数据库版本: ${group.ProductName}${group.ProductVersion}
CPU 型号: ${group.oa_cpu}
操作系统环境: ${group.oa_operating_system}
以下为测试显示:
oa_id: ${group.oa_id}
oa_request_id: ${group.oa_request_id}
`; // 将生成的组内容加入到 modalBody modalBody.appendChild(groupBox); } // 更新下拉框的值 selectPageDropdown.value = page; // 更新按钮状态 prevButton.disabled = (page === 1); nextButton.disabled = (page === totalPages); } // 下拉框页码选择事件 selectPageDropdown.addEventListener('change', function() { currentPage = parseInt(this.value); ModalForCardRenderPage(currentPage); // 根据选择的页码渲染对应页面 }); // 上一页按钮点击事件 prevButton.addEventListener('click', function() { if (currentPage > 1) { currentPage--; ModalForCardRenderPage(currentPage); } }); // 下一页按钮点击事件 nextButton.addEventListener('click', function() { if (currentPage < totalPages) { currentPage++; ModalForCardRenderPage(currentPage); } }); // 显示或隐藏分页相关控件 function togglePaginationVisibility() { if (totalPages <= 1) { // 隐藏下拉框和分页容器 paginationContainer.style.display = 'none'; } else { // 显示下拉框和分页容器 paginationContainer.style.display = 'flex'; } } // 初始化下拉框并渲染第一页 initializeDropdown(); // 渲染模态框内容 ModalForCardRenderPage(currentPage); // 根据页数决定是否显示分页控件 togglePaginationVisibility(); // 显示模态框 modal.style.display = 'flex'; // 显示模态框背景 setTimeout(() => { modalContent.classList.add('show'); // 添加动画效果 }, 10); // 延时确保动画生效 // 关闭模态框逻辑 const closeModal = document.querySelector('.license-info-close'); closeModal.addEventListener('click', () => { modalContent.classList.remove('show'); // 移除动画类 setTimeout(() => { modal.style.display = 'none'; // 完全隐藏模态框 }, 500); // 等待动画结束后再隐藏 }); // 点击模态框外部关闭模态框 window.addEventListener('click', (event) => { if (event.target === modal) { modalContent.classList.remove('show'); setTimeout(() => { modal.style.display = 'none'; // 完全隐藏模态框 }, 500); // 等待动画结束后再隐藏 } }); } //-------下载全部licstr按钮 // 下载许可证功能 function downloadAllLicenses(sortedApplicationArray) { const zip = new JSZip(); console.log("传进来的 sortedApplicationArray:", sortedApplicationArray); // 初始化计数器,从1开始 let idCounter = 1; let Project = sortedApplicationArray[0].Project; console.log("Project", Project); // 遍历 sortedApplicationArray,下载 lic1 和 lic2 数据 sortedApplicationArray.forEach(row => { if (row.LicenseFlage === "已生成") { // 替换 oa_main_mac 和 oa_second_mac 中的冒号为点 const mainMac = row.oa_main_mac.replace(/:/g, '.'); const secondMac = row.oa_second_mac.replace(/:/g, '.'); // 使用递增的 idCounter 替换 row.oa_id if (row.lic1) { const filename1 = `${idCounter}_license.dat_1_${mainMac}`; zip.file(filename1, row.lic1); } if (row.lic2) { const filename2 = `${idCounter}_license.dat_2_${secondMac}`; zip.file(filename2, row.lic2); } // 每次循环后递增计数器 idCounter++; } }); // 生成 ZIP 文件并触发下载 zip.generateAsync({ type: "blob" }).then(content => { const link = document.createElement('a'); link.href = URL.createObjectURL(content); link.download = `${Project}_license.zip`; link.click(); }); } // 刷新数据并滚动到目标卡片的函数 // 刷新数据并滚动到目标卡片,同时重新打开目标卡片的模态框 async function refreshLicenseDataAndScrollAndOpenModal(selfPage,selfPageSize, targetCardId) { const data = await fetchLicenseData(selfPage, selfPageSize); if (data.length > 0) { isLoading = true; console.log('加载的数据:', data); // 检查是否成功获取数据 renderLicenseCards(data, true); // 渲染数据到页面并清空之前的内容 page = selfPageSize+1; pageSize= selfPageSize +10; // 滚动到目标卡片 if (targetCardId) { const targetCard = document.querySelector(`[data-oa-request-id="${targetCardId}"]`); if (targetCard) { targetCard.scrollIntoView({ behavior: 'smooth', block: 'center' }); // 延迟打开模态框,确保页面滚动完成 setTimeout(() => { targetCard.click(); // 模拟点击卡片,触发模态框 }, 500); // 延时500ms确保滚动完成 } } setTimeout(() => { isLoading = false; }, 2000); } else { console.error('未加载到数据'); } } function generateLicense(id, isParentRow) { // 显示加载进度条并设置动态提示信息 showLoadingModal('正在生成 License...'); // const payload = isParentRow ? { oa_request_id: JSON.stringify(id) } : { uniqueID:JSON.stringify(id) }; const payload = isParentRow ? { oa_request_id: parseInt(id, 10) } // 将 id 转换为整数 : { uniqueID: parseInt(id, 10) }; // 将 id 转换为整数 console.log("generateLicense",payload ,id, isParentRow) fetch('http://127.0.0.1:8080/api/admin/GenerateLicense', { method: 'POST', headers: { 'Authorization': `Bearer ${authToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }) .then(response => response.json()) .then(data => { if (data.success) { // 隐藏加载进度条 hideLoadingModal(); alert('License 生成成功!'); //刷新页面 // 调用封装的更新方法 //updateCardAndModalStatus(id, isParentRow); // 调用刷新函数,传入当前的 page 和 pageSize refreshLicenseDataAndScrollAndOpenModal(1,pageSize,id); } else { // 请求失败时隐藏加载进度条 hideLoadingModal(); alert('License 生成失败:' + data.error); } }) .catch(error => { console.error('生成过程中出现错误Error:', error); // 请求失败时隐藏加载进度条 hideLoadingModal(); alert('生成过程中出现错误,请稍后再试。',error); }); } //更新卡片样式 function updateCardAndModalStatus(id, isParentRow) { // 获取对应的卡片元素,通过 oa_request_id 或 uniqueID 定位 const cardSelector = isParentRow ? `[data-oa-request-id="${id}"]` : `[data-unique-id="${id}"]`; const card = document.querySelector(cardSelector); console.log("generateLicense card", cardSelector, card); if (card) { // 1. 更新卡片内许可证状态 const statusElement = card.querySelector('.license-status'); console.log("statusElement:", statusElement); // 检查状态元素是否存在 if (statusElement) { // 只更新许可证状态部分的文本,而不是整个 p 标签 statusElement.innerHTML = '许可证状态:已生成'; // 更新状态的 CSS 类 statusElement.classList.remove('license-status-yellow', 'license-status-red'); statusElement.classList.add('license-status-green'); } else { console.error('找不到 .license-status 元素'); } // 2. 更新模态框中的状态(如果模态框已经打开) const modalStatusElement = document.querySelector('.license-info-modal-header .license-status'); console.log("modalStatusElement:", modalStatusElement); // 检查状态元素是否存在 if (modalStatusElement) { modalStatusElement.textContent = '许可证状态:已生成'; modalStatusElement.classList.remove('license-status-yellow', 'license-status-red'); modalStatusElement.classList.add('license-status-green'); } else { console.error('找不到 #license-info-modal-body .license-status 元素'); } // 3. 更新模态框中的按钮为“分发” const generateButton = document.getElementById('button1'); // 获取生成按钮 if (generateButton) { generateButton.textContent = '分发'; // 修改按钮文本为“分发” generateButton.removeEventListener('click', generateLicense); // 移除生成逻辑 generateButton.addEventListener('click', () => { distributeLicense(id); // 添加分发逻辑 }); } // 4. 启用 #downloadAllLicenses-button const downloadButton = document.getElementById('downloadAllLicenses-button'); if (downloadButton) { downloadButton.disabled = false; // 启用按钮 downloadButton.style.backgroundColor = '#007aff'; // 恢复正常的背景颜色 downloadButton.style.cursor = 'pointer'; // 修改鼠标样式为可点击 } } else { console.error(`找不到与 ID ${id} 对应的卡片`); } } ///-----------获取登录用户信息---------------------------------------- async function fetchUsername() { try { const response = await fetch(`http://127.0.0.1:8080/api/admin/userInfo`, { method: 'GET', headers: { 'Authorization': `Bearer ${authToken}`, 'Content-Type': 'application/json' } }); const data = await response.json(); currentUserRole = data.data.Role; // 存储当前用户的角色 currentUserName = data.data.Username; // 使用获取到的角色,调用获取权限的接口 await fetchPermissionsByRole(currentUserRole); console.log('当前用户角色:', data); return data.data; // 返回获取到的用户信息数据 } catch (error) { console.error('Error fetching user info or permissions:', error); } } // 将 fetchPermissionsByRole 转换为异步函数 async function fetchPermissionsByRole(role) { try { const response = await fetch(`http://127.0.0.1:8080/api/admin/GetSelfRoles`, { method: 'POST', headers: { 'Authorization': `Bearer ${authToken}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ name: role }) }); const data = await response.json(); currentUserPermissions = data.data.Permissions; // 获取用户的权限数组 console.log('currentUserPermissions:', currentUserPermissions); // 定义权限类别 // const licensePermissions = ['upload_license', 'read_license']; // const userPermissionsCheck = ['create_user', 'read_user', 'update_user', 'delete_user']; // const rolePermissions = ['create_role', 'delete_role', 'update_role', 'get_role']; // const hasLicenseAccess = licensePermissions.some(permission => userPermissions.includes(permission)); // const hasUserManagementAccess = userPermissionsCheck.some(permission => userPermissions.includes(permission)); // const hasRoleManagementAccess = rolePermissions.some(permission => userPermissions.includes(permission)); // 根据权限渲染菜单并显示初始页面 //renderMenuAndInitialPage(hasLicenseAccess, hasUserManagementAccess, hasRoleManagementAccess); } catch (error) { console.error('Error fetching permissions:', error); } } //--------------进度条------------------------------------------------- // 创建模态框的 DOM 元素并插入到 body 中 function createLoadingModal() { const modalHTML = ` `; document.body.insertAdjacentHTML('beforeend', modalHTML); } // 显示加载模态框 function showLoadingModal(message = "加载中...") { const loadingModal = document.getElementById('loadingModal'); const loadingMessage = document.getElementById('loadingMessage'); if (loadingModal && loadingMessage) { loadingMessage.textContent = message; // 设置显示的消息 loadingModal.style.display = 'flex'; // 显示模态框 } } // 隐藏加载模态框 function hideLoadingModal() { const loadingModal = document.getElementById('loadingModal'); if (loadingModal) { loadingModal.style.display = 'none'; // 隐藏模态框 } } // 页面加载时创建模态框 document.addEventListener('DOMContentLoaded', createLoadingModal); //-----------搜索栏---------------------------- // 获取搜索框元素 // 获取搜索框、状态下拉框、时间选择框和按钮元素 // 获取搜索框、状态下拉框、时间选择框和按钮元素 const searchBar = document.getElementById('search-bar'); const statusFilter = document.getElementById('license-status-filter'); const startDate = document.getElementById('start-date'); const endDate = document.getElementById('end-date'); const submitButton = document.getElementById('submit-button'); const licenseInfoContainer = document.getElementById('license-info-restaurant-list'); // 过滤功能实现 function filterContent() { console.log('过滤功能触发'); // 移除滚动监听器,停止滚动加载 // removeScrollListeners(); console.log('statusFilter.valuesdaad撒大啊', statusFilter.value,startDate.value ,endDate.value,searchBar.value ); // 检查如果所有输入框都是默认值,则直接返回,不发送数据 if (!statusFilter.value && !startDate.value && !endDate.value && !searchBar.value) { console.log("所有过滤条件为空,不发送请求"); return; // 不发送请求 } // 构建请求体参数 const requestData = { license_flag: statusFilter.value || undefined, starting_date: startDate.value || undefined, end_date: endDate.value || undefined, any_search: searchBar.value || undefined, }; console.log("requestData",requestData); // 发送 POST 请求到接口 fetch('http://127.0.0.1:8080/api/admin/GetConditionalSearch', { method: 'POST', headers: { 'Authorization': `Bearer ${authToken}`, 'Content-Type': 'application/json', }, body: JSON.stringify(requestData), }) .then(response => response.json()) .then(data => { console.log('成功获取数据:', data); // 处理返回的数据并更新界面 displayLicenseInfo(data.data); }) .catch(error => { console.error('获取数据时发生错误:', error); }); } // 处理并显示返回的 License 信息 function displayLicenseInfo(data) { // 清空之前的结果 licenseInfoContainer.innerHTML = ''; LicApplicationData =[]; // 遍历返回的数据,生成并插入卡片 // 处理返回的数据并更新界面,使用 renderLicenseCards 方法进行渲染 renderLicenseCards(data, true); } // 确定按钮点击事件的修改(停止滚动监听) submitButton.addEventListener('click', function() { // 检查下拉框和时间选择框是否为默认值 if (!statusFilter.value && !startDate.value && !endDate.value) { // 如果都是默认值,则刷新页面 location.reload(); } else { // 否则,执行过滤内容功能 filterContent(); // 隐藏滚动加载,防止继续加载更多内容 removeScrollListeners(); } }); // 监听返回按钮的点击事件,刷新页面并恢复滚动监听 const resetButton = document.getElementById('reset-button'); resetButton.addEventListener('click', function() { // 刷新页面 location.reload(); }); // 修改当搜索栏没有输入时,重新启动滚动监听 searchBar.addEventListener('input', function() { if (!searchBar.value && !statusFilter.value && !startDate.value && !endDate.value) { console.log("搜索条件清空,重新启动滚动监听"); restartScrollListeners(); } });