Files
appDistribute/script.js
sakurai d2c42ba4c2 feat: 添加文档管理功能并优化样式
添加文档管理功能,包括文档展示区域和API接口
新增文档相关CSS样式,优化移动端显示
更新Go模块依赖版本
2025-12-25 00:48:13 +08:00

331 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 配置
const ADMIN_PASSWORD = 'admin123'; // 管理员密码
const API_BASE_URL = 'http://localhost:6903/api'; // 后端API基础URL
// 初始化
function init() {
checkLoginStatus();
loadAppList();
getDoc();
}
// 检查登录状态
function checkLoginStatus() {
const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true';
if (isLoggedIn) {
showUploadSection();
}
}
// 登录功能
function login() {
const password = document.getElementById('password').value;
const message = document.getElementById('loginMessage');
if (!password.trim()) {
message.textContent = '密码不能为空!';
message.style.color = '#ff0000';
return;
}
if (password === ADMIN_PASSWORD) {
localStorage.setItem('isLoggedIn', 'true');
showUploadSection();
message.textContent = '登录成功!';
message.style.color = '#008000';
document.getElementById('password').value = '';
} else {
message.textContent = '密码错误,请重试!';
message.style.color = '#ff0000';
}
}
// 显示上传区域
function showUploadSection() {
document.getElementById('loginSection').style.display = 'none';
document.getElementById('uploadSection').style.display = 'block';
const loginSection = document.getElementById('loginSection');
// 检查是否已经添加了退出按钮,避免重复添加
if (!loginSection.querySelector('.logout-btn')) {
const logoutBtn = document.createElement('button');
logoutBtn.textContent = '退出登录';
logoutBtn.className = 'logout-btn';
logoutBtn.onclick = logout;
loginSection.appendChild(logoutBtn);
}
}
// 退出登录
function logout() {
localStorage.removeItem('isLoggedIn');
location.reload();
}
// 显示加载状态
function showLoading() {
const loadingIndicator = document.getElementById('loadingIndicator');
if (loadingIndicator) {
loadingIndicator.style.display = 'inline';
}
}
// 隐藏加载状态
function hideLoading() {
const loadingIndicator = document.getElementById('loadingIndicator');
if (loadingIndicator) {
loadingIndicator.style.display = 'none';
}
}
// 刷新App列表
function refreshAppList() {
loadAppList();
}
// 加载App列表
async function loadAppList() {
const appList = document.getElementById('appList');
try {
showLoading();
const response = await fetch(`${API_BASE_URL}/apps`, {
method: 'GET',
headers: {
'Accept': 'application/json'
},
timeout: 10000 // 10秒超时
});
if (!response.ok) {
throw new Error(`HTTP错误状态${response.status}`);
}
const apps = await response.json();
if (apps.length === 0) {
appList.innerHTML = '<p style="text-align: center;">暂无App</p>';
return;
}
appList.innerHTML = '';
apps.forEach(app => {
const appItem = document.createElement('div');
appItem.className = 'app-item';
const appInfo = document.createElement('div');
appInfo.className = 'app-info';
const appName = document.createElement('h3');
appName.textContent = app.name;
const appDate = document.createElement('p');
appDate.textContent = `上传时间:${app.date}`;
const downloadBtn = document.createElement('button');
downloadBtn.className = 'download-btn';
downloadBtn.textContent = '下载';
downloadBtn.onclick = () => downloadApp(app.id, app.fileName);
const deleteBtn = document.createElement('button');
deleteBtn.className = 'download-btn';
deleteBtn.textContent = '删除';
deleteBtn.onclick = () => deleteApp(app.id);
const copyLinkBtn = document.createElement('button');
copyLinkBtn.className = 'download-btn';
copyLinkBtn.textContent = '复制直链';
copyLinkBtn.onclick = () => copyDirectLink(app.id, app.fileName);
appInfo.appendChild(appName);
appInfo.appendChild(appDate);
appItem.appendChild(appInfo);
const buttonsContainer = document.createElement('div');
buttonsContainer.style.display = 'flex';
buttonsContainer.style.gap = '10px';
buttonsContainer.appendChild(downloadBtn);
buttonsContainer.appendChild(deleteBtn);
buttonsContainer.appendChild(copyLinkBtn);
appItem.appendChild(buttonsContainer);
appList.appendChild(appItem);
});
} catch (error) {
console.error('获取App列表失败:', error);
appList.innerHTML = `<p style="color: #ff0000; text-align: center;">获取App列表失败${error.message}</p>`;
} finally {
hideLoading();
}
}
// 上传App
async function uploadApp() {
let appName = document.getElementById('appName').value;
const appFile = document.getElementById('appFile').files[0];
const message = document.getElementById('uploadMessage');
if (!appFile) {
message.textContent = '请选择文件!';
message.style.color = '#ff0000';
return;
}
if (appFile.size > 100 * 1024 * 1024 * 1024) { // 限制1000MB
message.textContent = '文件大小不能超过1GB';
message.style.color = '#ff0000';
return;
}
// 如果App名称为空使用文件名作为默认值
if (!appName.trim()) {
// 去除文件扩展名
appName = appFile.name.replace(/\.[^/.]+$/, '');
}
const formData = new FormData();
formData.append('name', appName);
formData.append('file', appFile);
try {
message.textContent = '上传中...';
message.style.color = '#0000ff';
const response = await fetch(`${API_BASE_URL}/apps`, {
method: 'POST',
body: formData,
timeout: 30000 // 30秒超时
});
if (!response.ok) {
throw new Error(`HTTP错误状态${response.status}`);
}
const result = await response.json();
if (result.success) {
message.textContent = '上传成功!';
message.style.color = '#008000';
loadAppList();
// 清空表单
document.getElementById('appName').value = '';
document.getElementById('appFile').value = '';
} else {
message.textContent = '上传失败:' + (result.message || '未知错误');
message.style.color = '#ff0000';
}
} catch (error) {
console.error('上传失败:', error);
message.textContent = '上传失败:' + error.message;
message.style.color = '#ff0000';
}
}
// 删除App
function deleteApp(appId) {
if (!confirm('如果确定删除此APP,请将矩阵 A = [[3,1,0],[1,2,1],[0,1,1]]的特征值、特征向量并验证A是正定矩阵的解题步骤做出来然后我也没做')) {
return;
}
}
// 下载App
function downloadApp(appId, fileName) {
try {
const link = document.createElement('a');
link.href = `${API_BASE_URL}/apps/${appId}`;
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
} catch (error) {
console.error('下载失败:', error);
alert('下载失败:' + error.message);
}
}
// 复制直链
function copyDirectLink(appId, fileName) {
const directLink = `${API_BASE_URL}/apps/${appId}`;
// 复制到剪贴板
navigator.clipboard.writeText(directLink)
.then(() => {
// 显示复制成功提示
const message = document.createElement('div');
message.textContent = '直链已复制到剪贴板!';
message.style.cssText = `
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #000;
color: #fff;
padding: 10px 20px;
border-radius: 5px;
font-size: 14px;
z-index: 1000;
`;
document.body.appendChild(message);
// 3秒后移除提示
setTimeout(() => {
document.body.removeChild(message);
}, 3000);
})
.catch(err => {
console.error('复制失败:', err);
alert('复制失败:' + err.message);
});
}
async function getDocApi() {
try {
showLoading();
const docContent = document.getElementById('docContent');
docContent.innerHTML = ``;
const response = await fetch(`${API_BASE_URL}/docs`, {
method: 'GET',
headers: {
'Accept': 'application/json'
},
timeout: 10000 // 10秒超时
});
if (!response.ok) {
throw new Error(`HTTP错误状态${response.status}`);
}
const docs = await response.json();
if (docs.length === 0) {
docContent.innerHTML = '<p style="text-align: center;">暂无文档</p>';
return;
}
for (let i = 0; i < docs.length; i++) {
const doc = docs[i];
const docItem = document.createElement('div');
docItem.className = 'doc-item';
docItem.innerHTML = `
<p>${doc.docBody}</p>
`;
docContent.appendChild(docItem);
}
}
catch (error) {
console.error('获取文档失败:', error);
docContent.innerHTML = '<p style="text-align: center;">获取文档失败:' + error.message + '</p>';
return;
}
}
// 获取文档内容
function getDoc() {
getDocApi();
}
// 页面加载完成后初始化
window.onload = init;