license_info.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. // 全局变量定义
  2. const serverIP = '10.28.20.150';
  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. // 创建一个新的 div 元素
  193. const container = document.createElement('div');
  194. // 将瀑布流的 License 信息内容恢复到主内容区域
  195. const licenseInfoHtml = `
  196. <!-- 包裹搜索框、下拉框、时间选择框和确定按钮的 div -->
  197. <div class="search-container">
  198. <!-- License 状态下拉框 -->
  199. <select id="license-status-filter" aria-label="选择 License 状态">
  200. <option value="">License 状态</option>
  201. <option value="已生成">已生成</option>
  202. <option value="未生成">未生成</option>
  203. <option value="已失效">已失效</option>
  204. </select>
  205. <!-- 开始时间选择框,类型改为 date -->
  206. <input type="date" id="start-date" placeholder="开始时间" />
  207. <!-- 结束时间选择框,类型改为 date -->
  208. <input type="date" id="end-date" placeholder="结束时间" />
  209. <!-- 搜索框 -->
  210. <input type="text" id="search-bar" style="display: none;" placeholder="搜索..." />
  211. <!-- 确定按钮 -->
  212. <button id="submit-button">确定</button>
  213. <button id="reset-button">返回</button>
  214. </div>
  215. <div class="license-info-container" id="license-info-restaurant-list"> </div>
  216. `; // 这是原来的 License 信息区域 HTML
  217. // 将 licenseInfoHtml 插入到 div 元素中
  218. // container.innerHTML = licenseInfoHtml;
  219. // searchBar = container.querySelector('search-bar');
  220. // statusFilter = container.querySelector('license-status-filter');
  221. // startDate = container.querySelector('start-date');
  222. // endDate = container.querySelector('end-date');
  223. // submitButton = container.querySelector('submit-button');
  224. // licenseInfoContainer = container.querySelector('license-info-restaurant-list');
  225. license_info_mainElement.innerHTML = licenseInfoHtml;
  226. searchBar = document.getElementById('search-bar');
  227. statusFilter = document.getElementById('license-status-filter');
  228. startDate = document.getElementById('start-date');
  229. endDate = document.getElementById('end-date');
  230. submitButton = document.getElementById('submit-button');
  231. licenseInfoContainer = document.getElementById('license-info-restaurant-list');
  232. //清楚lic信息组的数据
  233. LicApplicationData = [];
  234. initializeScrollListeners(); // 重新初始化滚动监听器
  235. // 再次加载 License 信息数据并渲染卡片
  236. (async function() {
  237. const data = await fetchLicenseData(1, 10);
  238. if (data.length > 0) {
  239. console.log('加载的数据:', data); // 检查是否成功获取数据
  240. renderLicenseCards(data, true); // 渲染数据到页面并清空之前的内容
  241. } else {
  242. console.error('未加载到数据');
  243. }
  244. })();
  245. // 刷新页面
  246. location.reload();
  247. });
  248. roleManagementLink.addEventListener('click', function(event) {
  249. event.preventDefault(); // 阻止默认的跳转行为
  250. removeScrollListeners(); // 移除滚动监听器
  251. // 使用 fetch 来加载 user_management.html 的内容
  252. fetch('../role/role.html')
  253. .then(response => response.text())
  254. .then(data => {
  255. // 将 user_management.html 的内容插入到主内容区域
  256. license_info_mainElement.innerHTML = data;
  257. // 动态引入 user.js 文件
  258. const script = document.createElement('script');
  259. script.src = '../role/role.js';
  260. document.body.appendChild(script);
  261. })
  262. .catch(error => console.error('加载用户管理页面失败:', error));
  263. });
  264. //-------license数据显示------------------------------------------------------
  265. // 获取数据函数
  266. async function fetchLicenseData(page, pageSize) {
  267. try {
  268. const response = await fetch(`http://${serverIP}:${serverPort}/api/admin/GetAllLicenseInfo?page=${page}&pageSize=${pageSize}`, {
  269. method: 'GET',
  270. headers: {
  271. 'Authorization': `Bearer ${authToken}`,
  272. 'Content-Type': 'application/json'
  273. }
  274. });
  275. const result = await response.json();
  276. // 设置总量,如果第一次加载,获取total字段
  277. if (licenseTotal === 0 && result.total) {
  278. licenseTotal = result.total;
  279. }
  280. console.log("result total ",licenseTotal,result.total);
  281. // 使用 concat 方法将新数据与之前的数据进行累加
  282. LicApplicationData = LicApplicationData.concat(result.data || []);
  283. console.log("LicApplicationData: ",LicApplicationData,licenseTotal,result);
  284. return result.data || [];
  285. } catch (error) {
  286. console.error("加载数据失败", error);
  287. return []; // 返回空数组,防止后续操作出错
  288. }
  289. }
  290. // 渲染 license_info 卡片数据函数
  291. function renderLicenseCards(data, clearContainer = false) {
  292. console.log("-----------渲染次数");
  293. // 获取与 license_info 相关的 HTML 元素
  294. const license_info_container = document.getElementById('license-info-restaurant-list'); // 卡片列表容器
  295. if (clearContainer) {
  296. license_info_container.innerHTML = ''; // 清空容器内容
  297. isLoading = false; // 重置加载状态
  298. loadedItems = 0; // 重置已加载项数
  299. page = 21; // 每次请求后,page 增加10,表示从下一组数据开始
  300. pageSize = 30; // pageSize 每次递增10
  301. console.log("-----------渲染清除");
  302. }
  303. console.log("-----------data:",data);
  304. data.forEach(group => {
  305. const firstItem = group[0]; // 获取该组的第一个数据项
  306. // 获取子行的数量
  307. const childRowCount = group.length;
  308. let statusClass = '';
  309. if (firstItem.LicenseFlage === '已生成') {
  310. statusClass = 'license-status-green';
  311. } else if (firstItem.LicenseFlage === '未生成') {
  312. statusClass = 'license-status-yellow';
  313. } else if (firstItem.LicenseFlage === '已失效') {
  314. statusClass = 'license-status-red';
  315. }
  316. /*筛选名字*/
  317. let creatorUser = firstItem.Creator;
  318. if (creatorUser.includes('-')) {
  319. let firstDashIndex = creatorUser.indexOf('-');
  320. let secondDashIndex = creatorUser.indexOf('-', firstDashIndex + 1);
  321. creatorUser = creatorUser.substring(firstDashIndex + 1, secondDashIndex);
  322. }
  323. const card = document.createElement('div');
  324. card.className = 'license-info-card';
  325. // 给卡片添加一个唯一标识符的 data 属性
  326. card.setAttribute('data-oa-request-id', firstItem.oa_request_id);
  327. // 在卡片的第一行显示申请时间
  328. //<p class="card-text">oa_request_id:${firstItem.oa_request_id}</p>
  329. card.innerHTML = `
  330. <div class="license-info-card-header">
  331. <h3 class="card-title">${firstItem.GlxmName}</h3>
  332. </div>
  333. <div class="license-info-card-content">
  334. <p class="card-text date-time">${firstItem.ApplicationDate} ${firstItem.ApplicationTime}</p> <!-- 显示日期和时间 -->
  335. <p class="card-text">创建者:${creatorUser}</p>
  336. <p class="card-text">公司:${firstItem.Company}</p>
  337. <p class="card-text">集群:${childRowCount} 套 共计:${firstItem.TotalNodes} 节点</p>
  338. <p class="card-text license-status ${statusClass}">许可证状态:${firstItem.LicenseFlage}</p>
  339. </div>
  340. `;
  341. // 给卡片添加点击事件,点击后显示模态框
  342. card.addEventListener('click', () => {
  343. // 传递当前卡片的详细数据到模态框
  344. const oaRequestId = card.getAttribute('data-oa-request-id');
  345. showModalForCard(group, oaRequestId); // 传递 oa_request_id
  346. //showModalForCard(group); // 传递当前卡片的详细数据到模态框
  347. });
  348. // 将卡片添加到容器中
  349. license_info_container.appendChild(card);
  350. });
  351. }
  352. // 检查是否滚动到底部并触发加载
  353. // async function checkAndLoadMore(scrollHeight, scrollTop, clientHeight) {
  354. // if (isLoading || loadedItems >= licenseTotal) return; // 如果正在加载或已加载完所有数据则退出
  355. // // console.log(`Scroll Info - scrollHeight: ${scrollHeight}, scrollTop: ${scrollTop}, clientHeight: ${clientHeight}`);
  356. // if (scrollTop + clientHeight >= scrollHeight - preLoadDistance) {
  357. // console.log(`触发加载更多数据:page=${page}, pageSize=${pageSize}`); // 每次触发时打印输出
  358. // await loadMoreData();
  359. // }
  360. // }
  361. // 加载更多数据函数
  362. async function loadMoreData() {
  363. if (isLoading) return; // 防止重复加载
  364. isLoading = true;
  365. console.log('开始加载更多数据');
  366. // 显示加载提示
  367. // license_info_loadingIndicator.style.display = 'block'; // 显示提示
  368. // license_info_loadingIndicator.innerText = '正在加载...'; // 重置加载提示
  369. // 设置超时处理
  370. const timeout = setTimeout(() => {
  371. license_info_loadingIndicator.innerText = '加载超时,请重试'; // 修改提示语为超时提示
  372. isLoading = false;
  373. license_info_loadingIndicator.style.display = 'none'; // 超时后隐藏加载提示
  374. }, timeoutDuration);
  375. // 获取数据
  376. const data = await fetchLicenseData(page, pageSize);
  377. console.log(`加载的新数据 data`,data); // 每次触发时打印输出
  378. // 清除超时定时器
  379. clearTimeout(timeout);
  380. if (data.length > 0) {
  381. // 更新 page 和 pageSize,下一次请求从新的位置开始
  382. page += 10; // 每次请求后,page 增加10,表示从下一组数据开始
  383. pageSize += 10; // pageSize 每次递增10
  384. // 更新已加载的条目数
  385. loadedItems += data.length;
  386. // 渲染数据到页面
  387. renderLicenseCards(data);
  388. console.log('数据加载完成,更新页面');
  389. }
  390. // 隐藏加载提示
  391. //license_info_loadingIndicator.style.display = 'none'; // 加载完成后隐藏提示
  392. isLoading = false; // 请求完成,允许下次请求
  393. // 检查内容高度,必要时继续加载
  394. //checkContentHeight();
  395. checkAndLoadMore();
  396. }
  397. //--------------------------监听 window 滚动---监听 main 容器的滚动-----------------------------------------------
  398. // // 监听 window 滚动
  399. // window.addEventListener('scroll', () => {
  400. // checkAndLoadMore(document.body.scrollHeight, window.scrollY, window.innerHeight);
  401. // });
  402. // // 监听 main 容器的滚动
  403. // license_info_mainElement.addEventListener('scroll', () => {
  404. // checkAndLoadMore(license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  405. // });
  406. function initializeScrollListeners() {
  407. // 只监听 main 容器的滚动
  408. license_info_mainElement.addEventListener('scroll', handleMainScroll);
  409. // console.log('滚动监听已初始化');
  410. }
  411. function removeScrollListeners() {
  412. // 移除 main 容器的滚动监听
  413. license_info_mainElement.removeEventListener('scroll', handleMainScroll);
  414. }
  415. // 新增一个重启滑动监听功能函数,当用户清空搜索条件时调用
  416. function restartScrollListeners() {
  417. // 重新绑定滑动监听器
  418. initializeScrollListeners();
  419. }
  420. function handleMainScroll() {
  421. // console.log('handleMainScroll', license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  422. checkAndLoadMore(license_info_mainElement.scrollHeight, license_info_mainElement.scrollTop, license_info_mainElement.clientHeight);
  423. }
  424. async function checkAndLoadMore(scrollHeight, scrollTop, clientHeight) {
  425. if (isLoading || loadedItems >= licenseTotal) return; // 如果正在加载或已加载完所有数据则退出
  426. // console.log(`Scroll Info - scrollHeight: ${scrollHeight}, scrollTop: ${scrollTop}, clientHeight: ${clientHeight}`);
  427. if (scrollTop + clientHeight >= scrollHeight - preLoadDistance) {
  428. console.log(`触发加载更多数据:page=${page}, pageSize=${pageSize}`); // 每次触发时打印输出
  429. await loadMoreData();
  430. }
  431. }
  432. //-----------------------------------------------------------------------------------------
  433. // 初始化加载第一页
  434. (async function() {
  435. const data = await fetchLicenseData(1, 20);
  436. if (data.length > 0) {
  437. renderLicenseCards(data); // 渲染数据到页面
  438. loadedItems += data.length; // 更新已加载的条目数
  439. }
  440. //license_info_loadingIndicator.style.display = 'none'; // 初始化后隐藏加载提示
  441. // 检查内容高度
  442. // checkContentHeight();
  443. checkAndLoadMore()
  444. })();
  445. //初始化监听滚动条
  446. initializeScrollListeners()
  447. ///-----------获取登录用户信息----------------------------------------
  448. async function fetchUsername() {
  449. try {
  450. const response = await fetch(`http://${serverIP}:${serverPort}/api/admin/userInfo`, {
  451. method: 'GET',
  452. headers: {
  453. 'Authorization': `Bearer ${authToken}`,
  454. 'Content-Type': 'application/json'
  455. }
  456. });
  457. const data = await response.json();
  458. currentUserRole = data.data.Role; // 存储当前用户的角色
  459. currentUserName = data.data.Username;
  460. // 使用获取到的角色,调用获取权限的接口
  461. await fetchPermissionsByRole(currentUserRole);
  462. console.log('当前用户角色:', data);
  463. currentUserInfo = data.data;
  464. // 调用该函数,初始化模态框
  465. setupUserInfoModal(currentUserInfo);
  466. updateMenuVisibility();
  467. return data.data; // 返回获取到的用户信息数据
  468. } catch (error) {
  469. console.error('Error fetching user info or permissions:', error);
  470. }
  471. }
  472. fetchUsername();
  473. // 调用函数更新菜单可见性
  474. // 将 fetchPermissionsByRole 转换为异步函数
  475. async function fetchPermissionsByRole(role) {
  476. try {
  477. const response = await fetch(`http://${serverIP}:${serverPort}/api/admin/GetSelfRoles`, {
  478. method: 'POST',
  479. headers: {
  480. 'Authorization': `Bearer ${authToken}`,
  481. 'Content-Type': 'application/json'
  482. },
  483. body: JSON.stringify({ name: role })
  484. });
  485. const data = await response.json();
  486. currentUserPermissions = data.data.Permissions; // 获取用户的权限数组
  487. console.log('currentUserPermissions:', currentUserPermissions);
  488. // 检查用户是否有读取许可证的权限
  489. const hasReadLicensePermission = currentUserPermissions.includes("read_license");
  490. const hasReadLicenseAllPermission = currentUserPermissions.includes("read_all_license");
  491. const hasReadUserPermission = currentUserPermissions.includes("read_user");
  492. const hasReadRolePermission = currentUserPermissions.includes("get_role");
  493. if (!hasReadLicensePermission || !hasReadLicenseAllPermission) {
  494. if (hasReadRolePermission) {
  495. document.getElementById('role-management-link').click();
  496. }else if (hasReadUserPermission){
  497. document.getElementById('user-management-link').click();
  498. }
  499. }
  500. // if (hasReadLicensePermission || hasReadLicenseAllPermission) {
  501. // document.getElementById('license-info-link').click();
  502. // }else if (hasReadRolePermission) {// 根据权限触发相应的按钮点击事件
  503. // // 如果没有读取许可证的权限,则触发角色管理按钮的点击事件
  504. // document.getElementById('role-management-link').click();
  505. // } else if ( hasReadUserPermission) {
  506. // // 如果有读取许可证的权限,则触发用户管理按钮的点击事件
  507. // document.getElementById('user-management-link').click();
  508. // }
  509. // if (hasReadRolePermission) {// 根据权限触发相应的按钮点击事件
  510. // // 如果没有读取许可证的权限,则触发角色管理按钮的点击事件
  511. // document.getElementById('role-management-link').click();
  512. // } else if ( hasReadUserPermission) {
  513. // // 如果有读取许可证的权限,则触发用户管理按钮的点击事件
  514. // document.getElementById('user-management-link').click();
  515. // }
  516. // 定义权限类别
  517. // const licensePermissions = ['upload_license', 'read_license'];
  518. // const userPermissionsCheck = ['create_user', 'read_user', 'update_user', 'delete_user'];
  519. // const rolePermissions = ['create_role', 'delete_role', 'update_role', 'get_role'];
  520. // const hasLicenseAccess = licensePermissions.some(permission => userPermissions.includes(permission));
  521. // const hasUserManagementAccess = userPermissionsCheck.some(permission => userPermissions.includes(permission));
  522. // const hasRoleManagementAccess = rolePermissions.some(permission => userPermissions.includes(permission));
  523. // 根据权限渲染菜单并显示初始页面
  524. //renderMenuAndInitialPage(hasLicenseAccess, hasUserManagementAccess, hasRoleManagementAccess);
  525. } catch (error) {
  526. console.error('Error fetching permissions:', error);
  527. }
  528. }
  529. //--------------进度条-------------------------------------------------
  530. // 创建模态框的 DOM 元素并插入到 body 中
  531. function createLoadingModal() {
  532. const modalHTML = `
  533. <div id="loadingModal" class="loading-modal" style="display: none;">
  534. <div class="loading-modal-content">
  535. <div class="spinner"></div>
  536. <p id="loadingMessage">加载中...</p>
  537. </div>
  538. </div>
  539. `;
  540. document.body.insertAdjacentHTML('beforeend', modalHTML);
  541. }
  542. // 显示加载模态框
  543. function showLoadingModal(message = "加载中...") {
  544. const loadingModal = document.getElementById('loadingModal');
  545. const loadingMessage = document.getElementById('loadingMessage');
  546. if (loadingModal && loadingMessage) {
  547. loadingMessage.textContent = message; // 设置显示的消息
  548. loadingModal.style.display = 'flex'; // 显示模态框
  549. }
  550. }
  551. // 隐藏加载模态框
  552. function hideLoadingModal() {
  553. const loadingModal = document.getElementById('loadingModal');
  554. if (loadingModal) {
  555. loadingModal.style.display = 'none'; // 隐藏模态框
  556. }
  557. }
  558. // 页面加载时创建模态框
  559. document.addEventListener('DOMContentLoaded', createLoadingModal);
  560. //-----------搜索栏----------------------------
  561. // 获取搜索框元素
  562. // 获取搜索框、状态下拉框、时间选择框和按钮元素
  563. // 获取搜索框、状态下拉框、时间选择框和按钮元素
  564. let searchBar = document.getElementById('search-bar');
  565. let statusFilter = document.getElementById('license-status-filter');
  566. let startDate = document.getElementById('start-date');
  567. let endDate = document.getElementById('end-date');
  568. let submitButton = document.getElementById('submit-button');
  569. let licenseInfoContainer = document.getElementById('license-info-restaurant-list');
  570. // 过滤功能实现
  571. function filterContent() {
  572. console.log('过滤功能触发');
  573. // 移除滚动监听器,停止滚动加载
  574. // removeScrollListeners();
  575. console.log('statusFilter.valuesdaad撒大啊', statusFilter.value,startDate.value ,endDate.value,searchBar.value );
  576. // 检查如果所有输入框都是默认值,则直接返回,不发送数据
  577. if (!statusFilter.value && !startDate.value && !endDate.value && !searchBar.value) {
  578. console.log("所有过滤条件为空,不发送请求");
  579. return; // 不发送请求
  580. }
  581. // 构建请求体参数
  582. const requestData = {
  583. license_flag: statusFilter.value || undefined,
  584. starting_date: startDate.value || undefined,
  585. end_date: endDate.value || undefined,
  586. any_search: searchBar.value || undefined,
  587. };
  588. console.log("requestData",requestData);
  589. // 发送 POST 请求到接口
  590. fetch(`http://${serverIP}:${serverPort}/api/admin/GetConditionalSearch`, {
  591. method: 'POST',
  592. headers: {
  593. 'Authorization': `Bearer ${authToken}`,
  594. 'Content-Type': 'application/json',
  595. },
  596. body: JSON.stringify(requestData),
  597. })
  598. .then(response => response.json())
  599. .then(data => {
  600. console.log('成功获取数据:', data);
  601. // 处理返回的数据并更新界面
  602. displayLicenseInfo(data.data);
  603. })
  604. .catch(error => {
  605. console.error('获取数据时发生错误:', error);
  606. });
  607. }
  608. // 处理并显示返回的 License 信息
  609. function displayLicenseInfo(data) {
  610. // 清空之前的结果
  611. licenseInfoContainer.innerHTML = '';
  612. LicApplicationData =[];
  613. // 遍历返回的数据,生成并插入卡片
  614. // 处理返回的数据并更新界面,使用 renderLicenseCards 方法进行渲染
  615. renderLicenseCards(data, true);
  616. }
  617. // 确定按钮点击事件的修改(停止滚动监听)
  618. submitButton.addEventListener('click', function() {
  619. console.log('确定按钮点击事件触发');
  620. // 检查下拉框和时间选择框是否为默认值
  621. if (!statusFilter.value && !startDate.value && !endDate.value) {
  622. // 如果都是默认值,则刷新页面
  623. location.reload();
  624. } else {
  625. // 否则,执行过滤内容功能
  626. filterContent();
  627. // 隐藏滚动加载,防止继续加载更多内容
  628. removeScrollListeners();
  629. }
  630. });
  631. // 监听返回按钮的点击事件,刷新页面并恢复滚动监听
  632. const resetButton = document.getElementById('reset-button');
  633. resetButton.addEventListener('click', function() {
  634. // 刷新页面
  635. location.reload();
  636. });
  637. // 修改当搜索栏没有输入时,重新启动滚动监听
  638. searchBar.addEventListener('input', function() {
  639. if (!searchBar.value && !statusFilter.value && !startDate.value && !endDate.value) {
  640. console.log("搜索条件清空,重新启动滚动监听");
  641. restartScrollListeners();
  642. }
  643. });