license_info_modal.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. //-----------点击卡片弹出模态框------------------------------------------------------
  2. // 模态框显示函数
  3. // 模态框显示函数
  4. function showModalForCard(item,oaRequestId) {
  5. const modal = document.getElementById('license-info-modal');
  6. const modalContent = document.querySelector('.license-info-modal-content');
  7. const modalBody = document.getElementById('license-info-modal-body'); // 获取下半部分容器
  8. console.log(`当前点击的卡片 ID: ${oaRequestId}`);
  9. // 设置分页相关的变量
  10. let currentPage = 1;
  11. const itemsPerPage = 3; // 每页显示两组
  12. // 对 item 数组按 oa_id 进行升序排序
  13. const sortedItem = item.sort((a, b) => a.oa_id - b.oa_id);
  14. const totalPages = Math.ceil(sortedItem.length / itemsPerPage); // 计算总页数
  15. // 获取分页容器
  16. const paginationContainer = document.querySelector('.license-info-modal-pagination');
  17. // 清空分页容器,避免重复创建元素
  18. paginationContainer.innerHTML = '';
  19. // 创建"上一页"按钮
  20. const prevButton = document.createElement('button');
  21. prevButton.classList.add('prev-page');
  22. prevButton.innerText = '上一页';
  23. paginationContainer.appendChild(prevButton);
  24. // 创建下拉框
  25. const selectPageDropdown = document.createElement('select');
  26. paginationContainer.appendChild(selectPageDropdown);
  27. // 创建"下一页"按钮
  28. const nextButton = document.createElement('button');
  29. nextButton.classList.add('next-page');
  30. nextButton.innerText = '下一页';
  31. paginationContainer.appendChild(nextButton);
  32. // 初始化上半部分内容(Company, Creator, ApplicationDate, ApplicationTime 和两个按钮)
  33. function initializeHeaderContent(firstItem,sortedItem) {
  34. console.log("initializeHeaderContent"); // 检查是否找到按钮
  35. // 清空上半部分内容
  36. const modalHeader = document.querySelector('.license-info-modal-header');
  37. modalHeader.innerHTML = ''; // 确保不会重复创建
  38. let statusClass = '';
  39. if (firstItem.LicenseFlage === '已生成') {
  40. statusClass = 'license-status-green';
  41. } else if (firstItem.LicenseFlage === '未生成') {
  42. statusClass = 'license-status-yellow';
  43. } else if (firstItem.LicenseFlage === '已失效') {
  44. statusClass = 'license-status-red';
  45. }
  46. // 检查当前用户是否有权限生成或分发
  47. const hasGeneratePermission = currentUserPermissions.includes('generate_license');
  48. const hasDispatchPermission = currentUserPermissions.includes('dispat_license');
  49. console.log(`当前用户是否有生成权限: ${hasGeneratePermission}, ${hasDispatchPermission}`);
  50. /*筛选名字*/
  51. let creatorUser = firstItem.Creator;
  52. if (creatorUser.includes('-')) {
  53. let firstDashIndex = creatorUser.indexOf('-');
  54. let secondDashIndex = creatorUser.indexOf('-', firstDashIndex + 1);
  55. creatorUser = creatorUser.substring(firstDashIndex + 1, secondDashIndex);
  56. }
  57. console.log("筛选名字",creatorUser)
  58. // 设置卡片内容
  59. modalHeader.innerHTML = `
  60. <div class="license-info-card-header">
  61. <h3 class="card-title">${firstItem.GlxmName}</h3>
  62. </div>
  63. <div class="license-info-card-content">
  64. <p class="card-text date-time">${firstItem.ApplicationDate} ${firstItem.ApplicationTime}</p> <!-- 显示日期和时间 -->
  65. <p class="card-text">公司:${firstItem.Company}</p>
  66. <p class="card-text">项目信息:<div class="project-info">${firstItem.Project}</div></p>
  67. <p class="card-text">创建者:${creatorUser}</p>
  68. <p class="card-text license-status ${statusClass}">许可证状态:${firstItem.LicenseFlage}</p>
  69. <div class="license-info-card-buttons">
  70. ${
  71. firstItem.LicenseFlage === '已生成' && hasDispatchPermission
  72. ? `<button class="license-info-modal-button" id="distributeButton">分发</button>`
  73. : `<button class="license-info-modal-button" id="distributeButton" disabled style="background-color:#ccc;cursor:not-allowed;">分发</button>`
  74. }
  75. ${
  76. firstItem.LicenseFlage !== '已生成' && hasGeneratePermission
  77. ? `<button class="license-info-modal-button" id="generateOrDistribute">生成</button>`
  78. : ''
  79. }
  80. <button class="license-info-modal-button" id="downloadAllLicenses-button">打包下载所有license.dat</button>
  81. <button class="license-info-modal-button" id="viewDistributionHistory-button">查看分发历史</button>
  82. </div>
  83. </div>
  84. `;
  85. // 绑定 generateOrDistribute 的点击事件(如果按钮存在)
  86. const generateOrDistribute = modalHeader.querySelector('#generateOrDistribute');
  87. if (generateOrDistribute) {
  88. generateOrDistribute.addEventListener('click', () => {
  89. if (firstItem.LicenseFlage === '已生成') {
  90. // 执行分发逻辑
  91. showDistributeModal(firstItem);
  92. } else {
  93. // 执行生成逻辑
  94. const oaRequestID = parseInt(firstItem.oa_request_id, 10); // 10 表示10进制
  95. generateLicense(oaRequestID, true);
  96. }
  97. console.log('Button 1 clicked');
  98. });
  99. };
  100. // 绑定分发按钮的点击事件
  101. const distributeButton = modalHeader.querySelector('#distributeButton');
  102. if (distributeButton && !distributeButton.disabled) {
  103. distributeButton.addEventListener('click', () => {
  104. showDistributeModal(firstItem);
  105. });
  106. }
  107. // // 在生成或分发按钮点击时,调用 showDistributeModal 函数
  108. // generateOrDistribute.addEventListener('click', () => {
  109. // if (firstItem.LicenseFlage === '已生成') {
  110. // showDistributeModal(firstItem); // 显示分发模态框
  111. // } else {
  112. // const oaRequestID = parseInt(firstItem.oa_request_id, 10); // 生成许可证
  113. // generateLicense(oaRequestID, true);
  114. // }
  115. // });
  116. // 绑定 "打包下载所有license.dat" 按钮的点击事件
  117. const downloadButton = modalHeader.querySelector('#downloadAllLicenses-button');
  118. // 如果 LicenseFlage 是 "未生成" 或 "已失效",按钮变灰且不可点击
  119. if (firstItem.LicenseFlage === '未生成' || firstItem.LicenseFlage === '已失效') {
  120. downloadButton.disabled = true;
  121. downloadButton.style.backgroundColor = '#ccc'; // 设置为灰色
  122. downloadButton.style.cursor = 'not-allowed'; // 修改鼠标样式
  123. } else {
  124. // 只有当 LicenseFlage 是 "已生成" 时,才能点击下载按钮
  125. downloadButton.addEventListener('click', () => {
  126. downloadAllLicenses(sortedItem);
  127. });
  128. }
  129. // 在初始化完成后绑定 "查看分发历史" 按钮的点击事件
  130. const viewDistributionHistoryButton = document.getElementById('viewDistributionHistory-button');
  131. if (viewDistributionHistoryButton) {
  132. viewDistributionHistoryButton.addEventListener('click', () => {
  133. showDistributionHistory(firstItem);
  134. });
  135. }
  136. }
  137. // 初始化下拉框的页码选项
  138. function initializeDropdown() {
  139. selectPageDropdown.innerHTML = ''; // 清空下拉框中的选项
  140. for (let page = 1; page <= totalPages; page++) {
  141. const option = document.createElement('option');
  142. option.value = page;
  143. option.innerText = `第 ${page} 页`;
  144. selectPageDropdown.appendChild(option);
  145. }
  146. selectPageDropdown.value = currentPage; // 设置默认选项为当前页
  147. }
  148. // 渲染当前页内容
  149. function ModalForCardRenderPage(page) {
  150. modalBody.innerHTML = ''; // 清空之前的内容
  151. // 计算当前页的起始和结束索引
  152. const startIndex = (page - 1) * itemsPerPage;
  153. const endIndex = Math.min(startIndex + itemsPerPage, sortedItem.length);
  154. // 更新上半部分的字段内容 (以第一个元素为例)
  155. const firstItem = sortedItem[0];
  156. initializeHeaderContent(firstItem,sortedItem); // 动态生成上半部分内容
  157. // 遍历当前页的数据
  158. for (let i = startIndex; i < endIndex; i++) {
  159. const group = sortedItem[i];
  160. const groupBox = document.createElement('div');
  161. groupBox.classList.add('license-info-group-box');
  162. // 动态生成组内容,显示编号为 i+1(表示组1, 组2...)
  163. groupBox.innerHTML = `
  164. <div class="license-info-group-title">集群 ${i + 1} :</div>
  165. <p><strong>节点数:</strong> ${group.NodeCount}</p>
  166. <p><strong>数据库版本: </strong> ${group.ProductName}${group.ProductVersion}</p>
  167. <p><strong>CPU 型号:</strong> ${group.oa_cpu}</p>
  168. <p><strong>操作系统环境:</strong> ${group.oa_operating_system}</p>
  169. <p><strong>主IP/MAC地址:</strong>
  170. ${group.oa_main_mac ? group.oa_main_mac : '未填写'}
  171. </p>
  172. <p><strong>副IP/MAC地址:</strong>
  173. ${group.oa_second_mac ? group.oa_second_mac : '未填写'}
  174. </p>
  175. `;
  176. // 将生成的组内容加入到 modalBody
  177. modalBody.appendChild(groupBox);
  178. }
  179. // 更新下拉框的值
  180. selectPageDropdown.value = page;
  181. // 更新按钮状态
  182. prevButton.disabled = (page === 1);
  183. nextButton.disabled = (page === totalPages);
  184. }
  185. // 下拉框页码选择事件
  186. selectPageDropdown.addEventListener('change', function() {
  187. currentPage = parseInt(this.value);
  188. ModalForCardRenderPage(currentPage); // 根据选择的页码渲染对应页面
  189. });
  190. // 上一页按钮点击事件
  191. prevButton.addEventListener('click', function() {
  192. if (currentPage > 1) {
  193. currentPage--;
  194. ModalForCardRenderPage(currentPage);
  195. }
  196. });
  197. // 下一页按钮点击事件
  198. nextButton.addEventListener('click', function() {
  199. if (currentPage < totalPages) {
  200. currentPage++;
  201. ModalForCardRenderPage(currentPage);
  202. }
  203. });
  204. // 显示或隐藏分页相关控件
  205. function togglePaginationVisibility() {
  206. if (totalPages <= 1) {
  207. // 隐藏下拉框和分页容器
  208. paginationContainer.style.display = 'none';
  209. } else {
  210. // 显示下拉框和分页容器
  211. paginationContainer.style.display = 'flex';
  212. }
  213. }
  214. // 初始化下拉框并渲染第一页
  215. initializeDropdown();
  216. // 渲染模态框内容
  217. ModalForCardRenderPage(currentPage);
  218. // 根据页数决定是否显示分页控件
  219. togglePaginationVisibility();
  220. // 显示模态框
  221. modal.style.display = 'flex'; // 显示模态框背景
  222. setTimeout(() => {
  223. modalContent.classList.add('show'); // 添加动画效果
  224. }, 10); // 延时确保动画生效
  225. // 关闭模态框逻辑
  226. const closeModal = document.querySelector('.license-info-close');
  227. closeModal.addEventListener('click', () => {
  228. modalContent.classList.remove('show'); // 移除动画类
  229. setTimeout(() => {
  230. modal.style.display = 'none'; // 完全隐藏模态框
  231. }, 500); // 等待动画结束后再隐藏
  232. });
  233. // 点击模态框外部关闭模态框
  234. window.addEventListener('click', (event) => {
  235. if (event.target === modal) {
  236. modalContent.classList.remove('show');
  237. setTimeout(() => {
  238. modal.style.display = 'none'; // 完全隐藏模态框
  239. }, 500); // 等待动画结束后再隐藏
  240. }
  241. });
  242. }
  243. //-------下载全部licstr按钮
  244. // 下载许可证功能
  245. function downloadAllLicenses(sortedApplicationArray) {
  246. const zip = new JSZip();
  247. console.log("传进来的 sortedApplicationArray:", sortedApplicationArray);
  248. // 初始化计数器,从1开始
  249. let idCounter = 1;
  250. let Project = sortedApplicationArray[0].GlxmName;
  251. console.log("Project", Project);
  252. // 遍历 sortedApplicationArray,下载 lic1 和 lic2 数据
  253. sortedApplicationArray.forEach(row => {
  254. if (row.LicenseFlage === "已生成") {
  255. // 替换 oa_main_mac 和 oa_second_mac 中的冒号为点
  256. const mainMac = row.oa_main_mac.replace(/:/g, '.');
  257. const secondMac = row.oa_second_mac.replace(/:/g, '.');
  258. // 使用递增的 idCounter 替换 row.oa_id
  259. if (row.lic1) {
  260. const filename1 = `${idCounter}_license_1_${mainMac}.dat`;
  261. zip.file(filename1, row.lic1);
  262. }
  263. if (row.lic2) {
  264. const filename2 = `${idCounter}_license_2_${secondMac}.dat`;
  265. zip.file(filename2, row.lic2);
  266. }
  267. // 每次循环后递增计数器
  268. idCounter++;
  269. }
  270. });
  271. // 生成 ZIP 文件并触发下载
  272. zip.generateAsync({ type: "blob" }).then(content => {
  273. const link = document.createElement('a');
  274. link.href = URL.createObjectURL(content);
  275. link.download = `${Project}_license.zip`;
  276. link.click();
  277. });
  278. }
  279. // 刷新数据并滚动到目标卡片的函数
  280. // 刷新数据并滚动到目标卡片,同时重新打开目标卡片的模态框
  281. async function refreshLicenseDataAndScrollAndOpenModal(selfPage,selfPageSize, targetCardId) {
  282. const data = await fetchLicenseData(selfPage, selfPageSize);
  283. if (data.length > 0) {
  284. isLoading = true;
  285. console.log('加载的数据:', data); // 检查是否成功获取数据
  286. renderLicenseCards(data, true); // 渲染数据到页面并清空之前的内容
  287. page = selfPageSize+1;
  288. pageSize= selfPageSize +10;
  289. // 滚动到目标卡片
  290. if (targetCardId) {
  291. const targetCard = document.querySelector(`[data-oa-request-id="${targetCardId}"]`);
  292. if (targetCard) {
  293. targetCard.scrollIntoView({ behavior: 'smooth', block: 'center' });
  294. // 延迟打开模态框,确保页面滚动完成
  295. setTimeout(() => {
  296. targetCard.click(); // 模拟点击卡片,触发模态框
  297. }, 500); // 延时500ms确保滚动完成
  298. }
  299. }
  300. setTimeout(() => {
  301. isLoading = false;
  302. }, 2000);
  303. } else {
  304. console.error('未加载到数据');
  305. }
  306. }
  307. function generateLicense(id, isParentRow) {
  308. // 显示加载进度条并设置动态提示信息
  309. showLoadingModal('正在生成 License...');
  310. // const payload = isParentRow ? { oa_request_id: JSON.stringify(id) } : { uniqueID:JSON.stringify(id) };
  311. const payload = isParentRow
  312. ? { oa_request_id: parseInt(id, 10) } // 将 id 转换为整数
  313. : { uniqueID: parseInt(id, 10) }; // 将 id 转换为整数
  314. console.log("generateLicense",payload ,id, isParentRow)
  315. fetch(`http://${serverIP}:${serverPort}/api/admin/GenerateLicense`, {
  316. method: 'POST',
  317. headers: {
  318. 'Authorization': `Bearer ${authToken}`,
  319. 'Content-Type': 'application/json'
  320. },
  321. body: JSON.stringify(payload)
  322. })
  323. .then(response => response.json())
  324. .then(data => {
  325. if (data.success) {
  326. // 隐藏加载进度条
  327. hideLoadingModal();
  328. alert('License 生成成功!');
  329. //刷新页面
  330. // 调用封装的更新方法
  331. updateCardAndModalStatus(id, isParentRow);
  332. // 调用刷新函数,传入当前的 page 和 pageSize
  333. refreshLicenseDataAndScrollAndOpenModal(1,pageSize,id);
  334. } else {
  335. // 请求失败时隐藏加载进度条
  336. hideLoadingModal();
  337. alert('License 生成失败:' + data.error);
  338. }
  339. })
  340. .catch(error => {
  341. console.error('生成过程中出现错误Error:', error);
  342. // 请求失败时隐藏加载进度条
  343. hideLoadingModal();
  344. alert('生成过程中出现错误,请稍后再试。',error);
  345. });
  346. }
  347. //更新卡片样式
  348. function updateCardAndModalStatus(id, isParentRow) {
  349. // 获取对应的卡片元素,通过 oa_request_id 或 uniqueID 定位
  350. const cardSelector = isParentRow ? `[data-oa-request-id="${id}"]` : `[data-unique-id="${id}"]`;
  351. const card = document.querySelector(cardSelector);
  352. console.log("generateLicense card", cardSelector, card);
  353. if (card) {
  354. // 1. 更新卡片内许可证状态
  355. const statusElement = card.querySelector('.license-status');
  356. console.log("statusElement:", statusElement); // 检查状态元素是否存在
  357. if (statusElement) {
  358. // 只更新许可证状态部分的文本,而不是整个 p 标签
  359. statusElement.innerHTML = '许可证状态:已生成';
  360. // 更新状态的 CSS 类
  361. statusElement.classList.remove('license-status-yellow', 'license-status-red');
  362. statusElement.classList.add('license-status-green');
  363. } else {
  364. console.error('找不到 .license-status 元素');
  365. }
  366. // 2. 更新模态框中的状态(如果模态框已经打开)
  367. const modalStatusElement = document.querySelector('.license-info-modal-header .license-status');
  368. console.log("modalStatusElement:", modalStatusElement); // 检查状态元素是否存在
  369. if (modalStatusElement) {
  370. modalStatusElement.textContent = '许可证状态:已生成';
  371. modalStatusElement.classList.remove('license-status-yellow', 'license-status-red');
  372. modalStatusElement.classList.add('license-status-green');
  373. } else {
  374. console.error('找不到 #license-info-modal-body .license-status 元素');
  375. }
  376. // 3. 更新模态框中的按钮为“分发”
  377. const generateButton = document.getElementById('button1'); // 获取生成按钮
  378. if (generateButton) {
  379. generateButton.textContent = '分发'; // 修改按钮文本为“分发”
  380. generateButton.removeEventListener('click', generateLicense); // 移除生成逻辑
  381. generateButton.addEventListener('click', () => {
  382. distributeLicense(id); // 添加分发逻辑
  383. });
  384. }
  385. // 4. 启用 #downloadAllLicenses-button
  386. const downloadButton = document.getElementById('downloadAllLicenses-button');
  387. if (downloadButton) {
  388. downloadButton.disabled = false; // 启用按钮
  389. downloadButton.style.backgroundColor = '#007aff'; // 恢复正常的背景颜色
  390. downloadButton.style.cursor = 'pointer'; // 修改鼠标样式为可点击
  391. }
  392. } else {
  393. console.error(`找不到与 ID ${id} 对应的卡片`);
  394. }
  395. }