index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. var accessToken = localStorage.getItem("access_token");
  2. console.log(accessToken);
  3. if (accessToken === null) {
  4. alert('請先登入')
  5. window.location.href = "./login.html";
  6. }
  7. var usernameEmail = localStorage.getItem("username");
  8. console.log(usernameEmail)
  9. var userNameSpan = document.getElementById("userName");
  10. var table_body = document.getElementById("table_body");
  11. userNameSpan.textContent = usernameEmail;
  12. function logout() {
  13. localStorage.removeItem("access_token");
  14. }
  15. $(document).on("click", "#logout", function (event) {
  16. alert("登出成功");
  17. logout();
  18. window.location.href = "./login.html";
  19. });
  20. let currentPage = 1;
  21. var pageSize = 10;
  22. function formatDate(dateString) {
  23. const date = new Date(dateString);
  24. const year = date.getFullYear();
  25. const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从 0 开始,所以需要加 1
  26. const day = String(date.getDate()).padStart(2, '0');
  27. return `${year}-${month}-${day}`;
  28. }
  29. function fetchData(page) {
  30. // 定义请求头
  31. const headers = {
  32. 'Authorization': 'Bearer ' + accessToken, // 例如携带 JWT 令牌
  33. };
  34. // 发送 GET 请求
  35. axios.get(`http://cmm.ai:9000/auth/users/log?page_num=${page}&page_size=${pageSize}`, {
  36. headers: headers
  37. })
  38. .then(response => {
  39. console.log(response.data); // 成功处理返回的数据
  40. var log_data = response.data.data
  41. var table = ``
  42. var page_num = response.data.total;
  43. var page_size = 20;
  44. var page_amount = Math.ceil(page_num / page_size);
  45. console.log(page_amount)
  46. // for (var i = 0; i < log_data.length; i++) {
  47. // table += `
  48. // <tr>
  49. // <td>${log_data[i].created_at}</td>
  50. // <td>${log_data[i].question}</td>
  51. // <td>${log_data[i].answer}</td>
  52. // <tr>
  53. // `
  54. // }
  55. table_body.innerHTML = ''
  56. // 填充表格数据
  57. log_data.forEach(item => {
  58. const formattedDate = formatDate(item.created_at);
  59. const row = `<tr>
  60. <td>${formattedDate}</td>
  61. <td>${item.question}</td>
  62. <td>${item.answer}</td>
  63. </tr>`;
  64. table_body.innerHTML += row;
  65. });
  66. // table_body.innerHTML = table;
  67. updatePagination(page_amount, page);
  68. })
  69. .catch(error => {
  70. console.error('Error fetching data:', error); // 处理错误
  71. });
  72. }
  73. fetchData(currentPage);
  74. // function updatePagination(totalPages, currentPage) {
  75. // const pagination = document.getElementById('pagination');
  76. // pagination.innerHTML = ''; // 清空分页
  77. // // 生成分页按钮
  78. // for (let i = 1; i <= totalPages; i++) {
  79. // pagination.innerHTML += `
  80. // <li class="page-item ${i === currentPage ? 'active' : ''}">
  81. // <a class="page-link" href="#" onclick="fetchData(${i})">${i}</a>
  82. // </li>`;
  83. // }
  84. // }
  85. function updatePagination(totalPages, currentPage) {
  86. const pagination = document.getElementById('pagination');
  87. pagination.innerHTML = ''; // 清空分页
  88. // 设置你希望在分页栏中显示的页码数量
  89. const maxVisiblePages = 10;
  90. const halfVisiblePages = Math.floor(maxVisiblePages / 2);
  91. let startPage = Math.max(1, currentPage - halfVisiblePages);
  92. let endPage = Math.min(totalPages, currentPage + halfVisiblePages);
  93. // 调整 startPage 和 endPage,确保显示的页码数量是合理的
  94. if (endPage - startPage < maxVisiblePages - 1) {
  95. if (currentPage <= halfVisiblePages) {
  96. endPage = Math.min(totalPages, startPage + maxVisiblePages - 1);
  97. } else if (currentPage + halfVisiblePages >= totalPages) {
  98. startPage = Math.max(1, endPage - maxVisiblePages + 1);
  99. }
  100. }
  101. // 添加第一页按钮
  102. if (startPage > 1) {
  103. pagination.innerHTML += `
  104. <li class="page-item">
  105. <a class="page-link" href="#" onclick="fetchData(1)">1</a>
  106. </li>`;
  107. pagination.innerHTML += `
  108. <li class="page-item disabled">
  109. <a class="page-link" href="#">...</a>
  110. </li>`;
  111. }
  112. // 添加中间的页码按钮
  113. for (let i = startPage; i <= endPage; i++) {
  114. pagination.innerHTML += `
  115. <li class="page-item ${i === currentPage ? 'active' : ''}">
  116. <a class="page-link" href="#" onclick="fetchData(${i})">${i}</a>
  117. </li>`;
  118. }
  119. // 添加最后一页按钮
  120. if (endPage < totalPages) {
  121. pagination.innerHTML += `
  122. <li class="page-item disabled">
  123. <a class="page-link" href="#">...</a>
  124. </li>`;
  125. pagination.innerHTML += `
  126. <li class="page-item">
  127. <a class="page-link" href="#" onclick="fetchData(${totalPages})">${totalPages}</a>
  128. </li>`;
  129. }
  130. // 添加 "上一页" 按钮
  131. if (currentPage > 1) {
  132. pagination.innerHTML = `
  133. <li class="page-item">
  134. <a class="page-link" href="#" onclick="fetchData(${currentPage - 1})">&laquo;</a>
  135. </li>` + pagination.innerHTML;
  136. }
  137. // 添加 "下一页" 按钮
  138. if (currentPage < totalPages) {
  139. pagination.innerHTML += `
  140. <li class="page-item">
  141. <a class="page-link" href="#" onclick="fetchData(${currentPage + 1})">&raquo;</a>
  142. </li>`;
  143. }
  144. }