license_info.js 30 KB

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