license_info.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. // 全局变量定义
  2. let page = 21; // 初始页码为1,代表从第1条数据开始获取
  3. let pageSize = 30; // 初始每次固定获取10条数据
  4. let licenseTotal = 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. function checkPermissionsAndShowButton() {
  86. console.log("checkPermissionsAndShowButton ", currentUserPermissions);
  87. if (currentUserPermissions.includes('capture_license_once_to_db')) {
  88. const captureLicenseBtn = document.getElementById('capture-license-btn');
  89. captureLicenseBtn.style.display = 'block';
  90. // 为按钮添加点击事件监听
  91. captureLicenseBtn.addEventListener('click', CaptureLicenseOncefunc);
  92. }
  93. }
  94. // 主动抓取一次License数据函数
  95. function CaptureLicenseOncefunc() {
  96. // 显示加载模态框
  97. const loadingModal = document.createElement('div');
  98. loadingModal.classList.add('modal-content', 'apple-modal-content');
  99. loadingModal.innerHTML = `
  100. <h3>正在获取 License 信息...</h3>
  101. <div class="progress-bar" style="width: 100%; height: 20px; background-color: #e0e0e0;">
  102. <div class="progress" style="width: 50%; height: 100%; background-color: #007aff;"></div>
  103. </div>
  104. `;
  105. document.body.appendChild(loadingModal);
  106. // 定义超时函数
  107. const timeoutPromise = new Promise((_, reject) => {
  108. setTimeout(() => {
  109. reject(new Error('获取超时'));
  110. }, 10000); // 10秒超时
  111. });
  112. // 发起 GET 请求的 Promise
  113. const fetchPromise = fetch('http://10.28.20.105:8080/api/admin/GetCaptureLicenseOnce', {
  114. method: 'GET',
  115. headers: {
  116. 'Authorization': `Bearer ${authToken}`, // 假设 token 已定义
  117. 'Content-Type': 'application/json'
  118. }
  119. }).then(response => response.json());
  120. // 使用 Promise.race 来竞争两个 Promise,哪个先返回就使用哪个
  121. Promise.race([fetchPromise, timeoutPromise])
  122. .then(data => {
  123. // 先关闭进度条
  124. document.body.removeChild(loadingModal);
  125. // 显示成功或失败提示
  126. if (data.success) {
  127. alert('License 获取成功!');
  128. } else {
  129. alert('获取失败:' + data.error);
  130. }
  131. })
  132. .catch(error => {
  133. // 先关闭进度条
  134. document.body.removeChild(loadingModal);
  135. // 显示错误提示
  136. alert(error.message); // 如果超时或请求失败
  137. });
  138. }
  139. //-----------侧边栏----------------------------
  140. // 获取所有菜单项
  141. const menuItems = document.querySelectorAll('nav ul li a');
  142. // 为每个菜单项添加点击事件监听器
  143. menuItems.forEach(item => {
  144. item.addEventListener('click', function() {
  145. // 移除其他项的 active 类
  146. menuItems.forEach(i => i.classList.remove('active'));
  147. // 为当前点击的项添加 active 类
  148. this.classList.add('active');
  149. });
  150. });
  151. //用户管理-
  152. //获取用户管理和 License 信息按钮
  153. const userManagementLink = document.getElementById('user-management-link');
  154. const licenseInfoLink = document.getElementById('license-info-link');
  155. const roleManagementLink = document.getElementById('role-management-link');
  156. // 根据用户权限控制菜单显示
  157. function updateMenuVisibility() {
  158. console.log("updateMenuVisibility: ",currentUserPermissions);
  159. if (currentUserPermissions) {
  160. userManagementLink.style.display = currentUserPermissions.includes('read_user') ? 'block' : 'none';
  161. roleManagementLink.style.display = currentUserPermissions.includes('get_role') ? 'block' : 'none';
  162. licenseInfoLink.style.display = (currentUserPermissions.includes('read_license') || currentUserPermissions.includes('read_all_license')) ? 'block' : 'none';
  163. }
  164. // 调用函数检查权限并显示按钮
  165. checkPermissionsAndShowButton();
  166. }
  167. // 监听用户管理按钮的点击事件
  168. userManagementLink.addEventListener('click', function(event) {
  169. event.preventDefault(); // 阻止默认的跳转行为
  170. removeScrollListeners(); // 移除滚动监听器
  171. // 使用 fetch 来加载 user_management.html 的内容
  172. fetch('../user/user_management.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 = '../user/user.js';
  180. document.body.appendChild(script);
  181. })
  182. .catch(error => console.error('加载用户管理页面失败:', error));
  183. });
  184. // 监听 License 信息按钮的点击事件
  185. licenseInfoLink.addEventListener('click', function(event) {
  186. event.preventDefault(); // 阻止默认的跳转行为
  187. // 将瀑布流的 License 信息内容恢复到主内容区域
  188. const licenseInfoHtml = `
  189. <!-- 包裹搜索框、下拉框、时间选择框和确定按钮的 div -->
  190. <div class="search-container">
  191. <!-- License 状态下拉框 -->
  192. <select id="license-status-filter" aria-label="选择 License 状态">
  193. <option value="">License 状态</option>
  194. <option value="已生成">已生成</option>
  195. <option value="未生成">未生成</option>
  196. <option value="已失效">已失效</option>
  197. </select>
  198. <!-- 开始时间选择框,类型改为 date -->
  199. <input type="date" id="start-date" placeholder="开始时间" />
  200. <!-- 结束时间选择框,类型改为 date -->
  201. <input type="date" id="end-date" placeholder="结束时间" />
  202. <!-- 搜索框 -->
  203. <input type="text" id="search-bar" placeholder="搜索..." />
  204. <!-- 确定按钮 -->
  205. <button id="submit-button">确定</button>
  206. </div>
  207. <div class="license-info-container" id="license-info-restaurant-list"> </div>
  208. `; // 这是原来的 License 信息区域 HTML
  209. //mainContainer.innerHTML = licenseInfoHtml;
  210. license_info_mainElement.innerHTML = licenseInfoHtml;
  211. //清楚lic信息组的数据
  212. LicApplicationData = [];
  213. initializeScrollListeners(); // 重新初始化滚动监听器
  214. // 再次加载 License 信息数据并渲染卡片
  215. (async function() {
  216. const data = await fetchLicenseData(1, 10);
  217. if (data.length > 0) {
  218. console.log('加载的数据:', data); // 检查是否成功获取数据
  219. renderLicenseCards(data, true); // 渲染数据到页面并清空之前的内容
  220. } else {
  221. console.error('未加载到数据');
  222. }
  223. })();
  224. });
  225. roleManagementLink.addEventListener('click', function(event) {
  226. event.preventDefault(); // 阻止默认的跳转行为
  227. removeScrollListeners(); // 移除滚动监听器
  228. // 使用 fetch 来加载 user_management.html 的内容
  229. fetch('../role/role.html')
  230. .then(response => response.text())
  231. .then(data => {
  232. // 将 user_management.html 的内容插入到主内容区域
  233. license_info_mainElement.innerHTML = data;
  234. // 动态引入 user.js 文件
  235. const script = document.createElement('script');
  236. script.src = '../role/role.js';
  237. document.body.appendChild(script);
  238. })
  239. .catch(error => console.error('加载用户管理页面失败:', error));
  240. });
  241. //-------license数据显示------------------------------------------------------
  242. // 获取数据函数
  243. async function fetchLicenseData(page, pageSize) {
  244. try {
  245. const response = await fetch(`http://10.28.20.105:8080/api/admin/GetAllLicenseInfo?page=${page}&pageSize=${pageSize}`, {
  246. method: 'GET',
  247. headers: {
  248. 'Authorization': `Bearer ${authToken}`,
  249. 'Content-Type': 'application/json'
  250. }
  251. });
  252. const result = await response.json();
  253. // 设置总量,如果第一次加载,获取total字段
  254. if (licenseTotal === 0 && result.total) {
  255. licenseTotal = result.total;
  256. }
  257. console.log("result total ",licenseTotal,result.total);
  258. // 使用 concat 方法将新数据与之前的数据进行累加
  259. LicApplicationData = LicApplicationData.concat(result.data || []);
  260. console.log("LicApplicationData: ",LicApplicationData,licenseTotal,result);
  261. return result.data || [];
  262. } catch (error) {
  263. console.error("加载数据失败", error);
  264. return []; // 返回空数组,防止后续操作出错
  265. }
  266. }
  267. // 渲染 license_info 卡片数据函数
  268. function renderLicenseCards(data, clearContainer = false) {
  269. console.log("-----------渲染次数");
  270. // 获取与 license_info 相关的 HTML 元素
  271. const license_info_container = document.getElementById('license-info-restaurant-list'); // 卡片列表容器
  272. if (clearContainer) {
  273. license_info_container.innerHTML = ''; // 清空容器内容
  274. isLoading = false; // 重置加载状态
  275. loadedItems = 0; // 重置已加载项数
  276. page = 21; // 每次请求后,page 增加10,表示从下一组数据开始
  277. pageSize = 30; // pageSize 每次递增10
  278. console.log("-----------渲染清除");
  279. }
  280. console.log("-----------data:",data);
  281. data.forEach(group => {
  282. const firstItem = group[0]; // 获取该组的第一个数据项
  283. // 获取子行的数量
  284. const childRowCount = group.length;
  285. let statusClass = '';
  286. if (firstItem.LicenseFlage === '已生成') {
  287. statusClass = 'license-status-green';
  288. } else if (firstItem.LicenseFlage === '未生成') {
  289. statusClass = 'license-status-yellow';
  290. } else if (firstItem.LicenseFlage === '已失效') {
  291. statusClass = 'license-status-red';
  292. }
  293. const card = document.createElement('div');
  294. card.className = 'license-info-card';
  295. // 给卡片添加一个唯一标识符的 data 属性
  296. card.setAttribute('data-oa-request-id', firstItem.oa_request_id);
  297. // 在卡片的第一行显示申请时间
  298. card.innerHTML = `
  299. <div class="license-info-card-header">
  300. <h3 class="card-title">${firstItem.GlxmName}</h3>
  301. </div>
  302. <div class="license-info-card-content">
  303. <p class="card-text">${firstItem.ApplicationDate} ${firstItem.ApplicationTime}</p> <!-- 显示日期和时间 -->
  304. <p class="card-text">创建者:${firstItem.Creator}</p>
  305. <p class="card-text">公司:${firstItem.Company}</p>
  306. <p class="card-text">集群:${childRowCount} 套 共计:${firstItem.TotalNodes} 节点</p>
  307. <p class="card-text license-status ${statusClass}">许可证状态:${firstItem.LicenseFlage}</p>
  308. <p class="card-text">oa_request_id:${firstItem.oa_request_id}</p>
  309. </div>
  310. `;
  311. // 给卡片添加点击事件,点击后显示模态框
  312. card.addEventListener('click', () => {
  313. // 传递当前卡片的详细数据到模态框
  314. const oaRequestId = card.getAttribute('data-oa-request-id');
  315. showModalForCard(group, oaRequestId); // 传递 oa_request_id
  316. //showModalForCard(group); // 传递当前卡片的详细数据到模态框
  317. });
  318. // 将卡片添加到容器中
  319. license_info_container.appendChild(card);
  320. });
  321. }
  322. // 检查是否滚动到底部并触发加载
  323. // async function checkAndLoadMore(scrollHeight, scrollTop, clientHeight) {
  324. // if (isLoading || loadedItems >= licenseTotal) return; // 如果正在加载或已加载完所有数据则退出
  325. // // console.log(`Scroll Info - scrollHeight: ${scrollHeight}, scrollTop: ${scrollTop}, clientHeight: ${clientHeight}`);
  326. // if (scrollTop + clientHeight >= scrollHeight - preLoadDistance) {
  327. // console.log(`触发加载更多数据:page=${page}, pageSize=${pageSize}`); // 每次触发时打印输出
  328. // await loadMoreData();
  329. // }
  330. // }
  331. // 加载更多数据函数
  332. async function loadMoreData() {
  333. if (isLoading) return; // 防止重复加载
  334. isLoading = true;
  335. console.log('开始加载更多数据');
  336. // 显示加载提示
  337. // license_info_loadingIndicator.style.display = 'block'; // 显示提示
  338. // license_info_loadingIndicator.innerText = '正在加载...'; // 重置加载提示
  339. // 设置超时处理
  340. const timeout = setTimeout(() => {
  341. license_info_loadingIndicator.innerText = '加载超时,请重试'; // 修改提示语为超时提示
  342. isLoading = false;
  343. license_info_loadingIndicator.style.display = 'none'; // 超时后隐藏加载提示
  344. }, timeoutDuration);
  345. // 获取数据
  346. const data = await fetchLicenseData(page, pageSize);
  347. console.log(`加载的新数据 data`,data); // 每次触发时打印输出
  348. // 清除超时定时器
  349. clearTimeout(timeout);
  350. if (data.length > 0) {
  351. // 更新 page 和 pageSize,下一次请求从新的位置开始
  352. page += 10; // 每次请求后,page 增加10,表示从下一组数据开始
  353. pageSize += 10; // pageSize 每次递增10
  354. // 更新已加载的条目数
  355. loadedItems += data.length;
  356. // 渲染数据到页面
  357. renderLicenseCards(data);
  358. console.log('数据加载完成,更新页面');
  359. }
  360. // 隐藏加载提示
  361. //license_info_loadingIndicator.style.display = 'none'; // 加载完成后隐藏提示
  362. isLoading = false; // 请求完成,允许下次请求
  363. // 检查内容高度,必要时继续加载
  364. //checkContentHeight();
  365. checkAndLoadMore();
  366. }
  367. //--------------------------监听 window 滚动---监听 main 容器的滚动-----------------------------------------------
  368. // // 监听 window 滚动
  369. // window.addEventListener('scroll', () => {
  370. // checkAndLoadMore(document.body.scrollHeight, window.scrollY, window.innerHeight);
  371. // });
  372. // // 监听 main 容器的滚动
  373. // license_info_mainElement.addEventListener('scroll', () => {
  374. // checkAndLoadMore(license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  375. // });
  376. function initializeScrollListeners() {
  377. // 只监听 main 容器的滚动
  378. license_info_mainElement.addEventListener('scroll', handleMainScroll);
  379. // console.log('滚动监听已初始化');
  380. }
  381. function removeScrollListeners() {
  382. // 移除 main 容器的滚动监听
  383. license_info_mainElement.removeEventListener('scroll', handleMainScroll);
  384. }
  385. // 新增一个重启滑动监听功能函数,当用户清空搜索条件时调用
  386. function restartScrollListeners() {
  387. // 重新绑定滑动监听器
  388. initializeScrollListeners();
  389. }
  390. function handleMainScroll() {
  391. // console.log('handleMainScroll', license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  392. checkAndLoadMore(license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  393. }
  394. async function checkAndLoadMore(scrollHeight, scrollTop, clientHeight) {
  395. if (isLoading || loadedItems >= licenseTotal) return; // 如果正在加载或已加载完所有数据则退出
  396. // console.log(`Scroll Info - scrollHeight: ${scrollHeight}, scrollTop: ${scrollTop}, clientHeight: ${clientHeight}`);
  397. if (scrollTop + clientHeight >= scrollHeight - preLoadDistance) {
  398. console.log(`触发加载更多数据:page=${page}, pageSize=${pageSize}`); // 每次触发时打印输出
  399. await loadMoreData();
  400. }
  401. }
  402. //-----------------------------------------------------------------------------------------
  403. // 初始化加载第一页
  404. (async function() {
  405. const data = await fetchLicenseData(1, 20);
  406. if (data.length > 0) {
  407. renderLicenseCards(data); // 渲染数据到页面
  408. loadedItems += data.length; // 更新已加载的条目数
  409. }
  410. //license_info_loadingIndicator.style.display = 'none'; // 初始化后隐藏加载提示
  411. // 检查内容高度
  412. // checkContentHeight();
  413. checkAndLoadMore()
  414. })();
  415. //初始化监听滚动条
  416. initializeScrollListeners()
  417. ///-----------获取登录用户信息----------------------------------------
  418. async function fetchUsername() {
  419. try {
  420. const response = await fetch(`http://10.28.20.105:8080/api/admin/userInfo`, {
  421. method: 'GET',
  422. headers: {
  423. 'Authorization': `Bearer ${authToken}`,
  424. 'Content-Type': 'application/json'
  425. }
  426. });
  427. const data = await response.json();
  428. currentUserRole = data.data.Role; // 存储当前用户的角色
  429. currentUserName = data.data.Username;
  430. // 使用获取到的角色,调用获取权限的接口
  431. await fetchPermissionsByRole(currentUserRole);
  432. console.log('当前用户角色:', data);
  433. currentUserInfo = data.data;
  434. // 调用该函数,初始化模态框
  435. setupUserInfoModal(currentUserInfo);
  436. updateMenuVisibility();
  437. return data.data; // 返回获取到的用户信息数据
  438. } catch (error) {
  439. console.error('Error fetching user info or permissions:', error);
  440. }
  441. }
  442. fetchUsername();
  443. // 调用函数更新菜单可见性
  444. // 将 fetchPermissionsByRole 转换为异步函数
  445. async function fetchPermissionsByRole(role) {
  446. try {
  447. const response = await fetch(`http://10.28.20.105:8080/api/admin/GetSelfRoles`, {
  448. method: 'POST',
  449. headers: {
  450. 'Authorization': `Bearer ${authToken}`,
  451. 'Content-Type': 'application/json'
  452. },
  453. body: JSON.stringify({ name: role })
  454. });
  455. const data = await response.json();
  456. currentUserPermissions = data.data.Permissions; // 获取用户的权限数组
  457. console.log('currentUserPermissions:', currentUserPermissions);
  458. // 定义权限类别
  459. // const licensePermissions = ['upload_license', 'read_license'];
  460. // const userPermissionsCheck = ['create_user', 'read_user', 'update_user', 'delete_user'];
  461. // const rolePermissions = ['create_role', 'delete_role', 'update_role', 'get_role'];
  462. // const hasLicenseAccess = licensePermissions.some(permission => userPermissions.includes(permission));
  463. // const hasUserManagementAccess = userPermissionsCheck.some(permission => userPermissions.includes(permission));
  464. // const hasRoleManagementAccess = rolePermissions.some(permission => userPermissions.includes(permission));
  465. // 根据权限渲染菜单并显示初始页面
  466. //renderMenuAndInitialPage(hasLicenseAccess, hasUserManagementAccess, hasRoleManagementAccess);
  467. } catch (error) {
  468. console.error('Error fetching permissions:', error);
  469. }
  470. }
  471. //--------------进度条-------------------------------------------------
  472. // 创建模态框的 DOM 元素并插入到 body 中
  473. function createLoadingModal() {
  474. const modalHTML = `
  475. <div id="loadingModal" class="loading-modal" style="display: none;">
  476. <div class="loading-modal-content">
  477. <div class="spinner"></div>
  478. <p id="loadingMessage">加载中...</p>
  479. </div>
  480. </div>
  481. `;
  482. document.body.insertAdjacentHTML('beforeend', modalHTML);
  483. }
  484. // 显示加载模态框
  485. function showLoadingModal(message = "加载中...") {
  486. const loadingModal = document.getElementById('loadingModal');
  487. const loadingMessage = document.getElementById('loadingMessage');
  488. if (loadingModal && loadingMessage) {
  489. loadingMessage.textContent = message; // 设置显示的消息
  490. loadingModal.style.display = 'flex'; // 显示模态框
  491. }
  492. }
  493. // 隐藏加载模态框
  494. function hideLoadingModal() {
  495. const loadingModal = document.getElementById('loadingModal');
  496. if (loadingModal) {
  497. loadingModal.style.display = 'none'; // 隐藏模态框
  498. }
  499. }
  500. // 页面加载时创建模态框
  501. document.addEventListener('DOMContentLoaded', createLoadingModal);
  502. //-----------搜索栏----------------------------
  503. // 获取搜索框元素
  504. // 获取搜索框、状态下拉框、时间选择框和按钮元素
  505. // 获取搜索框、状态下拉框、时间选择框和按钮元素
  506. const searchBar = document.getElementById('search-bar');
  507. const statusFilter = document.getElementById('license-status-filter');
  508. const startDate = document.getElementById('start-date');
  509. const endDate = document.getElementById('end-date');
  510. const submitButton = document.getElementById('submit-button');
  511. const licenseInfoContainer = document.getElementById('license-info-restaurant-list');
  512. // 过滤功能实现
  513. function filterContent() {
  514. console.log('过滤功能触发');
  515. // 移除滚动监听器,停止滚动加载
  516. // removeScrollListeners();
  517. console.log('statusFilter.valuesdaad撒大啊', statusFilter.value,startDate.value ,endDate.value,searchBar.value );
  518. // 检查如果所有输入框都是默认值,则直接返回,不发送数据
  519. if (!statusFilter.value && !startDate.value && !endDate.value && !searchBar.value) {
  520. console.log("所有过滤条件为空,不发送请求");
  521. return; // 不发送请求
  522. }
  523. // 构建请求体参数
  524. const requestData = {
  525. license_flag: statusFilter.value || undefined,
  526. starting_date: startDate.value || undefined,
  527. end_date: endDate.value || undefined,
  528. any_search: searchBar.value || undefined,
  529. };
  530. console.log("requestData",requestData);
  531. // 发送 POST 请求到接口
  532. fetch('http://10.28.20.105:8080/api/admin/GetConditionalSearch', {
  533. method: 'POST',
  534. headers: {
  535. 'Authorization': `Bearer ${authToken}`,
  536. 'Content-Type': 'application/json',
  537. },
  538. body: JSON.stringify(requestData),
  539. })
  540. .then(response => response.json())
  541. .then(data => {
  542. console.log('成功获取数据:', data);
  543. // 处理返回的数据并更新界面
  544. displayLicenseInfo(data.data);
  545. })
  546. .catch(error => {
  547. console.error('获取数据时发生错误:', error);
  548. });
  549. }
  550. // 处理并显示返回的 License 信息
  551. function displayLicenseInfo(data) {
  552. // 清空之前的结果
  553. licenseInfoContainer.innerHTML = '';
  554. LicApplicationData =[];
  555. // 遍历返回的数据,生成并插入卡片
  556. // 处理返回的数据并更新界面,使用 renderLicenseCards 方法进行渲染
  557. renderLicenseCards(data, true);
  558. }
  559. // 确定按钮点击事件的修改(停止滚动监听)
  560. submitButton.addEventListener('click', function() {
  561. // 检查下拉框和时间选择框是否为默认值
  562. if (!statusFilter.value && !startDate.value && !endDate.value) {
  563. // 如果都是默认值,则刷新页面
  564. location.reload();
  565. } else {
  566. // 否则,执行过滤内容功能
  567. filterContent();
  568. // 隐藏滚动加载,防止继续加载更多内容
  569. removeScrollListeners();
  570. }
  571. });
  572. // 监听返回按钮的点击事件,刷新页面并恢复滚动监听
  573. const resetButton = document.getElementById('reset-button');
  574. resetButton.addEventListener('click', function() {
  575. // 刷新页面
  576. location.reload();
  577. });
  578. // 修改当搜索栏没有输入时,重新启动滚动监听
  579. searchBar.addEventListener('input', function() {
  580. if (!searchBar.value && !statusFilter.value && !startDate.value && !endDate.value) {
  581. console.log("搜索条件清空,重新启动滚动监听");
  582. restartScrollListeners();
  583. }
  584. });