905086
2026-11-09 cbb26873e88a7a13e868448c1cc578ce70b8c6ae
server/admin/index.html
@@ -100,6 +100,7 @@
      <button data-tab="cats" onclick="switchTab('cats')">类别管理</button>
      <button data-tab="ann" onclick="switchTab('ann')">公告设置</button>
      <button data-tab="reset" onclick="switchTab('reset')">密码重置</button>
      <button data-tab="versions" onclick="switchTab('versions')">版本管理</button>
    </div>
    <button id="logoutBtn" onclick="logout()">退出登录</button>
  </div>
@@ -186,6 +187,19 @@
        <tbody id="resetTable"></tbody>
      </table>
    </div>
    <!-- 客户端版本管理 -->
    <div id="tab-versions" class="panel hidden">
      <h2>客户端版本管理</h2>
      <p style="color:#888;font-size:12px;margin-bottom:14px">上传客户端安装包(整个 Release 目录打成的 zip)。最新一条记录会展示在公开下载页 <a href="/download" target="_blank" style="color:#4a92ff">/download</a>,用户无需登录即可下载。</p>
      <div class="toolbar">
        <button class="btn" onclick="showVersionModal()">+ 上传新版本</button>
      </div>
      <table>
        <thead><tr><th>ID</th><th>版本号</th><th>文件大小</th><th>更新说明</th><th>下载次数</th><th>发布时间</th><th>操作</th></tr></thead>
        <tbody id="versionTable"></tbody>
      </table>
    </div>
  </div>
</div>
@@ -241,6 +255,27 @@
<div id="toast" class="toast"></div>
<!-- 版本编辑弹窗 -->
<div id="versionModal" class="modal-mask hidden">
  <div class="modal">
    <h3 id="versionModalTitle">上传新版本</h3>
    <input type="hidden" id="v_id">
    <div class="form-row"><label>版本号 *(如 1.0.0)</label><input id="v_version" placeholder="1.0.0"></div>
    <div class="form-row"><label>更新说明(换行分隔,展示在下载页)</label><textarea id="v_notes" placeholder="新增xxx功能&#10;修复xxx问题"></textarea></div>
    <div class="upload-box">
      <div class="hint">客户端安装包(整个 Release 目录打成的 zip)</div>
      <input type="file" accept=".zip" onchange="uploadVersionFile(this)">
      <div id="v_file_list"></div>
      <input type="hidden" id="v_file_url">
      <input type="hidden" id="v_file_size">
    </div>
    <div style="display:flex;gap:10px;justify-content:flex-end;margin-top:16px">
      <button class="btn btn-gray" onclick="hideVersionModal()">取消</button>
      <button class="btn" id="saveVersionBtn" onclick="saveVersion()">保存</button>
    </div>
  </div>
</div>
<!-- 工具编辑弹窗 -->
<div id="toolModal" class="modal-mask hidden">
  <div class="modal">
@@ -309,11 +344,11 @@
function enterPanel() {
  document.getElementById('loginPage').style.display = 'none';
  document.getElementById('mainPage').style.display = 'flex';
  loadStats(); loadKeys(1); loadGames(); loadCategories(); loadAnnouncement(); loadTools(); loadResetRequests();
  loadStats(); loadKeys(1); loadGames(); loadCategories(); loadAnnouncement(); loadTools(); loadResetRequests(); loadVersions();
}
function switchTab(tab) {
  document.querySelectorAll('.nav-tabs button').forEach(b => b.classList.toggle('active', b.dataset.tab === tab));
  ['keys','games','cats','ann','tools','reset'].forEach(t => document.getElementById('tab-' + t).classList.toggle('hidden', t !== tab));
  ['keys','games','cats','ann','tools','reset','versions'].forEach(t => document.getElementById('tab-' + t).classList.toggle('hidden', t !== tab));
}
async function loadStats() {
@@ -644,6 +679,97 @@
  loadTools();
}
/* ---------- 客户端版本管理 ---------- */
async function loadVersions() {
  const res = await api('/admin/api/versions');
  if (res.code !== 0) return;
  const rows = res.data.map((v, i) => `<tr>
    <td>${v.id}</td>
    <td><span class="tag ${i===0?'tag-green':'tag-gray'}">${v.version}${i===0?' · 最新':''}</span></td>
    <td>${v.file_size || '-'}</td>
    <td style="max-width:260px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap" title="${(v.notes||'').replace(/"/g,'&quot;')}">${(v.notes||'-').replace(/\n/g,';')}</td>
    <td>${v.download_count}</td>
    <td>${(v.created_at || '').replace('T', ' ').slice(0, 19)}</td>
    <td>
      <button class="btn btn-sm" onclick='showVersionModal(${JSON.stringify(v).replace(/'/g,"&#39;")})'>修改</button>
      <button class="btn btn-sm btn-danger" onclick="deleteVersion(${v.id})">删除</button>
    </td></tr>`).join('');
  document.getElementById('versionTable').innerHTML = rows || '<tr><td colspan="7" style="text-align:center;color:#666">暂无版本,请上传第一个客户端版本</td></tr>';
}
function showVersionModal(v) {
  document.getElementById('versionModalTitle').innerText = v ? '修改版本' : '上传新版本';
  document.getElementById('v_id').value = v ? v.id : '';
  document.getElementById('v_version').value = v ? v.version : '';
  document.getElementById('v_notes').value = v ? (v.notes || '') : '';
  document.getElementById('v_file_url').value = v ? (v.file_url || '') : '';
  document.getElementById('v_file_size').value = v ? (v.file_size || '') : '';
  renderVersionFile();
  document.getElementById('versionModal').classList.remove('hidden');
}
function hideVersionModal() { document.getElementById('versionModal').classList.add('hidden'); }
function renderVersionFile(name, pct) {
  const url = document.getElementById('v_file_url').value;
  const size = document.getElementById('v_file_size').value;
  if (pct !== undefined) {
    document.getElementById('v_file_list').innerHTML = `<div class="file-item"><span>${name} 上传中 ${pct}%</span></div>`;
  } else {
    document.getElementById('v_file_list').innerHTML = url ? `<div class="file-item"><span>${url}${size ? '(' + size + ')' : ''}</span></div>` : '';
  }
}
async function uploadVersionFile(input) {
  if (!input.files.length) return;
  const f = input.files[0];
  const fd = new FormData();
  fd.append('file', f); fd.append('type', 'file');
  const btn = document.getElementById('saveVersionBtn');
  btn.disabled = true; btn.style.opacity = '0.5';
  try {
    const res = await new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      xhr.open('POST', '/admin/api/upload');
      if (token) xhr.setRequestHeader('Authorization', 'Bearer ' + token);
      xhr.upload.onprogress = e => { if (e.lengthComputable) renderVersionFile(f.name, Math.floor(e.loaded / e.total * 100)); };
      xhr.onload = () => { try { resolve(JSON.parse(xhr.responseText)); } catch (err) { reject(err); } };
      xhr.onerror = () => reject(new Error('网络错误'));
      xhr.send(fd);
    });
    if (res.code !== 0) return toast(res.msg || '上传失败', true);
    document.getElementById('v_file_url').value = res.data.url;
    document.getElementById('v_file_size').value = res.data.size;
    // 未填版本号时用文件名里的版本号(如 gamebox-1.0.0.zip)
    if (!document.getElementById('v_version').value.trim()) {
      const m = f.name.match(/(\d+\.\d+(\.\d+)?)/);
      if (m) document.getElementById('v_version').value = m[1];
    }
    toast('上传成功');
  } catch (e) {
    toast('上传失败(' + e.message + ')', true);
  } finally {
    btn.disabled = false; btn.style.opacity = '';
    renderVersionFile();
  }
}
async function saveVersion() {
  const body = {
    id: parseInt(document.getElementById('v_id').value) || 0,
    version: document.getElementById('v_version').value.trim(),
    notes: document.getElementById('v_notes').value,
    file_url: document.getElementById('v_file_url').value.trim(),
    file_size: document.getElementById('v_file_size').value.trim(),
  };
  if (!body.version) return toast('请填写版本号', true);
  if (!body.file_url) return toast('请上传客户端安装包', true);
  const res = await api('/admin/api/versions', {method: 'POST', body});
  toast(res.msg, res.code !== 0);
  if (res.code === 0) { hideVersionModal(); loadVersions(); }
}
async function deleteVersion(id) {
  if (!confirm('确定删除该版本?')) return;
  const res = await api(`/admin/api/versions/${id}`, {method: 'DELETE'});
  toast(res.msg, res.code !== 0);
  loadVersions();
}
/* ---------- 密码重置申请 ---------- */
async function loadResetRequests() {
  const res = await api('/admin/api/reset_requests');