@@ -0,0 +1,264 @@
|
||||
(function () {
|
||||
var QUEUE_VERSION = "2";
|
||||
var DISMISSED_KEY = "dialogAdDismissed";
|
||||
var PROGRESS_KEY = "dialogAdProgress";
|
||||
var mask = document.querySelector("[data-dialog-ad]");
|
||||
var previousBodyOverflow = "";
|
||||
var hasDisplayedItem = false;
|
||||
if (!mask) return;
|
||||
|
||||
var items = Array.prototype.slice.call(mask.querySelectorAll("[data-dialog-ad-item]"));
|
||||
if (!items.length) return;
|
||||
|
||||
var queueSignature =
|
||||
QUEUE_VERSION +
|
||||
"|" +
|
||||
items
|
||||
.map(function (item, index) {
|
||||
var image = item.querySelector(".dialog-ad-image");
|
||||
return (
|
||||
item.getAttribute("data-dialog-ad-id") ||
|
||||
(image && image.getAttribute("data-dialog-ad-src")) ||
|
||||
String(index)
|
||||
);
|
||||
})
|
||||
.join("|");
|
||||
|
||||
function readSession(key) {
|
||||
try {
|
||||
return window.sessionStorage.getItem(key) || "";
|
||||
} catch (error) {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function writeSession(key, value) {
|
||||
try {
|
||||
window.sessionStorage.setItem(key, value);
|
||||
} catch (error) {
|
||||
// sessionStorage 被禁用时仍允许本页依次展示和关闭广告。
|
||||
}
|
||||
}
|
||||
|
||||
function removeSession(key) {
|
||||
try {
|
||||
window.sessionStorage.removeItem(key);
|
||||
} catch (error) {
|
||||
// sessionStorage 被禁用时无需清理。
|
||||
}
|
||||
}
|
||||
|
||||
function isDismissed() {
|
||||
return readSession(DISMISSED_KEY) === queueSignature;
|
||||
}
|
||||
|
||||
function readProgress() {
|
||||
try {
|
||||
var progress = JSON.parse(readSession(PROGRESS_KEY));
|
||||
if (!progress || progress.signature !== queueSignature) return 0;
|
||||
var value = Number(progress.index);
|
||||
if (!Number.isFinite(value) || value < 0) return 0;
|
||||
return Math.floor(value);
|
||||
} catch (error) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function rememberProgress(index) {
|
||||
writeSession(
|
||||
PROGRESS_KEY,
|
||||
JSON.stringify({ signature: queueSignature, index: index }),
|
||||
);
|
||||
}
|
||||
|
||||
var currentIndex = readProgress();
|
||||
var displayedIndex = -1;
|
||||
var loadGeneration = 0;
|
||||
|
||||
function emitTrack(item, eventType) {
|
||||
if (!item || eventType === "close") return;
|
||||
if (window.tracker && typeof window.tracker.externalAd === "function") {
|
||||
window.tracker.externalAd(item, eventType);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
document.dispatchEvent(new CustomEvent("external-ad:track", {
|
||||
detail: { element: item, eventType: eventType },
|
||||
}));
|
||||
} catch (error) {
|
||||
// 埋点不可用时不影响广告展示与关闭。
|
||||
}
|
||||
}
|
||||
|
||||
function hideAllItems() {
|
||||
items.forEach(function (item) {
|
||||
item.hidden = true;
|
||||
item.setAttribute("hidden", "");
|
||||
});
|
||||
}
|
||||
|
||||
function setActiveItem(index) {
|
||||
hideAllItems();
|
||||
var item = items[index];
|
||||
if (!item) return false;
|
||||
item.hidden = false;
|
||||
item.removeAttribute("hidden");
|
||||
return true;
|
||||
}
|
||||
|
||||
function openMask() {
|
||||
if (isDismissed()) return;
|
||||
if (!mask.classList.contains("is-open")) {
|
||||
previousBodyOverflow = document.body.style.overflow;
|
||||
mask.hidden = false;
|
||||
mask.removeAttribute("hidden");
|
||||
mask.classList.add("is-open");
|
||||
document.body.style.overflow = "hidden";
|
||||
}
|
||||
var item = displayedIndex >= 0 ? items[displayedIndex] : null;
|
||||
if (item && !item.__dialogAdShowTracked) {
|
||||
item.__dialogAdShowTracked = true;
|
||||
emitTrack(item, "show");
|
||||
}
|
||||
}
|
||||
|
||||
function finishQueue() {
|
||||
loadGeneration += 1;
|
||||
displayedIndex = -1;
|
||||
if (hasDisplayedItem) {
|
||||
writeSession(DISMISSED_KEY, queueSignature);
|
||||
} else {
|
||||
removeSession(DISMISSED_KEY);
|
||||
}
|
||||
removeSession(PROGRESS_KEY);
|
||||
hideAllItems();
|
||||
mask.classList.remove("is-open");
|
||||
mask.hidden = true;
|
||||
mask.setAttribute("hidden", "");
|
||||
document.body.style.overflow = previousBodyOverflow;
|
||||
}
|
||||
|
||||
function showWhenAgeGateAllows() {
|
||||
if (isDismissed()) return;
|
||||
var ageGate = document.querySelector("[data-eighteen]");
|
||||
if (ageGate && ageGate.classList.contains("is-open")) {
|
||||
document.addEventListener("eighteen:closed", openMask, { once: true });
|
||||
return;
|
||||
}
|
||||
openMask();
|
||||
}
|
||||
|
||||
function waitForImage(image) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (image.complete) {
|
||||
if (image.naturalWidth > 0) resolve();
|
||||
else reject(new Error("image decode failed"));
|
||||
return;
|
||||
}
|
||||
|
||||
var timeout = window.setTimeout(function () {
|
||||
cleanup();
|
||||
reject(new Error("image load timeout"));
|
||||
}, 12000);
|
||||
|
||||
function cleanup() {
|
||||
window.clearTimeout(timeout);
|
||||
image.removeEventListener("load", handleLoad);
|
||||
image.removeEventListener("error", handleError);
|
||||
}
|
||||
|
||||
function handleLoad() {
|
||||
cleanup();
|
||||
if (image.naturalWidth > 0) resolve();
|
||||
else reject(new Error("image decode failed"));
|
||||
}
|
||||
|
||||
function handleError() {
|
||||
cleanup();
|
||||
reject(new Error("image load failed"));
|
||||
}
|
||||
|
||||
image.addEventListener("load", handleLoad, { once: true });
|
||||
image.addEventListener("error", handleError, { once: true });
|
||||
});
|
||||
}
|
||||
|
||||
function loadDialogImage(image) {
|
||||
var source = image.getAttribute("data-dialog-ad-src") || "";
|
||||
if (!source) return Promise.reject(new Error("image source is empty"));
|
||||
|
||||
if (typeof window.processImage === "function") {
|
||||
return window.processImage(source, image).then(function (result) {
|
||||
if (!result) throw new Error("image decrypt failed");
|
||||
return waitForImage(image);
|
||||
});
|
||||
}
|
||||
|
||||
image.src = source;
|
||||
return waitForImage(image);
|
||||
}
|
||||
|
||||
function showItem(index) {
|
||||
if (index >= items.length) {
|
||||
finishQueue();
|
||||
return;
|
||||
}
|
||||
|
||||
currentIndex = index;
|
||||
displayedIndex = -1;
|
||||
var generation = ++loadGeneration;
|
||||
rememberProgress(currentIndex);
|
||||
hideAllItems();
|
||||
var item = items[index];
|
||||
var image = item.querySelector(".dialog-ad-image");
|
||||
|
||||
function displayItem() {
|
||||
if (loadGeneration !== generation || currentIndex !== index) return;
|
||||
if (!setActiveItem(index)) return;
|
||||
displayedIndex = index;
|
||||
hasDisplayedItem = true;
|
||||
showWhenAgeGateAllows();
|
||||
}
|
||||
|
||||
function skipBrokenItem() {
|
||||
if (loadGeneration !== generation || currentIndex !== index) return;
|
||||
showItem(index + 1);
|
||||
}
|
||||
|
||||
if (!image) return skipBrokenItem();
|
||||
loadDialogImage(image).then(displayItem).catch(skipBrokenItem);
|
||||
}
|
||||
|
||||
function showNextItem() {
|
||||
var index = displayedIndex >= 0 ? displayedIndex : currentIndex;
|
||||
showItem(index + 1);
|
||||
}
|
||||
|
||||
mask.addEventListener("click", function (event) {
|
||||
var item = event.target.closest("[data-dialog-ad-item]");
|
||||
if (!item || item.hidden || !mask.contains(item)) return;
|
||||
var index = items.indexOf(item);
|
||||
if (index < 0 || index !== displayedIndex) return;
|
||||
if (event.target.closest("[data-dialog-ad-link]")) {
|
||||
emitTrack(item, "click");
|
||||
}
|
||||
if (event.target.closest("[data-dialog-ad-close]")) {
|
||||
emitTrack(item, "close");
|
||||
showItem(index + 1);
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener("keydown", function (event) {
|
||||
if (event.key === "Escape" && mask.classList.contains("is-open") && displayedIndex >= 0) {
|
||||
emitTrack(items[displayedIndex], "close");
|
||||
showNextItem();
|
||||
}
|
||||
});
|
||||
|
||||
if (isDismissed()) return;
|
||||
if (currentIndex >= items.length) {
|
||||
finishQueue();
|
||||
return;
|
||||
}
|
||||
showItem(currentIndex);
|
||||
})();
|
||||
@@ -0,0 +1,36 @@
|
||||
(function () {
|
||||
var root = document.querySelector("[data-download-pop]");
|
||||
if (!root) return;
|
||||
|
||||
var openers = document.querySelectorAll("[data-download-open]");
|
||||
var closers = root.querySelectorAll("[data-download-close]");
|
||||
var previousOverflow = "";
|
||||
|
||||
function open(event) {
|
||||
if (event) {
|
||||
event.preventDefault();
|
||||
}
|
||||
root.classList.add("is-open");
|
||||
previousOverflow = document.body.style.overflow;
|
||||
document.body.style.overflow = "hidden";
|
||||
}
|
||||
|
||||
function close() {
|
||||
root.classList.remove("is-open");
|
||||
document.body.style.overflow = previousOverflow;
|
||||
}
|
||||
|
||||
Array.prototype.forEach.call(openers, function (el) {
|
||||
el.addEventListener("click", open);
|
||||
});
|
||||
|
||||
Array.prototype.forEach.call(closers, function (el) {
|
||||
el.addEventListener("click", close);
|
||||
});
|
||||
|
||||
document.addEventListener("keydown", function (event) {
|
||||
if (event.key === "Escape" && root.classList.contains("is-open")) {
|
||||
close();
|
||||
}
|
||||
});
|
||||
})();
|
||||
@@ -0,0 +1,313 @@
|
||||
(function () {
|
||||
var form = document.querySelector("[data-feedback-form]");
|
||||
if (!form) return;
|
||||
|
||||
var types = form.querySelectorAll(".feedback-type");
|
||||
var content = form.querySelector("[data-feedback-content]");
|
||||
var count = form.querySelector("[data-feedback-count]");
|
||||
var uploads = form.querySelector("[data-feedback-uploads]");
|
||||
var fileInput = form.querySelector("[data-feedback-file]");
|
||||
var submitBtn = form.querySelector(".feedback-form__submit");
|
||||
var files = [];
|
||||
var maxFiles = 3;
|
||||
var submitting = false;
|
||||
var ALLOWED_MIME = {
|
||||
"image/jpeg": true,
|
||||
"image/jpg": true,
|
||||
"image/pjpeg": true,
|
||||
"image/png": true,
|
||||
"image/gif": true,
|
||||
};
|
||||
var ALLOWED_EXT = {
|
||||
jpg: true,
|
||||
jpeg: true,
|
||||
png: true,
|
||||
gif: true,
|
||||
};
|
||||
var TYPE_MAP = {
|
||||
play: 2, // 播放问题
|
||||
account: 3, // 账号问题
|
||||
report: 4, // 内容举报 → 其他
|
||||
suggest: 1, // 功能建议
|
||||
other: 4, // 其他
|
||||
};
|
||||
|
||||
function toast(message, type) {
|
||||
if (typeof window.showToast === "function") {
|
||||
window.showToast(message, type);
|
||||
return;
|
||||
}
|
||||
if (window.AuthAPI && typeof window.AuthAPI.toast === "function") {
|
||||
window.AuthAPI.toast(message, type || "info");
|
||||
return;
|
||||
}
|
||||
window.alert(message);
|
||||
}
|
||||
|
||||
Array.prototype.forEach.call(types, function (type) {
|
||||
var input = type.querySelector('input[type="radio"]');
|
||||
if (!input) return;
|
||||
input.addEventListener("change", function () {
|
||||
Array.prototype.forEach.call(types, function (item) {
|
||||
item.classList.toggle("is-active", item.querySelector("input").checked);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
if (content && count) {
|
||||
content.addEventListener("input", function () {
|
||||
count.textContent = String(Array.from(content.value || "").length);
|
||||
});
|
||||
}
|
||||
|
||||
function isAllowedImage(file) {
|
||||
if (!file) return false;
|
||||
var mime = String(file.type || "").toLowerCase();
|
||||
if (mime && ALLOWED_MIME[mime]) return true;
|
||||
var name = String(file.name || "");
|
||||
var dot = name.lastIndexOf(".");
|
||||
if (dot < 0) return false;
|
||||
var ext = name.slice(dot + 1).toLowerCase();
|
||||
return !!ALLOWED_EXT[ext];
|
||||
}
|
||||
|
||||
function renderUploads() {
|
||||
if (!uploads || !fileInput) return;
|
||||
uploads.innerHTML = "";
|
||||
|
||||
files.forEach(function (file, index) {
|
||||
var item = document.createElement("div");
|
||||
item.className = "feedback-upload feedback-upload--preview";
|
||||
if (file.uploading) item.classList.add("is-uploading");
|
||||
if (file.error) item.classList.add("is-error");
|
||||
var img = document.createElement("img");
|
||||
img.src = "/static/images/placeholder.png?v=2";
|
||||
img.setAttribute("data-src", file.previewUrl);
|
||||
img.alt = file.name || "截图";
|
||||
img.loading = "lazy";
|
||||
img.decoding = "async";
|
||||
var remove = document.createElement("button");
|
||||
remove.type = "button";
|
||||
remove.className = "feedback-upload__remove";
|
||||
remove.setAttribute("aria-label", "删除截图");
|
||||
remove.innerHTML = '<img class="icon" src="/static/images/placeholder.png?v=2" data-src="/static/images/close.svg" alt="" loading="lazy" decoding="async">';
|
||||
remove.addEventListener("click", function () {
|
||||
if (file.previewUrl) URL.revokeObjectURL(file.previewUrl);
|
||||
files.splice(index, 1);
|
||||
renderUploads();
|
||||
});
|
||||
item.appendChild(img);
|
||||
item.appendChild(remove);
|
||||
uploads.appendChild(item);
|
||||
});
|
||||
|
||||
if (files.length < maxFiles) {
|
||||
var label = document.createElement("label");
|
||||
label.className = "feedback-upload feedback-upload--add";
|
||||
label.appendChild(fileInput);
|
||||
fileInput.hidden = true;
|
||||
var icon = document.createElement("img");
|
||||
icon.className = "icon";
|
||||
icon.src = "/static/images/placeholder.png?v=2";
|
||||
icon.setAttribute("data-src", "/static/images/plus-dim.svg");
|
||||
icon.alt = "";
|
||||
icon.loading = "lazy";
|
||||
icon.decoding = "async";
|
||||
label.appendChild(icon);
|
||||
var text = document.createElement("span");
|
||||
text.textContent = "上传截图";
|
||||
label.appendChild(text);
|
||||
uploads.appendChild(label);
|
||||
} else {
|
||||
uploads.appendChild(fileInput);
|
||||
fileInput.hidden = true;
|
||||
}
|
||||
|
||||
if (typeof window.initLazyImages === "function") {
|
||||
window.initLazyImages(uploads);
|
||||
}
|
||||
}
|
||||
|
||||
function extractUploadURL(data) {
|
||||
if (!data) return "";
|
||||
if (typeof data === "string") return data;
|
||||
return String(data.sourceNameFs || data.url || data.path || data.id || "").trim();
|
||||
}
|
||||
|
||||
function uploadOne(fileItem) {
|
||||
if (!window.HttpRequest || typeof window.HttpRequest.post !== "function") {
|
||||
return Promise.reject(new Error("上传服务未就绪"));
|
||||
}
|
||||
if (fileItem.remoteURL) return Promise.resolve(fileItem.remoteURL);
|
||||
fileItem.uploading = true;
|
||||
renderUploads();
|
||||
var fd = new FormData();
|
||||
fd.append("file", fileItem.file, fileItem.name || "image.jpg");
|
||||
return window.HttpRequest.post("/api/upload/image", fd)
|
||||
.then(function (res) {
|
||||
if (!res || Number(res.code) !== 200 || !res.data) {
|
||||
throw new Error((res && (res.tip || res.msg)) || "上传失败");
|
||||
}
|
||||
var url = extractUploadURL(res.data);
|
||||
if (!url) throw new Error("上传成功但未返回图片地址");
|
||||
fileItem.remoteURL = url;
|
||||
fileItem.uploading = false;
|
||||
fileItem.error = false;
|
||||
renderUploads();
|
||||
return url;
|
||||
})
|
||||
.catch(function (err) {
|
||||
fileItem.uploading = false;
|
||||
fileItem.error = true;
|
||||
renderUploads();
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
function uploadAll() {
|
||||
var chain = Promise.resolve([]);
|
||||
files.forEach(function (item) {
|
||||
chain = chain.then(function (urls) {
|
||||
return uploadOne(item).then(function (url) {
|
||||
urls.push(url);
|
||||
return urls;
|
||||
});
|
||||
});
|
||||
});
|
||||
return chain;
|
||||
}
|
||||
|
||||
function selectedType() {
|
||||
var checked = form.querySelector('input[name="type"]:checked');
|
||||
var key = checked ? String(checked.value || "") : "play";
|
||||
return TYPE_MAP[key] || 2;
|
||||
}
|
||||
|
||||
function apiTip(payload, fallback) {
|
||||
if (!payload) return fallback;
|
||||
if (payload.tip || payload.msg) return String(payload.tip || payload.msg);
|
||||
if (payload.data && (payload.data.tip || payload.data.msg)) {
|
||||
return String(payload.data.tip || payload.data.msg);
|
||||
}
|
||||
if (payload.message) return String(payload.message);
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function setBusy(on) {
|
||||
submitting = !!on;
|
||||
if (submitBtn) {
|
||||
submitBtn.disabled = !!on;
|
||||
submitBtn.classList.toggle("is-loading", !!on);
|
||||
if (on) {
|
||||
if (!submitBtn.dataset.labelHtml) {
|
||||
submitBtn.dataset.labelHtml = submitBtn.innerHTML;
|
||||
}
|
||||
submitBtn.textContent = "提交中…";
|
||||
} else if (submitBtn.dataset.labelHtml) {
|
||||
submitBtn.innerHTML = submitBtn.dataset.labelHtml;
|
||||
}
|
||||
}
|
||||
if (content) content.disabled = !!on;
|
||||
if (fileInput) fileInput.disabled = !!on;
|
||||
Array.prototype.forEach.call(form.querySelectorAll("input, textarea, button"), function (el) {
|
||||
if (el === submitBtn) return;
|
||||
if (el === fileInput) return;
|
||||
if (el === content) return;
|
||||
if (el.type === "file") return;
|
||||
el.disabled = !!on;
|
||||
});
|
||||
}
|
||||
|
||||
if (fileInput) {
|
||||
fileInput.addEventListener("change", function () {
|
||||
var selected = Array.prototype.slice.call(fileInput.files || []);
|
||||
var rejectedFormat = false;
|
||||
var rejectedFull = false;
|
||||
selected.forEach(function (file) {
|
||||
if (files.length >= maxFiles) {
|
||||
rejectedFull = true;
|
||||
return;
|
||||
}
|
||||
if (!isAllowedImage(file)) {
|
||||
rejectedFormat = true;
|
||||
return;
|
||||
}
|
||||
files.push({
|
||||
name: file.name,
|
||||
file: file,
|
||||
previewUrl: URL.createObjectURL(file),
|
||||
remoteURL: "",
|
||||
uploading: false,
|
||||
error: false,
|
||||
});
|
||||
});
|
||||
fileInput.value = "";
|
||||
renderUploads();
|
||||
if (rejectedFormat) toast("仅支持 jpg / png / gif 格式", "error");
|
||||
else if (rejectedFull) toast("最多上传 3 张图片", "error");
|
||||
});
|
||||
}
|
||||
|
||||
renderUploads();
|
||||
|
||||
form.addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
if (submitting) return;
|
||||
if (!window.HttpRequest || typeof window.HttpRequest.post !== "function") {
|
||||
toast("反馈服务未就绪", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
var text = String((content && content.value) || "").trim();
|
||||
if (!text) {
|
||||
toast("请填写问题描述", "error");
|
||||
return;
|
||||
}
|
||||
if (Array.from(text).length > 500) {
|
||||
toast("问题描述最多 500 字", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
var contactInput = form.querySelector('input[name="contact"]');
|
||||
var contact = contactInput ? String(contactInput.value || "").trim() : "";
|
||||
var type = selectedType();
|
||||
|
||||
setBusy(true);
|
||||
uploadAll()
|
||||
.then(function (screenshots) {
|
||||
var payload = {
|
||||
type: type,
|
||||
content: text,
|
||||
contact: contact,
|
||||
screenshots: screenshots || [],
|
||||
};
|
||||
return window.HttpRequest.post("/api/feedback/add", payload);
|
||||
})
|
||||
.then(function (res) {
|
||||
if (!res || Number(res.code) !== 200) {
|
||||
var failTip = apiTip(res, "提交失败");
|
||||
var failErr = new Error(failTip);
|
||||
failErr.data = res;
|
||||
throw failErr;
|
||||
}
|
||||
toast("反馈提交成功", "success");
|
||||
form.reset();
|
||||
files.forEach(function (f) {
|
||||
if (f.previewUrl) URL.revokeObjectURL(f.previewUrl);
|
||||
});
|
||||
files = [];
|
||||
Array.prototype.forEach.call(types, function (item) {
|
||||
var input = item.querySelector("input");
|
||||
item.classList.toggle("is-active", !!(input && input.checked));
|
||||
});
|
||||
if (count) count.textContent = "0";
|
||||
renderUploads();
|
||||
})
|
||||
.catch(function (err) {
|
||||
toast(apiTip(err, "提交失败,请稍后重试"), "error");
|
||||
})
|
||||
.finally(function () {
|
||||
setBusy(false);
|
||||
});
|
||||
});
|
||||
})();
|
||||
@@ -0,0 +1,15 @@
|
||||
(function () {
|
||||
var root = document.querySelector("[data-help-faq]");
|
||||
if (!root) return;
|
||||
|
||||
var items = root.querySelectorAll(".help-faq__item");
|
||||
|
||||
Array.prototype.forEach.call(items, function (item) {
|
||||
item.addEventListener("toggle", function () {
|
||||
if (!item.open) return;
|
||||
Array.prototype.forEach.call(items, function (other) {
|
||||
if (other !== item) other.open = false;
|
||||
});
|
||||
});
|
||||
});
|
||||
})();
|
||||
@@ -0,0 +1,106 @@
|
||||
(function () {
|
||||
var toc = document.querySelector(".legal-toc");
|
||||
if (!toc) return;
|
||||
|
||||
var layout = document.querySelector(".legal-layout");
|
||||
var list = toc.querySelector(".legal-toc__list");
|
||||
var items = Array.prototype.slice.call(toc.querySelectorAll(".legal-toc__item"));
|
||||
var activeId = "";
|
||||
var TOP = 88;
|
||||
var desktopMq = window.matchMedia("(min-width: 769px)");
|
||||
var slot = null;
|
||||
var pinnedHeight = 0;
|
||||
|
||||
function ensureSlot() {
|
||||
if (slot || !layout || !toc.parentNode) return;
|
||||
if (toc.parentNode.classList && toc.parentNode.classList.contains("legal-toc-slot")) {
|
||||
slot = toc.parentNode;
|
||||
return;
|
||||
}
|
||||
slot = document.createElement("div");
|
||||
slot.className = "legal-toc-slot";
|
||||
toc.parentNode.insertBefore(slot, toc);
|
||||
slot.appendChild(toc);
|
||||
}
|
||||
|
||||
function clearPin() {
|
||||
toc.classList.remove("is-fixed");
|
||||
toc.style.top = "";
|
||||
toc.style.left = "";
|
||||
toc.style.width = "";
|
||||
if (slot) slot.style.height = "";
|
||||
}
|
||||
|
||||
function updatePin() {
|
||||
if (!layout || !desktopMq.matches) {
|
||||
clearPin();
|
||||
return;
|
||||
}
|
||||
ensureSlot();
|
||||
if (!slot) return;
|
||||
|
||||
if (!toc.classList.contains("is-fixed")) {
|
||||
pinnedHeight = toc.offsetHeight;
|
||||
}
|
||||
|
||||
var layoutRect = layout.getBoundingClientRect();
|
||||
var slotRect = slot.getBoundingClientRect();
|
||||
var width = slot.offsetWidth;
|
||||
|
||||
if (slotRect.top >= TOP) {
|
||||
clearPin();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!pinnedHeight) pinnedHeight = toc.offsetHeight;
|
||||
slot.style.height = pinnedHeight + "px";
|
||||
|
||||
var top = Math.min(TOP, layoutRect.bottom - pinnedHeight);
|
||||
toc.classList.add("is-fixed");
|
||||
toc.style.top = top + "px";
|
||||
toc.style.left = slotRect.left + "px";
|
||||
toc.style.width = width + "px";
|
||||
}
|
||||
|
||||
ensureSlot();
|
||||
updatePin();
|
||||
window.addEventListener("scroll", updatePin, { passive: true });
|
||||
window.addEventListener("resize", function () {
|
||||
clearPin();
|
||||
updatePin();
|
||||
});
|
||||
if (typeof desktopMq.addEventListener === "function") {
|
||||
desktopMq.addEventListener("change", function () {
|
||||
clearPin();
|
||||
updatePin();
|
||||
});
|
||||
} else if (typeof desktopMq.addListener === "function") {
|
||||
desktopMq.addListener(function () {
|
||||
clearPin();
|
||||
updatePin();
|
||||
});
|
||||
}
|
||||
|
||||
function centerChip(item) {
|
||||
if (!list || list.scrollWidth <= list.clientWidth) return;
|
||||
var listRect = list.getBoundingClientRect();
|
||||
var itemRect = item.getBoundingClientRect();
|
||||
var left = list.scrollLeft + itemRect.left - listRect.left - (list.clientWidth - item.clientWidth) / 2;
|
||||
list.scrollTo({ left: Math.max(0, left), behavior: "smooth" });
|
||||
}
|
||||
|
||||
function setActive(item) {
|
||||
if (!item) return;
|
||||
var id = (item.getAttribute("href") || "").slice(1);
|
||||
if (id === activeId) return;
|
||||
activeId = id;
|
||||
items.forEach(function (link) {
|
||||
link.classList.toggle("is-active", link === item);
|
||||
});
|
||||
centerChip(item);
|
||||
}
|
||||
|
||||
items.forEach(function (item) {
|
||||
item.addEventListener("click", function () { setActive(item); });
|
||||
});
|
||||
})();
|
||||
@@ -0,0 +1,137 @@
|
||||
(function () {
|
||||
var form = document.querySelector("[data-login-page-form]");
|
||||
var password = document.querySelector("[data-login-page-password]");
|
||||
var toggle = document.querySelector("[data-login-page-toggle]");
|
||||
var toggleIcon = document.querySelector("[data-login-page-toggle-icon]");
|
||||
var submitBtn = document.querySelector("[data-login-page-submit]") || (form && form.querySelector('button[type="submit"]'));
|
||||
var root = form || document;
|
||||
|
||||
if (toggle && password && toggleIcon) {
|
||||
toggle.addEventListener("click", function () {
|
||||
var show = password.getAttribute("type") === "password";
|
||||
password.setAttribute("type", show ? "text" : "password");
|
||||
toggleIcon.setAttribute("src", show ? "/static/images/eye-off.svg" : "/static/images/eye.svg");
|
||||
toggle.setAttribute("aria-label", show ? "隐藏密码" : "显示密码");
|
||||
});
|
||||
}
|
||||
|
||||
function clearFieldError(name) {
|
||||
var field = root.querySelector('[data-field="' + name + '"]');
|
||||
var err = root.querySelector('[data-field-error="' + name + '"]');
|
||||
if (field) field.classList.remove("is-error");
|
||||
if (err) {
|
||||
err.hidden = true;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = "";
|
||||
}
|
||||
}
|
||||
|
||||
function clearAllErrors() {
|
||||
root.querySelectorAll("[data-field]").forEach(function (el) {
|
||||
el.classList.remove("is-error");
|
||||
});
|
||||
root.querySelectorAll("[data-field-error]").forEach(function (err) {
|
||||
err.hidden = true;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = "";
|
||||
});
|
||||
}
|
||||
|
||||
function setFieldError(name, message) {
|
||||
var field = root.querySelector('[data-field="' + name + '"]');
|
||||
var err = root.querySelector('[data-field-error="' + name + '"]');
|
||||
if (field) field.classList.add("is-error");
|
||||
if (err) {
|
||||
err.hidden = false;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = message || "";
|
||||
}
|
||||
}
|
||||
|
||||
function isLen4to16(value) {
|
||||
var len = String(value || "").length;
|
||||
return len >= 4 && len <= 16;
|
||||
}
|
||||
|
||||
function validateForm() {
|
||||
clearAllErrors();
|
||||
var fd = new FormData(form);
|
||||
var account = String(fd.get("account") || "").trim();
|
||||
var pwd = String(fd.get("password") || "");
|
||||
var valid = true;
|
||||
|
||||
if (!account) {
|
||||
setFieldError("account", "请输入账号");
|
||||
valid = false;
|
||||
} else if (
|
||||
!(window.AuthAPI && AuthAPI.isValidLoginAccount
|
||||
? AuthAPI.isValidLoginAccount(account)
|
||||
: isLen4to16(account) || account.indexOf("@") >= 0)
|
||||
) {
|
||||
setFieldError("account", "账号需为 4–16 位,或合法邮箱");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!pwd) {
|
||||
setFieldError("password", "请输入密码");
|
||||
valid = false;
|
||||
} else if (!isLen4to16(pwd)) {
|
||||
setFieldError("password", "密码需为 4–16 位");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!valid) return null;
|
||||
return { account: account, password: pwd };
|
||||
}
|
||||
|
||||
function mapServerError(err) {
|
||||
var tip = (err && err.message) || "登录失败,请稍后重试";
|
||||
if (/账号|账户|用户|不存在|邮箱/.test(tip) && !/密码/.test(tip)) {
|
||||
setFieldError("account", tip);
|
||||
return;
|
||||
}
|
||||
setFieldError("password", tip);
|
||||
}
|
||||
|
||||
function setSubmitting(loading) {
|
||||
if (!submitBtn) return;
|
||||
submitBtn.disabled = !!loading;
|
||||
submitBtn.classList.toggle("is-loading", !!loading);
|
||||
submitBtn.textContent = loading ? "登录中…" : "登录";
|
||||
}
|
||||
|
||||
if (form) {
|
||||
form.querySelectorAll("input").forEach(function (input) {
|
||||
input.addEventListener("input", function () {
|
||||
var name = input.getAttribute("name");
|
||||
if (name) clearFieldError(name);
|
||||
});
|
||||
});
|
||||
|
||||
form.addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
if (!window.AuthAPI || !window.HttpRequest) {
|
||||
setFieldError("password", "认证脚本未加载,请刷新页面");
|
||||
return;
|
||||
}
|
||||
if (submitBtn && submitBtn.classList.contains("is-loading")) return;
|
||||
|
||||
var payload = validateForm();
|
||||
if (!payload) return;
|
||||
|
||||
setSubmitting(true);
|
||||
AuthAPI.login(payload)
|
||||
.then(function (res) {
|
||||
AuthAPI.toast((res && res.tip) || "登录成功", "success");
|
||||
window.setTimeout(function () {
|
||||
window.location.href = "/";
|
||||
}, 400);
|
||||
})
|
||||
.catch(function (err) {
|
||||
setSubmitting(false);
|
||||
mapServerError(err);
|
||||
AuthAPI.toast((err && err.message) || "登录失败", "error");
|
||||
});
|
||||
});
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,252 @@
|
||||
(function () {
|
||||
var root = document.querySelector("[data-login-pop]");
|
||||
if (!root) return;
|
||||
|
||||
var form = root.querySelector("[data-login-form]");
|
||||
var password = root.querySelector("[data-login-password]");
|
||||
var toggle = root.querySelector("[data-login-toggle]");
|
||||
var toggleIcon = root.querySelector("[data-login-toggle-icon]");
|
||||
var title = root.querySelector("[data-login-title]");
|
||||
var description = root.querySelector("[data-login-desc]");
|
||||
var submitBtn = root.querySelector("[data-login-submit]") || (form && form.querySelector('button[type="submit"]'));
|
||||
var previousOverflow = "";
|
||||
var switchAccountMode = false;
|
||||
|
||||
function closeRegister() {
|
||||
if (typeof window.closeRegisterPop === "function") {
|
||||
window.closeRegisterPop();
|
||||
return;
|
||||
}
|
||||
var register = document.querySelector("[data-register-pop]");
|
||||
if (register) register.classList.remove("is-open");
|
||||
}
|
||||
|
||||
function closeUserMenu() {
|
||||
var menu = document.querySelector("[data-user-menu].is-open");
|
||||
if (menu) {
|
||||
menu.classList.remove("is-open");
|
||||
var t = menu.querySelector("[data-user-menu-toggle]");
|
||||
if (t) t.setAttribute("aria-expanded", "false");
|
||||
}
|
||||
}
|
||||
|
||||
function clearFieldError(name) {
|
||||
var field = root.querySelector('[data-field="' + name + '"]');
|
||||
var err = root.querySelector('[data-field-error="' + name + '"]');
|
||||
if (field) field.classList.remove("is-error");
|
||||
if (err) {
|
||||
err.hidden = true;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = "";
|
||||
}
|
||||
}
|
||||
|
||||
function clearAllErrors() {
|
||||
root.querySelectorAll("[data-field]").forEach(function (el) {
|
||||
el.classList.remove("is-error");
|
||||
});
|
||||
root.querySelectorAll("[data-field-error]").forEach(function (err) {
|
||||
err.hidden = true;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = "";
|
||||
});
|
||||
}
|
||||
|
||||
function setFieldError(name, message) {
|
||||
var field = root.querySelector('[data-field="' + name + '"]');
|
||||
var err = root.querySelector('[data-field-error="' + name + '"]');
|
||||
if (field) field.classList.add("is-error");
|
||||
if (err) {
|
||||
err.hidden = false;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = message || "";
|
||||
}
|
||||
}
|
||||
|
||||
function isLen4to16(value) {
|
||||
var len = String(value || "").length;
|
||||
return len >= 4 && len <= 16;
|
||||
}
|
||||
|
||||
function validateForm() {
|
||||
clearAllErrors();
|
||||
var fd = new FormData(form);
|
||||
var account = String(fd.get("account") || "").trim();
|
||||
var pwd = String(fd.get("password") || "");
|
||||
var valid = true;
|
||||
|
||||
if (!account) {
|
||||
setFieldError("account", "请输入账号");
|
||||
valid = false;
|
||||
} else if (
|
||||
!(window.AuthAPI && AuthAPI.isValidLoginAccount
|
||||
? AuthAPI.isValidLoginAccount(account)
|
||||
: isLen4to16(account) || account.indexOf("@") >= 0)
|
||||
) {
|
||||
setFieldError("account", "账号需为 4–16 位,或合法邮箱");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!pwd) {
|
||||
setFieldError("password", "请输入密码");
|
||||
valid = false;
|
||||
} else if (!isLen4to16(pwd)) {
|
||||
setFieldError("password", "密码需为 4–16 位");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!valid) return null;
|
||||
return { account: account, password: pwd };
|
||||
}
|
||||
|
||||
function mapServerError(err) {
|
||||
var tip = (err && err.message) || "登录失败,请稍后重试";
|
||||
if (/账号|账户|用户|不存在|邮箱/.test(tip) && !/密码/.test(tip)) {
|
||||
setFieldError("account", tip);
|
||||
return;
|
||||
}
|
||||
setFieldError("password", tip);
|
||||
}
|
||||
|
||||
function isMobile() {
|
||||
return window.matchMedia && window.matchMedia("(max-width: 768px)").matches;
|
||||
}
|
||||
|
||||
function open(input) {
|
||||
var event = input && typeof input.preventDefault === "function" ? input : null;
|
||||
var options = event ? {} : input || {};
|
||||
if (isMobile() && !options.forcePopup) {
|
||||
if (event) event.preventDefault();
|
||||
window.location.href = "/login";
|
||||
return;
|
||||
}
|
||||
if (event) event.preventDefault();
|
||||
switchAccountMode = !!options.switchAccount;
|
||||
closeRegister();
|
||||
closeUserMenu();
|
||||
clearAllErrors();
|
||||
if (options.resetForm && form) form.reset();
|
||||
if (password) password.setAttribute("type", "password");
|
||||
if (toggle) toggle.setAttribute("aria-label", "显示密码");
|
||||
if (toggleIcon) {
|
||||
toggleIcon.setAttribute("src", "/static/images/eye.svg");
|
||||
toggleIcon.setAttribute("data-src", "/static/images/eye.svg");
|
||||
}
|
||||
if (title) title.textContent = switchAccountMode ? "切换账号" : "登录黄果短剧";
|
||||
if (description) {
|
||||
description.textContent = switchAccountMode
|
||||
? "请输入其他账号和密码完成切换"
|
||||
: "登录后可收藏作品、解锁付费剧集并查看观看历史";
|
||||
}
|
||||
root.classList.add("is-open");
|
||||
previousOverflow = document.body.style.overflow;
|
||||
document.body.style.overflow = "hidden";
|
||||
var first = root.querySelector(".login-pop__input");
|
||||
if (first) {
|
||||
window.setTimeout(function () {
|
||||
first.focus();
|
||||
}, 0);
|
||||
}
|
||||
}
|
||||
|
||||
function close() {
|
||||
root.classList.remove("is-open");
|
||||
document.body.style.overflow = previousOverflow;
|
||||
clearAllErrors();
|
||||
switchAccountMode = false;
|
||||
}
|
||||
|
||||
window.openLoginPop = open;
|
||||
window.openAccountSwitchLogin = function () {
|
||||
open({ forcePopup: true, switchAccount: true, resetForm: true });
|
||||
};
|
||||
window.closeLoginPop = close;
|
||||
|
||||
document.addEventListener("click", function (event) {
|
||||
var opener = event.target.closest("[data-login-open]");
|
||||
if (opener) {
|
||||
open(event);
|
||||
return;
|
||||
}
|
||||
|
||||
// PC:拦截站内 /login 链接,改为打开弹窗;H5 放行跳转登录页
|
||||
var loginLink = event.target.closest('a[href="/login"], a[href="/login/"], a[href="#login"]');
|
||||
if (loginLink && !isMobile()) {
|
||||
open(event);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!root.classList.contains("is-open")) return;
|
||||
if (event.target.closest("[data-login-close]")) {
|
||||
close();
|
||||
}
|
||||
});
|
||||
|
||||
if (toggle && password && toggleIcon) {
|
||||
toggle.addEventListener("click", function () {
|
||||
var show = password.getAttribute("type") === "password";
|
||||
password.setAttribute("type", show ? "text" : "password");
|
||||
toggleIcon.setAttribute("src", show ? "/static/images/eye-off.svg" : "/static/images/eye.svg");
|
||||
toggle.setAttribute("aria-label", show ? "隐藏密码" : "显示密码");
|
||||
});
|
||||
}
|
||||
|
||||
function setSubmitting(loading) {
|
||||
if (!submitBtn) return;
|
||||
submitBtn.disabled = !!loading;
|
||||
submitBtn.classList.toggle("is-loading", !!loading);
|
||||
submitBtn.textContent = loading ? "登录中…" : "登录";
|
||||
}
|
||||
|
||||
if (form) {
|
||||
form.querySelectorAll("input").forEach(function (input) {
|
||||
input.addEventListener("input", function () {
|
||||
var name = input.getAttribute("name");
|
||||
if (name) clearFieldError(name);
|
||||
});
|
||||
});
|
||||
|
||||
form.addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
if (!window.AuthAPI || !window.HttpRequest) {
|
||||
setFieldError("password", "认证脚本未加载,请刷新页面");
|
||||
return;
|
||||
}
|
||||
if (submitBtn && submitBtn.classList.contains("is-loading")) return;
|
||||
|
||||
var payload = validateForm();
|
||||
if (!payload) return;
|
||||
|
||||
setSubmitting(true);
|
||||
AuthAPI.login(payload)
|
||||
.then(function (res) {
|
||||
AuthAPI.toast((res && res.tip) || "登录成功", "success");
|
||||
window.setTimeout(function () {
|
||||
close();
|
||||
window.location.reload();
|
||||
}, 400);
|
||||
})
|
||||
.catch(function (err) {
|
||||
setSubmitting(false);
|
||||
mapServerError(err);
|
||||
AuthAPI.toast((err && err.message) || "登录失败", "error");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener("keydown", function (event) {
|
||||
if (event.key === "Escape" && root.classList.contains("is-open")) {
|
||||
close();
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
var params = new URLSearchParams(window.location.search || "");
|
||||
if (params.get("auth") === "login") {
|
||||
params.delete("auth");
|
||||
var qs = params.toString();
|
||||
window.history.replaceState(null, "", window.location.pathname + (qs ? "?" + qs : "") + window.location.hash);
|
||||
open();
|
||||
}
|
||||
} catch (e) {}
|
||||
})();
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,599 @@
|
||||
(function () {
|
||||
var root = document.querySelector("[data-play-composer]");
|
||||
if (!root) return;
|
||||
|
||||
var commentsRoot = document.querySelector("[data-play-comments]") || root.closest(".play-comments");
|
||||
var input = root.querySelector("[data-comment-input]");
|
||||
var countEl = root.querySelector("[data-comment-count]");
|
||||
var sendBtn = root.querySelector("[data-comment-send]");
|
||||
var listEl = document.querySelector("[data-comment-list]");
|
||||
var totalEl = document.querySelector("[data-comment-total-label]");
|
||||
var moreBtn = document.querySelector("[data-comment-more]");
|
||||
var sending = false;
|
||||
var inlineSending = false;
|
||||
var loadingMore = false;
|
||||
var MAX_LEN = 200;
|
||||
var PAGE_SIZE = 4;
|
||||
var replyTarget = null;
|
||||
|
||||
function toast(message, type) {
|
||||
if (typeof window.showToast === "function") {
|
||||
window.showToast(message, type);
|
||||
return;
|
||||
}
|
||||
if (window.AuthAPI && typeof window.AuthAPI.toast === "function") {
|
||||
window.AuthAPI.toast(message, type || "info");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function toInt(v, fallback) {
|
||||
var n = parseInt(v, 10);
|
||||
return isNaN(n) ? fallback || 0 : n;
|
||||
}
|
||||
|
||||
function escapeHtml(s) {
|
||||
return String(s == null ? "" : s)
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """);
|
||||
}
|
||||
|
||||
function getPageSize() {
|
||||
return Math.max(
|
||||
1,
|
||||
toInt(commentsRoot && commentsRoot.getAttribute("data-comment-size"), PAGE_SIZE)
|
||||
);
|
||||
}
|
||||
|
||||
function getPage() {
|
||||
return Math.max(
|
||||
1,
|
||||
toInt(commentsRoot && commentsRoot.getAttribute("data-comment-page"), 1)
|
||||
);
|
||||
}
|
||||
|
||||
function getLoaded() {
|
||||
if (!listEl) return 0;
|
||||
return listEl.querySelectorAll(".play-comment:not(.play-comment--empty)").length;
|
||||
}
|
||||
|
||||
function setPagerState(page, loaded, total) {
|
||||
if (!commentsRoot) return;
|
||||
if (page != null) commentsRoot.setAttribute("data-comment-page", String(page));
|
||||
if (loaded != null) commentsRoot.setAttribute("data-comment-loaded", String(loaded));
|
||||
if (total != null) commentsRoot.setAttribute("data-comment-total", String(total));
|
||||
syncMoreButton(total != null ? total : toInt(commentsRoot.getAttribute("data-comment-total"), 0));
|
||||
}
|
||||
|
||||
function syncMoreButton(total) {
|
||||
if (!moreBtn) return;
|
||||
var loaded = getLoaded();
|
||||
var hasMore = total > loaded;
|
||||
if (hasMore) {
|
||||
moreBtn.hidden = false;
|
||||
moreBtn.removeAttribute("hidden");
|
||||
} else {
|
||||
moreBtn.hidden = true;
|
||||
moreBtn.setAttribute("hidden", "");
|
||||
}
|
||||
}
|
||||
|
||||
function syncCount() {
|
||||
if (!input || !countEl) return;
|
||||
var len = Array.from(input.value || "").length;
|
||||
countEl.textContent = len + " / " + MAX_LEN;
|
||||
}
|
||||
|
||||
function formatTime(raw) {
|
||||
var s = String(raw || "").trim();
|
||||
if (!s) return "刚刚";
|
||||
var d = new Date(s);
|
||||
if (isNaN(d.getTime())) return s;
|
||||
return (
|
||||
d.getFullYear() +
|
||||
"/" +
|
||||
(d.getMonth() + 1) +
|
||||
"/" +
|
||||
d.getDate() +
|
||||
" " +
|
||||
String(d.getHours()).padStart(2, "0") +
|
||||
":" +
|
||||
String(d.getMinutes()).padStart(2, "0")
|
||||
);
|
||||
}
|
||||
|
||||
function renderCommentItem(item) {
|
||||
item = item || {};
|
||||
var avatar = String(item.userAvatar || "").trim();
|
||||
var name = escapeHtml(item.userName || "匿名用户");
|
||||
var text = escapeHtml(item.text || "");
|
||||
var time = escapeHtml(formatTime(item.createdAt));
|
||||
var like = toInt(item.countLike, 0);
|
||||
var liked = Number(item.isLike) === 1;
|
||||
var avatarHtml = avatar
|
||||
? '<img class="play-comment__avatar lazy" src="/static/images/placeholder.png?v=2" data-src="' +
|
||||
escapeHtml(avatar) +
|
||||
'" alt="" loading="lazy" decoding="async">'
|
||||
: '<img class="play-comment__avatar" src="/static/images/placeholder.png?v=2" data-src="/static/images/profile/avatar.png" alt="" loading="lazy" decoding="async">';
|
||||
var children = Array.isArray(item.children) ? item.children : [];
|
||||
var replies = children
|
||||
.map(function (child) {
|
||||
var cName = escapeHtml(child.userName || "用户");
|
||||
var replyTo = child.replyUserName
|
||||
? " 回复 " + escapeHtml(child.replyUserName)
|
||||
: "";
|
||||
return (
|
||||
'<div class="play-comment__reply">' +
|
||||
cName +
|
||||
replyTo +
|
||||
":" +
|
||||
escapeHtml(child.text || "") +
|
||||
"</div>"
|
||||
);
|
||||
})
|
||||
.join("");
|
||||
var commentId = escapeHtml(item.id || "");
|
||||
var rawName = String(item.userName || "匿名用户");
|
||||
return (
|
||||
'<li class="play-comment" data-comment-id="' +
|
||||
commentId +
|
||||
'">' +
|
||||
avatarHtml +
|
||||
'<div class="play-comment__body">' +
|
||||
'<div class="play-comment__meta">' +
|
||||
'<span class="play-comment__name">' +
|
||||
name +
|
||||
"</span>" +
|
||||
'<span class="play-comment__time">' +
|
||||
time +
|
||||
"</span>" +
|
||||
"</div>" +
|
||||
'<p class="play-comment__text">' +
|
||||
text +
|
||||
"</p>" +
|
||||
replies +
|
||||
'<div class="play-comment__actions">' +
|
||||
'<button class="play-comment__action' +
|
||||
(liked ? " is-active" : "") +
|
||||
'" type="button" aria-label="点赞" data-comment-like="' +
|
||||
commentId +
|
||||
'" data-liked="' +
|
||||
(liked ? "1" : "2") +
|
||||
'" data-like-raw="' +
|
||||
escapeHtml(like) +
|
||||
'">' +
|
||||
'<img class="icon" src="/static/images/placeholder.png?v=2" data-src="/static/images/heart.svg" alt="" loading="lazy" decoding="async">' +
|
||||
'<span data-like-count>' +
|
||||
escapeHtml(like) +
|
||||
"</span>" +
|
||||
"</button>" +
|
||||
'<button class="play-comment__action" type="button" data-comment-reply data-comment-id="' +
|
||||
commentId +
|
||||
'" data-parents-id="' +
|
||||
commentId +
|
||||
'" data-reply-name="' +
|
||||
escapeHtml(rawName) +
|
||||
'">回复</button>' +
|
||||
"</div></div></li>"
|
||||
);
|
||||
}
|
||||
|
||||
function getLoadedIds() {
|
||||
var ids = Object.create(null);
|
||||
if (!listEl) return ids;
|
||||
listEl.querySelectorAll(".play-comment[data-comment-id]").forEach(function (el) {
|
||||
var id = String(el.getAttribute("data-comment-id") || "").trim();
|
||||
if (id) ids[id] = true;
|
||||
});
|
||||
return ids;
|
||||
}
|
||||
|
||||
function updateTotal(n) {
|
||||
if (!totalEl) return;
|
||||
totalEl.textContent = n > 0 ? n + " 条" : "暂无评论";
|
||||
}
|
||||
|
||||
function renderList(list, totalCount) {
|
||||
if (!listEl) return;
|
||||
list = Array.isArray(list) ? list : [];
|
||||
clearReplyTarget();
|
||||
if (!list.length) {
|
||||
listEl.innerHTML =
|
||||
'<li class="play-comment play-comment--empty"><p class="play-comment__text">暂无评论</p></li>';
|
||||
} else {
|
||||
listEl.innerHTML = list.map(renderCommentItem).join("");
|
||||
if (typeof window.initLazyImages === "function") {
|
||||
window.initLazyImages(listEl);
|
||||
}
|
||||
}
|
||||
var total = typeof totalCount === "number" ? totalCount : toInt(commentsRoot && commentsRoot.getAttribute("data-comment-total"), 0);
|
||||
if (typeof totalCount === "number") {
|
||||
updateTotal(totalCount);
|
||||
}
|
||||
setPagerState(1, list.length, total);
|
||||
}
|
||||
|
||||
// 下一页追加到现有列表底部,不替换已展示评论。
|
||||
function appendList(list, totalCount) {
|
||||
if (!listEl) return;
|
||||
list = Array.isArray(list) ? list : [];
|
||||
var total =
|
||||
typeof totalCount === "number"
|
||||
? totalCount
|
||||
: toInt(commentsRoot && commentsRoot.getAttribute("data-comment-total"), 0);
|
||||
if (typeof totalCount === "number") {
|
||||
updateTotal(totalCount);
|
||||
}
|
||||
if (!list.length) {
|
||||
setPagerState(getPage(), getLoaded(), total);
|
||||
return;
|
||||
}
|
||||
|
||||
var empty = listEl.querySelector(".play-comment--empty");
|
||||
if (empty) empty.remove();
|
||||
|
||||
var existing = getLoadedIds();
|
||||
var fresh = list.filter(function (item) {
|
||||
var id = String((item && item.id) || "").trim();
|
||||
return id && !existing[id];
|
||||
});
|
||||
if (!fresh.length) {
|
||||
setPagerState(getPage(), getLoaded(), total);
|
||||
return;
|
||||
}
|
||||
|
||||
var wrap = document.createElement("ul");
|
||||
wrap.innerHTML = fresh.map(renderCommentItem).join("");
|
||||
var nodes = [];
|
||||
while (wrap.firstChild) {
|
||||
nodes.push(wrap.firstChild);
|
||||
listEl.appendChild(wrap.firstChild);
|
||||
}
|
||||
if (typeof window.initLazyImages === "function") {
|
||||
nodes.forEach(function (node) {
|
||||
if (node && node.nodeType === 1) window.initLazyImages(node);
|
||||
});
|
||||
window.initLazyImages(listEl);
|
||||
}
|
||||
setPagerState(getPage(), getLoaded(), total);
|
||||
}
|
||||
|
||||
function fetchCommentPage(pageNum) {
|
||||
if (!window.HttpRequest || typeof window.HttpRequest.get !== "function") {
|
||||
return Promise.reject(new Error("评论服务未就绪"));
|
||||
}
|
||||
var objectId = toInt(root.getAttribute("data-object-id"), 0);
|
||||
var objectType = toInt(root.getAttribute("data-object-type"), 10);
|
||||
if (objectId <= 0) return Promise.reject(new Error("缺少评论对象"));
|
||||
var pageSize = getPageSize();
|
||||
return window.HttpRequest.get("/api/comment/list", {
|
||||
params: {
|
||||
objectId: objectId,
|
||||
objectType: objectType,
|
||||
pageNum: pageNum,
|
||||
pageSize: pageSize,
|
||||
sortOrder: 1,
|
||||
},
|
||||
}).then(function (res) {
|
||||
if (!res || Number(res.code) !== 200 || !res.data) {
|
||||
throw new Error((res && (res.tip || res.msg)) || "加载失败");
|
||||
}
|
||||
return res.data;
|
||||
});
|
||||
}
|
||||
|
||||
function refreshList() {
|
||||
return fetchCommentPage(1)
|
||||
.then(function (data) {
|
||||
renderList(data.list || [], toInt(data.totalCount, 0));
|
||||
})
|
||||
.catch(function () {});
|
||||
}
|
||||
|
||||
function loadMoreComments() {
|
||||
if (loadingMore || !moreBtn) return;
|
||||
var nextPage = getPage() + 1;
|
||||
loadingMore = true;
|
||||
moreBtn.disabled = true;
|
||||
fetchCommentPage(nextPage)
|
||||
.then(function (data) {
|
||||
var list = data.list || [];
|
||||
var total = toInt(data.totalCount, 0);
|
||||
if (commentsRoot) {
|
||||
commentsRoot.setAttribute("data-comment-page", String(nextPage));
|
||||
}
|
||||
// 只追加,绝不 renderList 整表替换
|
||||
appendList(list, total);
|
||||
})
|
||||
.catch(function (err) {
|
||||
toast((err && err.message) || "加载失败,请稍后重试", "error");
|
||||
})
|
||||
.finally(function () {
|
||||
loadingMore = false;
|
||||
if (moreBtn) moreBtn.disabled = false;
|
||||
});
|
||||
}
|
||||
|
||||
function removeInlineComposer() {
|
||||
if (!listEl) return;
|
||||
listEl.querySelectorAll("[data-comment-inline-composer]").forEach(function (el) {
|
||||
el.remove();
|
||||
});
|
||||
}
|
||||
|
||||
function clearReplyTarget() {
|
||||
replyTarget = null;
|
||||
removeInlineComposer();
|
||||
}
|
||||
|
||||
function applyLikeUI(btn, isLike, countLike) {
|
||||
if (!btn) return;
|
||||
var liked = Number(isLike) === 1;
|
||||
btn.classList.toggle("is-active", liked);
|
||||
btn.setAttribute("data-liked", liked ? "1" : "2");
|
||||
if (countLike != null) {
|
||||
btn.setAttribute("data-like-raw", String(countLike));
|
||||
var countEl = btn.querySelector("[data-like-count]");
|
||||
if (countEl) countEl.textContent = String(countLike);
|
||||
}
|
||||
}
|
||||
|
||||
function toggleCommentLike(btn) {
|
||||
if (!btn || btn.getAttribute("data-liking") === "1") return;
|
||||
if (root.getAttribute("data-logged-in") !== "1") {
|
||||
var loginBtn = document.querySelector("[data-login-open]");
|
||||
if (loginBtn) loginBtn.click();
|
||||
return;
|
||||
}
|
||||
if (!window.HttpRequest || typeof window.HttpRequest.post !== "function") {
|
||||
toast("点赞服务未就绪", "error");
|
||||
return;
|
||||
}
|
||||
var commentId = toInt(btn.getAttribute("data-comment-like"), 0);
|
||||
if (commentId <= 0) return;
|
||||
var likedNow = Number(btn.getAttribute("data-liked") || 2) === 1;
|
||||
var nextLike = !likedNow;
|
||||
var prevCount = toInt(btn.getAttribute("data-like-raw"), 0);
|
||||
var nextCount = Math.max(0, prevCount + (nextLike ? 1 : -1));
|
||||
btn.setAttribute("data-liking", "1");
|
||||
btn.disabled = true;
|
||||
applyLikeUI(btn, nextLike ? 1 : 2, nextCount);
|
||||
var payload = { commentId: commentId, isLike: nextLike };
|
||||
window.HttpRequest.post("/api/comment/like/set", payload)
|
||||
.then(function (res) {
|
||||
if (!res || Number(res.code) !== 200) {
|
||||
throw new Error((res && (res.tip || res.msg)) || "操作失败");
|
||||
}
|
||||
var data = res.data || {};
|
||||
applyLikeUI(
|
||||
btn,
|
||||
data.isLike != null ? data.isLike : nextLike ? 1 : 2,
|
||||
data.countLike != null ? data.countLike : nextCount
|
||||
);
|
||||
})
|
||||
.catch(function (err) {
|
||||
applyLikeUI(btn, likedNow ? 1 : 2, prevCount);
|
||||
toast((err && err.message) || "操作失败,请稍后重试", "error");
|
||||
})
|
||||
.finally(function () {
|
||||
btn.removeAttribute("data-liking");
|
||||
btn.disabled = false;
|
||||
});
|
||||
}
|
||||
|
||||
function openInlineComposer(commentEl, target) {
|
||||
if (!commentEl || !target) return;
|
||||
removeInlineComposer();
|
||||
replyTarget = target;
|
||||
var body = commentEl.querySelector(".play-comment__body") || commentEl;
|
||||
var actions = body.querySelector(".play-comment__actions");
|
||||
var box = document.createElement("div");
|
||||
box.className = "play-comment__inline";
|
||||
box.setAttribute("data-comment-inline-composer", "");
|
||||
var name = target.replyName || "用户";
|
||||
box.innerHTML =
|
||||
'<div class="play-comment__inline-hint">' +
|
||||
"<span>回复 @" +
|
||||
escapeHtml(name) +
|
||||
"</span>" +
|
||||
'<button type="button" data-comment-reply-cancel>取消</button>' +
|
||||
"</div>" +
|
||||
'<div class="play-comment__inline-row">' +
|
||||
'<input class="play-comment__inline-input" type="text" maxlength="200" placeholder="回复 @' +
|
||||
escapeHtml(name) +
|
||||
'…" data-comment-inline-input autocomplete="off" />' +
|
||||
'<button class="play-comment__inline-send" type="button" data-comment-inline-send>发送</button>' +
|
||||
"</div>";
|
||||
if (actions && actions.parentNode === body) {
|
||||
if (actions.nextSibling) body.insertBefore(box, actions.nextSibling);
|
||||
else body.appendChild(box);
|
||||
} else {
|
||||
body.appendChild(box);
|
||||
}
|
||||
var inlineInput = box.querySelector("[data-comment-inline-input]");
|
||||
if (inlineInput) inlineInput.focus();
|
||||
}
|
||||
|
||||
function setSending(on) {
|
||||
sending = !!on;
|
||||
if (sendBtn) sendBtn.disabled = !!on;
|
||||
if (input) input.disabled = !!on;
|
||||
}
|
||||
|
||||
function submitTopComment() {
|
||||
if (sending) return;
|
||||
if (root.getAttribute("data-logged-in") !== "1") {
|
||||
var loginBtn = document.querySelector("[data-login-open]");
|
||||
if (loginBtn) loginBtn.click();
|
||||
return;
|
||||
}
|
||||
if (!window.HttpRequest || typeof window.HttpRequest.post !== "function") {
|
||||
toast("评论服务未就绪", "error");
|
||||
return;
|
||||
}
|
||||
var text = String((input && input.value) || "").trim();
|
||||
if (!text) {
|
||||
toast("请输入评论内容", "error");
|
||||
return;
|
||||
}
|
||||
if (Array.from(text).length > MAX_LEN) {
|
||||
toast("评论最多 200 字", "error");
|
||||
return;
|
||||
}
|
||||
var objectId = toInt(root.getAttribute("data-object-id"), 0);
|
||||
var objectType = toInt(root.getAttribute("data-object-type"), 10);
|
||||
if (objectId <= 0) {
|
||||
toast("缺少评论对象", "error");
|
||||
return;
|
||||
}
|
||||
|
||||
var payload = {
|
||||
objectId: objectId,
|
||||
objectType: objectType,
|
||||
text: text,
|
||||
parentsId: 0,
|
||||
replyCommentId: 0,
|
||||
};
|
||||
setSending(true);
|
||||
window.HttpRequest.post("/api/comment/send", payload)
|
||||
.then(function (res) {
|
||||
if (!res || Number(res.code) !== 200) {
|
||||
throw new Error((res && (res.tip || res.msg)) || "发表失败");
|
||||
}
|
||||
if (input) input.value = "";
|
||||
syncCount();
|
||||
clearReplyTarget();
|
||||
toast("发布成功,评论审核后才会展示", "success");
|
||||
return refreshList();
|
||||
})
|
||||
.catch(function (err) {
|
||||
toast((err && err.message) || "发表失败,请稍后重试", "error");
|
||||
})
|
||||
.finally(function () {
|
||||
setSending(false);
|
||||
});
|
||||
}
|
||||
|
||||
function sendInlineReply(box) {
|
||||
if (!box || inlineSending || !replyTarget) return;
|
||||
if (!window.HttpRequest || typeof window.HttpRequest.post !== "function") {
|
||||
toast("评论服务未就绪", "error");
|
||||
return;
|
||||
}
|
||||
var inlineInput = box.querySelector("[data-comment-inline-input]");
|
||||
var btn = box.querySelector("[data-comment-inline-send]");
|
||||
if (!inlineInput) return;
|
||||
var text = String(inlineInput.value || "").trim();
|
||||
if (!text) {
|
||||
toast("请输入评论内容", "error");
|
||||
return;
|
||||
}
|
||||
if (Array.from(text).length > MAX_LEN) {
|
||||
toast("评论最多 200 字", "error");
|
||||
return;
|
||||
}
|
||||
var objectId = toInt(root.getAttribute("data-object-id"), 0);
|
||||
var objectType = toInt(root.getAttribute("data-object-type"), 10);
|
||||
if (objectId <= 0) {
|
||||
toast("缺少评论对象", "error");
|
||||
return;
|
||||
}
|
||||
inlineSending = true;
|
||||
if (btn) btn.disabled = true;
|
||||
inlineInput.disabled = true;
|
||||
var payload = {
|
||||
objectId: objectId,
|
||||
objectType: objectType,
|
||||
text: text,
|
||||
parentsId: toInt(replyTarget.parentsId, 0),
|
||||
replyCommentId: toInt(
|
||||
replyTarget.replyCommentId || replyTarget.parentsId,
|
||||
0
|
||||
),
|
||||
};
|
||||
window.HttpRequest.post("/api/comment/send", payload)
|
||||
.then(function (res) {
|
||||
if (!res || Number(res.code) !== 200) {
|
||||
throw new Error((res && (res.tip || res.msg)) || "发表失败");
|
||||
}
|
||||
clearReplyTarget();
|
||||
toast("发布成功,评论审核后才会展示", "success");
|
||||
return refreshList();
|
||||
})
|
||||
.catch(function (err) {
|
||||
toast((err && err.message) || "发表失败,请稍后重试", "error");
|
||||
})
|
||||
.finally(function () {
|
||||
inlineSending = false;
|
||||
if (btn) btn.disabled = false;
|
||||
if (inlineInput) inlineInput.disabled = false;
|
||||
});
|
||||
}
|
||||
|
||||
if (input) {
|
||||
input.addEventListener("input", syncCount);
|
||||
input.addEventListener("keydown", function (event) {
|
||||
if (event.key === "Enter" && !event.shiftKey) {
|
||||
event.preventDefault();
|
||||
submitTopComment();
|
||||
}
|
||||
});
|
||||
syncCount();
|
||||
}
|
||||
if (sendBtn) {
|
||||
sendBtn.addEventListener("click", submitTopComment);
|
||||
}
|
||||
if (moreBtn) {
|
||||
moreBtn.addEventListener("click", function (event) {
|
||||
event.preventDefault();
|
||||
loadMoreComments();
|
||||
});
|
||||
}
|
||||
if (listEl) {
|
||||
listEl.addEventListener("click", function (event) {
|
||||
var likeBtn = window.domClosest(event.target, "[data-comment-like]");
|
||||
if (likeBtn && listEl.contains(likeBtn)) {
|
||||
event.preventDefault();
|
||||
toggleCommentLike(likeBtn);
|
||||
return;
|
||||
}
|
||||
var cancelBtn = window.domClosest(event.target, "[data-comment-reply-cancel]");
|
||||
if (cancelBtn && listEl.contains(cancelBtn)) {
|
||||
clearReplyTarget();
|
||||
return;
|
||||
}
|
||||
var sendInlineBtn = window.domClosest(event.target, "[data-comment-inline-send]");
|
||||
if (sendInlineBtn && listEl.contains(sendInlineBtn)) {
|
||||
var box = sendInlineBtn.closest("[data-comment-inline-composer]");
|
||||
sendInlineReply(box);
|
||||
return;
|
||||
}
|
||||
var replyBtn = window.domClosest(event.target, "[data-comment-reply]");
|
||||
if (!replyBtn || !listEl.contains(replyBtn)) return;
|
||||
if (root.getAttribute("data-logged-in") !== "1") {
|
||||
var loginBtn = document.querySelector("[data-login-open]");
|
||||
if (loginBtn) loginBtn.click();
|
||||
return;
|
||||
}
|
||||
var parentsId = toInt(replyBtn.getAttribute("data-parents-id"), 0);
|
||||
var commentId = toInt(replyBtn.getAttribute("data-comment-id"), parentsId);
|
||||
if (parentsId <= 0) return;
|
||||
var commentEl = replyBtn.closest(".play-comment");
|
||||
openInlineComposer(commentEl, {
|
||||
parentsId: parentsId,
|
||||
replyCommentId: commentId,
|
||||
replyName: replyBtn.getAttribute("data-reply-name") || "用户",
|
||||
});
|
||||
});
|
||||
listEl.addEventListener("keydown", function (event) {
|
||||
if (event.key !== "Enter" || event.shiftKey) return;
|
||||
var inlineInput = window.domClosest(event.target, "[data-comment-inline-input]");
|
||||
if (!inlineInput || !listEl.contains(inlineInput)) return;
|
||||
event.preventDefault();
|
||||
var box = inlineInput.closest("[data-comment-inline-composer]");
|
||||
sendInlineReply(box);
|
||||
});
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,169 @@
|
||||
(function () {
|
||||
var form = document.querySelector("[data-register-page-form]");
|
||||
var password = document.querySelector("[data-register-page-password]");
|
||||
var confirm = document.querySelector("[data-register-page-password-confirm]");
|
||||
var submitBtn = document.querySelector("[data-register-page-submit]") || (form && form.querySelector('button[type="submit"]'));
|
||||
var root = form || document;
|
||||
|
||||
function bindToggle(key, input) {
|
||||
var btn = document.querySelector('[data-register-page-toggle="' + key + '"]');
|
||||
var icon = document.querySelector('[data-register-page-toggle-icon="' + key + '"]');
|
||||
if (!btn || !input || !icon) return;
|
||||
btn.addEventListener("click", function () {
|
||||
var show = input.getAttribute("type") === "password";
|
||||
input.setAttribute("type", show ? "text" : "password");
|
||||
icon.setAttribute("src", show ? "/static/images/eye-off.svg" : "/static/images/eye.svg");
|
||||
btn.setAttribute("aria-label", show ? "隐藏密码" : "显示密码");
|
||||
});
|
||||
}
|
||||
|
||||
bindToggle("password", password);
|
||||
bindToggle("confirm", confirm);
|
||||
|
||||
function clearFieldError(name) {
|
||||
var field = root.querySelector('[data-field="' + name + '"]');
|
||||
var err = root.querySelector('[data-field-error="' + name + '"]');
|
||||
if (field) field.classList.remove("is-error");
|
||||
if (err) {
|
||||
err.hidden = true;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = "";
|
||||
}
|
||||
}
|
||||
|
||||
function clearAllErrors() {
|
||||
root.querySelectorAll("[data-field]").forEach(function (el) {
|
||||
el.classList.remove("is-error");
|
||||
});
|
||||
root.querySelectorAll("[data-field-error]").forEach(function (err) {
|
||||
err.hidden = true;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = "";
|
||||
});
|
||||
}
|
||||
|
||||
function setFieldError(name, message) {
|
||||
var field = root.querySelector('[data-field="' + name + '"]');
|
||||
var err = root.querySelector('[data-field-error="' + name + '"]');
|
||||
if (field) field.classList.add("is-error");
|
||||
if (err) {
|
||||
err.hidden = false;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = message || "";
|
||||
}
|
||||
}
|
||||
|
||||
function isAccountNo(value) {
|
||||
return /^[A-Za-z0-9]{6,20}$/.test(String(value || ""));
|
||||
}
|
||||
|
||||
function isPassword(value) {
|
||||
var pwd = String(value || "");
|
||||
if (pwd.length < 8) return false;
|
||||
return /[A-Za-z]/.test(pwd) && /[0-9]/.test(pwd);
|
||||
}
|
||||
|
||||
function validateForm() {
|
||||
clearAllErrors();
|
||||
var fd = new FormData(form);
|
||||
var account = String(fd.get("account") || "").trim();
|
||||
var pwd = String(fd.get("password") || "");
|
||||
var pwd2 = String(fd.get("passwordConfirm") || "");
|
||||
var agree = form.querySelector('input[name="agree"]');
|
||||
var valid = true;
|
||||
|
||||
if (!account) {
|
||||
setFieldError("account", "请输入账号");
|
||||
valid = false;
|
||||
} else if (!isAccountNo(account)) {
|
||||
setFieldError("account", "账号需为 6 到 20 位字母或数字");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!pwd) {
|
||||
setFieldError("password", "请输入密码");
|
||||
valid = false;
|
||||
} else if (!isPassword(pwd)) {
|
||||
setFieldError("password", "密码需 8 位以上,且同时包含字母与数字");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!pwd2) {
|
||||
setFieldError("passwordConfirm", "请再次输入密码");
|
||||
valid = false;
|
||||
} else if (pwd2 !== pwd) {
|
||||
setFieldError("passwordConfirm", "两次输入的密码不一致");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (agree && !agree.checked) {
|
||||
setFieldError("agree", "请先同意用户条款与隐私政策");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!valid) return null;
|
||||
return { accountNo: account, password: pwd };
|
||||
}
|
||||
|
||||
function mapServerError(err) {
|
||||
var tip = (err && err.message) || "注册失败,请稍后重试";
|
||||
var code = err && err.code;
|
||||
if (code === 5005 || /已存在|重复|占用/.test(tip)) {
|
||||
setFieldError("account", tip);
|
||||
return;
|
||||
}
|
||||
if (/密码|password/i.test(tip)) {
|
||||
setFieldError("password", tip);
|
||||
return;
|
||||
}
|
||||
setFieldError("account", tip);
|
||||
}
|
||||
|
||||
function setSubmitting(loading) {
|
||||
if (!submitBtn) return;
|
||||
submitBtn.disabled = !!loading;
|
||||
submitBtn.classList.toggle("is-loading", !!loading);
|
||||
submitBtn.textContent = loading ? "注册中…" : "注册";
|
||||
}
|
||||
|
||||
if (form) {
|
||||
form.querySelectorAll("input").forEach(function (input) {
|
||||
input.addEventListener("input", function () {
|
||||
var name = input.getAttribute("name");
|
||||
if (name === "agree") clearFieldError("agree");
|
||||
else if (name) clearFieldError(name);
|
||||
});
|
||||
if (input.type === "checkbox") {
|
||||
input.addEventListener("change", function () {
|
||||
clearFieldError("agree");
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
form.addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
if (!window.AuthAPI || !window.HttpRequest) {
|
||||
setFieldError("account", "认证脚本未加载,请刷新页面");
|
||||
return;
|
||||
}
|
||||
if (submitBtn && submitBtn.classList.contains("is-loading")) return;
|
||||
|
||||
var payload = validateForm();
|
||||
if (!payload) return;
|
||||
|
||||
setSubmitting(true);
|
||||
AuthAPI.register(payload)
|
||||
.then(function (res) {
|
||||
AuthAPI.toast((res && res.tip) || "注册成功", "success");
|
||||
window.setTimeout(function () {
|
||||
window.location.href = "/";
|
||||
}, 400);
|
||||
})
|
||||
.catch(function (err) {
|
||||
setSubmitting(false);
|
||||
mapServerError(err);
|
||||
AuthAPI.toast((err && err.message) || "注册失败", "error");
|
||||
});
|
||||
});
|
||||
}
|
||||
})();
|
||||
@@ -0,0 +1,256 @@
|
||||
(function () {
|
||||
var root = document.querySelector("[data-register-pop]");
|
||||
if (!root) return;
|
||||
|
||||
var form = root.querySelector("[data-register-form]");
|
||||
var password = root.querySelector("[data-register-password]");
|
||||
var confirm = root.querySelector("[data-register-password-confirm]");
|
||||
var submitBtn = root.querySelector("[data-register-submit]") || (form && form.querySelector('button[type="submit"]'));
|
||||
var previousOverflow = "";
|
||||
|
||||
function closeLogin() {
|
||||
if (typeof window.closeLoginPop === "function") {
|
||||
window.closeLoginPop();
|
||||
return;
|
||||
}
|
||||
var login = document.querySelector("[data-login-pop]");
|
||||
if (login) login.classList.remove("is-open");
|
||||
}
|
||||
|
||||
function closeUserMenu() {
|
||||
var menu = document.querySelector("[data-user-menu].is-open");
|
||||
if (menu) {
|
||||
menu.classList.remove("is-open");
|
||||
var t = menu.querySelector("[data-user-menu-toggle]");
|
||||
if (t) t.setAttribute("aria-expanded", "false");
|
||||
}
|
||||
}
|
||||
|
||||
function clearFieldError(name) {
|
||||
var field = root.querySelector('[data-field="' + name + '"]');
|
||||
var err = root.querySelector('[data-field-error="' + name + '"]');
|
||||
if (field) field.classList.remove("is-error");
|
||||
if (err) {
|
||||
err.hidden = true;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = "";
|
||||
}
|
||||
}
|
||||
|
||||
function clearAllErrors() {
|
||||
root.querySelectorAll("[data-field]").forEach(function (el) {
|
||||
el.classList.remove("is-error");
|
||||
});
|
||||
root.querySelectorAll("[data-field-error]").forEach(function (err) {
|
||||
err.hidden = true;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = "";
|
||||
});
|
||||
}
|
||||
|
||||
function setFieldError(name, message) {
|
||||
var field = root.querySelector('[data-field="' + name + '"]');
|
||||
var err = root.querySelector('[data-field-error="' + name + '"]');
|
||||
if (field) field.classList.add("is-error");
|
||||
if (err) {
|
||||
err.hidden = false;
|
||||
var text = err.querySelector("[data-field-error-text]");
|
||||
if (text) text.textContent = message || "";
|
||||
}
|
||||
}
|
||||
|
||||
function isAccountNo(value) {
|
||||
return /^[A-Za-z0-9]{6,20}$/.test(String(value || ""));
|
||||
}
|
||||
|
||||
function isPassword(value) {
|
||||
var pwd = String(value || "");
|
||||
if (pwd.length < 8) return false;
|
||||
return /[A-Za-z]/.test(pwd) && /[0-9]/.test(pwd);
|
||||
}
|
||||
|
||||
function bindingMessage(message, fallback) {
|
||||
return String(message || fallback || "").replace(/注册/g, "绑定");
|
||||
}
|
||||
|
||||
function validateForm() {
|
||||
clearAllErrors();
|
||||
var fd = new FormData(form);
|
||||
var account = String(fd.get("account") || "").trim();
|
||||
var pwd = String(fd.get("password") || "");
|
||||
var pwd2 = String(fd.get("passwordConfirm") || "");
|
||||
var agree = form.querySelector('input[name="agree"]');
|
||||
var valid = true;
|
||||
|
||||
if (!account) {
|
||||
setFieldError("account", "请输入账号");
|
||||
valid = false;
|
||||
} else if (!isAccountNo(account)) {
|
||||
setFieldError("account", "账号需为 6 到 20 位字母或数字");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!pwd) {
|
||||
setFieldError("password", "请输入密码");
|
||||
valid = false;
|
||||
} else if (!isPassword(pwd)) {
|
||||
setFieldError("password", "密码需 8 位以上,且同时包含字母与数字");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!pwd2) {
|
||||
setFieldError("passwordConfirm", "请再次输入密码");
|
||||
valid = false;
|
||||
} else if (pwd2 !== pwd) {
|
||||
setFieldError("passwordConfirm", "两次输入的密码不一致");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (agree && !agree.checked) {
|
||||
setFieldError("agree", "请先同意用户条款与隐私政策");
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (!valid) return null;
|
||||
return { accountNo: account, password: pwd };
|
||||
}
|
||||
|
||||
function mapServerError(err) {
|
||||
var tip = bindingMessage(err && err.message, "绑定失败,请稍后重试");
|
||||
var code = err && err.code;
|
||||
if (code === 5005 || /已存在|重复|占用/.test(tip)) {
|
||||
setFieldError("account", tip);
|
||||
return;
|
||||
}
|
||||
if (/密码|password/i.test(tip)) {
|
||||
setFieldError("password", tip);
|
||||
return;
|
||||
}
|
||||
setFieldError("account", tip);
|
||||
}
|
||||
|
||||
function open(event) {
|
||||
if (event) event.preventDefault();
|
||||
closeLogin();
|
||||
closeUserMenu();
|
||||
clearAllErrors();
|
||||
root.classList.add("is-open");
|
||||
previousOverflow = document.body.style.overflow;
|
||||
document.body.style.overflow = "hidden";
|
||||
var first = root.querySelector(".register-pop__input");
|
||||
if (first) {
|
||||
window.setTimeout(function () {
|
||||
first.focus();
|
||||
}, 0);
|
||||
}
|
||||
}
|
||||
|
||||
function close() {
|
||||
root.classList.remove("is-open");
|
||||
document.body.style.overflow = previousOverflow;
|
||||
clearAllErrors();
|
||||
}
|
||||
|
||||
window.openRegisterPop = open;
|
||||
window.closeRegisterPop = close;
|
||||
|
||||
function bindToggle(key, input) {
|
||||
var btn = root.querySelector('[data-register-toggle="' + key + '"]');
|
||||
var icon = root.querySelector('[data-register-toggle-icon="' + key + '"]');
|
||||
if (!btn || !input || !icon) return;
|
||||
btn.addEventListener("click", function () {
|
||||
var show = input.getAttribute("type") === "password";
|
||||
input.setAttribute("type", show ? "text" : "password");
|
||||
icon.setAttribute("src", show ? "/static/images/eye-off.svg" : "/static/images/eye.svg");
|
||||
btn.setAttribute("aria-label", show ? "隐藏密码" : "显示密码");
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener("click", function (event) {
|
||||
var opener = event.target.closest("[data-register-open]");
|
||||
if (opener) {
|
||||
open(event);
|
||||
return;
|
||||
}
|
||||
|
||||
// 站内注册入口统一使用绑定账号弹窗
|
||||
var registerLink = event.target.closest('a[href="/register"], a[href="/register/"], a[href="#register"]');
|
||||
if (registerLink) {
|
||||
open(event);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!root.classList.contains("is-open")) return;
|
||||
if (event.target.closest("[data-register-close]")) {
|
||||
close();
|
||||
}
|
||||
});
|
||||
|
||||
bindToggle("password", password);
|
||||
bindToggle("confirm", confirm);
|
||||
|
||||
function setSubmitting(loading) {
|
||||
if (!submitBtn) return;
|
||||
submitBtn.disabled = !!loading;
|
||||
submitBtn.classList.toggle("is-loading", !!loading);
|
||||
submitBtn.textContent = loading ? "绑定中…" : "绑定";
|
||||
}
|
||||
|
||||
if (form) {
|
||||
form.querySelectorAll("input").forEach(function (input) {
|
||||
input.addEventListener("input", function () {
|
||||
var name = input.getAttribute("name");
|
||||
if (name === "agree") clearFieldError("agree");
|
||||
else if (name) clearFieldError(name);
|
||||
});
|
||||
if (input.type === "checkbox") {
|
||||
input.addEventListener("change", function () {
|
||||
clearFieldError("agree");
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
form.addEventListener("submit", function (event) {
|
||||
event.preventDefault();
|
||||
if (!window.AuthAPI || !window.HttpRequest) {
|
||||
setFieldError("account", "认证脚本未加载,请刷新页面");
|
||||
return;
|
||||
}
|
||||
if (submitBtn && submitBtn.classList.contains("is-loading")) return;
|
||||
|
||||
var payload = validateForm();
|
||||
if (!payload) return;
|
||||
|
||||
setSubmitting(true);
|
||||
AuthAPI.register(payload)
|
||||
.then(function (res) {
|
||||
AuthAPI.toast(bindingMessage(res && res.tip, "绑定成功"), "success");
|
||||
window.setTimeout(function () {
|
||||
close();
|
||||
window.location.reload();
|
||||
}, 400);
|
||||
})
|
||||
.catch(function (err) {
|
||||
setSubmitting(false);
|
||||
mapServerError(err);
|
||||
AuthAPI.toast(bindingMessage(err && err.message, "绑定失败"), "error");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
document.addEventListener("keydown", function (event) {
|
||||
if (event.key === "Escape" && root.classList.contains("is-open")) {
|
||||
close();
|
||||
}
|
||||
});
|
||||
|
||||
try {
|
||||
var params = new URLSearchParams(window.location.search || "");
|
||||
if (params.get("auth") === "register") {
|
||||
params.delete("auth");
|
||||
var qs = params.toString();
|
||||
window.history.replaceState(null, "", window.location.pathname + (qs ? "?" + qs : "") + window.location.hash);
|
||||
open();
|
||||
}
|
||||
} catch (e) {}
|
||||
})();
|
||||
Reference in New Issue
Block a user