File size: 18,569 Bytes
dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 d10e42a dac5e28 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | <!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>DouYin Spark Helper - Admin 控制台</title>
<link rel="stylesheet" href="/static/style.css">
</head>
<body class="dash-body">
<header class="topbar">
<div>
<h1>Admin 后台管理</h1>
<p>用户管理 + 定时任务总览</p>
</div>
<div class="top-actions">
<button id="retryAllFailedBtn" class="btn ghost">一键重试失败任务</button>
<button id="refreshBtn" class="btn ghost">刷新</button>
<button id="logoutBtn" class="btn ghost">退出登录</button>
</div>
</header>
<main class="container">
<section class="panel">
<h2>用户与任务总览</h2>
<p id="summary" class="muted">加载中...</p>
<div id="dbAlert" class="alert warning" style="display:none;"></div>
<div class="table-wrap">
<table class="admin-table">
<thead>
<tr>
<th>发起用户</th>
<th>唯一标识</th>
<th>注册时间</th>
<th>定时状态</th>
<th>发送时间</th>
<th>消息内容</th>
<th>接收方</th>
<th>下次执行</th>
<th>最近状态</th>
<th>操作</th>
</tr>
</thead>
<tbody id="adminBody">
<tr><td colspan="10">暂无数据</td></tr>
</tbody>
</table>
</div>
<p id="adminMsg" class="msg"></p>
</section>
<section id="detailPanel" class="panel" style="display:none;">
<div class="panel-header">
<h2 id="detailTitle">任务详情</h2>
<div class="top-actions">
<button id="detailRetryBtn" class="btn" style="display:none;">重试失败任务</button>
<button id="detailRefreshBtn" class="btn">刷新详情</button>
</div>
</div>
<div id="detailMeta" class="detail-grid"></div>
<div class="detail-split">
<article class="detail-card">
<h3>消息内容</h3>
<pre id="detailMessage" class="detail-message">-</pre>
</article>
<article class="detail-card">
<h3>接收方(去重后)</h3>
<pre id="detailTargets" class="detail-message">-</pre>
</article>
</div>
<article class="detail-card">
<h3>账号明细</h3>
<div class="table-wrap">
<table class="admin-table detail-history">
<thead>
<tr>
<th>账号昵称</th>
<th>unique_id</th>
<th>目标数</th>
<th>cookies 数</th>
<th>目标列表</th>
</tr>
</thead>
<tbody id="detailAccountsBody">
<tr><td colspan="5">暂无数据</td></tr>
</tbody>
</table>
</div>
</article>
<article class="detail-card">
<h3>执行历史</h3>
<div class="table-wrap">
<table class="admin-table detail-history">
<thead>
<tr>
<th>触发方式</th>
<th>开始时间</th>
<th>结束时间</th>
<th>状态</th>
<th>耗时</th>
<th>信息</th>
</tr>
</thead>
<tbody id="detailHistoryBody">
<tr><td colspan="6">暂无记录</td></tr>
</tbody>
</table>
</div>
</article>
<article class="detail-card">
<h3>任务日志</h3>
<pre id="detailLogs" class="detail-logbox">暂无日志。</pre>
</article>
</section>
</main>
<script>
const adminBody = document.getElementById("adminBody");
const summary = document.getElementById("summary");
const retryAllFailedBtn = document.getElementById("retryAllFailedBtn");
const dbAlert = document.getElementById("dbAlert");
const adminMsg = document.getElementById("adminMsg");
const detailPanel = document.getElementById("detailPanel");
const detailTitle = document.getElementById("detailTitle");
const detailMeta = document.getElementById("detailMeta");
const detailMessage = document.getElementById("detailMessage");
const detailTargets = document.getElementById("detailTargets");
const detailAccountsBody = document.getElementById("detailAccountsBody");
const detailHistoryBody = document.getElementById("detailHistoryBody");
const detailLogs = document.getElementById("detailLogs");
const detailRetryBtn = document.getElementById("detailRetryBtn");
let currentDetailUser = null;
let dbConnected = true;
function setMsg(msg, isError = false) {
adminMsg.textContent = msg || "";
adminMsg.style.color = isError ? "#c0392b" : "#146356";
}
async function requestJSON(url, options = {}) {
const resp = await fetch(url, {
credentials: "same-origin",
headers: { "Content-Type": "application/json", ...(options.headers || {}) },
...options,
});
if (resp.status === 401) {
window.location.href = "/admin";
throw new Error("登录已失效,请重新登录。");
}
const data = await resp.json();
if (!resp.ok || data.ok === false) {
throw new Error(data.message || "请求失败");
}
return data;
}
function escapeHtml(value) {
return String(value ?? "")
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/\"/g, """)
.replace(/'/g, "'");
}
function renderDbStatus(status, fallbackMessage = "") {
dbConnected = !status || status.connected !== false;
if (dbConnected) {
dbAlert.style.display = "none";
dbAlert.textContent = "";
return;
}
const parts = ["当前无法连接 SQL 服务器。"];
if (fallbackMessage) {
parts.push(fallbackMessage);
} else if (status && status.last_error) {
parts.push(status.last_error);
}
if (status && status.last_checked_at && status.last_checked_at !== "-") {
parts.push(`最近检查:${status.last_checked_at}`);
}
if (status && status.last_ok_at && status.last_ok_at !== "-") {
parts.push(`最近成功连接:${status.last_ok_at}`);
}
dbAlert.textContent = parts.join(" ");
dbAlert.style.display = "block";
}
function clearDetailPanel() {
currentDetailUser = null;
detailPanel.style.display = "none";
detailRetryBtn.style.display = "none";
detailRetryBtn.dataset.user = "";
detailTitle.textContent = "任务详情";
detailMeta.innerHTML = "";
detailMessage.textContent = "-";
detailTargets.textContent = "-";
detailLogs.textContent = "暂无日志。";
detailAccountsBody.innerHTML = '<tr><td colspan="5">暂无数据</td></tr>';
detailHistoryBody.innerHTML = '<tr><td colspan="6">暂无记录</td></tr>';
}
function renderRows(users) {
if (!users || users.length === 0) {
adminBody.innerHTML = '<tr><td colspan="10">暂无用户</td></tr>';
clearDetailPanel();
return;
}
adminBody.innerHTML = users.map((u) => {
if (u.error) {
return `
<tr>
<td>${escapeHtml(u.username)}</td>
<td>${escapeHtml(u.unique_id || "-")}</td>
<td>${escapeHtml(u.created_at || "-")}</td>
<td colspan="6" style="color:#c0392b;">数据异常:${escapeHtml(u.error)}</td>
<td>
<div class="admin-actions">
<button class="btn admin-delete-user" data-user="${escapeHtml(u.username)}">删用户</button>
</div>
</td>
</tr>
`;
}
const targets = Array.isArray(u.targets) ? u.targets : [];
const targetText = targets.length ? targets.join(" / ") : "-";
const statusText = u.is_running ? "运行中" : (u.last_status || "-");
const retryAction = u.can_retry
? `<button class="btn admin-retry-task" data-user="${escapeHtml(u.username)}">重试失败</button>`
: "";
return `
<tr>
<td>${escapeHtml(u.username)}</td>
<td>${escapeHtml(u.unique_id)}</td>
<td>${escapeHtml(u.created_at)}</td>
<td>${u.scheduler_enabled ? "启用" : "禁用"}</td>
<td>${escapeHtml(u.schedule_time)} (${escapeHtml(u.schedule_timezone)})</td>
<td class="ellipsis-cell" title="${escapeHtml(u.message_template || "")}">${escapeHtml(u.message_template || "-")}</td>
<td class="ellipsis-cell" title="${escapeHtml(targetText)}">${escapeHtml(targetText)}</td>
<td>${escapeHtml(u.next_run || "-")}</td>
<td>${escapeHtml(statusText)}</td>
<td>
<div class="admin-actions">
<button class="btn admin-view-task" data-user="${escapeHtml(u.username)}">详情/日志</button>
${retryAction}
<button class="btn admin-del-task" data-user="${escapeHtml(u.username)}">删任务</button>
<button class="btn admin-delete-user" data-user="${escapeHtml(u.username)}">删用户</button>
</div>
</td>
</tr>
`;
}).join("");
document.querySelectorAll(".admin-view-task").forEach((btn) => {
btn.addEventListener("click", async () => {
const username = btn.dataset.user;
await loadTaskDetail(username, true);
});
});
document.querySelectorAll(".admin-del-task").forEach((btn) => {
btn.addEventListener("click", async () => {
const username = btn.dataset.user;
if (!confirm(`确认删除用户 ${username} 的定时任务?`)) return;
setMsg("正在删除任务...");
try {
const data = await requestJSON(`/api/admin/tasks/${encodeURIComponent(username)}/delete`, {
method: "POST",
body: "{}",
});
setMsg(data.message || "已删除任务");
await loadOverview();
if (currentDetailUser === username) {
await loadTaskDetail(username, false);
}
} catch (err) {
setMsg(err.message, true);
}
});
});
document.querySelectorAll(".admin-retry-task").forEach((btn) => {
btn.addEventListener("click", async () => {
const username = btn.dataset.user;
await retryFailedTask(username);
});
});
document.querySelectorAll(".admin-delete-user").forEach((btn) => {
btn.addEventListener("click", async () => {
const username = btn.dataset.user;
if (!confirm(`确认删除用户 ${username}?该操作不可恢复。`)) return;
setMsg("正在删除用户...");
try {
const data = await requestJSON(`/api/admin/users/${encodeURIComponent(username)}`, {
method: "DELETE",
});
setMsg(data.message || "用户已删除");
if (currentDetailUser === username) {
clearDetailPanel();
}
await loadOverview();
} catch (err) {
setMsg(err.message, true);
}
});
});
}
function renderDetail(task) {
currentDetailUser = task.username;
detailPanel.style.display = "block";
detailTitle.textContent = `任务详情 · ${task.username}`;
const runtime = task.runtime || {};
const config = task.config || {};
const targets = Array.isArray(task.targets) ? task.targets : [];
detailMeta.innerHTML = `
<div class="detail-item"><span>发起用户</span><strong>${escapeHtml(task.username || "-")}</strong></div>
<div class="detail-item"><span>唯一标识</span><strong>${escapeHtml(task.unique_id || "-")}</strong></div>
<div class="detail-item"><span>注册时间</span><strong>${escapeHtml(task.created_at || "-")}</strong></div>
<div class="detail-item"><span>定时状态</span><strong>${task.scheduler_enabled ? "启用" : "禁用"}</strong></div>
<div class="detail-item"><span>发送时间</span><strong>${escapeHtml(task.schedule_time || "-")} (${escapeHtml(task.schedule_timezone || "-")})</strong></div>
<div class="detail-item"><span>下一次执行</span><strong>${escapeHtml(runtime.next_run || "-")}</strong></div>
<div class="detail-item"><span>最近状态</span><strong>${runtime.is_running ? "运行中" : escapeHtml(runtime.last_status || "-")}</strong></div>
<div class="detail-item"><span>最近开始</span><strong>${escapeHtml(runtime.last_start || "-")}</strong></div>
<div class="detail-item"><span>账号数 / 目标数</span><strong>${runtime.account_count || 0} / ${runtime.target_count || 0}</strong></div>
<div class="detail-item"><span>并发设置</span><strong>multiTask=${config.multiTask ? "true" : "false"}, taskCount=${config.taskCount || 1}</strong></div>
`;
detailMessage.textContent = task.message_template || "-";
detailTargets.textContent = targets.length ? targets.join("\n") : "-";
detailLogs.textContent = task.logs || "暂无日志。";
detailRetryBtn.dataset.user = task.username || "";
detailRetryBtn.style.display = task.can_retry ? "inline-flex" : "none";
const accounts = Array.isArray(task.accounts) ? task.accounts : [];
if (!accounts.length) {
detailAccountsBody.innerHTML = '<tr><td colspan="5">暂无账号明细</td></tr>';
} else {
detailAccountsBody.innerHTML = accounts.map((item) => {
const t = Array.isArray(item.targets) ? item.targets : [];
return `
<tr>
<td>${escapeHtml(item.username || "-")}</td>
<td>${escapeHtml(item.unique_id || "-")}</td>
<td>${item.target_count || 0}</td>
<td>${item.cookie_count || 0}</td>
<td>${escapeHtml(t.join(" / ") || "-")}</td>
</tr>
`;
}).join("");
}
const history = Array.isArray(task.history) ? task.history : [];
if (!history.length) {
detailHistoryBody.innerHTML = '<tr><td colspan="6">暂无记录</td></tr>';
} else {
detailHistoryBody.innerHTML = history.map((row) => `
<tr>
<td>${escapeHtml(row.trigger || "-")}</td>
<td>${escapeHtml(row.start || "-")}</td>
<td>${escapeHtml(row.end || "-")}</td>
<td>${escapeHtml(row.status || "-")}</td>
<td>${escapeHtml(row.duration || "-")}</td>
<td>${escapeHtml(row.message || "-")}</td>
</tr>
`).join("");
}
}
async function loadTaskDetail(username, showMessage = false) {
try {
const data = await requestJSON(`/api/admin/tasks/${encodeURIComponent(username)}?log_limit=1200`);
renderDetail(data.task || {});
if (showMessage) {
setMsg(`已加载 ${username} 的任务详情与日志。`);
}
} catch (err) {
setMsg(err.message, true);
}
}
async function retryAllFailedTasks() {
if (!dbConnected) {
setMsg("SQL 连接异常,暂时无法批量重试失败任务。", true);
return;
}
setMsg("正在批量重试失败任务...");
try {
const data = await requestJSON("/api/admin/tasks/retry-failed", {
method: "POST",
body: "{}",
});
setMsg(data.message || "已开始批量重试失败任务");
await loadOverview();
if (currentDetailUser && dbConnected) {
await loadTaskDetail(currentDetailUser, false);
}
} catch (err) {
setMsg(err.message, true);
}
}
async function retryFailedTask(username) {
if (!username) {
setMsg("未找到可重试的任务用户。", true);
return;
}
setMsg("正在重试失败任务...");
try {
const data = await requestJSON(`/api/admin/tasks/${encodeURIComponent(username)}/retry`, {
method: "POST",
body: "{}",
});
setMsg(data.message || "已开始重试失败任务");
await loadOverview();
if (currentDetailUser === username && dbConnected) {
await loadTaskDetail(username, false);
}
} catch (err) {
setMsg(err.message, true);
}
}
async function loadOverview() {
const data = await requestJSON("/api/admin/overview");
renderDbStatus(data.db_status || null, data.message || "");
summary.textContent = dbConnected
? `共 ${data.task_count || 0} 个用户任务。`
: "SQL 连接异常,当前未能加载用户任务。";
renderRows(data.users || []);
if (!dbConnected) {
clearDetailPanel();
}
return data;
}
retryAllFailedBtn.addEventListener("click", async () => {
await retryAllFailedTasks();
});
document.getElementById("refreshBtn").addEventListener("click", async () => {
try {
await loadOverview();
if (dbConnected && currentDetailUser) {
await loadTaskDetail(currentDetailUser, false);
}
} catch (err) {
setMsg(err.message, true);
}
});
document.getElementById("detailRefreshBtn").addEventListener("click", async () => {
if (!currentDetailUser) {
setMsg("请先在上方选择一个任务。", true);
return;
}
await loadTaskDetail(currentDetailUser, true);
});
detailRetryBtn.addEventListener("click", async () => {
await retryFailedTask(detailRetryBtn.dataset.user || currentDetailUser);
});
document.getElementById("logoutBtn").addEventListener("click", async () => {
await fetch("/api/logout", { method: "POST", credentials: "same-origin" });
window.location.href = "/admin";
});
loadOverview().catch((err) => setMsg(err.message, true));
setInterval(async () => {
try {
await loadOverview();
if (dbConnected && currentDetailUser) {
await loadTaskDetail(currentDetailUser, false);
}
} catch (_) {
}
}, 8000);
</script>
</body>
</html>
|