settings.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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" 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 active" 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. <form id="basicSettingsForm">
  106. <div class="mb-3">
  107. <label for="siteName" class="form-label">站点名称</label>
  108. <input type="text" class="form-control" id="siteName">
  109. </div>
  110. <div class="mb-3">
  111. <label for="adminEmail" class="form-label">管理员邮箱</label>
  112. <input type="email" class="form-control" id="adminEmail">
  113. </div>
  114. <button type="submit" class="btn btn-primary">保存设置</button>
  115. </form>
  116. </div>
  117. </div>
  118. </div>
  119. <div class="col-md-6">
  120. <div class="card">
  121. <div class="card-body">
  122. <h5 class="card-title">安全设置</h5>
  123. <form id="securitySettingsForm">
  124. <div class="mb-3">
  125. <label for="currentPassword" class="form-label">当前密码</label>
  126. <input type="password" class="form-control" id="currentPassword">
  127. </div>
  128. <div class="mb-3">
  129. <label for="newPassword" class="form-label">新密码</label>
  130. <input type="password" class="form-control" id="newPassword">
  131. </div>
  132. <div class="mb-3">
  133. <label for="confirmPassword" class="form-label">确认新密码</label>
  134. <input type="password" class="form-control" id="confirmPassword">
  135. </div>
  136. <button type="submit" class="btn btn-primary">修改密码</button>
  137. </form>
  138. </div>
  139. </div>
  140. </div>
  141. </div>
  142. </main>
  143. </div>
  144. </div>
  145. <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
  146. <script>
  147. // 检查登录状态
  148. function checkAuth() {
  149. const token = localStorage.getItem('token');
  150. console.log('检查登录状态,token:', token ? '存在' : '不存在');
  151. if (!token) {
  152. console.log('未找到token,跳转到登录页');
  153. window.location.href = '/admin/views/login.html';
  154. return;
  155. }
  156. // 验证token是否有效
  157. fetch('/api/users/profile', {
  158. headers: {
  159. 'Authorization': `Bearer ${token}`
  160. }
  161. })
  162. .then(response => {
  163. if (!response.ok) {
  164. throw new Error('Token无效');
  165. }
  166. return response.json();
  167. })
  168. .then(data => {
  169. console.log('用户信息:', data);
  170. // 可以在这里保存用户信息到全局变量
  171. window.currentUser = data;
  172. })
  173. .catch(error => {
  174. console.error('验证token时出错:', error);
  175. localStorage.removeItem('token');
  176. window.location.href = '/admin/views/login.html';
  177. });
  178. }
  179. // 加载设置
  180. async function loadSettings() {
  181. try {
  182. const token = localStorage.getItem('token');
  183. const response = await fetch('/api/settings', {
  184. headers: {
  185. 'Authorization': `Bearer ${token}`
  186. }
  187. });
  188. if (response.ok) {
  189. const settings = await response.json();
  190. document.getElementById('siteName').value = settings.siteName;
  191. document.getElementById('adminEmail').value = settings.adminEmail;
  192. } else if (response.status === 401) {
  193. window.location.href = '/';
  194. }
  195. } catch (error) {
  196. console.error('加载设置失败:', error);
  197. }
  198. }
  199. // 保存基本设置
  200. document.getElementById('basicSettingsForm').addEventListener('submit', async (e) => {
  201. e.preventDefault();
  202. try {
  203. const token = localStorage.getItem('token');
  204. const response = await fetch('/api/settings', {
  205. method: 'PUT',
  206. headers: {
  207. 'Authorization': `Bearer ${token}`,
  208. 'Content-Type': 'application/json'
  209. },
  210. body: JSON.stringify({
  211. siteName: document.getElementById('siteName').value,
  212. adminEmail: document.getElementById('adminEmail').value
  213. })
  214. });
  215. if (response.ok) {
  216. alert('设置已保存');
  217. } else {
  218. const data = await response.json();
  219. alert(data.message || '保存设置失败');
  220. }
  221. } catch (error) {
  222. console.error('保存设置失败:', error);
  223. alert('保存设置失败,请稍后重试');
  224. }
  225. });
  226. // 修改密码
  227. document.getElementById('securitySettingsForm').addEventListener('submit', async (e) => {
  228. e.preventDefault();
  229. const newPassword = document.getElementById('newPassword').value;
  230. const confirmPassword = document.getElementById('confirmPassword').value;
  231. if (newPassword !== confirmPassword) {
  232. alert('两次输入的密码不一致');
  233. return;
  234. }
  235. try {
  236. const token = localStorage.getItem('token');
  237. const response = await fetch('/api/settings/password', {
  238. method: 'PUT',
  239. headers: {
  240. 'Authorization': `Bearer ${token}`,
  241. 'Content-Type': 'application/json'
  242. },
  243. body: JSON.stringify({
  244. currentPassword: document.getElementById('currentPassword').value,
  245. newPassword: newPassword
  246. })
  247. });
  248. if (response.ok) {
  249. alert('密码修改成功');
  250. document.getElementById('securitySettingsForm').reset();
  251. } else {
  252. const data = await response.json();
  253. alert(data.message || '修改密码失败');
  254. }
  255. } catch (error) {
  256. console.error('修改密码失败:', error);
  257. alert('修改密码失败,请稍后重试');
  258. }
  259. });
  260. // 退出登录
  261. document.getElementById('logoutBtn').addEventListener('click', () => {
  262. localStorage.removeItem('token');
  263. window.location.href = '/';
  264. });
  265. // 页面加载时检查登录状态并加载数据
  266. checkAuth();
  267. loadSettings();
  268. </script>
  269. </body>
  270. </html>