license_info.js 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. // 全局变量定义
  2. let page = 11; // 初始页码为1,代表从第1条数据开始获取
  3. let pageSize = 20; // 初始每次固定获取10条数据
  4. let total = 0; // 数据总量(从接口获取)
  5. let loadedItems = 0; // 已加载的数据条目数量
  6. let isLoading = false; // 防止多次加载
  7. const timeoutDuration = 10000; // 超时时间10秒
  8. const preLoadDistance = 300; // 距离底部300px时提前加载
  9. // 假设 Authorization 值存储在 localStorage 中,key 为 "authToken"
  10. const authToken = localStorage.getItem("Authorization");
  11. let currentUserInfo ; // 获取当前用户登录信息
  12. let currentUserPermissions; // 用于存储用户权限信息
  13. //获取 主内容区域
  14. const license_info_mainElement = document.querySelector('main'); // 主内容区域
  15. //模态框
  16. const license_info_modal = document.getElementById('license-info-modal'); // 模态框容器
  17. const license_info_modalContent = document.querySelector('.license-info-modal-content'); // 模态框内容区域
  18. const license_info_modalDescription = document.getElementById('license-info-modal-description'); // 模态框描述
  19. const license_info_modalPrice = document.getElementById('license-info-modal-price'); // 模态框产品信息
  20. const license_info_modalRating = document.getElementById('license-info-modal-rating'); // 模态框MAC地址
  21. const license_info_closeModal = document.querySelector('.license-info-close'); // 模态框关闭按钮
  22. const license_info_loadingIndicator = document.getElementById('loading-indicator'); // 加载提示元素
  23. //存储
  24. let LicApplicationData = []; // 用于存储从接口获取的数据
  25. // 统一的打开模态框函数
  26. function openModal(modalId) {
  27. const modal = document.getElementById(modalId);
  28. if (modal) {
  29. modal.style.display = "block"; // 显示模态框
  30. } else {
  31. console.error('模态框不存在,ID:', modalId);
  32. }
  33. }
  34. // 统一的关闭模态框函数
  35. function closeModal(modalId) {
  36. const modal = document.getElementById(modalId);
  37. if (modal) {
  38. modal.style.display = "none"; // 隐藏模态框
  39. } else {
  40. console.error('模态框不存在,ID:', modalId);
  41. }
  42. }
  43. //-----------头部--------------------------------
  44. function setupUserInfoModal(currentUserInfo) {
  45. console.log("currentUserInfo,", currentUserInfo);
  46. // 获取按钮、模态框和关闭按钮的 DOM 元素
  47. const loginButton = document.querySelector('.login-button');
  48. const loginModal = document.getElementById('loginModal');
  49. const loginModalClose = document.querySelector('.login-modal-close');
  50. const userInfoContainer = document.getElementById('userInfoContainer');
  51. // 设置按钮的显示内容为当前用户的名称
  52. loginButton.textContent = currentUserInfo.Username;
  53. // 点击按钮时显示模态框,并将用户信息填充到模态框中
  54. loginButton.addEventListener('click', () => {
  55. userInfoContainer.innerHTML = `
  56. <p><strong>用户名:</strong> ${currentUserInfo.Username}</p>
  57. <p><strong>邮箱:</strong> ${currentUserInfo.Email}</p>
  58. <p><strong>电话:</strong> ${currentUserInfo.Telephone}</p>
  59. <p><strong>账号:</strong> ${currentUserInfo.Account}</p>
  60. <p><strong>角色:</strong> ${currentUserInfo.Role}</p>
  61. <button class="logout-button">退出登录</button> <!-- 退出登录按钮 -->
  62. `;
  63. loginModal.style.display = 'flex';
  64. // 为退出登录按钮绑定点击事件
  65. const logoutButton = document.querySelector('.logout-button');
  66. logoutButton.addEventListener('click', logout);
  67. });
  68. // 点击关闭按钮时隐藏模态框
  69. loginModalClose.addEventListener('click', () => {
  70. loginModal.style.display = 'none';
  71. });
  72. // 点击模态框外部区域时关闭模态框
  73. window.addEventListener('click', (event) => {
  74. if (event.target === loginModal) {
  75. loginModal.style.display = 'none';
  76. }
  77. });
  78. }
  79. // 退出登录函数
  80. function logout() {
  81. localStorage.removeItem('Authorization');
  82. window.location.href = '/';
  83. }
  84. //-----------侧边栏----------------------------
  85. // 获取所有菜单项
  86. const menuItems = document.querySelectorAll('nav ul li a');
  87. // 为每个菜单项添加点击事件监听器
  88. menuItems.forEach(item => {
  89. item.addEventListener('click', function() {
  90. // 移除其他项的 active 类
  91. menuItems.forEach(i => i.classList.remove('active'));
  92. // 为当前点击的项添加 active 类
  93. this.classList.add('active');
  94. });
  95. });
  96. //用户管理-
  97. //获取用户管理和 License 信息按钮
  98. const userManagementLink = document.getElementById('user-management-link');
  99. const licenseInfoLink = document.getElementById('license-info-link');
  100. const roleManagementLink = document.getElementById('role-management-link');
  101. // 根据用户权限控制菜单显示
  102. function updateMenuVisibility() {
  103. console.log("updateMenuVisibility: ",currentUserPermissions);
  104. if (currentUserPermissions) {
  105. userManagementLink.style.display = currentUserPermissions.includes('read_user') ? 'block' : 'none';
  106. roleManagementLink.style.display = currentUserPermissions.includes('get_role') ? 'block' : 'none';
  107. licenseInfoLink.style.display = (currentUserPermissions.includes('read_license') || currentUserPermissions.includes('read_all_license')) ? 'block' : 'none';
  108. }
  109. }
  110. // 监听用户管理按钮的点击事件
  111. userManagementLink.addEventListener('click', function(event) {
  112. event.preventDefault(); // 阻止默认的跳转行为
  113. removeScrollListeners(); // 移除滚动监听器
  114. // 使用 fetch 来加载 user_management.html 的内容
  115. fetch('../user/user_management.html')
  116. .then(response => response.text())
  117. .then(data => {
  118. // 将 user_management.html 的内容插入到主内容区域
  119. license_info_mainElement.innerHTML = data;
  120. // 动态引入 user.js 文件
  121. const script = document.createElement('script');
  122. script.src = '../user/user.js';
  123. document.body.appendChild(script);
  124. })
  125. .catch(error => console.error('加载用户管理页面失败:', error));
  126. });
  127. // 监听 License 信息按钮的点击事件
  128. licenseInfoLink.addEventListener('click', function(event) {
  129. event.preventDefault(); // 阻止默认的跳转行为
  130. // 将瀑布流的 License 信息内容恢复到主内容区域
  131. const licenseInfoHtml = `
  132. <!-- 包裹搜索框、下拉框、时间选择框和确定按钮的 div -->
  133. <div class="search-container">
  134. <!-- License 状态下拉框 -->
  135. <select id="license-status-filter" aria-label="选择 License 状态">
  136. <option value="">License 状态</option>
  137. <option value="已生成">已生成</option>
  138. <option value="未生成">未生成</option>
  139. <option value="已失效">已失效</option>
  140. </select>
  141. <!-- 开始时间选择框,类型改为 date -->
  142. <input type="date" id="start-date" placeholder="开始时间" />
  143. <!-- 结束时间选择框,类型改为 date -->
  144. <input type="date" id="end-date" placeholder="结束时间" />
  145. <!-- 搜索框 -->
  146. <input type="text" id="search-bar" placeholder="搜索..." />
  147. <!-- 确定按钮 -->
  148. <button id="submit-button">确定</button>
  149. </div>
  150. <div class="license-info-container" id="license-info-restaurant-list"> </div>
  151. `; // 这是原来的 License 信息区域 HTML
  152. //mainContainer.innerHTML = licenseInfoHtml;
  153. license_info_mainElement.innerHTML = licenseInfoHtml;
  154. //清楚lic信息组的数据
  155. LicApplicationData = [];
  156. initializeScrollListeners(); // 重新初始化滚动监听器
  157. // 再次加载 License 信息数据并渲染卡片
  158. (async function() {
  159. const data = await fetchLicenseData(1, 10);
  160. if (data.length > 0) {
  161. console.log('加载的数据:', data); // 检查是否成功获取数据
  162. renderLicenseCards(data, true); // 渲染数据到页面并清空之前的内容
  163. } else {
  164. console.error('未加载到数据');
  165. }
  166. })();
  167. });
  168. roleManagementLink.addEventListener('click', function(event) {
  169. event.preventDefault(); // 阻止默认的跳转行为
  170. removeScrollListeners(); // 移除滚动监听器
  171. // 使用 fetch 来加载 user_management.html 的内容
  172. fetch('../role/role.html')
  173. .then(response => response.text())
  174. .then(data => {
  175. // 将 user_management.html 的内容插入到主内容区域
  176. license_info_mainElement.innerHTML = data;
  177. // 动态引入 user.js 文件
  178. const script = document.createElement('script');
  179. script.src = '../role/role.js';
  180. document.body.appendChild(script);
  181. })
  182. .catch(error => console.error('加载用户管理页面失败:', error));
  183. });
  184. //-------license数据显示------------------------------------------------------
  185. // 获取数据函数
  186. async function fetchLicenseData(page, pageSize) {
  187. try {
  188. const response = await fetch(`http://127.0.0.1:8080/api/admin/GetAllLicenseInfo?page=${page}&pageSize=${pageSize}`, {
  189. method: 'GET',
  190. headers: {
  191. 'Authorization': `Bearer ${authToken}`,
  192. 'Content-Type': 'application/json'
  193. }
  194. });
  195. const result = await response.json();
  196. // 设置总量,如果第一次加载,获取total字段
  197. if (total === 0 && result.total) {
  198. total = result.total;
  199. }
  200. // 使用 concat 方法将新数据与之前的数据进行累加
  201. LicApplicationData = LicApplicationData.concat(result.data || []);
  202. console.log("LicApplicationData: ",LicApplicationData);
  203. return result.data || [];
  204. } catch (error) {
  205. console.error("加载数据失败", error);
  206. return []; // 返回空数组,防止后续操作出错
  207. }
  208. }
  209. // 渲染 license_info 卡片数据函数
  210. function renderLicenseCards(data, clearContainer = false) {
  211. console.log("-----------渲染次数");
  212. // 获取与 license_info 相关的 HTML 元素
  213. const license_info_container = document.getElementById('license-info-restaurant-list'); // 卡片列表容器
  214. if (clearContainer) {
  215. license_info_container.innerHTML = ''; // 清空容器内容
  216. isLoading = false; // 重置加载状态
  217. loadedItems = 0; // 重置已加载项数
  218. page = 11; // 每次请求后,page 增加10,表示从下一组数据开始
  219. pageSize = 20; // pageSize 每次递增10
  220. console.log("-----------渲染清除");
  221. }
  222. console.log("-----------data:",data);
  223. data.forEach(group => {
  224. const firstItem = group[0]; // 获取该组的第一个数据项
  225. // 获取子行的数量
  226. const childRowCount = group.length;
  227. let statusClass = '';
  228. if (firstItem.LicenseFlage === '已生成') {
  229. statusClass = 'license-status-green';
  230. } else if (firstItem.LicenseFlage === '未生成') {
  231. statusClass = 'license-status-yellow';
  232. } else if (firstItem.LicenseFlage === '已失效') {
  233. statusClass = 'license-status-red';
  234. }
  235. const card = document.createElement('div');
  236. card.className = 'license-info-card';
  237. // 给卡片添加一个唯一标识符的 data 属性
  238. card.setAttribute('data-oa-request-id', firstItem.oa_request_id);
  239. // 在卡片的第一行显示申请时间
  240. card.innerHTML = `
  241. <div class="license-info-card-header">
  242. <h3 class="card-title">${firstItem.GlxmName}</h3>
  243. </div>
  244. <div class="license-info-card-content">
  245. <p class="card-text">${firstItem.ApplicationDate} ${firstItem.ApplicationTime}</p> <!-- 显示日期和时间 -->
  246. <p class="card-text">创建者:${firstItem.Creator}</p>
  247. <p class="card-text">公司:${firstItem.Company}</p>
  248. <p class="card-text">集群:${childRowCount} 套 共计:${firstItem.TotalNodes} 节点</p>
  249. <p class="card-text license-status ${statusClass}">许可证状态:${firstItem.LicenseFlage}</p>
  250. <p class="card-text">oa_request_id:${firstItem.oa_request_id}</p>
  251. </div>
  252. `;
  253. // 给卡片添加点击事件,点击后显示模态框
  254. card.addEventListener('click', () => {
  255. // 传递当前卡片的详细数据到模态框
  256. const oaRequestId = card.getAttribute('data-oa-request-id');
  257. showModalForCard(group, oaRequestId); // 传递 oa_request_id
  258. //showModalForCard(group); // 传递当前卡片的详细数据到模态框
  259. });
  260. // 将卡片添加到容器中
  261. license_info_container.appendChild(card);
  262. });
  263. }
  264. // 检查是否滚动到底部并触发加载
  265. // async function checkAndLoadMore(scrollHeight, scrollTop, clientHeight) {
  266. // if (isLoading || loadedItems >= total) return; // 如果正在加载或已加载完所有数据则退出
  267. // // console.log(`Scroll Info - scrollHeight: ${scrollHeight}, scrollTop: ${scrollTop}, clientHeight: ${clientHeight}`);
  268. // if (scrollTop + clientHeight >= scrollHeight - preLoadDistance) {
  269. // console.log(`触发加载更多数据:page=${page}, pageSize=${pageSize}`); // 每次触发时打印输出
  270. // await loadMoreData();
  271. // }
  272. // }
  273. // 加载更多数据函数
  274. async function loadMoreData() {
  275. if (isLoading) return; // 防止重复加载
  276. isLoading = true;
  277. console.log('开始加载更多数据');
  278. // 显示加载提示
  279. // license_info_loadingIndicator.style.display = 'block'; // 显示提示
  280. // license_info_loadingIndicator.innerText = '正在加载...'; // 重置加载提示
  281. // 设置超时处理
  282. const timeout = setTimeout(() => {
  283. license_info_loadingIndicator.innerText = '加载超时,请重试'; // 修改提示语为超时提示
  284. isLoading = false;
  285. license_info_loadingIndicator.style.display = 'none'; // 超时后隐藏加载提示
  286. }, timeoutDuration);
  287. // 获取数据
  288. const data = await fetchLicenseData(page, pageSize);
  289. console.log(`加载的新数据 data`,data); // 每次触发时打印输出
  290. // 清除超时定时器
  291. clearTimeout(timeout);
  292. if (data.length > 0) {
  293. // 更新 page 和 pageSize,下一次请求从新的位置开始
  294. page += 10; // 每次请求后,page 增加10,表示从下一组数据开始
  295. pageSize += 10; // pageSize 每次递增10
  296. // 更新已加载的条目数
  297. loadedItems += data.length;
  298. // 渲染数据到页面
  299. renderLicenseCards(data);
  300. console.log('数据加载完成,更新页面');
  301. }
  302. // 隐藏加载提示
  303. //license_info_loadingIndicator.style.display = 'none'; // 加载完成后隐藏提示
  304. isLoading = false; // 请求完成,允许下次请求
  305. // 检查内容高度,必要时继续加载
  306. //checkContentHeight();
  307. checkAndLoadMore();
  308. }
  309. //--------------------------监听 window 滚动---监听 main 容器的滚动-----------------------------------------------
  310. // // 监听 window 滚动
  311. // window.addEventListener('scroll', () => {
  312. // checkAndLoadMore(document.body.scrollHeight, window.scrollY, window.innerHeight);
  313. // });
  314. // // 监听 main 容器的滚动
  315. // license_info_mainElement.addEventListener('scroll', () => {
  316. // checkAndLoadMore(license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  317. // });
  318. function initializeScrollListeners() {
  319. // 只监听 main 容器的滚动
  320. license_info_mainElement.addEventListener('scroll', handleMainScroll);
  321. // console.log('滚动监听已初始化');
  322. }
  323. function removeScrollListeners() {
  324. // 移除 main 容器的滚动监听
  325. license_info_mainElement.removeEventListener('scroll', handleMainScroll);
  326. }
  327. // 新增一个重启滑动监听功能函数,当用户清空搜索条件时调用
  328. function restartScrollListeners() {
  329. // 重新绑定滑动监听器
  330. initializeScrollListeners();
  331. }
  332. function handleMainScroll() {
  333. // console.log('handleMainScroll', license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  334. checkAndLoadMore(license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  335. }
  336. async function checkAndLoadMore(scrollHeight, scrollTop, clientHeight) {
  337. if (isLoading || loadedItems >= total) return; // 如果正在加载或已加载完所有数据则退出
  338. // console.log(`Scroll Info - scrollHeight: ${scrollHeight}, scrollTop: ${scrollTop}, clientHeight: ${clientHeight}`);
  339. if (scrollTop + clientHeight >= scrollHeight - preLoadDistance) {
  340. console.log(`触发加载更多数据:page=${page}, pageSize=${pageSize}`); // 每次触发时打印输出
  341. await loadMoreData();
  342. }
  343. }
  344. //-----------------------------------------------------------------------------------------
  345. // 初始化加载第一页
  346. (async function() {
  347. const data = await fetchLicenseData(1, 10);
  348. if (data.length > 0) {
  349. renderLicenseCards(data); // 渲染数据到页面
  350. loadedItems += data.length; // 更新已加载的条目数
  351. }
  352. //license_info_loadingIndicator.style.display = 'none'; // 初始化后隐藏加载提示
  353. // 检查内容高度
  354. // checkContentHeight();
  355. checkAndLoadMore()
  356. })();
  357. //初始化监听滚动条
  358. initializeScrollListeners()
  359. //-----------点击卡片弹出模态框------------------------------------------------------
  360. // 模态框显示函数
  361. // 模态框显示函数
  362. function showModalForCard(item,oaRequestId) {
  363. const modal = document.getElementById('license-info-modal');
  364. const modalContent = document.querySelector('.license-info-modal-content');
  365. const modalBody = document.getElementById('license-info-modal-body'); // 获取下半部分容器
  366. console.log(`当前点击的卡片 ID: ${oaRequestId}`);
  367. // 设置分页相关的变量
  368. let currentPage = 1;
  369. const itemsPerPage = 3; // 每页显示两组
  370. // 对 item 数组按 oa_id 进行升序排序
  371. const sortedItem = item.sort((a, b) => a.oa_id - b.oa_id);
  372. const totalPages = Math.ceil(sortedItem.length / itemsPerPage); // 计算总页数
  373. // 获取分页容器
  374. const paginationContainer = document.querySelector('.license-info-modal-pagination');
  375. // 清空分页容器,避免重复创建元素
  376. paginationContainer.innerHTML = '';
  377. // 创建"上一页"按钮
  378. const prevButton = document.createElement('button');
  379. prevButton.classList.add('prev-page');
  380. prevButton.innerText = '上一页';
  381. paginationContainer.appendChild(prevButton);
  382. // 创建下拉框
  383. const selectPageDropdown = document.createElement('select');
  384. paginationContainer.appendChild(selectPageDropdown);
  385. // 创建"下一页"按钮
  386. const nextButton = document.createElement('button');
  387. nextButton.classList.add('next-page');
  388. nextButton.innerText = '下一页';
  389. paginationContainer.appendChild(nextButton);
  390. // 初始化上半部分内容(Company, Creator, ApplicationDate, ApplicationTime 和两个按钮)
  391. function initializeHeaderContent(firstItem,sortedItem) {
  392. console.log("initializeHeaderContent"); // 检查是否找到按钮
  393. // 清空上半部分内容
  394. const modalHeader = document.querySelector('.license-info-modal-header');
  395. modalHeader.innerHTML = ''; // 确保不会重复创建
  396. let statusClass = '';
  397. if (firstItem.LicenseFlage === '已生成') {
  398. statusClass = 'license-status-green';
  399. } else if (firstItem.LicenseFlage === '未生成') {
  400. statusClass = 'license-status-yellow';
  401. } else if (firstItem.LicenseFlage === '已失效') {
  402. statusClass = 'license-status-red';
  403. }
  404. // 检查当前用户是否有权限生成或分发
  405. const hasGeneratePermission = currentUserPermissions.includes('generate_license');
  406. const hasDispatchPermission = currentUserPermissions.includes('dispat_license');
  407. console.log(`当前用户是否有生成权限: ${hasGeneratePermission}, ${hasDispatchPermission}`);
  408. // 设置卡片内容
  409. // 修改后的卡片内容部分代码
  410. modalHeader.innerHTML = `
  411. <div class="license-info-card-header">
  412. <h3 class="card-title">${firstItem.GlxmName}</h3>
  413. </div>
  414. <div class="license-info-card-content">
  415. <p class="card-text">${firstItem.ApplicationDate} ${firstItem.ApplicationTime}</p> <!-- 显示日期和时间 -->
  416. <p class="card-text">公司:${firstItem.Company}</p>
  417. <p class="card-text">${firstItem.Project}</p>
  418. <p class="card-text">创建者:${firstItem.Creator}</p>
  419. <p class="card-text license-status ${statusClass}">许可证状态:${firstItem.LicenseFlage}</p>
  420. <div class="license-info-card-buttons">
  421. ${
  422. firstItem.LicenseFlage === '已生成' && hasDispatchPermission
  423. ? `<button class="license-info-modal-button" id="distributeButton">分发</button>`
  424. : `<button class="license-info-modal-button" id="distributeButton" disabled style="background-color:#ccc;cursor:not-allowed;">分发</button>`
  425. }
  426. ${
  427. firstItem.LicenseFlage !== '已生成' && hasGeneratePermission
  428. ? `<button class="license-info-modal-button" id="generateOrDistribute">生成</button>`
  429. : ''
  430. }
  431. <button class="license-info-modal-button" id="downloadAllLicenses-button">打包下载所有license.dat</button>
  432. <button class="license-info-modal-button" id="viewDistributionHistory-button">查看分发历史</button>
  433. </div>
  434. </div>
  435. `;
  436. // 绑定 generateOrDistribute 的点击事件(如果按钮存在)
  437. const generateOrDistribute = modalHeader.querySelector('#generateOrDistribute');
  438. if (generateOrDistribute) {
  439. generateOrDistribute.addEventListener('click', () => {
  440. if (firstItem.LicenseFlage === '已生成') {
  441. // 执行分发逻辑
  442. showDistributeModal(firstItem);
  443. } else {
  444. // 执行生成逻辑
  445. const oaRequestID = parseInt(firstItem.oa_request_id, 10); // 10 表示10进制
  446. generateLicense(oaRequestID, true);
  447. }
  448. console.log('Button 1 clicked');
  449. });
  450. };
  451. // 绑定分发按钮的点击事件
  452. const distributeButton = modalHeader.querySelector('#distributeButton');
  453. if (distributeButton && !distributeButton.disabled) {
  454. distributeButton.addEventListener('click', () => {
  455. showDistributeModal(firstItem);
  456. });
  457. }
  458. // // 在生成或分发按钮点击时,调用 showDistributeModal 函数
  459. // generateOrDistribute.addEventListener('click', () => {
  460. // if (firstItem.LicenseFlage === '已生成') {
  461. // showDistributeModal(firstItem); // 显示分发模态框
  462. // } else {
  463. // const oaRequestID = parseInt(firstItem.oa_request_id, 10); // 生成许可证
  464. // generateLicense(oaRequestID, true);
  465. // }
  466. // });
  467. // 绑定 "打包下载所有license.dat" 按钮的点击事件
  468. const downloadButton = modalHeader.querySelector('#downloadAllLicenses-button');
  469. // 如果 LicenseFlage 是 "未生成" 或 "已失效",按钮变灰且不可点击
  470. if (firstItem.LicenseFlage === '未生成' || firstItem.LicenseFlage === '已失效') {
  471. downloadButton.disabled = true;
  472. downloadButton.style.backgroundColor = '#ccc'; // 设置为灰色
  473. downloadButton.style.cursor = 'not-allowed'; // 修改鼠标样式
  474. } else {
  475. // 只有当 LicenseFlage 是 "已生成" 时,才能点击下载按钮
  476. downloadButton.addEventListener('click', () => {
  477. downloadAllLicenses(sortedItem);
  478. });
  479. }
  480. // 在初始化完成后绑定 "查看分发历史" 按钮的点击事件
  481. const viewDistributionHistoryButton = document.getElementById('viewDistributionHistory-button');
  482. if (viewDistributionHistoryButton) {
  483. viewDistributionHistoryButton.addEventListener('click', () => {
  484. showDistributionHistory(firstItem);
  485. });
  486. }
  487. }
  488. // 初始化下拉框的页码选项
  489. function initializeDropdown() {
  490. selectPageDropdown.innerHTML = ''; // 清空下拉框中的选项
  491. for (let page = 1; page <= totalPages; page++) {
  492. const option = document.createElement('option');
  493. option.value = page;
  494. option.innerText = `第 ${page} 页`;
  495. selectPageDropdown.appendChild(option);
  496. }
  497. selectPageDropdown.value = currentPage; // 设置默认选项为当前页
  498. }
  499. // 渲染当前页内容
  500. function ModalForCardRenderPage(page) {
  501. modalBody.innerHTML = ''; // 清空之前的内容
  502. // 计算当前页的起始和结束索引
  503. const startIndex = (page - 1) * itemsPerPage;
  504. const endIndex = Math.min(startIndex + itemsPerPage, sortedItem.length);
  505. // 更新上半部分的字段内容 (以第一个元素为例)
  506. const firstItem = sortedItem[0];
  507. initializeHeaderContent(firstItem,sortedItem); // 动态生成上半部分内容
  508. // 遍历当前页的数据
  509. for (let i = startIndex; i < endIndex; i++) {
  510. const group = sortedItem[i];
  511. const groupBox = document.createElement('div');
  512. groupBox.classList.add('license-info-group-box');
  513. // 动态生成组内容,显示编号为 i+1(表示组1, 组2...)
  514. groupBox.innerHTML = `
  515. <div class="license-info-group-title">集群 ${i + 1} :</div>
  516. <p><strong>节点数:</strong> ${group.NodeCount}</p>
  517. <p><strong>数据库版本: </strong> ${group.ProductName}${group.ProductVersion}</p>
  518. <p><strong>CPU 型号:</strong> ${group.oa_cpu}</p>
  519. <p><strong>操作系统环境:</strong> ${group.oa_operating_system}</p>
  520. <p><strong>以下为测试显示:</strong> </p>
  521. <p><strong>oa_id:</strong> ${group.oa_id}</p>
  522. <p><strong>oa_request_id:</strong> ${group.oa_request_id}</p>
  523. `;
  524. // 将生成的组内容加入到 modalBody
  525. modalBody.appendChild(groupBox);
  526. }
  527. // 更新下拉框的值
  528. selectPageDropdown.value = page;
  529. // 更新按钮状态
  530. prevButton.disabled = (page === 1);
  531. nextButton.disabled = (page === totalPages);
  532. }
  533. // 下拉框页码选择事件
  534. selectPageDropdown.addEventListener('change', function() {
  535. currentPage = parseInt(this.value);
  536. ModalForCardRenderPage(currentPage); // 根据选择的页码渲染对应页面
  537. });
  538. // 上一页按钮点击事件
  539. prevButton.addEventListener('click', function() {
  540. if (currentPage > 1) {
  541. currentPage--;
  542. ModalForCardRenderPage(currentPage);
  543. }
  544. });
  545. // 下一页按钮点击事件
  546. nextButton.addEventListener('click', function() {
  547. if (currentPage < totalPages) {
  548. currentPage++;
  549. ModalForCardRenderPage(currentPage);
  550. }
  551. });
  552. // 显示或隐藏分页相关控件
  553. function togglePaginationVisibility() {
  554. if (totalPages <= 1) {
  555. // 隐藏下拉框和分页容器
  556. paginationContainer.style.display = 'none';
  557. } else {
  558. // 显示下拉框和分页容器
  559. paginationContainer.style.display = 'flex';
  560. }
  561. }
  562. // 初始化下拉框并渲染第一页
  563. initializeDropdown();
  564. // 渲染模态框内容
  565. ModalForCardRenderPage(currentPage);
  566. // 根据页数决定是否显示分页控件
  567. togglePaginationVisibility();
  568. // 显示模态框
  569. modal.style.display = 'flex'; // 显示模态框背景
  570. setTimeout(() => {
  571. modalContent.classList.add('show'); // 添加动画效果
  572. }, 10); // 延时确保动画生效
  573. // 关闭模态框逻辑
  574. const closeModal = document.querySelector('.license-info-close');
  575. closeModal.addEventListener('click', () => {
  576. modalContent.classList.remove('show'); // 移除动画类
  577. setTimeout(() => {
  578. modal.style.display = 'none'; // 完全隐藏模态框
  579. }, 500); // 等待动画结束后再隐藏
  580. });
  581. // 点击模态框外部关闭模态框
  582. window.addEventListener('click', (event) => {
  583. if (event.target === modal) {
  584. modalContent.classList.remove('show');
  585. setTimeout(() => {
  586. modal.style.display = 'none'; // 完全隐藏模态框
  587. }, 500); // 等待动画结束后再隐藏
  588. }
  589. });
  590. }
  591. //-------下载全部licstr按钮
  592. // 下载许可证功能
  593. function downloadAllLicenses(sortedApplicationArray) {
  594. const zip = new JSZip();
  595. console.log("传进来的 sortedApplicationArray:", sortedApplicationArray);
  596. // 初始化计数器,从1开始
  597. let idCounter = 1;
  598. let Project = sortedApplicationArray[0].Project;
  599. console.log("Project", Project);
  600. // 遍历 sortedApplicationArray,下载 lic1 和 lic2 数据
  601. sortedApplicationArray.forEach(row => {
  602. if (row.LicenseFlage === "已生成") {
  603. // 替换 oa_main_mac 和 oa_second_mac 中的冒号为点
  604. const mainMac = row.oa_main_mac.replace(/:/g, '.');
  605. const secondMac = row.oa_second_mac.replace(/:/g, '.');
  606. // 使用递增的 idCounter 替换 row.oa_id
  607. if (row.lic1) {
  608. const filename1 = `${idCounter}_license.dat_1_${mainMac}`;
  609. zip.file(filename1, row.lic1);
  610. }
  611. if (row.lic2) {
  612. const filename2 = `${idCounter}_license.dat_2_${secondMac}`;
  613. zip.file(filename2, row.lic2);
  614. }
  615. // 每次循环后递增计数器
  616. idCounter++;
  617. }
  618. });
  619. // 生成 ZIP 文件并触发下载
  620. zip.generateAsync({ type: "blob" }).then(content => {
  621. const link = document.createElement('a');
  622. link.href = URL.createObjectURL(content);
  623. link.download = `${Project}_license.zip`;
  624. link.click();
  625. });
  626. }
  627. // 刷新数据并滚动到目标卡片的函数
  628. // 刷新数据并滚动到目标卡片,同时重新打开目标卡片的模态框
  629. async function refreshLicenseDataAndScrollAndOpenModal(selfPage,selfPageSize, targetCardId) {
  630. const data = await fetchLicenseData(selfPage, selfPageSize);
  631. if (data.length > 0) {
  632. isLoading = true;
  633. console.log('加载的数据:', data); // 检查是否成功获取数据
  634. renderLicenseCards(data, true); // 渲染数据到页面并清空之前的内容
  635. page = selfPageSize+1;
  636. pageSize= selfPageSize +10;
  637. // 滚动到目标卡片
  638. if (targetCardId) {
  639. const targetCard = document.querySelector(`[data-oa-request-id="${targetCardId}"]`);
  640. if (targetCard) {
  641. targetCard.scrollIntoView({ behavior: 'smooth', block: 'center' });
  642. // 延迟打开模态框,确保页面滚动完成
  643. setTimeout(() => {
  644. targetCard.click(); // 模拟点击卡片,触发模态框
  645. }, 500); // 延时500ms确保滚动完成
  646. }
  647. }
  648. setTimeout(() => {
  649. isLoading = false;
  650. }, 2000);
  651. } else {
  652. console.error('未加载到数据');
  653. }
  654. }
  655. function generateLicense(id, isParentRow) {
  656. // 显示加载进度条并设置动态提示信息
  657. showLoadingModal('正在生成 License...');
  658. // const payload = isParentRow ? { oa_request_id: JSON.stringify(id) } : { uniqueID:JSON.stringify(id) };
  659. const payload = isParentRow
  660. ? { oa_request_id: parseInt(id, 10) } // 将 id 转换为整数
  661. : { uniqueID: parseInt(id, 10) }; // 将 id 转换为整数
  662. console.log("generateLicense",payload ,id, isParentRow)
  663. fetch('http://127.0.0.1:8080/api/admin/GenerateLicense', {
  664. method: 'POST',
  665. headers: {
  666. 'Authorization': `Bearer ${authToken}`,
  667. 'Content-Type': 'application/json'
  668. },
  669. body: JSON.stringify(payload)
  670. })
  671. .then(response => response.json())
  672. .then(data => {
  673. if (data.success) {
  674. // 隐藏加载进度条
  675. hideLoadingModal();
  676. alert('License 生成成功!');
  677. //刷新页面
  678. // 调用封装的更新方法
  679. //updateCardAndModalStatus(id, isParentRow);
  680. // 调用刷新函数,传入当前的 page 和 pageSize
  681. refreshLicenseDataAndScrollAndOpenModal(1,pageSize,id);
  682. } else {
  683. // 请求失败时隐藏加载进度条
  684. hideLoadingModal();
  685. alert('License 生成失败:' + data.error);
  686. }
  687. })
  688. .catch(error => {
  689. console.error('生成过程中出现错误Error:', error);
  690. // 请求失败时隐藏加载进度条
  691. hideLoadingModal();
  692. alert('生成过程中出现错误,请稍后再试。',error);
  693. });
  694. }
  695. //更新卡片样式
  696. function updateCardAndModalStatus(id, isParentRow) {
  697. // 获取对应的卡片元素,通过 oa_request_id 或 uniqueID 定位
  698. const cardSelector = isParentRow ? `[data-oa-request-id="${id}"]` : `[data-unique-id="${id}"]`;
  699. const card = document.querySelector(cardSelector);
  700. console.log("generateLicense card", cardSelector, card);
  701. if (card) {
  702. // 1. 更新卡片内许可证状态
  703. const statusElement = card.querySelector('.license-status');
  704. console.log("statusElement:", statusElement); // 检查状态元素是否存在
  705. if (statusElement) {
  706. // 只更新许可证状态部分的文本,而不是整个 p 标签
  707. statusElement.innerHTML = '许可证状态:已生成';
  708. // 更新状态的 CSS 类
  709. statusElement.classList.remove('license-status-yellow', 'license-status-red');
  710. statusElement.classList.add('license-status-green');
  711. } else {
  712. console.error('找不到 .license-status 元素');
  713. }
  714. // 2. 更新模态框中的状态(如果模态框已经打开)
  715. const modalStatusElement = document.querySelector('.license-info-modal-header .license-status');
  716. console.log("modalStatusElement:", modalStatusElement); // 检查状态元素是否存在
  717. if (modalStatusElement) {
  718. modalStatusElement.textContent = '许可证状态:已生成';
  719. modalStatusElement.classList.remove('license-status-yellow', 'license-status-red');
  720. modalStatusElement.classList.add('license-status-green');
  721. } else {
  722. console.error('找不到 #license-info-modal-body .license-status 元素');
  723. }
  724. // 3. 更新模态框中的按钮为“分发”
  725. const generateButton = document.getElementById('button1'); // 获取生成按钮
  726. if (generateButton) {
  727. generateButton.textContent = '分发'; // 修改按钮文本为“分发”
  728. generateButton.removeEventListener('click', generateLicense); // 移除生成逻辑
  729. generateButton.addEventListener('click', () => {
  730. distributeLicense(id); // 添加分发逻辑
  731. });
  732. }
  733. // 4. 启用 #downloadAllLicenses-button
  734. const downloadButton = document.getElementById('downloadAllLicenses-button');
  735. if (downloadButton) {
  736. downloadButton.disabled = false; // 启用按钮
  737. downloadButton.style.backgroundColor = '#007aff'; // 恢复正常的背景颜色
  738. downloadButton.style.cursor = 'pointer'; // 修改鼠标样式为可点击
  739. }
  740. } else {
  741. console.error(`找不到与 ID ${id} 对应的卡片`);
  742. }
  743. }
  744. ///-----------获取登录用户信息----------------------------------------
  745. async function fetchUsername() {
  746. try {
  747. const response = await fetch(`http://127.0.0.1:8080/api/admin/userInfo`, {
  748. method: 'GET',
  749. headers: {
  750. 'Authorization': `Bearer ${authToken}`,
  751. 'Content-Type': 'application/json'
  752. }
  753. });
  754. const data = await response.json();
  755. currentUserRole = data.data.Role; // 存储当前用户的角色
  756. currentUserName = data.data.Username;
  757. // 使用获取到的角色,调用获取权限的接口
  758. await fetchPermissionsByRole(currentUserRole);
  759. console.log('当前用户角色:', data);
  760. currentUserInfo = data.data;
  761. // 调用该函数,初始化模态框
  762. setupUserInfoModal(currentUserInfo);
  763. updateMenuVisibility();
  764. return data.data; // 返回获取到的用户信息数据
  765. } catch (error) {
  766. console.error('Error fetching user info or permissions:', error);
  767. }
  768. }
  769. fetchUsername();
  770. // 调用函数更新菜单可见性
  771. // 将 fetchPermissionsByRole 转换为异步函数
  772. async function fetchPermissionsByRole(role) {
  773. try {
  774. const response = await fetch(`http://127.0.0.1:8080/api/admin/GetSelfRoles`, {
  775. method: 'POST',
  776. headers: {
  777. 'Authorization': `Bearer ${authToken}`,
  778. 'Content-Type': 'application/json'
  779. },
  780. body: JSON.stringify({ name: role })
  781. });
  782. const data = await response.json();
  783. currentUserPermissions = data.data.Permissions; // 获取用户的权限数组
  784. console.log('currentUserPermissions:', currentUserPermissions);
  785. // 定义权限类别
  786. // const licensePermissions = ['upload_license', 'read_license'];
  787. // const userPermissionsCheck = ['create_user', 'read_user', 'update_user', 'delete_user'];
  788. // const rolePermissions = ['create_role', 'delete_role', 'update_role', 'get_role'];
  789. // const hasLicenseAccess = licensePermissions.some(permission => userPermissions.includes(permission));
  790. // const hasUserManagementAccess = userPermissionsCheck.some(permission => userPermissions.includes(permission));
  791. // const hasRoleManagementAccess = rolePermissions.some(permission => userPermissions.includes(permission));
  792. // 根据权限渲染菜单并显示初始页面
  793. //renderMenuAndInitialPage(hasLicenseAccess, hasUserManagementAccess, hasRoleManagementAccess);
  794. } catch (error) {
  795. console.error('Error fetching permissions:', error);
  796. }
  797. }
  798. //--------------进度条-------------------------------------------------
  799. // 创建模态框的 DOM 元素并插入到 body 中
  800. function createLoadingModal() {
  801. const modalHTML = `
  802. <div id="loadingModal" class="loading-modal" style="display: none;">
  803. <div class="loading-modal-content">
  804. <div class="spinner"></div>
  805. <p id="loadingMessage">加载中...</p>
  806. </div>
  807. </div>
  808. `;
  809. document.body.insertAdjacentHTML('beforeend', modalHTML);
  810. }
  811. // 显示加载模态框
  812. function showLoadingModal(message = "加载中...") {
  813. const loadingModal = document.getElementById('loadingModal');
  814. const loadingMessage = document.getElementById('loadingMessage');
  815. if (loadingModal && loadingMessage) {
  816. loadingMessage.textContent = message; // 设置显示的消息
  817. loadingModal.style.display = 'flex'; // 显示模态框
  818. }
  819. }
  820. // 隐藏加载模态框
  821. function hideLoadingModal() {
  822. const loadingModal = document.getElementById('loadingModal');
  823. if (loadingModal) {
  824. loadingModal.style.display = 'none'; // 隐藏模态框
  825. }
  826. }
  827. // 页面加载时创建模态框
  828. document.addEventListener('DOMContentLoaded', createLoadingModal);
  829. //-----------搜索栏----------------------------
  830. // 获取搜索框元素
  831. // 获取搜索框、状态下拉框、时间选择框和按钮元素
  832. // 获取搜索框、状态下拉框、时间选择框和按钮元素
  833. const searchBar = document.getElementById('search-bar');
  834. const statusFilter = document.getElementById('license-status-filter');
  835. const startDate = document.getElementById('start-date');
  836. const endDate = document.getElementById('end-date');
  837. const submitButton = document.getElementById('submit-button');
  838. const licenseInfoContainer = document.getElementById('license-info-restaurant-list');
  839. // 过滤功能实现
  840. function filterContent() {
  841. console.log('过滤功能触发');
  842. // 移除滚动监听器,停止滚动加载
  843. // removeScrollListeners();
  844. console.log('statusFilter.valuesdaad撒大啊', statusFilter.value,startDate.value ,endDate.value,searchBar.value );
  845. // 检查如果所有输入框都是默认值,则直接返回,不发送数据
  846. if (!statusFilter.value && !startDate.value && !endDate.value && !searchBar.value) {
  847. console.log("所有过滤条件为空,不发送请求");
  848. return; // 不发送请求
  849. }
  850. // 构建请求体参数
  851. const requestData = {
  852. license_flag: statusFilter.value || undefined,
  853. starting_date: startDate.value || undefined,
  854. end_date: endDate.value || undefined,
  855. any_search: searchBar.value || undefined,
  856. };
  857. console.log("requestData",requestData);
  858. // 发送 POST 请求到接口
  859. fetch('http://127.0.0.1:8080/api/admin/GetConditionalSearch', {
  860. method: 'POST',
  861. headers: {
  862. 'Authorization': `Bearer ${authToken}`,
  863. 'Content-Type': 'application/json',
  864. },
  865. body: JSON.stringify(requestData),
  866. })
  867. .then(response => response.json())
  868. .then(data => {
  869. console.log('成功获取数据:', data);
  870. // 处理返回的数据并更新界面
  871. displayLicenseInfo(data.data);
  872. })
  873. .catch(error => {
  874. console.error('获取数据时发生错误:', error);
  875. });
  876. }
  877. // 处理并显示返回的 License 信息
  878. function displayLicenseInfo(data) {
  879. // 清空之前的结果
  880. licenseInfoContainer.innerHTML = '';
  881. LicApplicationData =[];
  882. // 遍历返回的数据,生成并插入卡片
  883. // 处理返回的数据并更新界面,使用 renderLicenseCards 方法进行渲染
  884. renderLicenseCards(data, true);
  885. }
  886. // 确定按钮点击事件的修改(停止滚动监听)
  887. submitButton.addEventListener('click', function() {
  888. // 检查下拉框和时间选择框是否为默认值
  889. if (!statusFilter.value && !startDate.value && !endDate.value) {
  890. // 如果都是默认值,则刷新页面
  891. location.reload();
  892. } else {
  893. // 否则,执行过滤内容功能
  894. filterContent();
  895. // 隐藏滚动加载,防止继续加载更多内容
  896. removeScrollListeners();
  897. }
  898. });
  899. // 监听返回按钮的点击事件,刷新页面并恢复滚动监听
  900. const resetButton = document.getElementById('reset-button');
  901. resetButton.addEventListener('click', function() {
  902. // 刷新页面
  903. location.reload();
  904. });
  905. // 修改当搜索栏没有输入时,重新启动滚动监听
  906. searchBar.addEventListener('input', function() {
  907. if (!searchBar.value && !statusFilter.value && !startDate.value && !endDate.value) {
  908. console.log("搜索条件清空,重新启动滚动监听");
  909. restartScrollListeners();
  910. }
  911. });