2026-08-06 16:33:45 +08:00
|
|
|
|
/* Admin 的全部 JavaScript。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 原生 JS,没有框架、没有构建步骤——见 admin/AGENTS.md 前端约束。
|
2026-08-09 13:50:55 +08:00
|
|
|
|
* 只做轻量界面交互:全选、操作前确认、按钮禁用和弹窗。
|
2026-08-06 16:33:45 +08:00
|
|
|
|
* 搜索、删除、导入本身都是普通表单 POST,服务端渲染,不需要 JS。
|
|
|
|
|
|
*
|
|
|
|
|
|
* 不要往这里加业务逻辑。业务逻辑在服务端。
|
|
|
|
|
|
*/
|
|
|
|
|
|
(function () {
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
2026-08-11 15:18:00 +08:00
|
|
|
|
/* ── 模块导航状态 ────────────────────────────
|
|
|
|
|
|
列表筛选和页码本来就在 URL 中。这里按账号、按浏览器标签页记住每个
|
|
|
|
|
|
主模块最后一次稳定 URL,让采购员切走再回来时不用重复筛选。
|
|
|
|
|
|
|
|
|
|
|
|
只接受已知主列表路径和查询参数;勾选、弹窗、临时提示及写操作状态
|
|
|
|
|
|
都不进入 sessionStorage。链接原始 href 保持模块根地址,因此脚本失效时
|
|
|
|
|
|
仍可正常导航。 */
|
|
|
|
|
|
var MODULE_QUERY_KEYS = {
|
|
|
|
|
|
"/shopee": ["goods_id", "status", "shop", "image", "page"],
|
|
|
|
|
|
"/pdd": ["q", "status", "page"],
|
|
|
|
|
|
"/syb": ["order_no", "shop", "stage", "page", "date_from", "date_to"],
|
|
|
|
|
|
"/tasks": ["type", "status", "creator", "q", "page"],
|
|
|
|
|
|
"/clients": ["name", "page"],
|
|
|
|
|
|
"/users": ["q", "status", "page"]
|
|
|
|
|
|
};
|
|
|
|
|
|
var MODULE_STATE_PREFIX = "cmautobuy:module-state:";
|
|
|
|
|
|
|
|
|
|
|
|
function moduleStateStorageKey(userID, moduleRoot) {
|
|
|
|
|
|
return MODULE_STATE_PREFIX + userID + ":" + moduleRoot;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function stableModuleURL(moduleRoot, rawURL) {
|
|
|
|
|
|
if (!Object.prototype.hasOwnProperty.call(MODULE_QUERY_KEYS, moduleRoot)) return moduleRoot;
|
|
|
|
|
|
if (!rawURL || rawURL.length > 2048) return moduleRoot;
|
|
|
|
|
|
|
|
|
|
|
|
var parsed;
|
|
|
|
|
|
try {
|
|
|
|
|
|
parsed = new URL(rawURL, window.location.origin);
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
return moduleRoot;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (parsed.origin !== window.location.origin || parsed.pathname !== moduleRoot) return moduleRoot;
|
|
|
|
|
|
|
|
|
|
|
|
var allowed = MODULE_QUERY_KEYS[moduleRoot];
|
|
|
|
|
|
var stable = new URLSearchParams();
|
|
|
|
|
|
parsed.searchParams.forEach(function (value, key) {
|
|
|
|
|
|
if (allowed.indexOf(key) !== -1 && value !== "") stable.set(key, value);
|
|
|
|
|
|
});
|
|
|
|
|
|
var query = stable.toString();
|
|
|
|
|
|
return moduleRoot + (query ? "?" + query : "");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function setupModuleNavigationState() {
|
|
|
|
|
|
var nav = document.querySelector("[data-module-navigation]");
|
|
|
|
|
|
if (!nav) return;
|
|
|
|
|
|
var userID = nav.getAttribute("data-ui-state-user") || "";
|
|
|
|
|
|
if (!userID) return;
|
|
|
|
|
|
|
|
|
|
|
|
var currentRoot = Object.prototype.hasOwnProperty.call(MODULE_QUERY_KEYS, window.location.pathname)
|
|
|
|
|
|
? window.location.pathname
|
|
|
|
|
|
: "";
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (currentRoot) {
|
|
|
|
|
|
sessionStorage.setItem(
|
|
|
|
|
|
moduleStateStorageKey(userID, currentRoot),
|
|
|
|
|
|
stableModuleURL(currentRoot, window.location.href)
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
nav.querySelectorAll("[data-module-root]").forEach(function (link) {
|
|
|
|
|
|
var moduleRoot = link.getAttribute("data-module-root") || "";
|
|
|
|
|
|
var saved = sessionStorage.getItem(moduleStateStorageKey(userID, moduleRoot));
|
|
|
|
|
|
link.href = stableModuleURL(moduleRoot, saved);
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
/* 隐私模式或浏览器策略可能禁用存储。默认 href 仍是安全回退。 */
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var logoutForm = nav.querySelector(".nav-logout");
|
|
|
|
|
|
if (logoutForm) {
|
|
|
|
|
|
logoutForm.addEventListener("submit", function () {
|
|
|
|
|
|
try {
|
|
|
|
|
|
Object.keys(MODULE_QUERY_KEYS).forEach(function (moduleRoot) {
|
|
|
|
|
|
sessionStorage.removeItem(moduleStateStorageKey(userID, moduleRoot));
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
/* 登出不能因为清理界面状态失败而被阻止。 */
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-06 16:33:45 +08:00
|
|
|
|
/* ── 全选 / 反选 ──────────────────────────────
|
|
|
|
|
|
表头的勾选框控制本表格所有行的勾选框。 */
|
|
|
|
|
|
function setupCheckAll(root) {
|
|
|
|
|
|
var master = root.querySelector("[data-check-all]");
|
|
|
|
|
|
if (!master) return;
|
|
|
|
|
|
|
|
|
|
|
|
var table = master.closest("table");
|
|
|
|
|
|
if (!table) return;
|
|
|
|
|
|
|
|
|
|
|
|
function rowBoxes() {
|
2026-08-09 22:55:14 +08:00
|
|
|
|
return table.querySelectorAll("tbody input[type=checkbox]:not(:disabled)");
|
2026-08-06 16:33:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
master.addEventListener("change", function () {
|
|
|
|
|
|
rowBoxes().forEach(function (box) {
|
|
|
|
|
|
box.checked = master.checked;
|
|
|
|
|
|
});
|
|
|
|
|
|
syncButtons();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
table.addEventListener("change", function (e) {
|
|
|
|
|
|
if (e.target.type !== "checkbox" || e.target === master) return;
|
|
|
|
|
|
var boxes = Array.prototype.slice.call(rowBoxes());
|
|
|
|
|
|
master.checked = boxes.length > 0 && boxes.every(function (b) {
|
|
|
|
|
|
return b.checked;
|
|
|
|
|
|
});
|
|
|
|
|
|
syncButtons();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ── 选中数量 ────────────────────────────── */
|
2026-08-11 14:59:26 +08:00
|
|
|
|
function supportsSelectAction(box, action) {
|
|
|
|
|
|
if (!action) return true;
|
|
|
|
|
|
var actions = (box.getAttribute("data-select-action") || "").split(/\s+/);
|
|
|
|
|
|
return actions.indexOf(action) !== -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function checkedCount(action) {
|
|
|
|
|
|
return Array.prototype.filter.call(
|
|
|
|
|
|
document.querySelectorAll("tbody input[type=checkbox]:checked:not(:disabled)"),
|
|
|
|
|
|
function (box) { return supportsSelectAction(box, action); }
|
2026-08-06 16:33:45 +08:00
|
|
|
|
).length;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ── 需要勾选才能点的按钮 ──────────────────
|
|
|
|
|
|
标了 data-need-checked 的按钮,没勾选任何行时禁用。 */
|
|
|
|
|
|
function syncButtons() {
|
|
|
|
|
|
document.querySelectorAll("[data-need-checked]").forEach(function (btn) {
|
2026-08-11 14:59:26 +08:00
|
|
|
|
var action = btn.getAttribute("data-need-checked") || "";
|
|
|
|
|
|
btn.disabled = checkedCount(action) === 0;
|
2026-08-06 16:33:45 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ── 删除前二次确认 ────────────────────────
|
2026-08-07 11:27:06 +08:00
|
|
|
|
标了 data-confirm-delete 的元素,提交前必须确认,
|
|
|
|
|
|
并明确告诉用户会删掉几条、能不能恢复。
|
|
|
|
|
|
|
|
|
|
|
|
标在表单上就拦 submit,标在按钮上就拦 click——
|
|
|
|
|
|
一个表单配多个提交按钮时(PDD 页就是),只有删除那个要确认。
|
|
|
|
|
|
|
|
|
|
|
|
属性值可以写自定义文案,里面的 {n} 会替换成勾选条数;
|
|
|
|
|
|
不写就用默认那句。 */
|
|
|
|
|
|
var DEFAULT_CONFIRM = "将删除 {n} 条记录,不可恢复。确定继续?";
|
|
|
|
|
|
|
2026-08-06 16:33:45 +08:00
|
|
|
|
function setupConfirmDelete() {
|
2026-08-07 11:27:06 +08:00
|
|
|
|
document.querySelectorAll("[data-confirm-delete]").forEach(function (el) {
|
|
|
|
|
|
var isForm = el.tagName === "FORM";
|
|
|
|
|
|
el.addEventListener(isForm ? "submit" : "click", function (e) {
|
2026-08-06 16:33:45 +08:00
|
|
|
|
var n = checkedCount();
|
|
|
|
|
|
if (n === 0) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2026-08-07 11:27:06 +08:00
|
|
|
|
var text = el.getAttribute("data-confirm-delete") || DEFAULT_CONFIRM;
|
|
|
|
|
|
if (!window.confirm(text.replace("{n}", n) + "\n\n确定继续?")) {
|
2026-08-06 16:33:45 +08:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-09 13:50:55 +08:00
|
|
|
|
/* ── 普通敏感操作的二次确认 ───────────────
|
|
|
|
|
|
账号启停和密码重置不是删除,但同样会撤销登录。服务端仍负责权限和事务,
|
|
|
|
|
|
这里仅降低误操作概率。 */
|
|
|
|
|
|
function setupConfirmSubmit() {
|
|
|
|
|
|
document.querySelectorAll("form[data-confirm-submit]").forEach(function (form) {
|
|
|
|
|
|
form.addEventListener("submit", function (e) {
|
|
|
|
|
|
if (!window.confirm(form.getAttribute("data-confirm-submit"))) {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 用户列表共用一个重置密码弹窗。按钮只把用户编号和显示名放进表单,
|
|
|
|
|
|
不在前端处理权限、密码或 Session。 */
|
|
|
|
|
|
function setupUserResetModal() {
|
|
|
|
|
|
var modal = document.getElementById("reset-password-modal");
|
|
|
|
|
|
if (!modal) return;
|
|
|
|
|
|
var idInput = modal.querySelector("[data-reset-user-id-input]");
|
|
|
|
|
|
var nameLabel = modal.querySelector("[data-reset-username-label]");
|
|
|
|
|
|
document.querySelectorAll("[data-reset-user-id]").forEach(function (button) {
|
|
|
|
|
|
button.addEventListener("click", function () {
|
|
|
|
|
|
idInput.value = button.getAttribute("data-reset-user-id") || "";
|
|
|
|
|
|
nameLabel.textContent = button.getAttribute("data-reset-username") || "";
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-09 16:02:25 +08:00
|
|
|
|
/* 客户端列表共用绑定/转交弹窗。这里只回填行数据,权限和写入由服务端处理。 */
|
|
|
|
|
|
function setupClientAssignmentModal() {
|
|
|
|
|
|
var modal = document.getElementById("assign-client-modal");
|
|
|
|
|
|
if (!modal) return;
|
|
|
|
|
|
var idInput = modal.querySelector("[data-assign-client-id-input]");
|
|
|
|
|
|
var nameLabel = modal.querySelector("[data-assign-client-label]");
|
|
|
|
|
|
var select = modal.querySelector("select[name=user_id]");
|
|
|
|
|
|
var title = modal.querySelector("#assign-client-title");
|
|
|
|
|
|
var note = modal.querySelector("[data-assignment-note]");
|
|
|
|
|
|
document.querySelectorAll("[data-assign-client-id]").forEach(function (button) {
|
|
|
|
|
|
button.addEventListener("click", function () {
|
|
|
|
|
|
var currentName = button.getAttribute("data-assign-username") || "";
|
|
|
|
|
|
idInput.value = button.getAttribute("data-assign-client-id") || "";
|
|
|
|
|
|
nameLabel.textContent = button.getAttribute("data-assign-client-name") || "";
|
|
|
|
|
|
if (select) select.value = button.getAttribute("data-assign-user-id") || "";
|
|
|
|
|
|
title.textContent = currentName ? "转交客户端" : "绑定客户端";
|
|
|
|
|
|
note.textContent = currentName
|
|
|
|
|
|
? "当前负责人是“" + currentName + "”。保存后新负责人立即生效;既有任务不变。"
|
|
|
|
|
|
: "绑定只改变负责人和网页可见范围,不影响 Client 接口或既有任务。";
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-09 22:55:14 +08:00
|
|
|
|
/* 采购确认弹窗只负责把服务端已经渲染的本页行按勾选结果显示出来。
|
|
|
|
|
|
规格有效性、价格和客户端权限仍由服务端重新校验。 */
|
|
|
|
|
|
function setupPurchaseModal() {
|
2026-08-10 16:34:55 +08:00
|
|
|
|
var openButtons = document.querySelectorAll("[data-purchase-open]");
|
2026-08-09 22:55:14 +08:00
|
|
|
|
var form = document.querySelector("[data-purchase-form]");
|
2026-08-10 16:34:55 +08:00
|
|
|
|
if (!openButtons.length || !form) return;
|
2026-08-10 17:52:48 +08:00
|
|
|
|
var submitButton = form.querySelector("[data-purchase-submit]");
|
2026-08-10 16:34:55 +08:00
|
|
|
|
openButtons.forEach(function (openButton) {
|
|
|
|
|
|
openButton.addEventListener("click", function () {
|
|
|
|
|
|
var selected = {};
|
|
|
|
|
|
var singleID = openButton.getAttribute("data-purchase-id");
|
|
|
|
|
|
if (singleID) {
|
|
|
|
|
|
selected[singleID] = true;
|
|
|
|
|
|
} else {
|
2026-08-11 14:59:26 +08:00
|
|
|
|
document.querySelectorAll("tbody input[name=ids]:checked:not(:disabled)").forEach(function (box) {
|
|
|
|
|
|
if (supportsSelectAction(box, "purchase")) selected[box.value] = true;
|
2026-08-10 16:34:55 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
var shown = 0;
|
|
|
|
|
|
form.querySelectorAll("[data-purchase-row]").forEach(function (row) {
|
|
|
|
|
|
var enabled = !!selected[row.getAttribute("data-purchase-row")];
|
|
|
|
|
|
row.hidden = !enabled;
|
|
|
|
|
|
row.querySelectorAll("input").forEach(function (input) {
|
|
|
|
|
|
input.disabled = !enabled;
|
|
|
|
|
|
});
|
|
|
|
|
|
if (enabled) shown += 1;
|
2026-08-09 22:55:14 +08:00
|
|
|
|
});
|
2026-08-10 16:34:55 +08:00
|
|
|
|
var empty = form.querySelector("[data-purchase-empty]");
|
|
|
|
|
|
if (empty) empty.hidden = shown > 0;
|
2026-08-09 22:55:14 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
2026-08-10 17:52:48 +08:00
|
|
|
|
form.addEventListener("submit", function (event) {
|
|
|
|
|
|
if (event.defaultPrevented || !submitButton) return;
|
|
|
|
|
|
submitButton.disabled = true;
|
|
|
|
|
|
submitButton.setAttribute("aria-busy", "true");
|
|
|
|
|
|
submitButton.textContent = "创建中…";
|
|
|
|
|
|
});
|
2026-08-09 22:55:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-10 00:49:51 +08:00
|
|
|
|
/* PDD 批量采集弹窗只同步已选数量并提供提交反馈。
|
|
|
|
|
|
客户端权限和任务初始状态始终由服务端决定。 */
|
2026-08-10 16:14:24 +08:00
|
|
|
|
function setupCollectModals() {
|
|
|
|
|
|
document.querySelectorAll("[data-collect-open]").forEach(function (openButton) {
|
|
|
|
|
|
var formID = openButton.getAttribute("data-collect-form") || "pdd-form";
|
|
|
|
|
|
var form = document.getElementById(formID);
|
|
|
|
|
|
var modal = document.getElementById(openButton.getAttribute("data-modal-open"));
|
|
|
|
|
|
if (!form || !modal) return;
|
|
|
|
|
|
var count = modal.querySelector("[data-collect-count]");
|
|
|
|
|
|
var submitButton = modal.querySelector("[data-collect-submit]");
|
2026-08-11 14:59:26 +08:00
|
|
|
|
var action = openButton.getAttribute("data-collect-action") || "";
|
2026-08-10 16:14:24 +08:00
|
|
|
|
if (!count || !submitButton) return;
|
2026-08-10 00:49:51 +08:00
|
|
|
|
|
2026-08-10 16:14:24 +08:00
|
|
|
|
openButton.addEventListener("click", function () {
|
|
|
|
|
|
var fixedCount = openButton.getAttribute("data-collect-count-value");
|
2026-08-11 14:59:26 +08:00
|
|
|
|
count.textContent = fixedCount === null ? checkedCount(action) : fixedCount;
|
2026-08-10 16:14:24 +08:00
|
|
|
|
});
|
|
|
|
|
|
form.addEventListener("submit", function (event) {
|
|
|
|
|
|
if (event.submitter !== submitButton) return;
|
|
|
|
|
|
submitButton.disabled = true;
|
|
|
|
|
|
submitButton.setAttribute("aria-busy", "true");
|
|
|
|
|
|
submitButton.textContent = "创建中…";
|
|
|
|
|
|
});
|
2026-08-10 00:49:51 +08:00
|
|
|
|
});
|
2026-08-10 16:14:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Excel 解析可能需要几秒。浏览器校验通过并开始上传后立即锁住提交按钮,
|
|
|
|
|
|
防止采购员因为没有反馈而重复提交同一批文件。 */
|
|
|
|
|
|
function setupUploadFeedback() {
|
|
|
|
|
|
document.querySelectorAll("[data-upload-form]").forEach(function (form) {
|
|
|
|
|
|
var button = form.querySelector("[data-upload-submit]");
|
|
|
|
|
|
if (!button) return;
|
|
|
|
|
|
form.addEventListener("submit", function () {
|
|
|
|
|
|
button.disabled = true;
|
|
|
|
|
|
button.setAttribute("aria-busy", "true");
|
|
|
|
|
|
button.textContent = "导入中…";
|
|
|
|
|
|
});
|
2026-08-10 00:49:51 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-07 11:27:06 +08:00
|
|
|
|
/* ── 弹窗 ──────────────────────────────────
|
|
|
|
|
|
弹窗**内容由服务端渲染**,这里只负责显示、隐藏和把内容取回来。
|
|
|
|
|
|
不要在这里拼业务数据——价格格式、规格顺序都是业务规则,
|
|
|
|
|
|
散到前端就没人维护得住了。 */
|
|
|
|
|
|
|
|
|
|
|
|
function openModal(modal) {
|
2026-08-09 13:24:02 +08:00
|
|
|
|
modal._returnFocus = document.activeElement;
|
2026-08-07 11:27:06 +08:00
|
|
|
|
modal.hidden = false;
|
2026-08-09 13:24:02 +08:00
|
|
|
|
var focusable = modal.querySelector("[autofocus]") ||
|
|
|
|
|
|
modal.querySelector("input, button");
|
2026-08-07 11:27:06 +08:00
|
|
|
|
if (focusable) focusable.focus();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function closeModal(modal) {
|
|
|
|
|
|
modal.hidden = true;
|
2026-08-09 13:24:02 +08:00
|
|
|
|
if (modal._returnFocus && modal._returnFocus.focus) {
|
|
|
|
|
|
modal._returnFocus.focus();
|
|
|
|
|
|
}
|
2026-08-07 11:27:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function setupModals() {
|
|
|
|
|
|
/* 打开:按钮上写 data-modal-open="弹窗的 id" */
|
|
|
|
|
|
document.querySelectorAll("[data-modal-open]").forEach(function (btn) {
|
|
|
|
|
|
btn.addEventListener("click", function () {
|
|
|
|
|
|
var modal = document.getElementById(btn.getAttribute("data-modal-open"));
|
|
|
|
|
|
if (modal) openModal(modal);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
document.querySelectorAll(".modal-backdrop").forEach(function (modal) {
|
2026-08-09 16:31:41 +08:00
|
|
|
|
/* 关闭:叉、取消按钮,以及真正点弹窗外面的灰底。
|
|
|
|
|
|
|
|
|
|
|
|
不能只看 click 的 target:从输入框按下拖选到灰底松开时,浏览器会把
|
|
|
|
|
|
合成的 click 目标设成灰底。只有手势也从灰底开始,才算有意关闭。 */
|
|
|
|
|
|
var pointerStartedOnBackdrop = false;
|
|
|
|
|
|
modal.addEventListener("pointerdown", function (e) {
|
|
|
|
|
|
pointerStartedOnBackdrop = e.target === modal;
|
|
|
|
|
|
});
|
|
|
|
|
|
modal.addEventListener("pointercancel", function () {
|
|
|
|
|
|
pointerStartedOnBackdrop = false;
|
|
|
|
|
|
});
|
2026-08-07 11:27:06 +08:00
|
|
|
|
modal.addEventListener("click", function (e) {
|
2026-08-09 16:31:41 +08:00
|
|
|
|
var clickedCloseButton = e.target.closest("[data-modal-close]");
|
|
|
|
|
|
var clickedBackdrop = e.target === modal && pointerStartedOnBackdrop;
|
|
|
|
|
|
pointerStartedOnBackdrop = false;
|
|
|
|
|
|
if (clickedBackdrop || clickedCloseButton) {
|
2026-08-07 11:27:06 +08:00
|
|
|
|
closeModal(modal);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/* Esc 关掉最上面那个打开着的弹窗 */
|
|
|
|
|
|
document.addEventListener("keydown", function (e) {
|
|
|
|
|
|
if (e.key !== "Escape") return;
|
|
|
|
|
|
var open = document.querySelectorAll(".modal-backdrop:not([hidden])");
|
|
|
|
|
|
if (open.length > 0) closeModal(open[open.length - 1]);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ── 双击行打开详情弹窗 ────────────────────
|
|
|
|
|
|
行上写 data-detail-id,双击时去 /<模块>/detail?id=… 取回
|
|
|
|
|
|
已经渲染好的 HTML 片段,塞进壳子里显示。 */
|
|
|
|
|
|
function setupRowDetail() {
|
|
|
|
|
|
var modal = document.getElementById("detail-modal");
|
|
|
|
|
|
if (!modal) return;
|
|
|
|
|
|
|
|
|
|
|
|
var slot = modal.querySelector("[data-detail-slot]");
|
|
|
|
|
|
var base = modal.getAttribute("data-detail-url") || "";
|
|
|
|
|
|
|
2026-08-09 22:32:19 +08:00
|
|
|
|
function loadDetail(id) {
|
2026-08-10 16:33:42 +08:00
|
|
|
|
if (!modal.hidden && modal.getAttribute("data-loaded-detail-id") === id) return;
|
|
|
|
|
|
modal.setAttribute("data-loaded-detail-id", id);
|
2026-08-09 22:32:19 +08:00
|
|
|
|
slot.innerHTML = "<div class='modal-body'>正在加载…</div>";
|
|
|
|
|
|
openModal(modal);
|
|
|
|
|
|
|
|
|
|
|
|
var separator = base.indexOf("?") === -1 ? "?" : "&";
|
|
|
|
|
|
fetch(base + separator + "id=" + encodeURIComponent(id), {
|
|
|
|
|
|
headers: { "X-Requested-With": "fetch" }
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(function (resp) {
|
|
|
|
|
|
if (!resp.ok) throw new Error("HTTP " + resp.status);
|
|
|
|
|
|
return resp.text();
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(function (html) {
|
|
|
|
|
|
slot.innerHTML = html;
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(function (err) {
|
|
|
|
|
|
/* 出错也要说清楚,不能让弹窗一直停在"正在加载…" */
|
|
|
|
|
|
slot.innerHTML =
|
|
|
|
|
|
"<div class='modal-body'><p class='missing'>打不开详情:" +
|
|
|
|
|
|
err.message +
|
|
|
|
|
|
"。刷新页面后重试。</p></div>" +
|
|
|
|
|
|
"<div class='modal-foot'><button type='button' data-modal-close>关闭</button></div>";
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-07 11:27:06 +08:00
|
|
|
|
document.querySelectorAll("tr[data-detail-id]").forEach(function (row) {
|
|
|
|
|
|
row.addEventListener("dblclick", function (e) {
|
|
|
|
|
|
/* 双击到勾选框或链接上时不要弹窗,那是另一个动作 */
|
|
|
|
|
|
if (e.target.closest("input, a, button")) return;
|
|
|
|
|
|
|
2026-08-09 22:32:19 +08:00
|
|
|
|
loadDetail(row.getAttribute("data-detail-id"));
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2026-08-07 11:27:06 +08:00
|
|
|
|
|
2026-08-09 22:32:19 +08:00
|
|
|
|
document.querySelectorAll("[data-detail-open-id]").forEach(function (button) {
|
|
|
|
|
|
button.addEventListener("click", function () {
|
|
|
|
|
|
loadDetail(button.getAttribute("data-detail-open-id"));
|
2026-08-07 11:27:06 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-10 16:33:42 +08:00
|
|
|
|
/* 顺运宝缩略图使用上游图片 URL。这里只在弹窗里放大展示,
|
|
|
|
|
|
不代理、不下载图片;加载失败时保留新窗口重试入口。 */
|
|
|
|
|
|
function setupImagePreview() {
|
|
|
|
|
|
var modal = document.getElementById("image-preview-modal");
|
|
|
|
|
|
if (!modal) return;
|
|
|
|
|
|
var image = modal.querySelector("[data-image-preview-image]");
|
|
|
|
|
|
var title = modal.querySelector("[data-image-preview-title]");
|
|
|
|
|
|
var status = modal.querySelector("[data-image-preview-status]");
|
|
|
|
|
|
var link = modal.querySelector("[data-image-preview-link]");
|
|
|
|
|
|
if (!image || !title || !status || !link) return;
|
|
|
|
|
|
|
|
|
|
|
|
document.querySelectorAll("[data-image-preview-url]").forEach(function (button) {
|
|
|
|
|
|
button.addEventListener("click", function () {
|
|
|
|
|
|
var url = button.getAttribute("data-image-preview-url") || "";
|
|
|
|
|
|
var productTitle = button.getAttribute("data-image-preview-title") || "商品";
|
|
|
|
|
|
title.textContent = productTitle + " · 原图";
|
|
|
|
|
|
link.href = url;
|
|
|
|
|
|
image.alt = productTitle + " 原图";
|
2026-08-11 11:18:51 +08:00
|
|
|
|
image.referrerPolicy = "no-referrer";
|
2026-08-10 16:33:42 +08:00
|
|
|
|
image.hidden = true;
|
|
|
|
|
|
status.hidden = false;
|
|
|
|
|
|
status.classList.remove("missing");
|
|
|
|
|
|
status.textContent = "正在加载原图…";
|
|
|
|
|
|
openModal(modal);
|
|
|
|
|
|
|
|
|
|
|
|
image.onload = function () {
|
|
|
|
|
|
status.hidden = true;
|
|
|
|
|
|
image.hidden = false;
|
|
|
|
|
|
};
|
|
|
|
|
|
image.onerror = function () {
|
|
|
|
|
|
image.hidden = true;
|
|
|
|
|
|
status.hidden = false;
|
|
|
|
|
|
status.classList.add("missing");
|
|
|
|
|
|
status.textContent = "原图加载失败,请在新窗口打开或稍后重试。";
|
|
|
|
|
|
};
|
|
|
|
|
|
image.src = url;
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-11 11:18:51 +08:00
|
|
|
|
function setupImageThumbnails() {
|
|
|
|
|
|
document.querySelectorAll("[data-image-thumb]").forEach(function (image) {
|
|
|
|
|
|
function showFallback() {
|
|
|
|
|
|
image.hidden = true;
|
|
|
|
|
|
var fallback = image.parentElement && image.parentElement.querySelector("[data-image-thumb-fallback]");
|
|
|
|
|
|
if (fallback) fallback.hidden = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
image.addEventListener("error", showFallback);
|
|
|
|
|
|
if (image.complete && image.naturalWidth === 0) showFallback();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-09 11:49:13 +08:00
|
|
|
|
/* ── 顺运宝登录弹窗:验证码"换一张" ─────────────
|
|
|
|
|
|
图片本身、以及旁边的"换一张"按钮,点了都重新请求验证码接口。
|
|
|
|
|
|
`[必须]` 每次请求带一个新的时间戳查询参数,绕开浏览器缓存——
|
|
|
|
|
|
否则点了"换一张"看到的还是同一张图(工单 #46)。 */
|
|
|
|
|
|
function setupCaptchaRefresh() {
|
|
|
|
|
|
var img = document.getElementById("login-captcha-img");
|
|
|
|
|
|
if (!img) return;
|
|
|
|
|
|
var base = img.getAttribute("data-captcha-refresh") || img.src;
|
|
|
|
|
|
|
|
|
|
|
|
function refresh() {
|
|
|
|
|
|
img.src = base + "?_=" + Date.now();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
img.addEventListener("click", refresh);
|
|
|
|
|
|
var btn = document.getElementById("login-captcha-refresh");
|
|
|
|
|
|
if (btn) btn.addEventListener("click", refresh);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-09 20:41:24 +08:00
|
|
|
|
/* 顺运宝同步是后台长任务。表单通过浏览器校验并提交后立即锁住按钮,
|
|
|
|
|
|
让操作员在页面跳转前就看到反馈;服务端互斥仍是最终保护。 */
|
|
|
|
|
|
function setupSybSyncFeedback() {
|
|
|
|
|
|
var form = document.querySelector("[data-syb-sync-form]");
|
|
|
|
|
|
if (!form) return;
|
|
|
|
|
|
var button = form.querySelector("[data-syb-sync-submit]");
|
|
|
|
|
|
if (!button) return;
|
|
|
|
|
|
form.addEventListener("submit", function () {
|
|
|
|
|
|
button.disabled = true;
|
|
|
|
|
|
button.setAttribute("aria-busy", "true");
|
|
|
|
|
|
button.textContent = "同步中…";
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-09 21:02:28 +08:00
|
|
|
|
/* 同步记录继续由服务端渲染,但只替换弹窗内部的 HTML 片段。
|
|
|
|
|
|
事件委托挂在不会被替换的 slot 上,所以刷新和翻页后不用重新绑定。 */
|
2026-08-09 20:41:24 +08:00
|
|
|
|
function setupSybHistoryRefresh() {
|
2026-08-09 21:02:28 +08:00
|
|
|
|
var slot = document.querySelector("[data-syb-history-slot]");
|
|
|
|
|
|
if (!slot) return;
|
|
|
|
|
|
|
|
|
|
|
|
slot.addEventListener("click", function (event) {
|
|
|
|
|
|
var trigger = event.target.closest("[data-history-fetch-url]");
|
|
|
|
|
|
if (!trigger || !slot.contains(trigger)) return;
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
if (slot.getAttribute("aria-busy") === "true") return;
|
|
|
|
|
|
|
|
|
|
|
|
var fetchURL = trigger.getAttribute("data-history-fetch-url");
|
|
|
|
|
|
var refreshButton = slot.querySelector("button[data-history-fetch-url]");
|
|
|
|
|
|
var loading = slot.querySelector("[data-history-loading]");
|
|
|
|
|
|
var previousError = slot.querySelector("[data-history-load-error]");
|
|
|
|
|
|
var isRefreshButton = trigger === refreshButton;
|
|
|
|
|
|
slot.setAttribute("aria-busy", "true");
|
|
|
|
|
|
slot.querySelectorAll("[data-history-fetch-url]").forEach(function (control) {
|
|
|
|
|
|
control.setAttribute("aria-disabled", "true");
|
|
|
|
|
|
});
|
|
|
|
|
|
if (refreshButton) refreshButton.disabled = true;
|
|
|
|
|
|
if (loading) loading.hidden = false;
|
|
|
|
|
|
if (previousError) previousError.hidden = true;
|
|
|
|
|
|
if (isRefreshButton) refreshButton.textContent = "刷新中…";
|
|
|
|
|
|
|
|
|
|
|
|
fetch(fetchURL, {
|
|
|
|
|
|
cache: "no-store",
|
|
|
|
|
|
headers: { "X-Requested-With": "fetch" }
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(function (response) {
|
|
|
|
|
|
if (response.redirected) {
|
|
|
|
|
|
window.location.assign(response.url);
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!response.ok) throw new Error("HTTP " + response.status);
|
|
|
|
|
|
return response.text();
|
|
|
|
|
|
})
|
|
|
|
|
|
.then(function (html) {
|
|
|
|
|
|
if (html === null) return;
|
|
|
|
|
|
slot.innerHTML = html;
|
|
|
|
|
|
slot.removeAttribute("aria-busy");
|
|
|
|
|
|
var nextRefreshButton = slot.querySelector("button[data-history-fetch-url]");
|
|
|
|
|
|
if (nextRefreshButton) nextRefreshButton.focus({ preventScroll: true });
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch(function (error) {
|
|
|
|
|
|
slot.removeAttribute("aria-busy");
|
|
|
|
|
|
slot.querySelectorAll("[data-history-fetch-url]").forEach(function (control) {
|
|
|
|
|
|
control.removeAttribute("aria-disabled");
|
|
|
|
|
|
});
|
|
|
|
|
|
refreshButton = slot.querySelector("button[data-history-fetch-url]");
|
|
|
|
|
|
loading = slot.querySelector("[data-history-loading]");
|
|
|
|
|
|
var errorBox = slot.querySelector("[data-history-load-error]");
|
|
|
|
|
|
if (refreshButton) {
|
|
|
|
|
|
refreshButton.disabled = false;
|
|
|
|
|
|
refreshButton.textContent = "刷新同步记录";
|
|
|
|
|
|
refreshButton.focus({ preventScroll: true });
|
|
|
|
|
|
}
|
|
|
|
|
|
if (loading) loading.hidden = true;
|
|
|
|
|
|
if (errorBox) {
|
|
|
|
|
|
errorBox.textContent = "刷新同步记录失败:" + error.message + "。请重试。";
|
|
|
|
|
|
errorBox.hidden = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2026-08-09 20:41:24 +08:00
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-08-06 16:33:45 +08:00
|
|
|
|
document.addEventListener("DOMContentLoaded", function () {
|
2026-08-11 15:18:00 +08:00
|
|
|
|
setupModuleNavigationState();
|
2026-08-06 16:33:45 +08:00
|
|
|
|
document.querySelectorAll("table").forEach(setupCheckAll);
|
|
|
|
|
|
setupConfirmDelete();
|
2026-08-09 13:50:55 +08:00
|
|
|
|
setupConfirmSubmit();
|
|
|
|
|
|
setupUserResetModal();
|
2026-08-09 16:02:25 +08:00
|
|
|
|
setupClientAssignmentModal();
|
2026-08-09 22:55:14 +08:00
|
|
|
|
setupPurchaseModal();
|
2026-08-10 16:14:24 +08:00
|
|
|
|
setupCollectModals();
|
|
|
|
|
|
setupUploadFeedback();
|
2026-08-07 11:27:06 +08:00
|
|
|
|
setupModals();
|
|
|
|
|
|
setupRowDetail();
|
2026-08-10 16:33:42 +08:00
|
|
|
|
setupImagePreview();
|
2026-08-11 11:18:51 +08:00
|
|
|
|
setupImageThumbnails();
|
2026-08-09 11:49:13 +08:00
|
|
|
|
setupCaptchaRefresh();
|
2026-08-09 20:41:24 +08:00
|
|
|
|
setupSybSyncFeedback();
|
|
|
|
|
|
setupSybHistoryRefresh();
|
2026-08-06 16:33:45 +08:00
|
|
|
|
syncButtons();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-08-09 11:49:13 +08:00
|
|
|
|
/* TODO(骨架): 蝦皮数据页 -> 编辑弹窗(填 PDD 链接)。
|
|
|
|
|
|
顺运宝页的登录弹窗已经实现(setupCaptchaRefresh + 通用 setupModals);
|
|
|
|
|
|
规格匹配弹窗仍是后续工单的范围。
|
2026-08-07 11:27:06 +08:00
|
|
|
|
页面里放一个 id="detail-modal" 的壳子、行上写 data-detail-id,
|
|
|
|
|
|
服务端出一个返回片段的 /<模块>/detail,这里就不用再加代码了。 */
|
2026-08-06 16:33:45 +08:00
|
|
|
|
})();
|