admin.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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" />
  6. <title>VoiceFlow AI Reader 【小满TTS英文听书】 - 管理员面板</title>
  7. <style>
  8. * { box-sizing: border-box; }
  9. body {
  10. margin: 0;
  11. min-height: 100vh;
  12. background: linear-gradient(145deg, #f2f6fb, #e7eef8);
  13. font-family: Arial, sans-serif;
  14. color: #1f2937;
  15. padding: 20px;
  16. }
  17. .container { max-width: 1100px; margin: 0 auto; }
  18. .topbar, .panel {
  19. background: #fff;
  20. border-radius: 14px;
  21. box-shadow: 0 10px 35px rgba(17, 24, 39, .10);
  22. padding: 18px;
  23. margin-bottom: 14px;
  24. }
  25. .title { margin: 0; font-size: 22px; color: #0f172a; }
  26. .sub { margin: 4px 0 0; font-size: 13px; color: #64748b; }
  27. .row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
  28. input, button, select {
  29. border-radius: 10px;
  30. padding: 10px 12px;
  31. font-size: 14px;
  32. border: 1px solid #d7e0ec;
  33. }
  34. input:focus, select:focus { outline: none; border-color: #3b82f6; box-shadow: 0 0 0 3px rgba(59,130,246,.12); }
  35. button { border: none; color: #fff; cursor: pointer; }
  36. .btn-primary { background: #2563eb; }
  37. .btn-primary:hover { background: #1d4ed8; }
  38. .btn-danger { background: #dc2626; }
  39. .btn-danger:hover { background: #b91c1c; }
  40. .btn-warn { background: #ea580c; }
  41. .btn-warn:hover { background: #c2410c; }
  42. .btn-green { background: #16a34a; }
  43. .btn-green:hover { background: #15803d; }
  44. table { width: 100%; border-collapse: collapse; margin-top: 10px; }
  45. th, td { border-bottom: 1px solid #e5e7eb; padding: 10px; text-align: left; font-size: 13px; vertical-align: middle; }
  46. th { color: #334155; background: #f8fafc; }
  47. .muted { color: #64748b; font-size: 12px; }
  48. .tag { display: inline-block; border-radius: 8px; padding: 2px 8px; font-size: 12px; }
  49. .tag-ok { background: #dcfce7; color: #166534; }
  50. .tag-off { background: #fee2e2; color: #991b1b; }
  51. .toolbar { display: flex; justify-content: space-between; align-items: center; gap: 10px; flex-wrap: wrap; }
  52. .msg { min-height: 18px; margin-top: 6px; color: #b00020; font-size: 13px; }
  53. @media (max-width: 768px) {
  54. th:nth-child(1), td:nth-child(1), th:nth-child(4), td:nth-child(4) { display: none; }
  55. }
  56. </style>
  57. </head>
  58. <body>
  59. <div class="container">
  60. <div class="topbar">
  61. <div class="toolbar">
  62. <div>
  63. <h1 class="title">VoiceFlow AI Reader</h1>
  64. <p class="sub">【小满TTS英文听书】 管理员面板</p>
  65. </div>
  66. <div class="row">
  67. <button class="btn-primary" onclick="goReader()">进入阅读器</button>
  68. <button class="btn-warn" onclick="logout()">退出登录</button>
  69. </div>
  70. </div>
  71. </div>
  72. <div class="panel">
  73. <h3>用户管理</h3>
  74. <div class="row">
  75. <input id="newUser" placeholder="用户名(至少3位)" />
  76. <input id="newPass" placeholder="密码(至少4位)" type="password" />
  77. <label><input id="newAdmin" type="checkbox" /> 管理员</label>
  78. <button class="btn-green" onclick="createOrUpdateUser()">新增/重置</button>
  79. </div>
  80. <div id="userMsg" class="msg"></div>
  81. <table>
  82. <thead>
  83. <tr>
  84. <th>ID</th>
  85. <th>用户名</th>
  86. <th>状态</th>
  87. <th>管理员</th>
  88. <th>创建时间</th>
  89. <th>操作</th>
  90. </tr>
  91. </thead>
  92. <tbody id="userTable"></tbody>
  93. </table>
  94. </div>
  95. <div class="panel">
  96. <h3>配置管理</h3>
  97. <div class="row">
  98. <input id="cfgKey" placeholder="配置键" />
  99. <input id="cfgVal" placeholder="配置值" />
  100. <button class="btn-primary" onclick="setConfig()">保存配置</button>
  101. </div>
  102. <div id="cfgMsg" class="msg"></div>
  103. <table>
  104. <thead><tr><th>键</th><th>值</th><th>更新时间</th></tr></thead>
  105. <tbody id="cfgTable"></tbody>
  106. </table>
  107. </div>
  108. </div>
  109. <script>
  110. async function ensureAdmin() {
  111. const r = await fetch('/auth/me');
  112. if (!r.ok) { location.href = '/login'; return false; }
  113. const d = await r.json();
  114. if (!d.success || !d.is_admin) { location.href = '/'; return false; }
  115. return true;
  116. }
  117. function showMsg(id, text, ok = false) {
  118. const el = document.getElementById(id);
  119. el.textContent = text || '';
  120. el.style.color = ok ? '#0f7b0f' : '#b00020';
  121. }
  122. async function apiJson(url, options = {}) {
  123. const r = await fetch(url, options);
  124. const d = await r.json().catch(() => ({}));
  125. if (!r.ok || d.success === false) throw new Error(d.error || `请求失败(${r.status})`);
  126. return d;
  127. }
  128. async function loadUsers() {
  129. const d = await apiJson('/admin/users');
  130. const tb = document.getElementById('userTable');
  131. tb.innerHTML = '';
  132. (d.users || []).forEach(u => {
  133. const tr = document.createElement('tr');
  134. const activeTag = u.is_active ? '<span class="tag tag-ok">启用</span>' : '<span class="tag tag-off">禁用</span>';
  135. const adminTag = u.is_admin ? '<span class="tag tag-ok">是</span>' : '<span class="muted">否</span>';
  136. tr.innerHTML = `
  137. <td>${u.id}</td>
  138. <td>${u.username}</td>
  139. <td>${activeTag}</td>
  140. <td>${adminTag}</td>
  141. <td>${u.created_at || ''}</td>
  142. <td>
  143. <button class="btn-primary" onclick="resetPwd('${u.username}')">重置密码</button>
  144. <button class="${u.is_active ? 'btn-warn' : 'btn-green'}" onclick="toggleUser('${u.username}', ${u.is_active ? 'false' : 'true'})">${u.is_active ? '禁用' : '启用'}</button>
  145. ${u.username === 'admin' ? '' : `<button class="btn-danger" onclick="deleteUser('${u.username}')">删除</button>`}
  146. </td>
  147. `;
  148. tb.appendChild(tr);
  149. });
  150. }
  151. async function createOrUpdateUser() {
  152. const username = document.getElementById('newUser').value.trim();
  153. const password = document.getElementById('newPass').value;
  154. const is_admin = document.getElementById('newAdmin').checked;
  155. try {
  156. await apiJson('/admin/users', {
  157. method: 'POST',
  158. headers: { 'Content-Type': 'application/json' },
  159. body: JSON.stringify({ username, password, is_admin })
  160. });
  161. showMsg('userMsg', '用户已保存', true);
  162. document.getElementById('newPass').value = '';
  163. await loadUsers();
  164. } catch (e) {
  165. showMsg('userMsg', e.message);
  166. }
  167. }
  168. async function resetPwd(username) {
  169. const p = prompt(`请输入用户 ${username} 的新密码:`);
  170. if (!p) return;
  171. try {
  172. await apiJson(`/admin/users/${encodeURIComponent(username)}/reset-password`, {
  173. method: 'POST',
  174. headers: { 'Content-Type': 'application/json' },
  175. body: JSON.stringify({ password: p })
  176. });
  177. showMsg('userMsg', `已重置 ${username} 密码`, true);
  178. } catch (e) {
  179. showMsg('userMsg', e.message);
  180. }
  181. }
  182. async function toggleUser(username, is_active) {
  183. if (!confirm(`确认${is_active ? '启用' : '禁用'}用户 ${username} ?`)) return;
  184. try {
  185. await apiJson(`/admin/users/${encodeURIComponent(username)}/status`, {
  186. method: 'POST',
  187. headers: { 'Content-Type': 'application/json' },
  188. body: JSON.stringify({ is_active })
  189. });
  190. showMsg('userMsg', `用户 ${username} 状态已更新`, true);
  191. await loadUsers();
  192. } catch (e) {
  193. showMsg('userMsg', e.message);
  194. }
  195. }
  196. async function deleteUser(username) {
  197. const delete_files = confirm(`删除用户 ${username} 时,是否连同该用户上传文件一起删除?\n点击“确定”=删除文件,点击“取消”=仅删用户。`);
  198. if (!confirm(`确认删除用户 ${username} ?`)) return;
  199. try {
  200. await apiJson(`/admin/users/${encodeURIComponent(username)}?delete_files=${delete_files ? 'true' : 'false'}`, {
  201. method: 'DELETE'
  202. });
  203. showMsg('userMsg', `用户 ${username} 已删除`, true);
  204. await loadUsers();
  205. } catch (e) {
  206. showMsg('userMsg', e.message);
  207. }
  208. }
  209. async function loadConfig() {
  210. const d = await apiJson('/admin/config');
  211. const tb = document.getElementById('cfgTable');
  212. tb.innerHTML = '';
  213. (d.configs || []).forEach(c => {
  214. const tr = document.createElement('tr');
  215. tr.innerHTML = `<td>${c.config_key}</td><td>${c.config_value || ''}</td><td>${c.updated_at || ''}</td>`;
  216. tb.appendChild(tr);
  217. });
  218. }
  219. async function setConfig() {
  220. const config_key = document.getElementById('cfgKey').value.trim();
  221. const config_value = document.getElementById('cfgVal').value;
  222. try {
  223. await apiJson('/admin/config', {
  224. method: 'POST',
  225. headers: { 'Content-Type': 'application/json' },
  226. body: JSON.stringify({ config_key, config_value })
  227. });
  228. showMsg('cfgMsg', '配置已保存', true);
  229. await loadConfig();
  230. } catch (e) {
  231. showMsg('cfgMsg', e.message);
  232. }
  233. }
  234. async function logout() {
  235. await fetch('/auth/logout', { method: 'POST' });
  236. location.href = '/login';
  237. }
  238. function goReader() {
  239. location.href = '/';
  240. }
  241. (async () => {
  242. const ok = await ensureAdmin();
  243. if (!ok) return;
  244. try {
  245. await loadUsers();
  246. await loadConfig();
  247. } catch (e) {
  248. showMsg('userMsg', e.message);
  249. }
  250. })();
  251. </script>
  252. </body>
  253. </html>