"use client"; import { useCallback, useEffect, useState } from "react"; import { Loader2, Save, ShieldOff } from "lucide-react"; import { Shell } from "@/components/Shell"; import { useToast } from "@/components/Toast"; import { fetchJson, fmtTs } from "@/lib/api"; type Settings = Record; const FIELDS: Array<{ key: string; label: string; hint: string; step?: number }> = [ { key: "max_concurrency", label: "最大并发", hint: "同时检查的直播间数,建议 1–3", step: 1 }, { key: "min_request_gap_sec", label: "最小请求间隔(秒)", hint: "任意两次请求之间的最小间隔", step: 0.1 }, { key: "request_jitter_sec", label: "随机抖动(秒)", hint: "在间隔上叠加随机延迟", step: 0.1 }, { key: "offline_interval_sec", label: "未开播复查间隔(秒)", hint: "离线房间下次检查等待", step: 1 }, { key: "live_interval_sec", label: "在播复查间隔(秒)", hint: "直播中更勤,便于捕捉停播", step: 1 }, { key: "error_backoff_base_sec", label: "错误退避基数(秒)", hint: "失败后指数退避", step: 1 }, { key: "error_backoff_max_sec", label: "错误退避上限(秒)", hint: "退避封顶", step: 1 }, { key: "global_pause_on_risk_sec", label: "风控全局暂停(秒)", hint: "触发验证码后全体停检", step: 1 }, ]; export default function SettingsPage() { const toast = useToast(); const [settings, setSettings] = useState({}); const [status, setStatus] = useState | null>(null); const [ytStatus, setYtStatus] = useState | null>(null); const [shortsStatus, setShortsStatus] = useState | null>(null); const [loading, setLoading] = useState(false); const [msg, setMsg] = useState(""); const [error, setError] = useState(""); const [notifPerm, setNotifPerm] = useState("default"); const load = useCallback(async () => { setLoading(true); try { const [s, m, yt, sh] = await Promise.all([ fetchJson("/api/settings"), fetchJson("/api/monitor/status"), fetchJson("/api/youtube-monitor/status").catch(() => null), fetchJson("/api/shorts/status").catch(() => null), ]); setSettings(s); setStatus(m); setYtStatus(yt); setShortsStatus(sh); if (typeof window !== "undefined" && "Notification" in window) { setNotifPerm(Notification.permission); } } catch (e) { setError((e as Error).message); } finally { setLoading(false); } }, []); useEffect(() => { void load(); }, [load]); async function save() { setMsg(""); setError(""); try { const body: Record = {}; for (const f of FIELDS) body[f.key] = Number(settings[f.key]); body.capture_cover_on_start = Boolean(settings.capture_cover_on_start); body.monitor_enabled = Boolean(settings.monitor_enabled); body.dy_cookie = String(settings.dy_cookie || ""); body.http_proxy = String(settings.http_proxy || ""); body.notify_webhook_url = String(settings.notify_webhook_url || ""); body.notify_on_live = Boolean(settings.notify_on_live); body.notify_on_offline = Boolean(settings.notify_on_offline); body.notify_on_risk = Boolean(settings.notify_on_risk); body.notify_on_shorts_fail = settings.notify_on_shorts_fail !== false; body.notify_on_shorts_ok = Boolean(settings.notify_on_shorts_ok); body.notify_on_shorts_quota = settings.notify_on_shorts_quota !== false; body.quiet_hours_enabled = Boolean(settings.quiet_hours_enabled); body.quiet_hours_start = Number(settings.quiet_hours_start ?? 2); body.quiet_hours_end = Number(settings.quiet_hours_end ?? 7); body.quiet_hours_allow_priority = settings.quiet_hours_allow_priority !== false; body.priority_interval_sec = Number(settings.priority_interval_sec ?? 25); body.yt_monitor_enabled = Boolean(settings.yt_monitor_enabled); body.yt_offline_interval_sec = Number(settings.yt_offline_interval_sec ?? 180); body.yt_live_interval_sec = Number(settings.yt_live_interval_sec ?? 90); body.yt_batch_size = Number(settings.yt_batch_size ?? 8); body.yt_request_gap_sec = Number(settings.yt_request_gap_sec ?? 2); body.shorts_enabled = Boolean(settings.shorts_enabled); body.shorts_clip_sec = Number(settings.shorts_clip_sec ?? 15); body.shorts_trigger = String(settings.shorts_trigger || "tagged"); body.shorts_tag = String(settings.shorts_tag || "shorts"); body.shorts_dedupe_room_day = settings.shorts_dedupe_room_day !== false; body.shorts_max_concurrent = Number(settings.shorts_max_concurrent ?? 1); body.shorts_auto_upload = settings.shorts_auto_upload !== false; body.shorts_live_sample = settings.shorts_live_sample !== false; body.shorts_live_sample_interval_sec = Number(settings.shorts_live_sample_interval_sec ?? 180); body.shorts_live_sample_batch = Number(settings.shorts_live_sample_batch ?? 1); body.shorts_sample_any_live = settings.shorts_sample_any_live !== false; body.shorts_upload_spread = settings.shorts_upload_spread !== false; body.shorts_upload_min_gap_sec = Number(settings.shorts_upload_min_gap_sec ?? 720); body.shorts_channel_min_gap_sec = Number(settings.shorts_channel_min_gap_sec ?? 2400); body.shorts_delete_clip_after_upload = settings.shorts_delete_clip_after_upload !== false; body.shorts_auto_retry_failed = settings.shorts_auto_retry_failed !== false; body.shorts_auto_retry_max_attempts = Number(settings.shorts_auto_retry_max_attempts ?? 3); body.shorts_auto_retry_interval_sec = Number(settings.shorts_auto_retry_interval_sec ?? 300); body.shorts_oauth_check_interval_sec = Number(settings.shorts_oauth_check_interval_sec ?? 3600); body.shorts_oauth_client_id = String(settings.shorts_oauth_client_id || ""); body.shorts_oauth_client_secret = String(settings.shorts_oauth_client_secret || ""); body.shorts_oauth_redirect_uri = String( settings.shorts_oauth_redirect_uri || "https://dylive.nomadro.com/api/shorts/oauth/callback" ); const next = await fetchJson("/api/settings", { method: "PATCH", body: JSON.stringify(body) }); setSettings(next); setMsg("已保存"); if (typeof window !== "undefined") { window.dispatchEvent(new CustomEvent("dy-settings-changed")); } await load(); } catch (e) { setError((e as Error).message); } } async function toggleMonitor(enabled: boolean) { try { await fetchJson(enabled ? "/api/monitor/start" : "/api/monitor/stop", { method: "POST" }); toast.push(enabled ? "抖音监控已启用" : "抖音监控已暂停", "ok"); await load(); } catch (e) { toast.push((e as Error).message, "err"); } } async function toggleYtMonitor(enabled: boolean) { try { await fetchJson(enabled ? "/api/youtube-monitor/start" : "/api/youtube-monitor/stop", { method: "POST" }); toast.push(enabled ? "YouTube 分组轮询已开启" : "YouTube 分组轮询已暂停", "ok"); await load(); } catch (e) { toast.push((e as Error).message, "err"); } } async function toggleShorts(enabled: boolean) { try { await fetchJson(enabled ? "/api/shorts/start" : "/api/shorts/stop", { method: "POST" }); toast.push(enabled ? "Shorts 旁路已启用" : "Shorts 旁路已关闭", "ok"); await load(); } catch (e) { toast.push((e as Error).message, "err"); } } async function clearRisk() { try { await fetchJson("/api/monitor/clear-risk", { method: "POST" }); setMsg("已清除风控暂停"); toast.push("已清除风控暂停", "ok"); await load(); } catch (e) { toast.push((e as Error).message, "err"); } } return (

⚙️ 设置中心

防风控参数、Cookie/代理、Webhook 通知。

监控开关: 状态:{status?.running ? "循环运行中" : "未运行"} · 上次检查 {fmtTs(Number(status?.last_check_at || 0))}
YouTube 分组轮询: {ytStatus?.running ? "循环运行中" : "未运行"} · 直播 {String((ytStatus?.groups as any)?.live ?? "—")} · 启用{" "} {String((ytStatus?.groups as any)?.enabled ?? "—")} · 上次 {fmtTs(Number(ytStatus?.last_check_at || 0))}
{( [ ["yt_offline_interval_sec", "离线复查(秒)", 180], ["yt_live_interval_sec", "在播复查(秒)", 90], ["yt_batch_size", "每批数量", 8], ["yt_request_gap_sec", "请求间隔(秒)", 2], ] as const ).map(([key, label, fallback]) => ( ))}

开播/停播沿用上方「Webhook / 浏览器通知」开关;静默时段内暂停 YouTube 轮询。

Shorts 旁路(默认关,不影响监控): 管理频道 / 任务 → {shortsStatus?.running ? "worker 运行中" : "worker 未运行"} ·{" "} {shortsStatus?.enabled ? "已启用" : "已关闭"} · 今日{" "} {JSON.stringify((shortsStatus?.stats as any)?.by_status || {})}

全局只需配置一次默认 Client。各上传频道可在编辑里覆盖专属 Client ID/Secret(谷歌账号 A/B/C/D / 不同 Cloud 项目),再点「Google 授权」写入该频道的 refresh_token。

{FIELDS.map((f) => ( ))}

🌙 静默时段 / 重点加速(北京时间)

点「收藏」会打上「收藏 + 重点」标签,优先检查、间隔更短;也可在各页快捷拉黑。

Cookie / 代理(降低风控)