admin.html 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. <p></h1>配置添加或修改后,点击【保存更改】保存配置</p>
  166. <form id="adminForm">
  167. <label for="logoUrl">Logo URL: (可选)</label>
  168. <input type="url" id="logoUrl" name="logoUrl">
  169. <label for="proxyDomain">Docker镜像代理地址: (必填)</label>
  170. <input type="text" id="proxyDomain" name="proxyDomain" required>
  171. <h2 class="menu-label">菜单项管理</h2>
  172. <table id="menuTable">
  173. <thead>
  174. <tr>
  175. <th>文本</th>
  176. <th>链接 (可选)</th>
  177. <th>新标签页打开</th>
  178. <th>操作</th>
  179. </tr>
  180. </thead>
  181. <tbody id="menuTableBody">
  182. <!-- 菜单项将在这里动态添加 -->
  183. </tbody>
  184. </table>
  185. <button type="button" class="add-btn" onclick="showNewMenuItemRow()">添加菜单项</button>
  186. <h2 class="menu-label">广告管理</h2>
  187. <table id="adTable">
  188. <thead>
  189. <tr>
  190. <th>广告URL</th>
  191. <th>跳转URL</th>
  192. <th>操作</th>
  193. </tr>
  194. </thead>
  195. <tbody id="adTableBody">
  196. <!-- 广告项将在这里动态添加 -->
  197. </tbody>
  198. </table>
  199. <button type="button" class="add-btn" onclick="showNewAdRow()">添加广告</button>
  200. <button type="submit">保存更改</button>
  201. </form>
  202. <!-- 修改密码的独立表单 -->
  203. <div id="passwordChangeForm" style="margin-top: 20px;">
  204. <h2 class="menu-label">修改密码</h2>
  205. <label for="currentPassword">当前密码</label>
  206. <input type="password" id="currentPassword" name="currentPassword">
  207. <label for="newPassword">新密码</label>
  208. <input type="password" id="newPassword" name="newPassword">
  209. <button type="button" onclick="changePassword()">修改密码</button>
  210. </div>
  211. </div>
  212. <div class="login-modal" id="loginModal">
  213. <div class="login-content">
  214. <h2>登录</h2>
  215. <label for="username">用户名</label>
  216. <input type="text" id="username" name="username" required>
  217. <label for="password">密码</label>
  218. <input type="password" id="password" name="password" required>
  219. <button type="button" onclick="login()">登录</button>
  220. </div>
  221. </div>
  222. <script src="https://cdnjs.cloudflare.com/ajax/libs/dragula/3.7.2/dragula.min.js"></script>
  223. <script>
  224. let menuItems = [];
  225. let adImages = [];
  226. let isLoggedIn = false;
  227. let editingIndex = -1; // 用于记录当前编辑的菜单项索引
  228. function getMenuItems() {
  229. return menuItems;
  230. }
  231. function setMenuItems(items) {
  232. menuItems = items;
  233. renderMenuItems();
  234. }
  235. function setupEditButtons() {
  236. const editButtons = document.querySelectorAll('.edit-btn');
  237. editButtons.forEach((button, index) => {
  238. button.addEventListener('click', () => {
  239. const row = button.closest('tr');
  240. const textInput = row.querySelector('.menu-text');
  241. const linkInput = row.querySelector('.menu-link');
  242. const newTabSelect = row.querySelector('.menu-newtab');
  243. if (textInput.disabled) {
  244. textInput.disabled = false;
  245. linkInput.disabled = false;
  246. newTabSelect.disabled = false;
  247. button.textContent = '保存';
  248. } else {
  249. const text = textInput.value;
  250. const link = linkInput.value;
  251. const newTab = newTabSelect.value === 'true';
  252. if (text) {
  253. const rowIndex = row.getAttribute('data-index');
  254. menuItems[rowIndex] = { text, link, newTab };
  255. renderMenuItems();
  256. } else {
  257. alert('请填写菜单项文本');
  258. }
  259. }
  260. });
  261. });
  262. }
  263. function renderMenuItems() {
  264. const tbody = document.getElementById('menuTableBody');
  265. tbody.innerHTML = '';
  266. menuItems.forEach((item, index) => {
  267. const row = `
  268. <tr data-index="${index}">
  269. <td><input type="text" class="menu-text" value="${item.text}" disabled></td>
  270. <td><input type="url" class="menu-link" value="${item.link || ''}" disabled></td>
  271. <td>
  272. <select class="menu-newtab" disabled>
  273. <option value="false" ${item.newTab ? '' : 'selected'}>否</option>
  274. <option value="true" ${item.newTab ? 'selected' : ''}>是</option>
  275. </select>
  276. </td>
  277. <td>
  278. <button type="button" class="action-btn edit-btn">编辑</button>
  279. <button type="button" class="action-btn delete-btn">删除</button>
  280. <span class="drag-handle" style="cursor: move;">☰</span>
  281. </td>
  282. </tr>
  283. `;
  284. tbody.innerHTML += row;
  285. });
  286. setupDragAndDrop();
  287. setupEditButtons();
  288. setupDeleteButtons();
  289. }
  290. function setupDeleteButtons() {
  291. const deleteButtons = document.querySelectorAll('.delete-btn');
  292. deleteButtons.forEach((button, index) => {
  293. button.addEventListener('click', () => {
  294. const row = button.closest('tr');
  295. const index = row.getAttribute('data-index');
  296. menuItems.splice(index, 1);
  297. renderMenuItems();
  298. });
  299. });
  300. }
  301. function showNewMenuItemRow() {
  302. const tbody = document.getElementById('menuTableBody');
  303. const newRow = `
  304. <tr id="newMenuItemRow">
  305. <td><input type="text" class="menu-text" placeholder="菜单项文本"></td>
  306. <td><input type="url" class="menu-link" placeholder="菜单项链接 (可选)"></td>
  307. <td>
  308. <select class="menu-newtab">
  309. <option value="false">否</option>
  310. <option value="true">是</option>
  311. </select>
  312. </td>
  313. <td>
  314. <button type="button" class="action-btn" onclick="saveNewMenuItem()">保存</button>
  315. <button type="button" class="action-btn" onclick="cancelNewMenuItem()">取消</button>
  316. </td>
  317. </tr>
  318. `;
  319. tbody.insertAdjacentHTML('beforeend', newRow);
  320. }
  321. function saveNewMenuItem() {
  322. const newRow = document.getElementById('newMenuItemRow');
  323. const textInput = newRow.querySelector('.menu-text');
  324. const linkInput = newRow.querySelector('.menu-link');
  325. const newTabSelect = newRow.querySelector('.menu-newtab');
  326. const text = textInput.value;
  327. const link = linkInput.value;
  328. const newTab = newTabSelect.value === 'true';
  329. if (text) {
  330. menuItems.push({ text, link, newTab }); // 确保新菜单项被添加到 menuItems 数组中
  331. renderMenuItems();
  332. cancelNewMenuItem();
  333. } else {
  334. alert('请填写菜单项文本');
  335. }
  336. }
  337. function cancelNewMenuItem() {
  338. const newRow = document.getElementById('newMenuItemRow');
  339. if (newRow) {
  340. newRow.remove();
  341. }
  342. }
  343. function showNewAdRow() {
  344. const tbody = document.getElementById('adTableBody');
  345. const newRow = `
  346. <tr id="newAdRow">
  347. <td><input type="url" id="newAdUrl" placeholder="广告URL"></td>
  348. <td><input type="url" id="newAdLink" placeholder="跳转URL"></td>
  349. <td>
  350. <button type="button" class="action-btn" onclick="saveNewAd()">保存</button>
  351. <button type="button" class="action-btn" onclick="cancelNewAd()">取消</button>
  352. </td>
  353. </tr>
  354. `;
  355. tbody.insertAdjacentHTML('beforeend', newRow);
  356. }
  357. function renderAdItems() {
  358. const tbody = document.getElementById('adTableBody');
  359. tbody.innerHTML = '';
  360. adImages.forEach((ad, index) => {
  361. const row = `
  362. <tr data-index="${index}">
  363. <td><input type="url" class="ad-url" value="${ad.url}" disabled></td>
  364. <td><input type="url" class="ad-link" value="${ad.link}" disabled></td>
  365. <td>
  366. <button type="button" class="action-btn edit-btn">编辑</button>
  367. <button type="button" class="action-btn delete-btn">删除</button>
  368. </td>
  369. </tr>
  370. `;
  371. tbody.innerHTML += row;
  372. });
  373. setupAdEditButtons();
  374. setupAdDeleteButtons();
  375. }
  376. function saveNewAd() {
  377. const url = document.getElementById('newAdUrl').value || '';
  378. const link = document.getElementById('newAdLink').value || '';
  379. adImages.push({ url, link });
  380. renderAdItems();
  381. cancelNewAd();
  382. }
  383. function cancelNewAd() {
  384. const newRow = document.getElementById('newAdRow');
  385. if (newRow) {
  386. newRow.remove();
  387. }
  388. }
  389. function renderAdItems() {
  390. const tbody = document.getElementById('adTableBody');
  391. tbody.innerHTML = '';
  392. adImages.forEach((ad, index) => {
  393. const row = `
  394. <tr data-index="${index}">
  395. <td><input type="url" class="ad-url" value="${ad.url}" disabled></td>
  396. <td><input type="url" class="ad-link" value="${ad.link}" disabled></td>
  397. <td>
  398. <button type="button" class="action-btn edit-btn">编辑</button>
  399. <button type="button" class="action-btn delete-btn">删除</button>
  400. </td>
  401. </tr>
  402. `;
  403. tbody.innerHTML += row;
  404. });
  405. setupAdEditButtons();
  406. setupAdDeleteButtons();
  407. }
  408. function setupAdEditButtons() {
  409. const editButtons = document.querySelectorAll('.edit-btn');
  410. editButtons.forEach((button, index) => {
  411. button.addEventListener('click', () => {
  412. const row = button.closest('tr');
  413. const urlInput = row.querySelector('.ad-url');
  414. const linkInput = row.querySelector('.ad-link');
  415. if (urlInput.disabled) {
  416. urlInput.disabled = false;
  417. linkInput.disabled = false;
  418. button.textContent = '保存';
  419. editingIndex = row.getAttribute('data-index');
  420. } else {
  421. const url = urlInput.value || '';
  422. const link = linkInput.value || '';
  423. adImages[editingIndex] = { url, link };
  424. renderAdItems();
  425. editingIndex = -1;
  426. }
  427. });
  428. });
  429. }
  430. function setupAdDeleteButtons() {
  431. const deleteButtons = document.querySelectorAll('.delete-btn');
  432. deleteButtons.forEach((button, index) => {
  433. button.addEventListener('click', () => {
  434. const row = button.closest('tr');
  435. const index = row.getAttribute('data-index');
  436. adImages.splice(index, 1);
  437. renderAdItems();
  438. });
  439. });
  440. }
  441. async function saveConfig() {
  442. const config = {
  443. logo: document.getElementById('logoUrl').value,
  444. proxyDomain: document.getElementById('proxyDomain').value,
  445. menuItems: menuItems,
  446. adImages: adImages
  447. };
  448. try {
  449. const response = await fetch('/api/config', {
  450. method: 'POST',
  451. headers: { 'Content-Type': 'application/json' },
  452. body: JSON.stringify(config)
  453. });
  454. if (response.ok) {
  455. alert('配置已保存');
  456. } else {
  457. throw new Error('保存失败');
  458. }
  459. } catch (error) {
  460. alert('保存失败: ' + error.message);
  461. }
  462. }
  463. async function loadConfig() {
  464. try {
  465. const response = await fetch('/api/config');
  466. const config = await response.json();
  467. document.getElementById('logoUrl').value = config.logo || '';
  468. document.getElementById('proxyDomain').value = config.proxyDomain || '';
  469. setMenuItems(config.menuItems || []);
  470. adImages = config.adImages || [];
  471. renderAdItems();
  472. } catch (error) {
  473. console.error('加载配置失败:', error);
  474. }
  475. }
  476. async function login() {
  477. const username = document.getElementById('username').value;
  478. const password = document.getElementById('password').value;
  479. try {
  480. const response = await fetch('/api/login', {
  481. method: 'POST',
  482. headers: { 'Content-Type': 'application/json' },
  483. body: JSON.stringify({ username, password })
  484. });
  485. if (response.ok) {
  486. isLoggedIn = true;
  487. localStorage.setItem('isLoggedIn', 'true'); // 存储登录状态
  488. document.getElementById('loginModal').style.display = 'none';
  489. document.getElementById('adminContainer').classList.remove('hidden');
  490. loadConfig();
  491. } else {
  492. alert('登录失败');
  493. }
  494. } catch (error) {
  495. alert('登录失败: ' + error.message);
  496. }
  497. }
  498. async function changePassword() {
  499. const currentPassword = document.getElementById('currentPassword').value;
  500. const newPassword = document.getElementById('newPassword').value;
  501. if (!currentPassword || !newPassword) {
  502. alert('请填写当前密码和新密码');
  503. return;
  504. }
  505. try {
  506. const response = await fetch('/api/change-password', {
  507. method: 'POST',
  508. headers: { 'Content-Type': 'application/json' },
  509. body: JSON.stringify({ currentPassword, newPassword })
  510. });
  511. if (response.ok) {
  512. alert('密码已修改');
  513. } else {
  514. alert('修改密码失败');
  515. }
  516. } catch (error) {
  517. alert('修改密码失败: ' + error.message);
  518. }
  519. }
  520. // 页面加载时检查登录状态
  521. window.onload = async function() {
  522. try {
  523. const response = await fetch('/api/check-session');
  524. if (response.ok) {
  525. isLoggedIn = localStorage.getItem('isLoggedIn') === 'true';
  526. if (isLoggedIn) {
  527. document.getElementById('loginModal').style.display = 'none';
  528. document.getElementById('adminContainer').classList.remove('hidden');
  529. loadConfig();
  530. } else {
  531. document.getElementById('loginModal').style.display = 'block';
  532. }
  533. } else {
  534. localStorage.removeItem('isLoggedIn');
  535. document.getElementById('loginModal').style.display = 'block';
  536. }
  537. } catch (error) {
  538. localStorage.removeItem('isLoggedIn');
  539. document.getElementById('loginModal').style.display = 'block';
  540. }
  541. };
  542. // 表单提交事件监听器
  543. document.getElementById('adminForm').addEventListener('submit', async function(e) {
  544. e.preventDefault();
  545. await saveConfig();
  546. });
  547. function setupDragAndDrop() {
  548. const drake = dragula([document.getElementById('menuTableBody')], {
  549. moves: function (el, container, handle) {
  550. return handle.classList.contains('drag-handle');
  551. }
  552. });
  553. drake.on('drop', (el, target, source, sibling) => {
  554. const newIndex = Array.from(target.children).indexOf(el);
  555. const oldIndex = el.getAttribute('data-index');
  556. const movedItem = menuItems.splice(oldIndex, 1)[0];
  557. menuItems.splice(newIndex, 0, movedItem);
  558. renderMenuItems();
  559. });
  560. }
  561. function updateAdImage(adImages) {
  562. const adContainer = document.getElementById('adContainer');
  563. adContainer.innerHTML = '';
  564. if (adImages && adImages.length > 0) {
  565. adImages.forEach(ad => {
  566. const adLink = document.createElement('a');
  567. adLink.href = ad.link;
  568. adLink.target = '_blank';
  569. const adImage = document.createElement('img');
  570. adImage.src = ad.url;
  571. adImage.alt = ad.alt || '广告图片';
  572. adLink.appendChild(adImage);
  573. adContainer.appendChild(adLink);
  574. });
  575. // 轮播功能
  576. let currentAdIndex = 0;
  577. setInterval(() => {
  578. currentAdIndex = (currentAdIndex + 1) % adImages.length;
  579. adImages.forEach((ad, index) => {
  580. const adImage = adContainer.children[index];
  581. if (index === currentAdIndex) {
  582. adImage.style.display = 'block';
  583. } else {
  584. adImage.style.display = 'none';
  585. }
  586. });
  587. }, 5000); // 每5秒切换一次广告
  588. }
  589. }
  590. </script>
  591. </body>
  592. </html>