statistics.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>统计报表 - 后台管理系统</title>
  7. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
  8. <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css" rel="stylesheet">
  9. <style>
  10. .sidebar {
  11. position: fixed;
  12. top: 60px;
  13. bottom: 0;
  14. left: 0;
  15. z-index: 100;
  16. padding: 0;
  17. box-shadow: inset -1px 0 0 rgba(0, 0, 0, .1);
  18. background-color: #f8f9fa;
  19. }
  20. .sidebar-sticky {
  21. position: relative;
  22. top: 0;
  23. height: calc(100vh - 60px);
  24. padding-top: .5rem;
  25. overflow-x: hidden;
  26. overflow-y: auto;
  27. }
  28. .navbar {
  29. box-shadow: 0 2px 4px rgba(0,0,0,.1);
  30. height: 60px;
  31. }
  32. .main-content {
  33. margin-left: 240px;
  34. padding: 20px;
  35. margin-top: 60px;
  36. }
  37. .nav-link {
  38. color: #333;
  39. padding: 10px 20px;
  40. }
  41. .nav-link:hover {
  42. background-color: #e9ecef;
  43. }
  44. .nav-link.active {
  45. color: #0d6efd;
  46. background-color: #e9ecef;
  47. }
  48. .card {
  49. margin-bottom: 20px;
  50. box-shadow: 0 0 10px rgba(0,0,0,0.1);
  51. }
  52. </style>
  53. </head>
  54. <body>
  55. <nav class="navbar navbar-expand-lg navbar-light bg-white fixed-top">
  56. <div class="container-fluid">
  57. <a class="navbar-brand" href="#">后台管理系统</a>
  58. <div class="d-flex">
  59. <button class="btn btn-outline-danger" id="logoutBtn">退出登录</button>
  60. </div>
  61. </div>
  62. </nav>
  63. <div class="container-fluid">
  64. <div class="row">
  65. <nav class="col-md-3 col-lg-2 d-md-block sidebar">
  66. <div class="sidebar-sticky">
  67. <ul class="nav flex-column">
  68. <li class="nav-item">
  69. <a class="nav-link" href="/admin/views/dashboard.html" data-page="dashboard">
  70. <i class="bi bi-speedometer2"></i> 仪表板
  71. </a>
  72. </li>
  73. <li class="nav-item">
  74. <a class="nav-link" href="/admin/views/groups.html" data-page="groups">
  75. <i class="bi bi-people"></i> 群组管理
  76. </a>
  77. </li>
  78. <li class="nav-item">
  79. <a class="nav-link" href="/admin/views/transactions.html" data-page="transactions">
  80. <i class="bi bi-cash-stack"></i> 交易记录
  81. </a>
  82. </li>
  83. <li class="nav-item">
  84. <a class="nav-link active" href="/admin/views/statistics.html" data-page="statistics">
  85. <i class="bi bi-graph-up"></i> 统计报表
  86. </a>
  87. </li>
  88. <li class="nav-item">
  89. <a class="nav-link" href="/admin/views/settings.html" data-page="settings">
  90. <i class="bi bi-gear"></i> 系统设置
  91. </a>
  92. </li>
  93. </ul>
  94. </div>
  95. </nav>
  96. <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4 main-content">
  97. <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
  98. <h1 class="h2">统计报表</h1>
  99. </div>
  100. <div class="row">
  101. <div class="col-md-6">
  102. <div class="card">
  103. <div class="card-body">
  104. <h5 class="card-title">交易统计</h5>
  105. <canvas id="transactionChart"></canvas>
  106. </div>
  107. </div>
  108. </div>
  109. <div class="col-md-6">
  110. <div class="card">
  111. <div class="card-body">
  112. <h5 class="card-title">群组统计</h5>
  113. <canvas id="groupChart"></canvas>
  114. </div>
  115. </div>
  116. </div>
  117. </div>
  118. </main>
  119. </div>
  120. </div>
  121. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
  122. <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
  123. <script>
  124. // 检查登录状态
  125. function checkAuth() {
  126. const token = localStorage.getItem('token');
  127. console.log('检查登录状态,token:', token ? '存在' : '不存在');
  128. if (!token) {
  129. console.log('未找到token,跳转到登录页');
  130. window.location.href = '/admin/views/login.html';
  131. return;
  132. }
  133. // 验证token是否有效
  134. fetch('/api/users/profile', {
  135. headers: {
  136. 'Authorization': `Bearer ${token}`
  137. }
  138. })
  139. .then(response => {
  140. if (!response.ok) {
  141. throw new Error('Token无效');
  142. }
  143. return response.json();
  144. })
  145. .then(data => {
  146. console.log('用户信息:', data);
  147. // 可以在这里保存用户信息到全局变量
  148. window.currentUser = data;
  149. })
  150. .catch(error => {
  151. console.error('验证token时出错:', error);
  152. localStorage.removeItem('token');
  153. window.location.href = '/admin/views/login.html';
  154. });
  155. }
  156. // 加载统计数据
  157. async function loadStatistics() {
  158. try {
  159. const token = localStorage.getItem('token');
  160. const response = await fetch('/api/statistics', {
  161. headers: {
  162. 'Authorization': `Bearer ${token}`
  163. }
  164. });
  165. if (response.ok) {
  166. const data = await response.json();
  167. updateCharts(data);
  168. } else if (response.status === 401) {
  169. window.location.href = '/';
  170. }
  171. } catch (error) {
  172. console.error('加载统计数据失败:', error);
  173. }
  174. }
  175. // 更新图表
  176. function updateCharts(data) {
  177. // 交易统计图表
  178. const transactionCtx = document.getElementById('transactionChart').getContext('2d');
  179. new Chart(transactionCtx, {
  180. type: 'line',
  181. data: {
  182. labels: data.transactions.labels,
  183. datasets: [{
  184. label: '交易金额',
  185. data: data.transactions.data,
  186. borderColor: 'rgb(75, 192, 192)',
  187. tension: 0.1
  188. }]
  189. }
  190. });
  191. // 群组统计图表
  192. const groupCtx = document.getElementById('groupChart').getContext('2d');
  193. new Chart(groupCtx, {
  194. type: 'pie',
  195. data: {
  196. labels: data.groups.labels,
  197. datasets: [{
  198. data: data.groups.data,
  199. backgroundColor: [
  200. 'rgb(255, 99, 132)',
  201. 'rgb(54, 162, 235)',
  202. 'rgb(255, 205, 86)'
  203. ]
  204. }]
  205. }
  206. });
  207. }
  208. // 退出登录
  209. document.getElementById('logoutBtn').addEventListener('click', () => {
  210. localStorage.removeItem('token');
  211. window.location.href = '/';
  212. });
  213. // 页面加载时检查登录状态并加载数据
  214. checkAuth();
  215. loadStatistics();
  216. </script>
  217. </body>
  218. </html>