| | |
| | | <div id="g_shots_list"></div> |
| | | </div> |
| | | <div class="upload-box"> |
| | | <div class="hint">游戏安装包(zip/rar/exe/iso 等,可多选)</div> |
| | | <div class="hint">游戏压缩包(游戏安装目录打包成的 zip/rar/7z,可多选。用户下载后自动解压到游戏目录并删除压缩包)</div> |
| | | <input type="file" multiple onchange="uploadPackages(this)"> |
| | | <div id="g_files_list"></div> |
| | | </div> |
| | | <div class="form-row"><label>游戏启动程序(相对路径,如 Binaries\Win64\Game.exe,用户启动游戏时直接打开)</label><input id="g_launch_exe" placeholder="Binaries\Win64\Game.exe"></div> |
| | | <div class="form-row"><label>游戏介绍(说明图片文字)</label><textarea id="g_description"></textarea></div> |
| | | <div style="display:flex;gap:10px;justify-content:flex-end;margin-top:16px"> |
| | | <button class="btn btn-gray" onclick="hideGameModal()">取消</button> |
| | | <button class="btn" onclick="saveGame()">保存</button> |
| | | <button class="btn" id="saveGameBtn" onclick="saveGame()">保存</button> |
| | | </div> |
| | | </div> |
| | | </div> |
| | |
| | | setTimeout(() => t.className = 'toast', 2500); |
| | | } |
| | | async function api(path, opts = {}) { |
| | | opts.headers = Object.assign({'Content-Type': 'application/json'}, opts.headers || {}); |
| | | opts.headers = Object.assign({}, opts.headers || {}); |
| | | if (token) opts.headers['Authorization'] = 'Bearer ' + token; |
| | | if (opts.body && typeof opts.body !== 'string' && !(opts.body instanceof FormData)) { |
| | | // FormData(文件上传)不能手动设置 Content-Type,由浏览器自动生成 multipart 边界 |
| | | if (opts.body instanceof FormData) { |
| | | if (opts.headers['Content-Type']) delete opts.headers['Content-Type']; |
| | | } else { |
| | | opts.headers['Content-Type'] = 'application/json'; |
| | | if (opts.body && typeof opts.body !== 'string') { |
| | | opts.body = JSON.stringify(opts.body); |
| | | } |
| | | } |
| | | const res = await fetch(path, opts); |
| | | if (res.status === 401) { localStorage.removeItem('admin_token'); location.reload(); throw new Error('未登录'); } |
| | | return res.json(); |
| | |
| | | g_size.value = g.size || ''; g_install_size.value = g.install_size || ''; |
| | | g_os.value = g.os_req || ''; g_cpu.value = g.cpu_req || ''; g_gpu.value = g.gpu_req || ''; g_ram.value = g.ram_req || ''; |
| | | g_version_note.value = g.version_note || ''; g_description.value = g.description || ''; |
| | | g_launch_exe.value = g.launch_exe || ''; |
| | | g_platform.value = g.platform || 'pc'; g_banner.value = g.is_banner ? '1' : '0'; |
| | | g_cover.value = g.cover || ''; g_video.value = g.video || ''; |
| | | try { editingScreenshots = JSON.parse(g.screenshots || '[]'); } catch(e) {} |
| | |
| | | toast('上传成功'); |
| | | } |
| | | async function uploadScreenshots(input) { |
| | | let ok = 0, fail = 0; |
| | | for (const f of input.files) { |
| | | const fd = new FormData(); |
| | | fd.append('file', f); fd.append('type', 'image'); |
| | | const res = await api('/admin/api/upload', {method: 'POST', body: fd}); |
| | | if (res.code === 0) editingScreenshots.push(res.data.url); |
| | | if (res.code === 0) { editingScreenshots.push(res.data.url); ok++; } |
| | | else { fail++; toast(f.name + ':' + (res.msg || '上传失败'), true); } |
| | | } |
| | | renderShots(); toast('截图上传完成'); |
| | | renderShots(); |
| | | if (ok > 0) toast(`截图上传完成(${ok}个${fail > 0 ? ',失败' + fail + '个' : ''})`); |
| | | } |
| | | async function uploadPackages(input) { |
| | | if (!input.files.length) return; |
| | | let ok = 0, fail = 0; |
| | | setSaveEnabled(false); |
| | | for (const f of input.files) { |
| | | const fd = new FormData(); |
| | | fd.append('file', f); fd.append('type', 'file'); |
| | | const res = await api('/admin/api/upload', {method: 'POST', body: fd}); |
| | | if (res.code === 0) editingFiles.push({name: res.data.name, size: res.data.size, url: res.data.url}); |
| | | try { |
| | | // 大文件用 XHR 上传并显示进度 |
| | | 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) renderUploadProgress(f.name, e.loaded, e.total); |
| | | }; |
| | | xhr.onload = () => { try { resolve(JSON.parse(xhr.responseText)); } catch (err) { reject(err); } }; |
| | | xhr.onerror = () => reject(new Error('网络错误')); |
| | | xhr.send(fd); |
| | | }); |
| | | if (res.code === 0) { editingFiles.push({name: res.data.name, size: res.data.size, url: res.data.url}); ok++; } |
| | | else { fail++; toast(f.name + ':' + (res.msg || '上传失败'), true); } |
| | | } catch (e) { |
| | | fail++; toast(f.name + ':上传失败(' + e.message + ')', true); |
| | | } |
| | | renderFiles(); toast('安装包上传完成'); |
| | | } |
| | | setSaveEnabled(true); |
| | | renderFiles(); |
| | | if (ok > 0) toast(`安装包上传完成(${ok}个${fail > 0 ? ',失败' + fail + '个' : ''})`); |
| | | } |
| | | /// 压缩包上传中时禁用保存按钮,防止保存了空的文件列表 |
| | | function setSaveEnabled(enabled) { |
| | | const btn = document.getElementById('saveGameBtn'); |
| | | if (btn) { btn.disabled = !enabled; btn.style.opacity = enabled ? '' : '0.5'; } |
| | | } |
| | | function renderUploadProgress(name, loaded, total) { |
| | | const pct = Math.floor(loaded / total * 100); |
| | | const size = total > 1024*1024*1024 ? (total/1024/1024/1024).toFixed(2)+' GB' |
| | | : total > 1024*1024 ? (total/1024/1024).toFixed(1)+' MB' : (total/1024).toFixed(0)+' KB'; |
| | | document.getElementById('g_files_list').innerHTML = |
| | | `<div class="file-item"><span>${name}(${size})上传中 ${pct}%</span></div>`; |
| | | } |
| | | function renderShots() { |
| | | document.getElementById('g_shots_list').innerHTML = editingScreenshots.map((u, i) => |
| | |
| | | platform: g_platform.value, tags: g_tags.value.trim(), |
| | | size: g_size.value.trim(), install_size: g_install_size.value.trim(), |
| | | os_req: g_os.value.trim(), cpu_req: g_cpu.value.trim(), gpu_req: g_gpu.value.trim(), ram_req: g_ram.value.trim(), |
| | | version_note: g_version_note.value.trim(), cover: g_cover.value, video: g_video.value, |
| | | version_note: g_version_note.value.trim(), launch_exe: g_launch_exe.value.trim(), cover: g_cover.value, video: g_video.value, |
| | | screenshots: JSON.stringify(editingScreenshots), description: g_description.value, |
| | | files: JSON.stringify(editingFiles), is_banner: g_banner.value === '1', |
| | | }; |