chore: ui js refactor

This commit is contained in:
sigoden 2023-06-01 22:22:36 +08:00
parent d22be95dda
commit 577eea5fa4

View file

@ -218,7 +218,7 @@ Uploader.runQueue = async () => {
let uploader = Uploader.queues.shift(); let uploader = Uploader.queues.shift();
if (!Uploader.auth) { if (!Uploader.auth) {
Uploader.auth = true; Uploader.auth = true;
const success = await login(true); const success = await checkAuth(true);
Uploader.auth = !!success; Uploader.auth = !!success;
} }
uploader.ajax(); uploader.ajax();
@ -439,7 +439,7 @@ function setupAuth() {
} else { } else {
const $loginBtn = document.querySelector(".login-btn"); const $loginBtn = document.querySelector(".login-btn");
$loginBtn.classList.remove("hidden"); $loginBtn.classList.remove("hidden");
$loginBtn.addEventListener("click", () => login(true)); $loginBtn.addEventListener("click", () => checkAuth(true));
} }
} }
@ -573,7 +573,7 @@ async function deletePath(index) {
async function doDeletePath(name, url, cb) { async function doDeletePath(name, url, cb) {
if (!confirm(`Delete \`${name}\`?`)) return; if (!confirm(`Delete \`${name}\`?`)) return;
try { try {
await login(); await checkAuth();
const res = await fetch(url, { const res = await fetch(url, {
method: "DELETE", method: "DELETE",
}); });
@ -613,7 +613,7 @@ async function doMovePath(fileUrl) {
const newFileUrl = fileUrlObj.origin + prefix + newPath.split("/").map(encodeURIComponent).join("/"); const newFileUrl = fileUrlObj.origin + prefix + newPath.split("/").map(encodeURIComponent).join("/");
try { try {
await login(); await checkAuth();
const res1 = await fetch(newFileUrl, { const res1 = await fetch(newFileUrl, {
method: "HEAD", method: "HEAD",
}); });
@ -651,7 +651,7 @@ async function saveChange() {
} }
} }
async function login(alert = false) { async function checkAuth(alert = false) {
if (!DATA.auth) return; if (!DATA.auth) return;
try { try {
const res = await fetch(baseUrl(), { const res = await fetch(baseUrl(), {
@ -663,7 +663,7 @@ async function login(alert = false) {
$userBtn.title = ""; $userBtn.title = "";
return true; return true;
} catch (err) { } catch (err) {
let message = `Cannot login, ${err.message}`; let message = `Check auth, ${err.message}`;
if (alert) { if (alert) {
alert(message); alert(message);
} else { } else {
@ -679,7 +679,7 @@ async function login(alert = false) {
async function createFolder(name) { async function createFolder(name) {
const url = newUrl(name); const url = newUrl(name);
try { try {
await login(); await checkAuth();
const res = await fetch(url, { const res = await fetch(url, {
method: "MKCOL", method: "MKCOL",
}); });
@ -693,7 +693,7 @@ async function createFolder(name) {
async function createFile(name) { async function createFile(name) {
const url = newUrl(name); const url = newUrl(name);
try { try {
await login(); await checkAuth();
const res = await fetch(url, { const res = await fetch(url, {
method: "PUT", method: "PUT",
body: "", body: "",