license_info.js 39 KB

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