license_info_modal.js 19 KB

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