新增 FastAPI 文件服务器核心模块(配置、鉴权、存储、心跳上报),新增 HTTP 路由接口(健康检查、文件上传/下载/删除、管理登录/密码修改/统计),新增 Vue3 管理页面源码;新增 MySQL 元数据存储支持,新增 PyMySQL 依赖;新增 .gitignore 忽略 IDE 与备份文件;新增文件服务器工具脚本;删除旧的 build/ 脚本与 exe 构建产物,优化 Electron 平台登录逻辑为纯接口模式,新增平台账号环境变量注入,优化试用期异常检查逻辑,拆分服务岗转正流程为独立脚本。
317 lines
9.7 KiB
JavaScript
317 lines
9.7 KiB
JavaScript
// 37du 文件服务器管理页面 v2.0 - Vue 3 应用逻辑
|
|
// API 走同源 /fs-api 反代;管理接口用登录后的会话 Bearer Token 鉴权。
|
|
"use strict";
|
|
|
|
const API_BASE = "/fs-api";
|
|
const TOKEN_KEY = "fs_admin_token";
|
|
|
|
const { createApp } = Vue;
|
|
|
|
createApp({
|
|
data() {
|
|
return {
|
|
files: [],
|
|
loading: false,
|
|
keyword: "",
|
|
sortKey: "created",
|
|
sortDir: -1, // -1 降序 1 升序
|
|
uploading: false,
|
|
uploadIdx: 0,
|
|
uploadTotal: 0,
|
|
uploadPercent: 0,
|
|
statusMsg: "",
|
|
statusClass: "",
|
|
baseUrl: location.origin,
|
|
// 登录态
|
|
token: localStorage.getItem(TOKEN_KEY) || "",
|
|
username: "",
|
|
password: "",
|
|
showPwd: false,
|
|
loginMsg: "",
|
|
loggingIn: false,
|
|
// 改密码
|
|
showPwdModal: false,
|
|
oldPwd: "",
|
|
newPwd: "",
|
|
newPwd2: "",
|
|
pwdMsg: "",
|
|
savingPwd: false,
|
|
};
|
|
},
|
|
computed: {
|
|
loggedIn() {
|
|
return !!this.token;
|
|
},
|
|
tokenUser() {
|
|
try {
|
|
const raw = atob(this.token.split(".")[0]);
|
|
return raw.split("|")[0] || "?";
|
|
} catch (e) {
|
|
return "?";
|
|
}
|
|
},
|
|
sortedFiles() {
|
|
const k = this.keyword.trim().toLowerCase();
|
|
let list = this.files;
|
|
if (k) list = list.filter((f) => (f.filename || "").toLowerCase().includes(k));
|
|
const dir = this.sortDir;
|
|
const key = this.sortKey;
|
|
return list.slice().sort((a, b) => {
|
|
let r = 0;
|
|
if (key === "filename") r = (a.filename || "").localeCompare(b.filename || "", "zh");
|
|
else if (key === "size") r = (Number(a.size) || 0) - (Number(b.size) || 0);
|
|
else if (key === "created") r = (a.created || 0) - (b.created || 0);
|
|
else if (key === "downloads") r = (Number(a.downloads) || 0) - (Number(b.downloads) || 0);
|
|
return r * dir;
|
|
});
|
|
},
|
|
totalCount() {
|
|
return this.files.length;
|
|
},
|
|
totalSizeText() {
|
|
return this.fmtSize(this.files.reduce((s, f) => s + (Number(f.size) || 0), 0));
|
|
},
|
|
totalDownloads() {
|
|
return this.files.reduce((s, f) => s + (Number(f.downloads) || 0), 0);
|
|
},
|
|
lastUploadText() {
|
|
if (!this.files.length) return "-";
|
|
const t = Math.max(...this.files.map((f) => Number(f.created) || 0));
|
|
return this.fmtTime(t);
|
|
},
|
|
},
|
|
methods: {
|
|
authHeaders() {
|
|
return { Authorization: "Bearer " + this.token };
|
|
},
|
|
flash(msg, ok) {
|
|
this.statusMsg = msg;
|
|
this.statusClass = ok ? "ok" : "err";
|
|
clearTimeout(this._t);
|
|
this._t = setTimeout(() => {
|
|
this.statusMsg = "";
|
|
this.statusClass = "";
|
|
}, 3000);
|
|
},
|
|
sortBy(key) {
|
|
if (this.sortKey === key) {
|
|
this.sortDir = -this.sortDir;
|
|
} else {
|
|
this.sortKey = key;
|
|
this.sortDir = key === "filename" ? 1 : -1;
|
|
}
|
|
},
|
|
// ===== 登录 =====
|
|
async login() {
|
|
this.loggingIn = true;
|
|
this.loginMsg = "";
|
|
try {
|
|
const r = await fetch(`${API_BASE}/api/admin/login`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ username: this.username, password: this.password }),
|
|
});
|
|
const j = await r.json().catch(() => ({}));
|
|
if (!r.ok) {
|
|
this.loginMsg = j.detail || ("登录失败 HTTP " + r.status);
|
|
return;
|
|
}
|
|
this.token = j.token;
|
|
localStorage.setItem(TOKEN_KEY, j.token);
|
|
this.password = "";
|
|
this.load();
|
|
} catch (e) {
|
|
this.loginMsg = "网络错误:" + e.message;
|
|
} finally {
|
|
this.loggingIn = false;
|
|
}
|
|
},
|
|
logout() {
|
|
this.token = "";
|
|
localStorage.removeItem(TOKEN_KEY);
|
|
this.files = [];
|
|
this.loginMsg = "";
|
|
this.keyword = "";
|
|
},
|
|
// ===== 数据 =====
|
|
async load() {
|
|
this.loading = true;
|
|
try {
|
|
const r = await fetch(`${API_BASE}/api/files`, { headers: this.authHeaders() });
|
|
if (r.status === 401) {
|
|
this.logout();
|
|
this.loginMsg = "登录已过期,请重新登录";
|
|
return;
|
|
}
|
|
if (!r.ok) throw new Error("HTTP " + r.status);
|
|
const j = await r.json();
|
|
this.files = j.files || [];
|
|
this.flash("已刷新,共 " + this.files.length + " 个文件", true);
|
|
} catch (e) {
|
|
this.flash("加载文件列表失败:" + e.message, false);
|
|
} finally {
|
|
this.loading = false;
|
|
}
|
|
},
|
|
// ===== 上传(支持多文件)=====
|
|
async onFilesPicked(ev) {
|
|
const files = Array.from(ev.target.files || []);
|
|
ev.target.value = "";
|
|
if (!files.length) return;
|
|
this.uploading = true;
|
|
this.uploadTotal = files.length;
|
|
let okCount = 0;
|
|
for (let i = 0; i < files.length; i++) {
|
|
this.uploadIdx = i + 1;
|
|
this.uploadPercent = 0;
|
|
if (await this.uploadOne(files[i])) okCount++;
|
|
}
|
|
this.uploading = false;
|
|
this.flash(`上传完成:成功 ${okCount}/${files.length} 个`, okCount > 0);
|
|
this.load();
|
|
},
|
|
uploadOne(file) {
|
|
return new Promise((resolve) => {
|
|
const fd = new FormData();
|
|
fd.append("upload", file);
|
|
const xhr = new XMLHttpRequest();
|
|
xhr.open("POST", `${API_BASE}/api/upload`);
|
|
xhr.setRequestHeader("Authorization", "Bearer " + this.token);
|
|
xhr.upload.onprogress = (e) => {
|
|
if (e.lengthComputable) this.uploadPercent = Math.round((e.loaded / e.total) * 100);
|
|
};
|
|
xhr.onload = () => {
|
|
if (xhr.status === 401) {
|
|
this.logout();
|
|
this.loginMsg = "登录已过期,请重新登录";
|
|
resolve(false);
|
|
return;
|
|
}
|
|
if (xhr.status < 300) resolve(true);
|
|
else {
|
|
this.flash(`上传 ${file.name} 失败:HTTP ${xhr.status}`, false);
|
|
resolve(false);
|
|
}
|
|
};
|
|
xhr.onerror = () => {
|
|
this.flash(`上传 ${file.name} 失败:网络错误`, false);
|
|
resolve(false);
|
|
};
|
|
xhr.send(fd);
|
|
});
|
|
},
|
|
// ===== 删除 =====
|
|
async remove(f) {
|
|
if (!confirm(`确定删除文件「${f.filename}」?此操作不可恢复。`)) return;
|
|
try {
|
|
const r = await fetch(`${API_BASE}/api/files/${f.fid}`, {
|
|
method: "DELETE",
|
|
headers: this.authHeaders(),
|
|
});
|
|
if (r.status === 401) {
|
|
this.logout();
|
|
this.loginMsg = "登录已过期,请重新登录";
|
|
return;
|
|
}
|
|
if (!r.ok) throw new Error("HTTP " + r.status);
|
|
const j = await r.json();
|
|
if (j.ok) {
|
|
this.files = this.files.filter((x) => x.fid !== f.fid);
|
|
this.flash("已删除:" + f.filename, true);
|
|
} else {
|
|
this.flash("删除失败", false);
|
|
}
|
|
} catch (e) {
|
|
this.flash("删除失败:" + e.message, false);
|
|
}
|
|
},
|
|
// ===== 链接复制 =====
|
|
async copyLink(f) {
|
|
const url = this.downloadUrl(f);
|
|
try {
|
|
await navigator.clipboard.writeText(url);
|
|
this.flash("下载链接已复制", true);
|
|
} catch (e) {
|
|
const ta = document.createElement("textarea");
|
|
ta.value = url;
|
|
document.body.appendChild(ta);
|
|
ta.select();
|
|
document.execCommand("copy");
|
|
ta.remove();
|
|
this.flash("下载链接已复制", true);
|
|
}
|
|
},
|
|
// ===== 修改密码 =====
|
|
openPwdModal() {
|
|
this.oldPwd = this.newPwd = this.newPwd2 = "";
|
|
this.pwdMsg = "";
|
|
this.showPwdModal = true;
|
|
},
|
|
closePwdModal() {
|
|
this.showPwdModal = false;
|
|
},
|
|
async changePwd() {
|
|
this.pwdMsg = "";
|
|
if (this.newPwd !== this.newPwd2) {
|
|
this.pwdMsg = "两次输入的新密码不一致";
|
|
return;
|
|
}
|
|
if (this.newPwd.length < 6) {
|
|
this.pwdMsg = "新密码至少 6 位";
|
|
return;
|
|
}
|
|
this.savingPwd = true;
|
|
try {
|
|
const r = await fetch(`${API_BASE}/api/admin/change-password`, {
|
|
method: "POST",
|
|
headers: Object.assign({ "Content-Type": "application/json" }, this.authHeaders()),
|
|
body: JSON.stringify({ old_password: this.oldPwd, new_password: this.newPwd }),
|
|
});
|
|
const j = await r.json().catch(() => ({}));
|
|
if (!r.ok) {
|
|
this.pwdMsg = j.detail || ("修改失败 HTTP " + r.status);
|
|
return;
|
|
}
|
|
this.showPwdModal = false;
|
|
this.logout();
|
|
this.loginMsg = "密码已修改,请用新密码重新登录";
|
|
} catch (e) {
|
|
this.pwdMsg = "网络错误:" + e.message;
|
|
} finally {
|
|
this.savingPwd = false;
|
|
}
|
|
},
|
|
// ===== 工具 =====
|
|
downloadUrl(f) {
|
|
return `${API_BASE}/files/${f.fid}?dl=${encodeURIComponent(f.filename || f.fid)}`;
|
|
},
|
|
extOf(name) {
|
|
const m = /\.([A-Za-z0-9]+)$/.exec(name || "");
|
|
return m ? m[1].toUpperCase().slice(0, 6) : "FILE";
|
|
},
|
|
whoLabel(who) {
|
|
if (!who) return "-";
|
|
return who === "center" ? "中心机" : who;
|
|
},
|
|
fmtSize(bytes) {
|
|
const b = Number(bytes) || 0;
|
|
if (b < 1024) return b + " B";
|
|
if (b < 1024 * 1024) return (b / 1024).toFixed(1) + " KB";
|
|
if (b < 1024 * 1024 * 1024) return (b / 1024 / 1024).toFixed(1) + " MB";
|
|
return (b / 1024 / 1024 / 1024).toFixed(2) + " GB";
|
|
},
|
|
fmtTime(ts) {
|
|
if (!ts) return "-";
|
|
const d = new Date(Number(ts) * 1000);
|
|
const p = (n) => String(n).padStart(2, "0");
|
|
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`;
|
|
},
|
|
shortSha(s) {
|
|
return (s || "").slice(0, 12) + "…";
|
|
},
|
|
},
|
|
mounted() {
|
|
if (this.token) this.load();
|
|
},
|
|
}).mount("#app");
|