settings.html 12 KB

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