admin.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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>Docker 镜像代理加速 - 管理面板</title>
  7. <link rel="icon" href="https://cdn.jsdelivr.net/gh/dqzboy/Blog-Image/BlogCourse/docker-proxy.png" type="image/png">
  8. <style>
  9. body {
  10. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Lato', 'Helvetica', 'Arial', sans-serif;
  11. line-height: 1.6;
  12. color: #24292e;
  13. margin: 0;
  14. padding: 20px;
  15. background-color: #f6f8fa;
  16. }
  17. .container {
  18. max-width: 1000px;
  19. margin: 0 auto;
  20. background: white;
  21. padding: 20px;
  22. border-radius: 8px;
  23. box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  24. }
  25. h1, h2 {
  26. border-bottom: 2px solid #eaecef;
  27. padding-bottom: 0.5em;
  28. margin-bottom: 1em;
  29. color: #0366d6;
  30. }
  31. label {
  32. display: block;
  33. margin-top: 20px;
  34. border-bottom: none;
  35. padding-bottom: 0;
  36. color: #24292e;
  37. font-size: 20px;
  38. }
  39. input[type="text"], input[type="url"], input[type="password"], select {
  40. width: 100%;
  41. padding: 8px;
  42. margin-top: 5px;
  43. border: 1px solid #d1d5da;
  44. border-radius: 4px;
  45. box-sizing: border-box;
  46. font-family: inherit;
  47. font-size: inherit;
  48. }
  49. button {
  50. background-color: #2ea44f;
  51. color: white;
  52. border: none;
  53. padding: 10px 20px;
  54. margin-top: 20px;
  55. border-radius: 4px;
  56. cursor: pointer;
  57. font-family: inherit;
  58. font-size: inherit;
  59. }
  60. button:hover {
  61. background-color: #2c974b;
  62. }
  63. table {
  64. width: 100%;
  65. border-collapse: collapse;
  66. margin-top: 20px;
  67. }
  68. th, td {
  69. border: 1px solid #d1d5da;
  70. padding: 8px;
  71. text-align: left;
  72. }
  73. th {
  74. background-color: #f6f8fa;
  75. font-weight: normal;
  76. }
  77. .action-btn {
  78. background-color: #0366d6;
  79. color: white;
  80. border: none;
  81. padding: 5px 10px;
  82. border-radius: 4px;
  83. cursor: pointer;
  84. margin-right: 5px;
  85. font-family: inherit;
  86. font-size: inherit;
  87. }
  88. .action-btn:hover {
  89. background-color: #0256b9;
  90. }
  91. .add-btn {
  92. background-color: #2ea44f;
  93. color: white;
  94. border: none;
  95. padding: 10px 20px;
  96. margin-top: 10px;
  97. border-radius: 4px;
  98. cursor: pointer;
  99. font-family: inherit;
  100. font-size: inherit;
  101. }
  102. .add-btn:hover {
  103. background-color: #2c974b;
  104. }
  105. #menuPreview {
  106. margin-top: 20px;
  107. padding: 10px;
  108. background-color: #f6f8fa;
  109. border: 1px solid #d1d5da;
  110. border-radius: 4px;
  111. }
  112. #menuPreview a {
  113. margin-right: 15px;
  114. color: #0366d6;
  115. text-decoration: none;
  116. }
  117. #menuPreview a:hover {
  118. text-decoration: underline;
  119. }
  120. .hidden {
  121. display: none;
  122. }
  123. .login-modal {
  124. display: none;
  125. position: fixed;
  126. z-index: 1;
  127. left: 0;
  128. top: 0;
  129. width: 100%;
  130. height: 100%;
  131. overflow: auto;
  132. background-color: rgba(0,0,0,0.4);
  133. }
  134. .login-content {
  135. background-color: #fefefe;
  136. margin: 15% auto;
  137. padding: 20px;
  138. border: 1px solid #888;
  139. width: 30%;
  140. border-radius: 8px;
  141. }
  142. .user-management {
  143. position: absolute;
  144. top: 20px;
  145. right: 20px;
  146. cursor: pointer;
  147. }
  148. .user-management i {
  149. font-size: 24px;
  150. color: #0366d6;
  151. }
  152. .menu-label {
  153. font-size: 20px;
  154. color: #24292e;
  155. }
  156. .admin-title {
  157. font-size: 24px;
  158. color: #0366d6;
  159. }
  160. </style>
  161. </head>
  162. <body>
  163. <div class="container hidden" id="adminContainer">
  164. <h1 class="admin-title">Docker 镜像代理加速 - 管理面板</h1>
  165. <form id="adminForm">
  166. <label for="logoUrl">Logo URL: (可选)</label>
  167. <input type="url" id="logoUrl" name="logoUrl">
  168. <label for="proxyDomain">Docker镜像代理地址: (必填)</label>
  169. <input type="text" id="proxyDomain" name="proxyDomain" required>
  170. <h2 class="menu-label">菜单项管理</h2>
  171. <table id="menuTable">
  172. <thead>
  173. <tr>
  174. <th>文本</th>
  175. <th>链接 (可选)</th>
  176. <th>新标签页打开</th>
  177. <th>操作</th>
  178. </tr>
  179. </thead>
  180. <tbody id="menuTableBody">
  181. <!-- 菜单项将在这里动态添加 -->
  182. </tbody>
  183. <tfoot>
  184. <tr id="newMenuItemRow" class="hidden">
  185. <td><input type="text" id="newMenuItemText" placeholder="菜单项文本"></td>
  186. <td><input type="url" id="newMenuItemLink" placeholder="菜单项链接 (可选)"></td>
  187. <td>
  188. <select id="newMenuItemNewTab">
  189. <option value="false">否</option>
  190. <option value="true">是</option>
  191. </select>
  192. </td>
  193. <td>
  194. <button type="button" class="action-btn" onclick="saveNewMenuItem()">保存</button>
  195. <button type="button" class="action-btn" onclick="cancelNewMenuItem()">取消</button>
  196. </td>
  197. </tr>
  198. </tfoot>
  199. </table>
  200. <button type="button" class="add-btn" onclick="showNewMenuItemRow()">添加菜单项</button>
  201. <h2 class="menu-label">菜单预览</h2>
  202. <div id="menuPreview">
  203. <!-- 菜单预览将在这里显示 -->
  204. </div>
  205. <label for="adImageUrl">广告图片 URL: (可选)</label>
  206. <input type="url" id="adImageUrl" name="adImageUrl">
  207. <!-- 添加修改密码的选项 -->
  208. <h2 class="menu-label">修改密码</h2>
  209. <label for="currentPassword">当前密码</label>
  210. <input type="password" id="currentPassword" name="currentPassword" required>
  211. <label for="newPassword">新密码</label>
  212. <input type="password" id="newPassword" name="newPassword" required>
  213. <button type="button" onclick="changePassword()">修改密码</button>
  214. <button type="submit">保存更改</button>
  215. </form>
  216. </div>
  217. <div class="login-modal" id="loginModal">
  218. <div class="login-content">
  219. <h2>登录</h2>
  220. <label for="username">用户名</label>
  221. <input type="text" id="username" name="username" required>
  222. <label for="password">密码</label>
  223. <input type="password" id="password" name="password" required>
  224. <button type="button" onclick="login()">登录</button>
  225. </div>
  226. </div>
  227. <script>
  228. let menuItems = [];
  229. let isLoggedIn = false;
  230. function getMenuItems() {
  231. return menuItems;
  232. }
  233. function setMenuItems(items) {
  234. menuItems = items;
  235. renderMenuItems();
  236. updateMenuPreview();
  237. }
  238. function renderMenuItems() {
  239. const tbody = document.getElementById('menuTableBody');
  240. tbody.innerHTML = '';
  241. menuItems.forEach((item, index) => {
  242. const row = `
  243. <tr>
  244. <td>${item.text}</td>
  245. <td>${item.link || ''}</td>
  246. <td>${item.newTab ? '是' : '否'}</td>
  247. <td>
  248. <button type="button" class="action-btn" onclick="editMenuItem(${index})">编辑</button>
  249. <button type="button" class="action-btn" onclick="deleteMenuItem(${index})">删除</button>
  250. </td>
  251. </tr>
  252. `;
  253. tbody.innerHTML += row;
  254. });
  255. }
  256. function updateMenuPreview() {
  257. const preview = document.getElementById('menuPreview');
  258. preview.innerHTML = menuItems.map(item =>
  259. `<a href="${item.link || '#'}" ${item.newTab ? 'target="_blank"' : ''}>${item.text}</a>`
  260. ).join(' ');
  261. }
  262. function showNewMenuItemRow() {
  263. document.getElementById('newMenuItemRow').classList.remove('hidden');
  264. }
  265. function saveNewMenuItem() {
  266. const text = document.getElementById('newMenuItemText').value;
  267. const link = document.getElementById('newMenuItemLink').value;
  268. const newTab = document.getElementById('newMenuItemNewTab').value === 'true';
  269. if (text) {
  270. menuItems.push({ text, link, newTab });
  271. renderMenuItems();
  272. updateMenuPreview();
  273. cancelNewMenuItem();
  274. } else {
  275. alert('请填写菜单项文本');
  276. }
  277. }
  278. function cancelNewMenuItem() {
  279. document.getElementById('newMenuItemRow').classList.add('hidden');
  280. document.getElementById('newMenuItemText').value = '';
  281. document.getElementById('newMenuItemLink').value = '';
  282. document.getElementById('newMenuItemNewTab').value = 'false';
  283. }
  284. function editMenuItem(index) {
  285. const item = menuItems[index];
  286. document.getElementById('newMenuItemText').value = item.text;
  287. document.getElementById('newMenuItemLink').value = item.link;
  288. document.getElementById('newMenuItemNewTab').value = item.newTab.toString();
  289. showNewMenuItemRow();
  290. menuItems.splice(index, 1);
  291. renderMenuItems();
  292. updateMenuPreview();
  293. }
  294. function deleteMenuItem(index) {
  295. menuItems.splice(index, 1);
  296. renderMenuItems();
  297. updateMenuPreview();
  298. }
  299. async function saveConfig() {
  300. const config = {
  301. logo: document.getElementById('logoUrl').value,
  302. proxyDomain: document.getElementById('proxyDomain').value,
  303. menuItems: getMenuItems(),
  304. adImage: document.getElementById('adImageUrl').value
  305. };
  306. try {
  307. const response = await fetch('/api/config', {
  308. method: 'POST',
  309. headers: { 'Content-Type': 'application/json' },
  310. body: JSON.stringify(config)
  311. });
  312. if (response.ok) {
  313. alert('配置已保存');
  314. } else {
  315. throw new Error('保存失败');
  316. }
  317. } catch (error) {
  318. alert('保存失败: ' + error.message);
  319. }
  320. }
  321. async function loadConfig() {
  322. try {
  323. const response = await fetch('/api/config');
  324. const config = await response.json();
  325. document.getElementById('logoUrl').value = config.logo || '';
  326. document.getElementById('proxyDomain').value = config.proxyDomain || '';
  327. setMenuItems(config.menuItems || []);
  328. document.getElementById('adImageUrl').value = config.adImage || '';
  329. } catch (error) {
  330. console.error('加载配置失败:', error);
  331. }
  332. }
  333. async function login() {
  334. const username = document.getElementById('username').value;
  335. const password = document.getElementById('password').value;
  336. try {
  337. const response = await fetch('/api/login', {
  338. method: 'POST',
  339. headers: { 'Content-Type': 'application/json' },
  340. body: JSON.stringify({ username, password })
  341. });
  342. if (response.ok) {
  343. isLoggedIn = true;
  344. localStorage.setItem('isLoggedIn', 'true'); // 存储登录状态
  345. document.getElementById('loginModal').style.display = 'none';
  346. document.getElementById('adminContainer').classList.remove('hidden');
  347. loadConfig();
  348. } else {
  349. alert('登录失败');
  350. }
  351. } catch (error) {
  352. alert('登录失败: ' + error.message);
  353. }
  354. }
  355. async function changePassword() {
  356. const currentPassword = document.getElementById('currentPassword').value;
  357. const newPassword = document.getElementById('newPassword').value;
  358. try {
  359. const response = await fetch('/api/change-password', {
  360. method: 'POST',
  361. headers: { 'Content-Type': 'application/json' },
  362. body: JSON.stringify({ currentPassword, newPassword })
  363. });
  364. if (response.ok) {
  365. alert('密码已修改');
  366. } else {
  367. alert('修改密码失败');
  368. }
  369. } catch (error) {
  370. alert('修改密码失败: ' + error.message);
  371. }
  372. }
  373. // 页面加载时检查登录状态
  374. window.onload = async function() {
  375. try {
  376. const response = await fetch('/api/check-session');
  377. if (response.ok) {
  378. isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'; // 读取登录状态
  379. if (isLoggedIn) {
  380. document.getElementById('loginModal').style.display = 'none';
  381. document.getElementById('adminContainer').classList.remove('hidden');
  382. loadConfig();
  383. } else {
  384. document.getElementById('loginModal').style.display = 'block';
  385. }
  386. } else {
  387. localStorage.removeItem('isLoggedIn'); // 清除登录状态
  388. document.getElementById('loginModal').style.display = 'block';
  389. }
  390. } catch (error) {
  391. localStorage.removeItem('isLoggedIn'); // 清除登录状态
  392. document.getElementById('loginModal').style.display = 'block';
  393. }
  394. };
  395. // 表单提交事件监听器
  396. document.getElementById('adminForm').addEventListener('submit', async function(e) {
  397. e.preventDefault();
  398. await saveConfig();
  399. });
  400. </script>
  401. </body>
  402. </html>