You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.7 KiB
JavaScript
130 lines
3.7 KiB
JavaScript
(function () {
|
|
"use strict";
|
|
|
|
function findMessage(form) {
|
|
return form ? form.querySelector("[data-auth-message]") : null;
|
|
}
|
|
|
|
function copy(key, parameters) {
|
|
return window.VenixUserCopy.text(key, parameters || {});
|
|
}
|
|
|
|
function showMessage(form, type, text) {
|
|
var message = findMessage(form);
|
|
if (!message) return;
|
|
message.className = "auth-message is-visible " + (type || "success");
|
|
var fallback = copy(type === "success"
|
|
? "auth.message.completed" : "auth.message.request_failed");
|
|
var safeText = typeof text === "string" ? text.trim() : "";
|
|
message.textContent = safeText || fallback;
|
|
}
|
|
|
|
function errorMessage(error, fallbackKey) {
|
|
var key = error && typeof error.copyKey === "string"
|
|
? error.copyKey : (fallbackKey || "auth.message.request_failed");
|
|
var parameters = error && error.copyParameters
|
|
&& typeof error.copyParameters === "object"
|
|
? error.copyParameters : {};
|
|
return copy(key, parameters);
|
|
}
|
|
|
|
function setLoading(button, loadingText) {
|
|
if (!button) return function () {};
|
|
|
|
var original = button.textContent;
|
|
button.disabled = true;
|
|
if (loadingText) button.textContent = loadingText;
|
|
|
|
return function () {
|
|
button.disabled = false;
|
|
button.textContent = original;
|
|
};
|
|
}
|
|
|
|
function post(url, data) {
|
|
return fetch(url, {
|
|
method: "POST",
|
|
credentials: "same-origin",
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
|
"X-Requested-With": "XMLHttpRequest"
|
|
},
|
|
body: new URLSearchParams(data)
|
|
}).then(function (response) {
|
|
return response.text().then(function (body) {
|
|
var payload = null;
|
|
if (body) {
|
|
try {
|
|
payload = JSON.parse(body);
|
|
} catch (ignore) {
|
|
payload = null;
|
|
}
|
|
}
|
|
|
|
if (!response.ok) {
|
|
var error = new Error();
|
|
error.status = response.status;
|
|
error.copyKey = "auth.message.request_http_failed";
|
|
error.copyParameters = { status: response.status };
|
|
throw error;
|
|
}
|
|
|
|
if (!payload || typeof payload !== "object") {
|
|
var formatError = new Error();
|
|
formatError.status = response.status;
|
|
formatError.copyKey = "auth.message.response_invalid";
|
|
throw formatError;
|
|
}
|
|
return payload;
|
|
});
|
|
});
|
|
}
|
|
|
|
function refreshCaptcha(selector) {
|
|
var img = document.querySelector(selector || "[data-captcha-img]");
|
|
if (img) {
|
|
img.src = "/api/captch?t=" + Math.random();
|
|
}
|
|
}
|
|
|
|
function blockWeixin() {
|
|
var ua = navigator.userAgent.toLowerCase();
|
|
if (ua.indexOf("micromessenger") !== -1) {
|
|
var notice = document.createElement("div");
|
|
notice.style.cssText = "padding:24px;font-size:16px;line-height:1.7";
|
|
notice.textContent = copy("auth.message.weixin_blocked");
|
|
document.body.replaceChildren(notice);
|
|
}
|
|
}
|
|
|
|
function countdown(button, seconds, doneText) {
|
|
var left = parseInt(seconds, 10);
|
|
if (!button) return;
|
|
if (Number.isNaN(left) || left <= 0) left = 60;
|
|
|
|
button.disabled = true;
|
|
button.textContent = left + "s";
|
|
|
|
var timer = setInterval(function () {
|
|
left -= 1;
|
|
if (left <= 0) {
|
|
clearInterval(timer);
|
|
button.disabled = false;
|
|
button.textContent = doneText || copy("auth.action.send_code");
|
|
return;
|
|
}
|
|
button.textContent = left + "s";
|
|
}, 1000);
|
|
}
|
|
|
|
window.VenixAuth = {
|
|
post: post,
|
|
showMessage: showMessage,
|
|
setLoading: setLoading,
|
|
refreshCaptcha: refreshCaptcha,
|
|
countdown: countdown,
|
|
blockWeixin: blockWeixin,
|
|
errorMessage: errorMessage
|
|
};
|
|
})();
|