script.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. //获取 主内容区域
  12. const license_info_mainElement = document.querySelector('main'); // 主内容区域
  13. //模态框
  14. const license_info_modal = document.getElementById('license-info-modal'); // 模态框容器
  15. const license_info_modalContent = document.querySelector('.license-info-modal-content'); // 模态框内容区域
  16. const license_info_modalDescription = document.getElementById('license-info-modal-description'); // 模态框描述
  17. const license_info_modalPrice = document.getElementById('license-info-modal-price'); // 模态框产品信息
  18. const license_info_modalRating = document.getElementById('license-info-modal-rating'); // 模态框MAC地址
  19. const license_info_closeModal = document.querySelector('.license-info-close'); // 模态框关闭按钮
  20. const license_info_loadingIndicator = document.getElementById('loading-indicator'); // 加载提示元素
  21. //存储
  22. let LicApplicationData = []; // 用于存储从接口获取的数据
  23. // 获取数据函数
  24. async function fetchLicenseData(page, pageSize) {
  25. try {
  26. const response = await fetch(`http://127.0.0.1:8080/api/admin/GetAllLicenseInfo?page=${page}&pageSize=${pageSize}`, {
  27. method: 'GET',
  28. headers: {
  29. 'Authorization': `Bearer ${authToken}`,
  30. 'Content-Type': 'application/json'
  31. }
  32. });
  33. const result = await response.json();
  34. // 设置总量,如果第一次加载,获取total字段
  35. if (total === 0 && result.total) {
  36. total = result.total;
  37. }
  38. // 使用 concat 方法将新数据与之前的数据进行累加
  39. LicApplicationData = LicApplicationData.concat(result.data || []);
  40. console.log("LicApplicationData: ",LicApplicationData);
  41. return result.data || [];
  42. } catch (error) {
  43. console.error("加载数据失败", error);
  44. return []; // 返回空数组,防止后续操作出错
  45. }
  46. }
  47. // 渲染 license_info 卡片数据函数
  48. function renderLicenseCards(data, clearContainer = false) {
  49. console.log("-----------渲染次数");
  50. // 获取与 license_info 相关的 HTML 元素
  51. const license_info_container = document.getElementById('license-info-restaurant-list'); // 卡片列表容器
  52. if (clearContainer) {
  53. license_info_container.innerHTML = ''; // 清空容器内容
  54. isLoading = false; // 重置加载状态
  55. loadedItems = 0; // 重置已加载项数
  56. page = 11; // 每次请求后,page 增加10,表示从下一组数据开始
  57. pageSize = 20; // pageSize 每次递增10
  58. console.log("-----------渲染清除");
  59. }
  60. console.log("-----------data:",data);
  61. data.forEach(group => {
  62. const firstItem = group[0]; // 获取该组的第一个数据项
  63. let statusClass = '';
  64. if (firstItem.LicenseFlage === '已生成') {
  65. statusClass = 'license-status-green';
  66. } else if (firstItem.LicenseFlage === '未生成') {
  67. statusClass = 'license-status-yellow';
  68. } else if (firstItem.LicenseFlage === '已失效') {
  69. statusClass = 'license-status-red';
  70. }
  71. const card = document.createElement('div');
  72. card.className = 'license-info-card';
  73. // 在卡片的第一行显示申请时间
  74. card.innerHTML = `
  75. <div class="license-info-card-header">
  76. <h3 class="card-title">${firstItem.GlxmName}</h3>
  77. </div>
  78. <div class="license-info-card-content">
  79. <p class="card-text">${firstItem.ApplicationDate} ${firstItem.ApplicationTime}</p> <!-- 显示日期和时间 -->
  80. <p class="card-text">创建者:${firstItem.Creator}</p>
  81. <p class="card-text">公司:${firstItem.Company}</p>
  82. <p class="card-text ${statusClass}">许可证状态:${firstItem.LicenseFlage}</p>
  83. <p class="card-text">oa_request_id:${firstItem.oa_request_id}</p>
  84. </div>
  85. `;
  86. // 给卡片添加点击事件,点击后显示模态框
  87. card.addEventListener('click', () => {
  88. showModalForCard(group); // 传递当前卡片的详细数据到模态框
  89. });
  90. // 将卡片添加到容器中
  91. license_info_container.appendChild(card);
  92. });
  93. }
  94. // 检查页面是否填满,如果没有则继续加载更多数据
  95. function checkContentHeight() {
  96. const documentHeight = document.documentElement.scrollHeight;
  97. const windowHeight = window.innerHeight;
  98. if (documentHeight <= windowHeight) {
  99. console.log("页面内容不足,继续加载更多数据");
  100. loadMoreData(); // 当内容高度不足时,继续加载更多数据
  101. }
  102. }
  103. // 检查是否滚动到底部并触发加载
  104. async function checkAndLoadMore(scrollHeight, scrollTop, clientHeight) {
  105. if (isLoading || loadedItems >= total) return; // 如果正在加载或已加载完所有数据则退出
  106. // console.log(`Scroll Info - scrollHeight: ${scrollHeight}, scrollTop: ${scrollTop}, clientHeight: ${clientHeight}`);
  107. if (scrollTop + clientHeight >= scrollHeight - preLoadDistance) {
  108. console.log(`触发加载更多数据:page=${page}, pageSize=${pageSize}`); // 每次触发时打印输出
  109. await loadMoreData();
  110. }
  111. }
  112. // 加载更多数据函数
  113. async function loadMoreData() {
  114. if (isLoading) return; // 防止重复加载
  115. isLoading = true;
  116. console.log('开始加载更多数据');
  117. // 显示加载提示
  118. // license_info_loadingIndicator.style.display = 'block'; // 显示提示
  119. // license_info_loadingIndicator.innerText = '正在加载...'; // 重置加载提示
  120. // 设置超时处理
  121. const timeout = setTimeout(() => {
  122. license_info_loadingIndicator.innerText = '加载超时,请重试'; // 修改提示语为超时提示
  123. isLoading = false;
  124. license_info_loadingIndicator.style.display = 'none'; // 超时后隐藏加载提示
  125. }, timeoutDuration);
  126. // 获取数据
  127. const data = await fetchLicenseData(page, pageSize);
  128. console.log(`加载的新数据 data`,data); // 每次触发时打印输出
  129. // 清除超时定时器
  130. clearTimeout(timeout);
  131. if (data.length > 0) {
  132. // 更新 page 和 pageSize,下一次请求从新的位置开始
  133. page += 10; // 每次请求后,page 增加10,表示从下一组数据开始
  134. pageSize += 10; // pageSize 每次递增10
  135. // 更新已加载的条目数
  136. loadedItems += data.length;
  137. // 渲染数据到页面
  138. renderLicenseCards(data);
  139. console.log('数据加载完成,更新页面');
  140. }
  141. // 隐藏加载提示
  142. //license_info_loadingIndicator.style.display = 'none'; // 加载完成后隐藏提示
  143. isLoading = false; // 请求完成,允许下次请求
  144. // 检查内容高度,必要时继续加载
  145. checkContentHeight();
  146. }
  147. //--------------------------监听 window 滚动---监听 main 容器的滚动-----------------------------------------------
  148. // // 监听 window 滚动
  149. // window.addEventListener('scroll', () => {
  150. // checkAndLoadMore(document.body.scrollHeight, window.scrollY, window.innerHeight);
  151. // });
  152. // // 监听 main 容器的滚动
  153. // license_info_mainElement.addEventListener('scroll', () => {
  154. // checkAndLoadMore(license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  155. // });
  156. function initializeScrollListeners() {
  157. // 监听 window 滚动
  158. window.addEventListener('scroll', handleWindowScroll);
  159. // 监听 main 容器的滚动
  160. license_info_mainElement.addEventListener('scroll', handleMainScroll);
  161. // console.log('滚动监听已初始化');
  162. }
  163. function removeScrollListeners() {
  164. // 移除 window 滚动监听
  165. window.removeEventListener('scroll', handleWindowScroll);
  166. // 移除 main 容器的滚动监听
  167. license_info_mainElement.removeEventListener('scroll', handleMainScroll);
  168. }
  169. function handleWindowScroll() {
  170. // console.log('handleWindowScroll',document.body.scrollHeight, window.scrollY, window.innerHeight);
  171. checkAndLoadMore(document.body.scrollHeight, window.scrollY, window.innerHeight);
  172. }
  173. function handleMainScroll() {
  174. // console.log('handleMainScroll',license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  175. checkAndLoadMore(license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  176. }
  177. //-----------------------------------------------------------------------------------------
  178. // 初始化加载第一页
  179. (async function() {
  180. const data = await fetchLicenseData(1, 10);
  181. if (data.length > 0) {
  182. renderLicenseCards(data); // 渲染数据到页面
  183. loadedItems += data.length; // 更新已加载的条目数
  184. }
  185. //license_info_loadingIndicator.style.display = 'none'; // 初始化后隐藏加载提示
  186. // 检查内容高度
  187. checkContentHeight();
  188. })();
  189. initializeScrollListeners()
  190. //-----------点击卡片弹出模态框------------------------------------------------------
  191. // 模态框显示函数
  192. // 模态框显示函数
  193. function showModalForCard(item) {
  194. const modal = document.getElementById('license-info-modal');
  195. const modalContent = document.querySelector('.license-info-modal-content');
  196. const modalBody = document.getElementById('license-info-modal-body'); // 获取下半部分容器
  197. // 设置分页相关的变量
  198. let currentPage = 1;
  199. const itemsPerPage = 2; // 每页显示两组
  200. // 对 item 数组按 oa_id 进行升序排序
  201. const sortedItem = item.sort((a, b) => a.oa_id - b.oa_id);
  202. const totalPages = Math.ceil(sortedItem.length / itemsPerPage); // 计算总页数
  203. // 获取分页容器
  204. const paginationContainer = document.querySelector('.license-info-modal-pagination');
  205. // 清空分页容器,避免重复创建元素
  206. paginationContainer.innerHTML = '';
  207. // 创建"上一页"按钮
  208. const prevButton = document.createElement('button');
  209. prevButton.classList.add('prev-page');
  210. prevButton.innerText = '上一页';
  211. paginationContainer.appendChild(prevButton);
  212. // 创建下拉框
  213. const selectPageDropdown = document.createElement('select');
  214. paginationContainer.appendChild(selectPageDropdown);
  215. // 创建"下一页"按钮
  216. const nextButton = document.createElement('button');
  217. nextButton.classList.add('next-page');
  218. nextButton.innerText = '下一页';
  219. paginationContainer.appendChild(nextButton);
  220. // 初始化上半部分内容(Company, Creator, ApplicationDate, ApplicationTime 和两个按钮)
  221. function initializeHeaderContent(firstItem) {
  222. // 清空上半部分内容
  223. const modalHeader = document.querySelector('.license-info-modal-header');
  224. modalHeader.innerHTML = ''; // 确保不会重复创建
  225. // 创建动态字段
  226. const companyDiv = document.createElement('div');
  227. companyDiv.classList.add('license-info-company');
  228. companyDiv.innerHTML = `<strong>Company: </strong> ${firstItem.Company}`;
  229. const creatorDiv = document.createElement('div');
  230. creatorDiv.classList.add('license-info-creator');
  231. creatorDiv.innerHTML = `<strong>Creator: </strong> ${firstItem.Creator}`;
  232. const applicationDateDiv = document.createElement('div');
  233. applicationDateDiv.classList.add('license-info-application-date');
  234. applicationDateDiv.innerHTML = `<strong>Application Date: </strong> ${firstItem.ApplicationDate}`;
  235. const applicationTimeDiv = document.createElement('div');
  236. applicationTimeDiv.classList.add('license-info-application-time');
  237. applicationTimeDiv.innerHTML = `<strong>Application Time: </strong> ${firstItem.ApplicationTime}`;
  238. // 创建两个按钮
  239. const button1 = document.createElement('button');
  240. button1.classList.add('license-info-modal-button');
  241. button1.innerText = '按钮1';
  242. button1.addEventListener('click', () => {
  243. alert('按钮1被点击');
  244. });
  245. const button2 = document.createElement('button');
  246. button2.classList.add('license-info-modal-button');
  247. button2.innerText = '按钮2';
  248. button2.addEventListener('click', () => {
  249. alert('按钮2被点击');
  250. });
  251. // 将这些 div 和按钮插入到模态框的上半部分
  252. modalHeader.appendChild(companyDiv);
  253. modalHeader.appendChild(creatorDiv);
  254. modalHeader.appendChild(applicationDateDiv);
  255. modalHeader.appendChild(applicationTimeDiv);
  256. modalHeader.appendChild(button1);
  257. modalHeader.appendChild(button2);
  258. }
  259. // 初始化下拉框的页码选项
  260. function initializeDropdown() {
  261. selectPageDropdown.innerHTML = ''; // 清空下拉框中的选项
  262. for (let page = 1; page <= totalPages; page++) {
  263. const option = document.createElement('option');
  264. option.value = page;
  265. option.innerText = `第 ${page} 页`;
  266. selectPageDropdown.appendChild(option);
  267. }
  268. selectPageDropdown.value = currentPage; // 设置默认选项为当前页
  269. }
  270. // 渲染当前页内容
  271. function renderPage(page) {
  272. modalBody.innerHTML = ''; // 清空之前的内容
  273. // 计算当前页的起始和结束索引
  274. const startIndex = (page - 1) * itemsPerPage;
  275. const endIndex = Math.min(startIndex + itemsPerPage, sortedItem.length);
  276. // 更新上半部分的字段内容 (以第一个元素为例)
  277. const firstItem = sortedItem[0];
  278. initializeHeaderContent(firstItem); // 动态生成上半部分内容
  279. // 遍历当前页的数据
  280. for (let i = startIndex; i < endIndex; i++) {
  281. const group = sortedItem[i];
  282. const groupBox = document.createElement('div');
  283. groupBox.classList.add('license-info-group-box');
  284. // 动态生成组内容,显示编号为 i+1(表示组1, 组2...)
  285. groupBox.innerHTML = `
  286. <div class="license-info-group-title">组 ${i + 1}</div>
  287. <p><strong>UniqueID:</strong> ${group.UniqueID}</p>
  288. <p><strong>oa_id:</strong> ${group.oa_id}</p>
  289. <p><strong>oa_request_id:</strong> ${group.oa_request_id}</p>
  290. <p><strong>Creator:</strong> ${group.Creator}</p>
  291. <p><strong>oa_request_name_new:</strong> ${group.oa_request_name_new}</p>
  292. `;
  293. // 将生成的组内容加入到 modalBody
  294. modalBody.appendChild(groupBox);
  295. }
  296. // 更新下拉框的值
  297. selectPageDropdown.value = page;
  298. // 更新按钮状态
  299. prevButton.disabled = (page === 1);
  300. nextButton.disabled = (page === totalPages);
  301. }
  302. // 下拉框页码选择事件
  303. selectPageDropdown.addEventListener('change', function() {
  304. currentPage = parseInt(this.value);
  305. renderPage(currentPage); // 根据选择的页码渲染对应页面
  306. });
  307. // 上一页按钮点击事件
  308. prevButton.addEventListener('click', function() {
  309. if (currentPage > 1) {
  310. currentPage--;
  311. renderPage(currentPage);
  312. }
  313. });
  314. // 下一页按钮点击事件
  315. nextButton.addEventListener('click', function() {
  316. if (currentPage < totalPages) {
  317. currentPage++;
  318. renderPage(currentPage);
  319. }
  320. });
  321. // 显示或隐藏分页相关控件
  322. function togglePaginationVisibility() {
  323. if (totalPages <= 1) {
  324. // 隐藏下拉框和分页容器
  325. paginationContainer.style.display = 'none';
  326. } else {
  327. // 显示下拉框和分页容器
  328. paginationContainer.style.display = 'flex';
  329. }
  330. }
  331. // 初始化下拉框并渲染第一页
  332. initializeDropdown();
  333. renderPage(currentPage);
  334. // 根据页数决定是否显示分页控件
  335. togglePaginationVisibility();
  336. // 显示模态框
  337. modal.style.display = 'flex'; // 显示模态框背景
  338. setTimeout(() => {
  339. modalContent.classList.add('show'); // 添加动画效果
  340. }, 10); // 延时确保动画生效
  341. // 关闭模态框逻辑
  342. const closeModal = document.querySelector('.license-info-close');
  343. closeModal.addEventListener('click', () => {
  344. modalContent.classList.remove('show'); // 移除动画类
  345. setTimeout(() => {
  346. modal.style.display = 'none'; // 完全隐藏模态框
  347. }, 500); // 等待动画结束后再隐藏
  348. });
  349. // 点击模态框外部关闭模态框
  350. window.addEventListener('click', (event) => {
  351. if (event.target === modal) {
  352. modalContent.classList.remove('show');
  353. setTimeout(() => {
  354. modal.style.display = 'none'; // 完全隐藏模态框
  355. }, 500); // 等待动画结束后再隐藏
  356. }
  357. });
  358. }
  359. //----------------------------------------
  360. //获取用户管理和 License 信息按钮
  361. const userManagementLink = document.getElementById('user-management-link');
  362. const licenseInfoLink = document.getElementById('license-info-link');
  363. // 监听用户管理按钮的点击事件
  364. userManagementLink.addEventListener('click', function(event) {
  365. event.preventDefault(); // 阻止默认的跳转行为
  366. removeScrollListeners(); // 移除滚动监听器
  367. // 使用 fetch 来加载 user_management.html 的内容
  368. fetch('user_management.html')
  369. .then(response => response.text())
  370. .then(data => {
  371. // 将 user_management.html 的内容插入到主内容区域
  372. license_info_mainElement.innerHTML = data;
  373. })
  374. .catch(error => console.error('加载用户管理页面失败:', error));
  375. });
  376. // 监听 License 信息按钮的点击事件
  377. licenseInfoLink.addEventListener('click', function(event) {
  378. event.preventDefault(); // 阻止默认的跳转行为
  379. // 将瀑布流的 License 信息内容恢复到主内容区域
  380. const licenseInfoHtml = `
  381. <div class="license-info-container" id="license-info-restaurant-list"> </div>
  382. `; // 这是原来的 License 信息区域 HTML
  383. //mainContainer.innerHTML = licenseInfoHtml;
  384. license_info_mainElement.innerHTML = licenseInfoHtml;
  385. //清楚lic信息组的数据
  386. LicApplicationData = [];
  387. initializeScrollListeners(); // 重新初始化滚动监听器
  388. // 再次加载 License 信息数据并渲染卡片
  389. (async function() {
  390. const data = await fetchLicenseData(1, 10);
  391. if (data.length > 0) {
  392. console.log('加载的数据:', data); // 检查是否成功获取数据
  393. renderLicenseCards(data, true); // 渲染数据到页面并清空之前的内容
  394. } else {
  395. console.error('未加载到数据');
  396. }
  397. })();
  398. });
  399. //----------------------------------------
  400. // 获取用户管理模态框元素