README covers YouTube RTMPS relay, time-slot mode, ops/Netdata, and global SOCKS plan. Relay pipeline keeps FFmpeg alive across probe flicker, stabilizes time-slot source selection, and fixes misleading start-button copy in the UI. Co-authored-by: Cursor <cursoragent@cursor.com>
1478 lines
60 KiB
TypeScript
1478 lines
60 KiB
TypeScript
"use client";
|
||
|
||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||
import Link from "next/link";
|
||
import {
|
||
Clock3,
|
||
ExternalLink,
|
||
KeyRound,
|
||
Music2,
|
||
Plus,
|
||
RefreshCw,
|
||
Trash2,
|
||
Zap,
|
||
} from "lucide-react";
|
||
|
||
import { Shell } from "@/components/Shell";
|
||
import { useConfirm } from "@/components/ConfirmDialog";
|
||
import { useToast } from "@/components/Toast";
|
||
import { EnableSwitch } from "@/components/EnableSwitch";
|
||
import { PageLoader } from "@/components/ui";
|
||
import {
|
||
TikTokUpcomingUploadRow,
|
||
TikTokUploadConfirmModal,
|
||
useTikTokUpload,
|
||
type TikTokUploadTarget,
|
||
} from "@/components/TikTokUploadUx";
|
||
import { UploadTimeline } from "@/components/UploadTimeline";
|
||
import { fetchJson, fmtDuration, fmtTs, todayBeijingISO } from "@/lib/api";
|
||
|
||
type Channel = {
|
||
id: string;
|
||
name: string;
|
||
tiktok_username?: string;
|
||
tiktok_uid?: string;
|
||
nickname?: string;
|
||
avatar_url?: string;
|
||
follower_count?: number;
|
||
following_count?: number;
|
||
video_count?: number;
|
||
heart_count?: number;
|
||
profile_fetched_at?: number;
|
||
profile_error?: string;
|
||
enabled?: boolean;
|
||
authorized?: boolean;
|
||
oauth_ok?: number;
|
||
oauth_error?: string;
|
||
privacy_level?: string;
|
||
title_template?: string;
|
||
description_template?: string;
|
||
note?: string;
|
||
cookie_saved_at?: number;
|
||
cookie_age_days?: number | null;
|
||
cookie_ttl_days?: number;
|
||
cookie_expires_in_days?: number | null;
|
||
cookie_status?: "ok" | "warn" | "expired" | "missing" | string;
|
||
socks_enabled?: boolean;
|
||
socks_profile_id?: string;
|
||
socks_profile_label?: string;
|
||
platform_limited?: boolean;
|
||
platform_limit_at?: number;
|
||
platform_limit_msg?: string;
|
||
platform_limit_probe_at?: number;
|
||
};
|
||
|
||
type SocksProfile = {
|
||
id: string;
|
||
label?: string;
|
||
type?: string;
|
||
server?: string;
|
||
port?: number;
|
||
username?: string;
|
||
password?: string;
|
||
has_password?: boolean;
|
||
};
|
||
|
||
type BoardChannel = {
|
||
id: string;
|
||
name?: string;
|
||
tiktok_username?: string;
|
||
nickname?: string;
|
||
avatar_url?: string;
|
||
tiktok_uid?: string;
|
||
follower_count?: number;
|
||
video_count?: number;
|
||
heart_count?: number;
|
||
enabled?: boolean;
|
||
authorized?: boolean;
|
||
used: number;
|
||
scheduled: number;
|
||
uploaded: number;
|
||
pending: number;
|
||
gap?: number;
|
||
max_met?: boolean;
|
||
daily_max?: number;
|
||
};
|
||
|
||
type Upcoming = {
|
||
id: string;
|
||
anchor_name?: string;
|
||
channel_name?: string;
|
||
channel_pk?: string;
|
||
upload_at?: number;
|
||
status?: string;
|
||
can_upload_now?: boolean;
|
||
clip_ready?: boolean;
|
||
clip_seconds?: number;
|
||
room_id?: string;
|
||
};
|
||
|
||
type Job = {
|
||
id: string;
|
||
anchor_name?: string;
|
||
channel_name?: string;
|
||
channel_pk?: string;
|
||
status?: string;
|
||
push_date?: string;
|
||
error?: string;
|
||
clip_ready?: boolean;
|
||
clip_seconds?: number;
|
||
tiktok_publish_id?: string;
|
||
upload_at?: number;
|
||
created_at?: number;
|
||
finished_at?: number;
|
||
room_id?: string;
|
||
};
|
||
|
||
export default function TikTokPage() {
|
||
const toast = useToast();
|
||
const confirm = useConfirm();
|
||
const leftPanelRef = useRef<HTMLElement>(null);
|
||
const rightPanelRef = useRef<HTMLElement>(null);
|
||
|
||
const [loading, setLoading] = useState(true);
|
||
const [togglingId, setTogglingId] = useState("");
|
||
const [status, setStatus] = useState<any>(null);
|
||
const [channels, setChannels] = useState<Channel[]>([]);
|
||
const [socksProfiles, setSocksProfiles] = useState<SocksProfile[]>([]);
|
||
const [socksDraft, setSocksDraft] = useState<SocksProfile[]>([]);
|
||
const [socksSelectedId, setSocksSelectedId] = useState("");
|
||
const [savingSocks, setSavingSocks] = useState(false);
|
||
const [jobs, setJobs] = useState<Job[]>([]);
|
||
const [jobTotal, setJobTotal] = useState(0);
|
||
|
||
const [showForm, setShowForm] = useState(false);
|
||
const [editId, setEditId] = useState("");
|
||
const [formName, setFormName] = useState("TikTok 账号");
|
||
const [formPrivacy, setFormPrivacy] = useState("PUBLIC_TO_EVERYONE");
|
||
const [formTitle, setFormTitle] = useState("");
|
||
const [formSocksEnabled, setFormSocksEnabled] = useState(false);
|
||
const [formSocksProfileId, setFormSocksProfileId] = useState("");
|
||
|
||
const [sessionChannelId, setSessionChannelId] = useState("");
|
||
const [sessionChannelName, setSessionChannelName] = useState("");
|
||
const [sessionId, setSessionId] = useState("");
|
||
const [sessionDcId, setSessionDcId] = useState("");
|
||
const [sessionUsername, setSessionUsername] = useState("");
|
||
const [savingSession, setSavingSession] = useState(false);
|
||
const [showSessionForm, setShowSessionForm] = useState(false);
|
||
|
||
const load = useCallback(async () => {
|
||
const iso = todayBeijingISO();
|
||
const [st, j] = await Promise.all([
|
||
fetchJson("/api/tiktok/status"),
|
||
fetchJson(`/api/tiktok/jobs?push_date=${iso}&limit=30`),
|
||
]);
|
||
setStatus(st);
|
||
setChannels(st?.channels || []);
|
||
const profiles = Array.isArray(st?.socks_profiles) ? (st.socks_profiles as SocksProfile[]) : [];
|
||
setSocksProfiles(profiles);
|
||
setSocksDraft(profiles.map((p) => ({ ...p })));
|
||
setSocksSelectedId((prev) => {
|
||
if (prev && profiles.some((p) => p.id === prev)) return prev;
|
||
return String(profiles[0]?.id || "");
|
||
});
|
||
setJobs(j?.jobs || []);
|
||
setJobTotal(j?.total || 0);
|
||
setLoading(false);
|
||
}, []);
|
||
|
||
const uploadUx = useTikTokUpload(load);
|
||
|
||
useEffect(() => {
|
||
void load();
|
||
let n = 0;
|
||
const t = setInterval(() => {
|
||
if (document.hidden) return;
|
||
n += 1;
|
||
if (n % 2 === 0) {
|
||
void load();
|
||
} else {
|
||
void fetchJson("/api/tiktok/status?light=1")
|
||
.then((st) => {
|
||
setStatus((prev: Record<string, unknown> | null) => ({
|
||
...(prev || {}),
|
||
...st,
|
||
channels: prev?.channels || st?.channels || [],
|
||
socks_profiles: prev?.socks_profiles || st?.socks_profiles || [],
|
||
setup: prev?.setup ?? st?.setup,
|
||
}));
|
||
})
|
||
.catch(() => undefined);
|
||
}
|
||
}, 20000);
|
||
const onVis = () => {
|
||
if (!document.hidden) void load();
|
||
};
|
||
document.addEventListener("visibilitychange", onVis);
|
||
return () => {
|
||
clearInterval(t);
|
||
document.removeEventListener("visibilitychange", onVis);
|
||
};
|
||
}, [load]);
|
||
|
||
useEffect(() => {
|
||
const left = leftPanelRef.current;
|
||
const right = rightPanelRef.current;
|
||
if (!left || !right) return;
|
||
const sync = () => {
|
||
right.style.maxHeight = `${left.getBoundingClientRect().height}px`;
|
||
};
|
||
sync();
|
||
const ro = new ResizeObserver(sync);
|
||
ro.observe(left);
|
||
return () => ro.disconnect();
|
||
});
|
||
|
||
const setup = status?.setup;
|
||
const hasChannels = channels.length > 0;
|
||
const authorizedCount = channels.filter((c) => c.authorized).length;
|
||
const allReady = hasChannels && authorizedCount === channels.length;
|
||
const setupDone = allReady;
|
||
const board = status?.board || {};
|
||
const boardChannels: BoardChannel[] = board.channels || [];
|
||
const upcoming: Upcoming[] = board.upcoming || [];
|
||
const beijingWindow = board.beijing_window || "";
|
||
|
||
const openAddForm = () => {
|
||
setEditId("");
|
||
setFormName("TikTok 账号");
|
||
setFormPrivacy("PUBLIC_TO_EVERYONE");
|
||
setFormTitle("");
|
||
setFormSocksEnabled(false);
|
||
setFormSocksProfileId(String(socksProfiles[0]?.id || ""));
|
||
setShowForm(true);
|
||
setShowSessionForm(false);
|
||
};
|
||
|
||
const togglePipeline = async () => {
|
||
const on = status?.enabled;
|
||
await fetchJson(on ? "/api/tiktok/stop" : "/api/tiktok/start", { method: "POST" });
|
||
toast.push(on ? "TikTok 已暂停" : "TikTok 已启动", "ok");
|
||
void load();
|
||
};
|
||
|
||
const applyCookiePayload = (raw: unknown, source: "clipboard" | "extension" = "clipboard") => {
|
||
const data = typeof raw === "string" ? JSON.parse(raw) : raw;
|
||
const sessionid = String((data as any)?.sessionid || "").trim();
|
||
const dc_id = String(
|
||
(data as any)?.dc_id || (data as any)?.["tt-target-idc"] || (data as any)?.tt_target_idc || "",
|
||
).trim();
|
||
const username = String((data as any)?.tiktok_username || "").trim();
|
||
if (!sessionid) {
|
||
throw new Error(source === "extension" ? "插件未返回 sessionid" : "剪贴板里没有 sessionid");
|
||
}
|
||
setSessionId(sessionid);
|
||
setSessionDcId(dc_id);
|
||
if (username) setSessionUsername(username);
|
||
return { sessionid, dc_id, username };
|
||
};
|
||
|
||
const importFromClipboard = async () => {
|
||
try {
|
||
const text = await navigator.clipboard.readText();
|
||
const filled = applyCookiePayload(text, "clipboard");
|
||
toast.push(
|
||
filled.dc_id ? `已从剪贴板填入(机房 ${filled.dc_id})` : "已从剪贴板填入 Cookie(未含机房)",
|
||
"ok",
|
||
);
|
||
} catch (e: any) {
|
||
toast.push(e?.message || "读取剪贴板失败", "err");
|
||
}
|
||
};
|
||
|
||
const importFromExtension = () => {
|
||
// Prefer postMessage — content script isolated world may miss CustomEvent.
|
||
window.postMessage({ source: "dylive-web", type: "dylive-tiktok-cookie-request" }, "*");
|
||
window.dispatchEvent(new Event("dylive-tiktok-cookie-request"));
|
||
window.setTimeout(() => {
|
||
const flag = (window as any).__dyliveTiktokCookieGot;
|
||
if (!flag) {
|
||
toast.push("插件无响应:请加载 extensions/dylive-helper/dist,并先登录 tiktok.com", "err");
|
||
}
|
||
(window as any).__dyliveTiktokCookieGot = false;
|
||
}, 2500);
|
||
};
|
||
|
||
useEffect(() => {
|
||
let lastKey = "";
|
||
const applyFromExt = (detail: unknown, error?: string) => {
|
||
(window as any).__dyliveTiktokCookieGot = true;
|
||
if (error) {
|
||
toast.push(error, "err");
|
||
return;
|
||
}
|
||
try {
|
||
const filled = applyCookiePayload(detail, "extension");
|
||
const key = `${filled.sessionid}|${filled.dc_id}`;
|
||
if (key === lastKey) return; // postMessage + CustomEvent 各推一次
|
||
lastKey = key;
|
||
toast.push(
|
||
filled.dc_id
|
||
? `已从插件填入(机房 ${filled.dc_id})`
|
||
: "已从插件填入 sessionid;tt-target-idc 未读到,可手动填或留空自动探测",
|
||
filled.dc_id ? "ok" : "warn",
|
||
);
|
||
} catch (e: any) {
|
||
toast.push(e?.message || "插件数据无效", "err");
|
||
}
|
||
};
|
||
const onFill = (ev: Event) => {
|
||
applyFromExt((ev as CustomEvent).detail);
|
||
};
|
||
const onMessage = (ev: MessageEvent) => {
|
||
if (ev.source !== window) return;
|
||
const data = ev.data;
|
||
if (!data || data.source !== "dylive-helper" || data.type !== "dylive-tiktok-cookie") return;
|
||
if (data.ok === false) {
|
||
applyFromExt({}, String(data.error || "插件读取失败"));
|
||
return;
|
||
}
|
||
applyFromExt(data.payload || {});
|
||
};
|
||
window.addEventListener("dylive-tiktok-cookie", onFill as EventListener);
|
||
window.addEventListener("message", onMessage);
|
||
return () => {
|
||
window.removeEventListener("dylive-tiktok-cookie", onFill as EventListener);
|
||
window.removeEventListener("message", onMessage);
|
||
};
|
||
}, [toast]);
|
||
|
||
const saveSession = async () => {
|
||
if (!sessionChannelId) {
|
||
toast.push("请选择账号", "err");
|
||
return;
|
||
}
|
||
if (!sessionId.trim()) {
|
||
toast.push("请填写 sessionid", "err");
|
||
return;
|
||
}
|
||
setSavingSession(true);
|
||
try {
|
||
await fetchJson(`/api/tiktok/channels/${sessionChannelId}/session`, {
|
||
method: "POST",
|
||
body: JSON.stringify({
|
||
sessionid: sessionId.trim(),
|
||
dc_id: sessionDcId.trim(),
|
||
tiktok_username: sessionUsername.trim(),
|
||
}),
|
||
});
|
||
toast.push("Cookie 会话已保存", "ok");
|
||
setShowSessionForm(false);
|
||
setSessionId("");
|
||
void load();
|
||
} catch (e: any) {
|
||
toast.push(e?.message || "保存失败", "err");
|
||
} finally {
|
||
setSavingSession(false);
|
||
}
|
||
};
|
||
|
||
const addChannel = async () => {
|
||
const r = await fetchJson("/api/tiktok/channels", {
|
||
method: "POST",
|
||
body: JSON.stringify({
|
||
name: formName,
|
||
privacy_level: formPrivacy,
|
||
title_template: formTitle,
|
||
description_template: "",
|
||
socks_enabled: formSocksEnabled,
|
||
socks_profile_id: formSocksEnabled ? formSocksProfileId : "",
|
||
}),
|
||
});
|
||
toast.push("账号已添加,请粘贴 Cookie", "ok");
|
||
setShowForm(false);
|
||
const ch = r?.channel as Channel | undefined;
|
||
if (ch?.id) {
|
||
openSessionForm(ch.id, ch);
|
||
}
|
||
void load();
|
||
};
|
||
|
||
const saveChannel = async () => {
|
||
if (!editId) return;
|
||
await fetchJson(`/api/tiktok/channels/${editId}`, {
|
||
method: "PATCH",
|
||
body: JSON.stringify({
|
||
name: formName,
|
||
privacy_level: formPrivacy,
|
||
title_template: formTitle,
|
||
description_template: "",
|
||
socks_enabled: formSocksEnabled,
|
||
socks_profile_id: formSocksEnabled ? formSocksProfileId : "",
|
||
}),
|
||
});
|
||
toast.push("已保存", "ok");
|
||
setShowForm(false);
|
||
setEditId("");
|
||
void load();
|
||
};
|
||
|
||
const removeChannel = async (id: string) => {
|
||
const ok = await confirm({
|
||
title: "删除 TikTok 账号",
|
||
message: "确定删除此账号?",
|
||
confirmLabel: "删除",
|
||
destructive: true,
|
||
});
|
||
if (!ok) return;
|
||
await fetchJson(`/api/tiktok/channels/${id}`, { method: "DELETE" });
|
||
toast.push("已删除", "ok");
|
||
void load();
|
||
};
|
||
|
||
const openSessionForm = (id: string, ch?: Channel) => {
|
||
setSessionChannelId(id);
|
||
setSessionChannelName(ch?.name || "");
|
||
setSessionId("");
|
||
setSessionDcId("");
|
||
setSessionUsername(ch?.tiktok_username || "");
|
||
setShowForm(false);
|
||
setShowSessionForm(true);
|
||
};
|
||
|
||
const refreshProfile = async (ch: Channel) => {
|
||
try {
|
||
await fetchJson(`/api/tiktok/channels/${ch.id}/profile/refresh`, { method: "POST" });
|
||
toast.push(`已刷新「${ch.name}」资料`, "ok");
|
||
void load();
|
||
} catch (e: any) {
|
||
toast.push(e?.message || "刷新资料失败", "err");
|
||
}
|
||
};
|
||
|
||
const fmtCount = (n?: number) => {
|
||
const v = Number(n || 0);
|
||
if (v >= 10000) return `${(v / 10000).toFixed(v >= 100000 ? 0 : 1)}万`;
|
||
return String(v);
|
||
};
|
||
|
||
const toggleChannelEnabled = async (ch: Channel) => {
|
||
const turningOn = ch.enabled === false;
|
||
if (!turningOn) {
|
||
const ok = await confirm({
|
||
title: `停用「${ch.name}」?`,
|
||
message: "关闭后将立即清除该账号全部任务与成片,并停止取样/录制/上传。可随时再开。",
|
||
confirmLabel: "停用并清任务",
|
||
destructive: true,
|
||
});
|
||
if (!ok) return;
|
||
}
|
||
setTogglingId(ch.id);
|
||
try {
|
||
const r = await fetchJson(`/api/tiktok/channels/${ch.id}`, {
|
||
method: "PATCH",
|
||
body: JSON.stringify({ enabled: turningOn }),
|
||
});
|
||
const cleared = r.cleared as { jobs?: number; clips?: number } | undefined;
|
||
if (!turningOn && cleared) {
|
||
toast.push(
|
||
`已停用 · 清除任务 ${cleared.jobs ?? 0} · 成片 ${cleared.clips ?? 0}`,
|
||
"ok",
|
||
);
|
||
} else {
|
||
toast.push(turningOn ? `已启用「${ch.name}」` : `已停用「${ch.name}」`, "ok");
|
||
}
|
||
void load();
|
||
} catch (e: any) {
|
||
toast.push(e?.message || "切换失败", "err");
|
||
} finally {
|
||
setTogglingId("");
|
||
}
|
||
};
|
||
|
||
const sampleNow = async () => {
|
||
const r = await fetchJson("/api/tiktok/sample-now", { method: "POST" });
|
||
const n = Number(r.enqueued || 0);
|
||
const live = Number(r.live_count || 0);
|
||
if (n > 0) {
|
||
toast.push(`正在从 ${live} 路在播中录制 ${n} 条`, "ok");
|
||
} else {
|
||
toast.push(String(r.reason || "没有可取样的在播直播间"), "err");
|
||
}
|
||
void load();
|
||
};
|
||
|
||
const fillQuota = async () => {
|
||
try {
|
||
const r = await fetchJson("/api/tiktok/fill-quota", { method: "POST" });
|
||
const n = Number(r.just_quota_filled || 0);
|
||
toast.push(n > 0 ? `已补入 ${n} 条待录任务` : "今日配额已满或暂无在播可补", n > 0 ? "ok" : "warn");
|
||
void load();
|
||
} catch (e: any) {
|
||
toast.push(e?.message || "补齐失败", "err");
|
||
}
|
||
};
|
||
|
||
const toUploadTarget = (u: {
|
||
id: string;
|
||
anchor_name?: string;
|
||
room_id?: string;
|
||
channel_name?: string;
|
||
channel_pk?: string;
|
||
upload_at?: number;
|
||
clip_ready?: boolean;
|
||
clip_seconds?: number;
|
||
status?: string;
|
||
}): TikTokUploadTarget => {
|
||
const ch = channels.find((c) => c.id === (u.channel_pk || ""));
|
||
return {
|
||
id: u.id,
|
||
anchor_name: u.anchor_name,
|
||
room_id: u.room_id,
|
||
channel_name: u.channel_name || ch?.name || "",
|
||
channel_pk: u.channel_pk,
|
||
upload_at: u.upload_at,
|
||
clip_ready: u.clip_ready !== false,
|
||
clip_seconds: Number(u.clip_seconds || 0) || undefined,
|
||
status: u.status,
|
||
title_template: ch?.title_template || "",
|
||
};
|
||
};
|
||
|
||
const retryJob = async (jobId: string) => {
|
||
await fetchJson(`/api/tiktok/jobs/${jobId}/retry`, { method: "POST" });
|
||
toast.push("已重新排队", "ok");
|
||
void load();
|
||
};
|
||
|
||
const startEdit = (ch: Channel) => {
|
||
setEditId(ch.id);
|
||
setFormName(ch.name || "TikTok 账号");
|
||
setFormPrivacy(ch.privacy_level || "PUBLIC_TO_EVERYONE");
|
||
setFormTitle(ch.title_template || "");
|
||
setFormSocksEnabled(Boolean(ch.socks_enabled));
|
||
setFormSocksProfileId(String(ch.socks_profile_id || socksProfiles[0]?.id || ""));
|
||
setShowForm(true);
|
||
};
|
||
|
||
const saveSocksProfiles = async () => {
|
||
setSavingSocks(true);
|
||
try {
|
||
const r = await fetchJson("/api/tiktok/socks-profiles", {
|
||
method: "PUT",
|
||
body: JSON.stringify({ profiles: socksDraft }),
|
||
});
|
||
const next = Array.isArray(r?.socks_profiles) ? (r.socks_profiles as SocksProfile[]) : [];
|
||
setSocksProfiles(next);
|
||
setSocksDraft(next.map((p) => ({ ...p })));
|
||
toast.push("SOCKS 配置已保存", "ok");
|
||
void load();
|
||
} catch (e: any) {
|
||
toast.push(e?.message || "保存 SOCKS 失败", "err");
|
||
} finally {
|
||
setSavingSocks(false);
|
||
}
|
||
};
|
||
|
||
const statusLabel = useMemo(
|
||
() => ({
|
||
pending: "待录制",
|
||
recording: "录制中",
|
||
recorded: "成片就绪",
|
||
scheduled: "已排程",
|
||
uploading: "上传中",
|
||
uploaded: "已上传",
|
||
failed: "失败",
|
||
}),
|
||
[],
|
||
);
|
||
|
||
const cookieAgeLabel = (ch?: Channel) => {
|
||
if (!ch?.authorized) return "未配置 Cookie";
|
||
const days = Number(ch.cookie_age_days ?? 0);
|
||
const left = Number(ch.cookie_expires_in_days ?? 0);
|
||
const used =
|
||
days < 1 ? `已用 ${Math.max(0, Math.round(days * 24))} 小时` : `已用 ${days} 天`;
|
||
if (ch.cookie_status === "expired") return `${used} · 已过期,请更新`;
|
||
if (ch.cookie_status === "warn") return `${used} · 即将过期(剩 ${left} 天)`;
|
||
return `${used} · 约剩 ${left} 天`;
|
||
};
|
||
|
||
const cookieAgeClass = (ch?: Channel) => {
|
||
if (!ch?.authorized || ch.cookie_status === "missing") return "text-rose-300";
|
||
if (ch.cookie_status === "expired") return "text-rose-300";
|
||
if (ch.cookie_status === "warn") return "text-amber-300";
|
||
return "text-teal-300";
|
||
};
|
||
|
||
const displayChannels: BoardChannel[] = (() => {
|
||
const fromBoard = boardChannels.length
|
||
? boardChannels
|
||
: channels.map((c) => ({
|
||
id: c.id,
|
||
name: c.name,
|
||
tiktok_username: c.tiktok_username,
|
||
enabled: c.enabled,
|
||
authorized: c.authorized,
|
||
used: 0,
|
||
scheduled: 0,
|
||
uploaded: 0,
|
||
pending: 0,
|
||
gap: 15,
|
||
max_met: false,
|
||
daily_max: 15,
|
||
}));
|
||
const seen = new Set(fromBoard.map((c) => c.id));
|
||
const extra = channels
|
||
.filter((c) => !seen.has(c.id))
|
||
.map((c) => ({
|
||
id: c.id,
|
||
name: c.name,
|
||
tiktok_username: c.tiktok_username,
|
||
enabled: c.enabled,
|
||
authorized: c.authorized,
|
||
used: 0,
|
||
scheduled: 0,
|
||
uploaded: 0,
|
||
pending: 0,
|
||
gap: 15,
|
||
max_met: false,
|
||
daily_max: 15,
|
||
}));
|
||
return [...fromBoard, ...extra];
|
||
})();
|
||
|
||
if (loading && !status) {
|
||
return (
|
||
<Shell>
|
||
<PageLoader label="加载 TikTok…" />
|
||
</Shell>
|
||
);
|
||
}
|
||
|
||
return (
|
||
<Shell>
|
||
<TikTokUploadConfirmModal
|
||
target={uploadUx.confirmTarget}
|
||
channels={channels}
|
||
onCancel={uploadUx.cancelConfirm}
|
||
onConfirm={(channelPk) => uploadUx.confirmUpload(channelPk)}
|
||
busy={uploadUx.busy}
|
||
/>
|
||
<div className="py-4 sm:py-6">
|
||
{/* Header */}
|
||
<div className="mb-6 flex flex-wrap items-center justify-between gap-3">
|
||
<div>
|
||
<h1 className="font-display flex items-center gap-2 text-xl font-semibold text-slate-50">
|
||
<Music2 className="h-6 w-6 text-pink-400" />
|
||
TikTok 自动上传
|
||
</h1>
|
||
<p className="mt-1 text-sm text-slate-500">
|
||
Cookie 上传 · TiktokAutoUploader · 抖音直播录屏 → TikTok
|
||
</p>
|
||
</div>
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => void load()}
|
||
className="rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm text-slate-200 hover:bg-white/10"
|
||
>
|
||
<RefreshCw className="mr-1 inline h-4 w-4" />
|
||
刷新
|
||
</button>
|
||
{setupDone && (
|
||
<>
|
||
<button
|
||
type="button"
|
||
onClick={() => void sampleNow()}
|
||
className="rounded-xl border border-pink-500/30 bg-pink-500/10 px-3 py-2 text-sm text-pink-200 hover:bg-pink-500/20"
|
||
>
|
||
立即取样
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => void fillQuota()}
|
||
className="rounded-xl border border-teal-500/30 bg-teal-500/10 px-3 py-2 text-sm text-teal-200 hover:bg-teal-500/20"
|
||
>
|
||
补齐配额
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => void togglePipeline()}
|
||
className={`rounded-xl px-4 py-2 text-sm font-medium ${
|
||
status?.enabled
|
||
? "border border-amber-500/40 bg-amber-500/15 text-amber-200"
|
||
: "bg-pink-500 text-white hover:bg-pink-400"
|
||
}`}
|
||
>
|
||
{status?.enabled ? "暂停自动上传" : "启动自动上传"}
|
||
</button>
|
||
</>
|
||
)}
|
||
</div>
|
||
</div>
|
||
|
||
{/* 首次使用:仅显示引导或表单,不堆叠 */}
|
||
{!hasChannels && !showForm && !showSessionForm && (
|
||
<section className="mb-6 rounded-2xl border border-pink-500/25 bg-gradient-to-br from-pink-500/10 to-transparent p-5">
|
||
<h2 className="mb-1 text-base font-medium text-slate-100">开始使用 TikTok 上传</h2>
|
||
<p className="mb-4 text-sm text-slate-400">
|
||
无需 TikTok 开发者应用。浏览器登录 tiktok.com 后复制{" "}
|
||
<code className="text-pink-300">sessionid</code>,粘贴即可上传。
|
||
</p>
|
||
<ol className="mb-5 list-inside list-decimal space-y-2 text-sm text-slate-300">
|
||
<li>点击下方「添加账号」</li>
|
||
<li>保存后在弹窗中粘贴 Cookie</li>
|
||
<li>全部账号配置完成后,再点「启动自动上传」</li>
|
||
</ol>
|
||
<button
|
||
type="button"
|
||
onClick={openAddForm}
|
||
className="flex items-center gap-2 rounded-xl bg-pink-500 px-4 py-2.5 text-sm font-medium text-white hover:bg-pink-400"
|
||
>
|
||
<Plus className="h-4 w-4" />
|
||
添加 TikTok 账号
|
||
</button>
|
||
{setup?.note && <p className="mt-4 text-xs text-amber-300/90">⚠️ {setup.note}</p>}
|
||
</section>
|
||
)}
|
||
|
||
{/* 有账号但未全部配置 Cookie */}
|
||
{hasChannels && !allReady && !showForm && !showSessionForm && (
|
||
<section className="mb-6 rounded-xl border border-amber-500/25 bg-amber-500/5 px-4 py-3 text-sm text-amber-100/90">
|
||
还有 {channels.length - authorizedCount} 个账号未粘贴 Cookie,配置完成后才能启动自动上传。
|
||
</section>
|
||
)}
|
||
|
||
{/* Summary cards */}
|
||
{setupDone && (
|
||
<div className="mb-6 grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
|
||
<div className="rounded-2xl border border-white/10 bg-black/25 p-4">
|
||
<div className="text-xs text-slate-500">今日剩余配额</div>
|
||
<div className="mt-1 text-2xl font-semibold text-pink-300">{status?.quota_gap ?? "—"}</div>
|
||
<div className="text-[11px] text-slate-500">{beijingWindow}</div>
|
||
</div>
|
||
<div className="rounded-2xl border border-white/10 bg-black/25 p-4">
|
||
<div className="text-xs text-slate-500">抖音直播中</div>
|
||
<div className="mt-1 text-2xl font-semibold text-teal-300">{status?.douyin_live_count ?? 0}</div>
|
||
<div className="text-[11px] text-slate-500">可取样来源</div>
|
||
</div>
|
||
<div className="rounded-2xl border border-white/10 bg-black/25 p-4">
|
||
<div className="text-xs text-slate-500">即将上传</div>
|
||
<div className="mt-1 text-2xl font-semibold text-amber-300">{upcoming.length}</div>
|
||
<div className="text-[11px] text-slate-500">已排程素材</div>
|
||
</div>
|
||
<div className="rounded-2xl border border-white/10 bg-black/25 p-4">
|
||
<div className="text-xs text-slate-500">运行状态</div>
|
||
<div className="mt-1 flex items-center gap-2">
|
||
<span className={`h-2 w-2 rounded-full ${status?.enabled ? "bg-teal-400" : "bg-slate-500"}`} />
|
||
<span className="text-sm text-slate-200">{status?.enabled ? "自动上传中" : "已暂停"}</span>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
)}
|
||
|
||
{/* 有账号时:顶部操作栏 */}
|
||
{hasChannels && !showForm && !showSessionForm && (
|
||
<div className="mb-6 flex flex-wrap items-center gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={openAddForm}
|
||
className="flex items-center gap-1 rounded-xl border border-white/10 bg-white/5 px-3 py-2 text-sm text-slate-200 hover:bg-white/10"
|
||
>
|
||
<Plus className="h-4 w-4" />
|
||
新增账号
|
||
</button>
|
||
<Link
|
||
href="https://www.tiktok.com"
|
||
target="_blank"
|
||
className="flex items-center gap-1 rounded-xl border border-white/10 px-3 py-2 text-xs text-slate-400 hover:text-slate-200"
|
||
>
|
||
<ExternalLink className="h-3 w-3" />
|
||
打开 TikTok 登录
|
||
</Link>
|
||
</div>
|
||
)}
|
||
|
||
{/* Channel form */}
|
||
{showForm && (
|
||
<section className="mb-6 rounded-2xl border border-white/10 bg-black/30 p-4">
|
||
<h3 className="mb-3 text-sm font-medium text-slate-100">{editId ? "编辑账号" : "新增 TikTok 账号"}</h3>
|
||
<p className="mb-3 text-xs text-slate-500">
|
||
可添加多个账号,每个账号各自粘贴 Cookie、各自计时,互不影响。
|
||
</p>
|
||
<div className="grid gap-3 sm:grid-cols-2">
|
||
<label className="text-xs text-slate-400">
|
||
显示名称
|
||
<input
|
||
value={formName}
|
||
onChange={(e) => setFormName(e.target.value)}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="text-xs text-slate-400">
|
||
可见性
|
||
<select
|
||
value={formPrivacy}
|
||
onChange={(e) => setFormPrivacy(e.target.value)}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
>
|
||
<option value="PUBLIC_TO_EVERYONE">公开</option>
|
||
<option value="SELF_ONLY">仅自己可见</option>
|
||
<option value="MUTUAL_FOLLOW_FRIENDS">互关好友</option>
|
||
</select>
|
||
</label>
|
||
<label className="text-xs text-slate-400 sm:col-span-2">
|
||
自定义文案(+1,与库内 100 条英文 street food 随机)
|
||
<input
|
||
value={formTitle}
|
||
onChange={(e) => setFormTitle(e.target.value)}
|
||
placeholder="例如 street food"
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<p className="text-[11px] text-slate-500 sm:col-span-2">
|
||
上传时随机:你的自定义文案 + 数据库 100 条英文(#streetfood #fyp),约 ≤150 字;不会自动拼直播间名。
|
||
</p>
|
||
<div className="sm:col-span-2 rounded-xl border border-white/10 bg-white/[0.03] p-3">
|
||
<label className="inline-flex items-center gap-2 text-sm text-slate-200">
|
||
<input
|
||
type="checkbox"
|
||
checked={formSocksEnabled}
|
||
onChange={(e) => setFormSocksEnabled(e.target.checked)}
|
||
/>
|
||
上传走 SOCKS(静态住宅 IP)
|
||
</label>
|
||
<p className="mt-1 text-[11px] text-slate-500">
|
||
默认不勾选(直连)。勾选后使用下方 SOCKS 配置池中的某一套,按账号独立选择。
|
||
</p>
|
||
{formSocksEnabled && (
|
||
<label className="mt-2 block text-xs text-slate-400">
|
||
SOCKS 配置
|
||
<select
|
||
value={formSocksProfileId}
|
||
onChange={(e) => setFormSocksProfileId(e.target.value)}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
>
|
||
{socksProfiles.length === 0 && <option value="">请先在下方添加 SOCKS 配置</option>}
|
||
{socksProfiles.map((p) => (
|
||
<option key={p.id} value={p.id}>
|
||
{p.label || p.server || p.id}
|
||
{p.server ? ` · ${p.server}:${p.port || ""}` : ""}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</label>
|
||
)}
|
||
</div>
|
||
</div>
|
||
<div className="mt-3 flex gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => void (editId ? saveChannel() : addChannel())}
|
||
className="rounded-lg bg-pink-500 px-4 py-2 text-sm text-white hover:bg-pink-400"
|
||
>
|
||
保存
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => setShowForm(false)}
|
||
className="rounded-lg border border-white/10 px-4 py-2 text-sm text-slate-400"
|
||
>
|
||
取消
|
||
</button>
|
||
</div>
|
||
</section>
|
||
)}
|
||
|
||
{showSessionForm && (
|
||
<section className="mb-6 rounded-2xl border border-pink-500/30 bg-black/30 p-4">
|
||
<h3 className="mb-2 text-sm font-medium text-slate-100">粘贴 TikTok Cookie</h3>
|
||
<p className="mb-3 text-xs leading-relaxed text-slate-500">
|
||
每个账号单独保存 Cookie。sessionid 约 30 天过期,保存后会开始计时。sessionid 等同密码,请勿泄露。
|
||
</p>
|
||
<label className="mb-3 block text-xs text-slate-400">
|
||
保存到账号
|
||
<select
|
||
value={sessionChannelId}
|
||
onChange={(e) => {
|
||
const id = e.target.value;
|
||
const ch = channels.find((c) => c.id === id);
|
||
setSessionChannelId(id);
|
||
setSessionChannelName(ch?.name || sessionChannelName);
|
||
setSessionUsername(ch?.tiktok_username || "");
|
||
}}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm text-slate-100"
|
||
>
|
||
{sessionChannelId && !channels.some((c) => c.id === sessionChannelId) && (
|
||
<option value={sessionChannelId}>{sessionChannelName || "新账号"}</option>
|
||
)}
|
||
{channels.map((c) => (
|
||
<option key={c.id} value={c.id}>
|
||
{c.name}
|
||
{c.tiktok_username ? ` · ${c.tiktok_username}` : ""}
|
||
{c.authorized ? ` · ${cookieAgeLabel(c)}` : " · 未配置 Cookie"}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</label>
|
||
{(() => {
|
||
const current = channels.find((c) => c.id === sessionChannelId);
|
||
if (!current?.authorized) return null;
|
||
return (
|
||
<p className={`mb-3 text-xs ${cookieAgeClass(current)}`}>
|
||
当前 Cookie:{cookieAgeLabel(current)}
|
||
{current.cookie_saved_at ? ` · 保存于 ${fmtTs(current.cookie_saved_at)}` : ""}
|
||
。保存新 Cookie 会重新开始计时。
|
||
</p>
|
||
);
|
||
})()}
|
||
<div className="mb-3 flex flex-wrap gap-2">
|
||
<button
|
||
type="button"
|
||
onClick={() => void importFromClipboard()}
|
||
className="rounded-lg border border-pink-500/30 bg-pink-500/10 px-3 py-1.5 text-xs text-pink-200"
|
||
>
|
||
从剪贴板导入
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={importFromExtension}
|
||
className="rounded-lg border border-white/10 bg-white/5 px-3 py-1.5 text-xs text-slate-200"
|
||
>
|
||
从插件读取
|
||
</button>
|
||
</div>
|
||
<div className="grid gap-3 sm:grid-cols-2">
|
||
<label className="text-xs text-slate-400 sm:col-span-2">
|
||
sessionid
|
||
<input
|
||
id="tiktok-sessionid"
|
||
type="password"
|
||
value={sessionId}
|
||
onChange={(e) => setSessionId(e.target.value)}
|
||
placeholder="必填"
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="text-xs text-slate-400">
|
||
tt-target-idc
|
||
<input
|
||
id="tiktok-dc-id"
|
||
value={sessionDcId}
|
||
onChange={(e) => setSessionDcId(e.target.value)}
|
||
placeholder="留空则自动探测"
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="text-xs text-slate-400">
|
||
TikTok 用户名(可选)
|
||
<input
|
||
id="tiktok-username"
|
||
value={sessionUsername}
|
||
onChange={(e) => setSessionUsername(e.target.value)}
|
||
placeholder="@handle"
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
</div>
|
||
<div className="mt-3 flex gap-2">
|
||
<button
|
||
type="button"
|
||
disabled={savingSession}
|
||
onClick={() => void saveSession()}
|
||
className="rounded-lg bg-pink-500 px-4 py-2 text-sm text-white hover:bg-pink-400 disabled:opacity-50"
|
||
>
|
||
{savingSession ? "保存中…" : "保存 Cookie"}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
onClick={() => setShowSessionForm(false)}
|
||
className="rounded-lg border border-white/10 px-4 py-2 text-sm text-slate-400"
|
||
>
|
||
取消
|
||
</button>
|
||
</div>
|
||
</section>
|
||
)}
|
||
|
||
{/* TikTok SOCKS profile pool */}
|
||
<section className="mb-6 rounded-2xl border border-cyan-500/20 bg-cyan-500/5 p-4">
|
||
<div className="mb-3 flex flex-wrap items-start justify-between gap-2">
|
||
<div>
|
||
<h3 className="text-sm font-medium text-slate-100">TikTok 上传 SOCKS(静态住宅 IP)</h3>
|
||
<p className="mt-1 text-[11px] leading-relaxed text-slate-500">
|
||
先在这里维护多套 SOCKS,再在每个账号的「编辑」里勾选并选择要用哪一套。默认不勾选 = 直连上传。
|
||
</p>
|
||
</div>
|
||
<button
|
||
type="button"
|
||
onClick={() => void saveSocksProfiles()}
|
||
disabled={savingSocks}
|
||
className="rounded-lg bg-cyan-500/20 px-3 py-2 text-xs text-cyan-100 hover:bg-cyan-500/30 disabled:opacity-50"
|
||
>
|
||
{savingSocks ? "保存中…" : "保存 SOCKS 配置"}
|
||
</button>
|
||
</div>
|
||
{(() => {
|
||
const selected =
|
||
socksDraft.find((p) => String(p.id) === String(socksSelectedId)) || socksDraft[0] || null;
|
||
const setList = (next: SocksProfile[]) => {
|
||
setSocksDraft(next);
|
||
if (!next.some((p) => String(p.id) === String(socksSelectedId))) {
|
||
setSocksSelectedId(String(next[0]?.id || ""));
|
||
}
|
||
};
|
||
const patchSelected = (patch: Partial<SocksProfile>) => {
|
||
if (!selected) return;
|
||
setList(
|
||
socksDraft.map((p) => (String(p.id) === String(selected.id) ? { ...p, ...patch } : p)),
|
||
);
|
||
};
|
||
return (
|
||
<div className="space-y-3">
|
||
<div className="flex flex-wrap items-end gap-2">
|
||
<label className="block min-w-[12rem] flex-1 text-xs text-slate-400">
|
||
当前配置
|
||
<select
|
||
value={String(selected?.id || "")}
|
||
onChange={(e) => setSocksSelectedId(e.target.value)}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
>
|
||
{socksDraft.map((p) => (
|
||
<option key={p.id} value={p.id}>
|
||
{p.label || p.server || p.id}
|
||
</option>
|
||
))}
|
||
</select>
|
||
</label>
|
||
<button
|
||
type="button"
|
||
onClick={() => {
|
||
const id =
|
||
typeof crypto !== "undefined" && crypto.randomUUID
|
||
? crypto.randomUUID()
|
||
: `socks-${Date.now()}`;
|
||
const next = [
|
||
...socksDraft,
|
||
{
|
||
id,
|
||
label: `住宅 ${socksDraft.length + 1}`,
|
||
type: "socks5",
|
||
server: "",
|
||
port: 1080,
|
||
username: "",
|
||
password: "",
|
||
},
|
||
];
|
||
setList(next);
|
||
setSocksSelectedId(id);
|
||
}}
|
||
className="rounded-lg border border-white/10 bg-white/5 px-3 py-2 text-xs text-slate-200 hover:bg-white/10"
|
||
>
|
||
新增配置
|
||
</button>
|
||
{selected && (
|
||
<button
|
||
type="button"
|
||
onClick={() => {
|
||
if (socksDraft.length <= 0) return;
|
||
const next = socksDraft.filter((p) => String(p.id) !== String(selected.id));
|
||
setList(next);
|
||
}}
|
||
className="rounded-lg border border-rose-500/30 bg-rose-500/10 px-3 py-2 text-xs text-rose-200"
|
||
>
|
||
删除当前
|
||
</button>
|
||
)}
|
||
</div>
|
||
{selected ? (
|
||
<div className="grid gap-3 sm:grid-cols-2">
|
||
<label className="text-xs text-slate-400">
|
||
名称
|
||
<input
|
||
value={selected.label || ""}
|
||
onChange={(e) => patchSelected({ label: e.target.value })}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="text-xs text-slate-400">
|
||
类型
|
||
<select
|
||
value={selected.type || "socks5"}
|
||
onChange={(e) => patchSelected({ type: e.target.value })}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
>
|
||
<option value="socks5">socks5</option>
|
||
<option value="socks5h">socks5h</option>
|
||
</select>
|
||
</label>
|
||
<label className="text-xs text-slate-400">
|
||
主机
|
||
<input
|
||
value={selected.server || ""}
|
||
onChange={(e) => patchSelected({ server: e.target.value })}
|
||
placeholder="x.x.x.x"
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="text-xs text-slate-400">
|
||
端口
|
||
<input
|
||
type="number"
|
||
value={Number(selected.port || 0) || ""}
|
||
onChange={(e) => patchSelected({ port: Number(e.target.value) || 0 })}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="text-xs text-slate-400">
|
||
用户名
|
||
<input
|
||
value={selected.username || ""}
|
||
onChange={(e) => patchSelected({ username: e.target.value })}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
<label className="text-xs text-slate-400">
|
||
密码
|
||
<input
|
||
type="password"
|
||
value={selected.password || ""}
|
||
onChange={(e) => patchSelected({ password: e.target.value })}
|
||
placeholder={selected.has_password ? "已保存(留空或 *** 表示不改)" : ""}
|
||
className="mt-1 w-full rounded-lg border border-white/10 bg-black/40 px-3 py-2 text-sm"
|
||
/>
|
||
</label>
|
||
</div>
|
||
) : (
|
||
<p className="text-xs text-amber-200">暂无 SOCKS 配置,请点「新增配置」。</p>
|
||
)}
|
||
</div>
|
||
);
|
||
})()}
|
||
</section>
|
||
|
||
{/* Two-column board */}
|
||
{channels.length > 0 && (
|
||
<div className="mb-6 grid gap-4 xl:grid-cols-[1.4fr_1fr] xl:items-start">
|
||
<section ref={leftPanelRef} className="rounded-2xl border border-white/10 bg-black/25 p-4">
|
||
<div className="mb-3 flex items-center justify-between">
|
||
<h3 className="text-sm font-medium text-slate-100">账号配额({channels.length})</h3>
|
||
<span className="text-[11px] text-slate-500">
|
||
北京日 {beijingWindow || "—"} · 按账号日上限合计(默认可改)
|
||
</span>
|
||
</div>
|
||
<div className="grid gap-3 sm:grid-cols-2">
|
||
{displayChannels.map((ch) => {
|
||
const full = channels.find((x) => x.id === ch.id);
|
||
const pct = Math.min(100, Math.round(((ch.used || 0) / Math.max(1, ch.daily_max || 15)) * 100));
|
||
return (
|
||
<div
|
||
key={ch.id}
|
||
className={`rounded-xl border bg-gradient-to-br from-white/[0.04] to-transparent p-3 ${
|
||
full?.enabled === false
|
||
? "border-white/10 opacity-60"
|
||
: full?.platform_limited
|
||
? "border-amber-500/40"
|
||
: ch.max_met
|
||
? "border-teal-500/30"
|
||
: "border-white/10"
|
||
}`}
|
||
>
|
||
<div className="flex items-start justify-between gap-2">
|
||
<div className="flex min-w-0 items-center gap-2.5">
|
||
{full?.avatar_url ? (
|
||
// eslint-disable-next-line @next/next/no-img-element
|
||
<img
|
||
src={full.avatar_url}
|
||
alt=""
|
||
className="h-10 w-10 shrink-0 rounded-full object-cover ring-1 ring-white/10"
|
||
/>
|
||
) : (
|
||
<div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-full bg-pink-500/15 text-xs text-pink-200">
|
||
TT
|
||
</div>
|
||
)}
|
||
<div className="min-w-0">
|
||
<div className="truncate font-medium text-slate-100">
|
||
{full?.nickname || ch.name || "未命名"}
|
||
</div>
|
||
<div className="truncate text-[10px] text-slate-500">
|
||
{full?.tiktok_username || ch.tiktok_username
|
||
? `@${(full?.tiktok_username || ch.tiktok_username || "").replace(/^@/, "")}`
|
||
: ch.name || "—"}
|
||
{full?.tiktok_uid ? ` · id ${full.tiktok_uid}` : ""}
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div className="flex shrink-0 flex-col items-end gap-1.5">
|
||
{full ? (
|
||
<EnableSwitch
|
||
checked={full.enabled !== false}
|
||
busy={togglingId === full.id}
|
||
accent="pink"
|
||
label={full.enabled === false ? "关" : "开"}
|
||
onChange={() => void toggleChannelEnabled(full)}
|
||
/>
|
||
) : null}
|
||
<span className={`text-[10px] ${cookieAgeClass(full)}`}>
|
||
{full?.authorized ? "Cookie 就绪" : "未配置"}
|
||
</span>
|
||
</div>
|
||
</div>
|
||
{(full?.follower_count || full?.video_count || full?.heart_count) ? (
|
||
<div className="mt-2 flex flex-wrap gap-2 text-[10px] text-slate-400">
|
||
<span>粉丝 {fmtCount(full?.follower_count)}</span>
|
||
<span>视频 {fmtCount(full?.video_count)}</span>
|
||
<span>赞 {fmtCount(full?.heart_count)}</span>
|
||
</div>
|
||
) : full?.authorized ? (
|
||
<div className="mt-1 text-[10px] text-slate-500">
|
||
{full.profile_error || "资料未拉取,可点「刷新资料」"}
|
||
</div>
|
||
) : null}
|
||
<div className={`mt-1 text-[10px] ${cookieAgeClass(full)}`}>{cookieAgeLabel(full)}</div>
|
||
{full?.authorized && full.cookie_saved_at ? (
|
||
<div className="text-[10px] text-slate-500">保存于 {fmtTs(full.cookie_saved_at)}</div>
|
||
) : null}
|
||
<div className="mt-1 text-[10px] text-slate-500">
|
||
{full?.socks_enabled
|
||
? `SOCKS · ${full.socks_profile_label || full.socks_profile_id || "已启用"}`
|
||
: "SOCKS · 直连"}
|
||
</div>
|
||
{full?.platform_limited ? (
|
||
<div className="mt-1 rounded-lg border border-amber-500/30 bg-amber-500/10 px-2 py-1 text-[10px] leading-relaxed text-amber-100">
|
||
官方限流中
|
||
{full.platform_limit_msg ? `:${full.platform_limit_msg}` : ""}
|
||
。每小时自动试传,成功即解除。
|
||
{full.platform_limit_probe_at
|
||
? ` 上次试传 ${fmtTs(full.platform_limit_probe_at)}`
|
||
: ""}
|
||
</div>
|
||
) : null}
|
||
<div className="mt-2 h-1.5 overflow-hidden rounded-full bg-white/10">
|
||
<div className="h-full rounded-full bg-pink-500/70" style={{ width: `${pct}%` }} />
|
||
</div>
|
||
<div className="mt-2 flex flex-wrap gap-2 text-[10px] text-slate-400">
|
||
<span>已传 {ch.uploaded ?? 0}</span>
|
||
<span>排程 {ch.scheduled ?? 0}</span>
|
||
<span>待录 {ch.pending ?? 0}</span>
|
||
<span className={ch.max_met ? "text-teal-300" : "text-amber-300"}>
|
||
{ch.max_met ? "已满" : `余 ${ch.gap ?? 0}`}
|
||
</span>
|
||
</div>
|
||
<div className="mt-2 flex flex-wrap gap-1">
|
||
<button
|
||
type="button"
|
||
onClick={() => openSessionForm(ch.id, full)}
|
||
className="inline-flex items-center gap-1 rounded-lg bg-pink-500/15 px-2 py-1 text-[11px] text-pink-200"
|
||
>
|
||
<KeyRound className="h-3 w-3" />
|
||
{full?.authorized ? "更新 Cookie" : "粘贴 Cookie"}
|
||
</button>
|
||
{full?.authorized ? (
|
||
<button
|
||
type="button"
|
||
onClick={() => void refreshProfile(full)}
|
||
className="inline-flex items-center gap-1 rounded-lg bg-white/5 px-2 py-1 text-[11px] text-slate-300"
|
||
>
|
||
<RefreshCw className="h-3 w-3" />
|
||
刷新资料
|
||
</button>
|
||
) : null}
|
||
{full && (
|
||
<button
|
||
type="button"
|
||
onClick={() => startEdit(full)}
|
||
className="rounded-lg bg-white/5 px-2 py-1 text-[11px]"
|
||
>
|
||
编辑
|
||
</button>
|
||
)}
|
||
<button
|
||
type="button"
|
||
onClick={() => void removeChannel(ch.id)}
|
||
className="inline-flex items-center gap-1 rounded-lg bg-rose-500/10 px-2 py-1 text-[11px] text-rose-200"
|
||
>
|
||
<Trash2 className="h-3 w-3" />
|
||
</button>
|
||
</div>
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
</section>
|
||
|
||
<section
|
||
ref={rightPanelRef}
|
||
className="flex flex-col overflow-hidden rounded-2xl border border-white/10 bg-black/25 p-4"
|
||
>
|
||
<div className="mb-3 flex items-center justify-between">
|
||
<div className="flex items-center gap-2">
|
||
<Clock3 className="h-4 w-4 text-amber-300" />
|
||
<h3 className="text-sm font-medium text-slate-100">即将上传</h3>
|
||
</div>
|
||
<span className="rounded-full border border-amber-500/25 bg-amber-500/10 px-2 py-0.5 text-xs text-amber-200">
|
||
素材 {upcoming.length}
|
||
</span>
|
||
</div>
|
||
{board?.timeline?.hours ? (
|
||
<div className="mb-3 shrink-0 rounded-xl border border-white/5 bg-black/20 p-2.5">
|
||
<UploadTimeline
|
||
hours={board.timeline.hours}
|
||
nowHour={board.timeline.now_hour}
|
||
onUploadNow={(j) => {
|
||
const full = upcoming.find((u: { id: string }) => u.id === j.id);
|
||
if (full) uploadUx.openConfirm(toUploadTarget(full));
|
||
}}
|
||
/>
|
||
</div>
|
||
) : null}
|
||
<div className="min-h-0 flex-1 space-y-2 overflow-y-auto pr-1">
|
||
{upcoming.length === 0 ? (
|
||
<div className="rounded-xl border border-dashed border-white/10 py-8 text-center text-sm text-slate-500">
|
||
暂无排程。启动自动上传后,系统会从抖音直播中取样。
|
||
</div>
|
||
) : (
|
||
upcoming.map((u) => (
|
||
<TikTokUpcomingUploadRow
|
||
key={u.id}
|
||
item={toUploadTarget(u)}
|
||
track={uploadUx.tracks[u.id]}
|
||
onRequestUpload={uploadUx.openConfirm}
|
||
onRetry={uploadUx.retryUpload}
|
||
/>
|
||
))
|
||
)}
|
||
</div>
|
||
</section>
|
||
</div>
|
||
)}
|
||
|
||
{/* Jobs table — 有账号即显示 */}
|
||
{hasChannels && (
|
||
<section className="rounded-2xl border border-white/10 bg-black/25 p-4">
|
||
<div className="mb-3 flex items-center justify-between">
|
||
<h3 className="text-sm font-medium text-slate-100">任务流水(今日 {jobTotal})</h3>
|
||
</div>
|
||
<div className="table-scroll">
|
||
<table className="w-full text-left text-sm">
|
||
<thead>
|
||
<tr className="border-b border-white/10 text-xs text-slate-500">
|
||
<th className="py-2 pr-3">主播</th>
|
||
<th className="py-2 pr-3">账号</th>
|
||
<th className="py-2 pr-3">时长</th>
|
||
<th className="py-2 pr-3">状态</th>
|
||
<th className="py-2 pr-3">时间</th>
|
||
<th className="py-2">操作</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{jobs.length === 0 ? (
|
||
<tr>
|
||
<td colSpan={6} className="py-8 text-center text-slate-500">
|
||
暂无任务
|
||
</td>
|
||
</tr>
|
||
) : (
|
||
jobs.map((j) => (
|
||
<tr key={j.id} className="border-b border-white/5">
|
||
<td className="py-2 pr-3 text-slate-200">{j.anchor_name || "—"}</td>
|
||
<td className="py-2 pr-3 text-slate-400">
|
||
{j.channel_name || (j.channel_pk ? "—" : "待分配")}
|
||
</td>
|
||
<td className="py-2 pr-3 tabular-nums text-xs text-slate-300">
|
||
{Number(j.clip_seconds || 0) > 0 ? (
|
||
fmtDuration(Number(j.clip_seconds))
|
||
) : j.clip_ready ? (
|
||
<span className="text-slate-500">成片就绪</span>
|
||
) : j.status === "recording" || j.status === "pending" ? (
|
||
<span className="text-amber-300/80">录制中</span>
|
||
) : (
|
||
"—"
|
||
)}
|
||
</td>
|
||
<td className="py-2 pr-3">
|
||
<span
|
||
className={
|
||
j.status === "uploaded"
|
||
? "text-teal-300"
|
||
: j.status === "failed"
|
||
? "text-rose-300"
|
||
: j.status === "uploading"
|
||
? "text-pink-300"
|
||
: "text-slate-400"
|
||
}
|
||
>
|
||
{statusLabel[j.status as keyof typeof statusLabel] || j.status}
|
||
</span>
|
||
{j.error && (
|
||
<div className="max-w-xs text-[10px] text-rose-400/80" title={j.error}>
|
||
<div className="font-medium text-rose-300">
|
||
{(j as any).error_class?.label || "失败"}
|
||
{(j as any).error_class?.action_label
|
||
? ` · ${(j as any).error_class.action_label}`
|
||
: ""}
|
||
</div>
|
||
<div className="truncate">{j.error}</div>
|
||
</div>
|
||
)}
|
||
</td>
|
||
<td className="py-2 pr-3 text-xs text-slate-500">
|
||
{j.finished_at ? fmtTs(j.finished_at) : j.created_at ? fmtTs(j.created_at) : "—"}
|
||
</td>
|
||
<td className="py-2">
|
||
<div className="flex gap-1">
|
||
{j.clip_ready && j.status !== "uploaded" && (
|
||
<button
|
||
type="button"
|
||
disabled={uploadUx.busy || uploadUx.tracks[j.id]?.phase === "uploading"}
|
||
onClick={() => uploadUx.openConfirm(toUploadTarget(j))}
|
||
className="rounded bg-pink-500/15 px-2 py-0.5 text-[10px] text-pink-200 hover:bg-pink-500/25 disabled:opacity-50"
|
||
>
|
||
{uploadUx.tracks[j.id]?.phase === "uploading"
|
||
? "上传中…"
|
||
: j.status === "uploading"
|
||
? "重试上传"
|
||
: "立即上传"}
|
||
</button>
|
||
)}
|
||
{j.status === "failed" && (
|
||
<button
|
||
type="button"
|
||
onClick={() => void retryJob(j.id)}
|
||
className="rounded bg-white/5 px-2 py-0.5 text-[10px]"
|
||
>
|
||
重试
|
||
</button>
|
||
)}
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
))
|
||
)}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</section>
|
||
)}
|
||
|
||
{/* Quick help — 未完成配置时显示 */}
|
||
{!allReady && (
|
||
<section className="mt-6 rounded-2xl border border-white/5 bg-black/15 p-4 text-xs text-slate-500">
|
||
<div className="mb-2 flex items-center gap-2 font-medium text-slate-400">
|
||
<Zap className="h-4 w-4 text-pink-400" />
|
||
如何获取 Cookie
|
||
</div>
|
||
<ol className="list-inside list-decimal space-y-1">
|
||
<li>
|
||
加载 Chrome 插件:<code className="text-pink-300">extensions/dylive-helper/dist</code>
|
||
(chrome://extensions → 开发者模式 → 加载已解压)
|
||
</li>
|
||
<li>
|
||
登录{" "}
|
||
<a href="https://www.tiktok.com" target="_blank" rel="noreferrer" className="text-pink-300 hover:underline">
|
||
tiktok.com
|
||
</a>
|
||
,再在本页点「从插件读取」,或插件 TikTok 页「填入控制台」
|
||
</li>
|
||
<li>确认 sessionid 已填入后点「保存 Cookie」</li>
|
||
</ol>
|
||
</section>
|
||
)}
|
||
</div>
|
||
</Shell>
|
||
);
|
||
}
|