Avoid F12 cookie copy: read HttpOnly sessionid, fill dylive paste form. Co-authored-by: Cursor <cursoragent@cursor.com>
854 lines
33 KiB
TypeScript
854 lines
33 KiB
TypeScript
"use client";
|
||
|
||
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
||
import Link from "next/link";
|
||
import {
|
||
Clock3,
|
||
ExternalLink,
|
||
KeyRound,
|
||
Loader2,
|
||
Music2,
|
||
Plus,
|
||
RefreshCw,
|
||
Trash2,
|
||
Upload,
|
||
Zap,
|
||
} from "lucide-react";
|
||
|
||
import { Shell } from "@/components/Shell";
|
||
import { useConfirm } from "@/components/ConfirmDialog";
|
||
import { useToast } from "@/components/Toast";
|
||
import { PageLoader } from "@/components/ui";
|
||
import { fetchJson, fmtTs, todayBeijingISO } from "@/lib/api";
|
||
|
||
type Channel = {
|
||
id: string;
|
||
name: string;
|
||
tiktok_username?: string;
|
||
enabled?: boolean;
|
||
authorized?: boolean;
|
||
oauth_ok?: number;
|
||
oauth_error?: string;
|
||
privacy_level?: string;
|
||
title_template?: string;
|
||
description_template?: string;
|
||
note?: string;
|
||
};
|
||
|
||
type BoardChannel = {
|
||
id: string;
|
||
name?: string;
|
||
tiktok_username?: string;
|
||
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;
|
||
upload_at?: number;
|
||
status?: string;
|
||
can_upload_now?: boolean;
|
||
};
|
||
|
||
type Job = {
|
||
id: string;
|
||
anchor_name?: string;
|
||
channel_name?: string;
|
||
status?: string;
|
||
push_date?: string;
|
||
error?: string;
|
||
clip_ready?: boolean;
|
||
tiktok_publish_id?: string;
|
||
created_at?: number;
|
||
finished_at?: number;
|
||
};
|
||
|
||
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 [status, setStatus] = useState<any>(null);
|
||
const [channels, setChannels] = useState<Channel[]>([]);
|
||
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("{anchor} #fyp");
|
||
const [formDesc, setFormDesc] = useState("{anchor}\n{url}");
|
||
|
||
const [sessionChannelId, setSessionChannelId] = useState("");
|
||
const [sessionId, setSessionId] = useState("");
|
||
const [sessionDcId, setSessionDcId] = useState("useast2a");
|
||
const [sessionUsername, setSessionUsername] = useState("");
|
||
const [savingSession, setSavingSession] = useState(false);
|
||
const [showSessionForm, setShowSessionForm] = useState(false);
|
||
|
||
const [uploadingId, setUploadingId] = useState("");
|
||
|
||
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 || []);
|
||
setJobs(j?.jobs || []);
|
||
setJobTotal(j?.total || 0);
|
||
setLoading(false);
|
||
}, []);
|
||
|
||
useEffect(() => {
|
||
void load();
|
||
const t = setInterval(() => void load(), 12000);
|
||
return () => clearInterval(t);
|
||
}, [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("{anchor} #fyp");
|
||
setFormDesc("{anchor}\n{url}");
|
||
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) => {
|
||
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"] || "").trim();
|
||
const username = String((data as any)?.tiktok_username || "").trim();
|
||
if (!sessionid) throw new Error("剪贴板里没有 sessionid");
|
||
setSessionId(sessionid);
|
||
setSessionDcId(dc_id || "useast2a");
|
||
if (username) setSessionUsername(username);
|
||
};
|
||
|
||
const importFromClipboard = async () => {
|
||
try {
|
||
const text = await navigator.clipboard.readText();
|
||
applyCookiePayload(text);
|
||
toast.push("已从剪贴板填入 Cookie", "ok");
|
||
} catch (e: any) {
|
||
toast.push(e?.message || "读取剪贴板失败", "err");
|
||
}
|
||
};
|
||
|
||
const importFromExtension = () => {
|
||
window.dispatchEvent(new Event("dylive-tiktok-cookie-request"));
|
||
};
|
||
|
||
useEffect(() => {
|
||
const onFill = (ev: Event) => {
|
||
const detail = (ev as CustomEvent).detail;
|
||
try {
|
||
applyCookiePayload(detail);
|
||
toast.push("已从插件填入 Cookie", "ok");
|
||
} catch (e: any) {
|
||
toast.push(e?.message || "插件数据无效", "err");
|
||
}
|
||
};
|
||
window.addEventListener("dylive-tiktok-cookie", onFill as EventListener);
|
||
return () => window.removeEventListener("dylive-tiktok-cookie", onFill as EventListener);
|
||
}, [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() || "useast2a",
|
||
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: formDesc,
|
||
}),
|
||
});
|
||
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: formDesc,
|
||
}),
|
||
});
|
||
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);
|
||
setSessionId("");
|
||
setSessionDcId("useast2a");
|
||
setSessionUsername(ch?.tiktok_username || "");
|
||
setShowForm(false);
|
||
setShowSessionForm(true);
|
||
};
|
||
|
||
const sampleNow = async () => {
|
||
const r = await fetchJson("/api/tiktok/sample-now", { method: "POST" });
|
||
toast.push(`已取样 ${r.enqueued || 0} 条`, "ok");
|
||
void load();
|
||
};
|
||
|
||
const uploadNow = async (jobId: string) => {
|
||
setUploadingId(jobId);
|
||
try {
|
||
await fetchJson(`/api/tiktok/jobs/${jobId}/upload-now`, { method: "POST" });
|
||
toast.push("上传成功", "ok");
|
||
void load();
|
||
} catch (e: any) {
|
||
toast.push(e?.message || "上传失败", "err");
|
||
} finally {
|
||
setUploadingId("");
|
||
}
|
||
};
|
||
|
||
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 || "{anchor} #fyp");
|
||
setFormDesc(ch.description_template || "{anchor}\n{url}");
|
||
setShowForm(true);
|
||
};
|
||
|
||
const statusLabel = useMemo(
|
||
() => ({
|
||
pending: "待录制",
|
||
recording: "录制中",
|
||
recorded: "成片就绪",
|
||
scheduled: "已排程",
|
||
uploading: "上传中",
|
||
uploaded: "已上传",
|
||
failed: "失败",
|
||
}),
|
||
[],
|
||
);
|
||
|
||
const displayChannels: BoardChannel[] = 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: 9,
|
||
max_met: false,
|
||
daily_max: 9,
|
||
}));
|
||
|
||
if (loading && !status) {
|
||
return (
|
||
<Shell>
|
||
<PageLoader label="加载 TikTok…" />
|
||
</Shell>
|
||
);
|
||
}
|
||
|
||
return (
|
||
<Shell>
|
||
<div className="mx-auto max-w-7xl px-4 py-6 sm:px-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 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>
|
||
<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">
|
||
标题模板
|
||
<input
|
||
value={formTitle}
|
||
onChange={(e) => setFormTitle(e.target.value)}
|
||
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"
|
||
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">
|
||
推荐用 Chrome 插件或油猴导出 Cookie(无需 F12)。sessionid 等同密码,请勿泄露。
|
||
</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="useast2a"
|
||
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>
|
||
)}
|
||
|
||
{/* 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 || "—"} · 每账号 9 条/日</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 || 9)) * 100));
|
||
return (
|
||
<div
|
||
key={ch.id}
|
||
className={`rounded-xl border bg-gradient-to-br from-white/[0.04] to-transparent p-3 ${
|
||
ch.max_met ? "border-teal-500/30" : "border-white/10"
|
||
}`}
|
||
>
|
||
<div className="flex items-start justify-between gap-2">
|
||
<div className="min-w-0">
|
||
<div className="truncate font-medium text-slate-100">{ch.name || "未命名"}</div>
|
||
<div className="truncate text-[10px] text-slate-500">{ch.tiktok_username || "—"}</div>
|
||
</div>
|
||
<span className={`text-[10px] ${full?.authorized ? "text-teal-300" : "text-rose-300"}`}>
|
||
{full?.authorized ? "Cookie 就绪" : "未配置"}
|
||
</span>
|
||
</div>
|
||
<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 && (
|
||
<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>
|
||
<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) => (
|
||
<div
|
||
key={u.id}
|
||
className="flex items-center justify-between gap-2 rounded-xl border border-white/10 bg-white/[0.03] p-3"
|
||
>
|
||
<div className="min-w-0">
|
||
<div className="truncate text-sm text-slate-100">{u.anchor_name || "—"}</div>
|
||
<div className="truncate text-[10px] text-slate-500">
|
||
{u.channel_name} · {u.upload_at ? fmtTs(u.upload_at) : "—"}
|
||
</div>
|
||
</div>
|
||
{u.can_upload_now && (
|
||
<button
|
||
type="button"
|
||
disabled={uploadingId === u.id}
|
||
onClick={() => void uploadNow(u.id)}
|
||
className="shrink-0 rounded-lg bg-teal-500/20 px-2 py-1 text-[11px] text-teal-200 hover:bg-teal-500/30 disabled:opacity-50"
|
||
>
|
||
{uploadingId === u.id ? (
|
||
<Loader2 className="h-3 w-3 animate-spin" />
|
||
) : (
|
||
<>
|
||
<Upload className="mr-1 inline h-3 w-3" />
|
||
立即上传
|
||
</>
|
||
)}
|
||
</button>
|
||
)}
|
||
</div>
|
||
))
|
||
)}
|
||
</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="overflow-x-auto">
|
||
<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">操作</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{jobs.length === 0 ? (
|
||
<tr>
|
||
<td colSpan={5} 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 || "—"}</td>
|
||
<td className="py-2 pr-3">
|
||
<span
|
||
className={
|
||
j.status === "uploaded"
|
||
? "text-teal-300"
|
||
: j.status === "failed"
|
||
? "text-rose-300"
|
||
: "text-slate-400"
|
||
}
|
||
>
|
||
{statusLabel[j.status as keyof typeof statusLabel] || j.status}
|
||
</span>
|
||
{j.error && (
|
||
<div className="max-w-xs truncate text-[10px] text-rose-400/80" title={j.error}>
|
||
{j.error}
|
||
</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={uploadingId === j.id}
|
||
onClick={() => void uploadNow(j.id)}
|
||
className="rounded bg-teal-500/15 px-2 py-0.5 text-[10px] text-teal-200"
|
||
>
|
||
上传
|
||
</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/tiktok-cookie-helper</code>
|
||
(chrome://extensions → 开发者模式 → 加载已解压)
|
||
</li>
|
||
<li>
|
||
登录{" "}
|
||
<a href="https://www.tiktok.com" target="_blank" rel="noreferrer" className="text-pink-300 hover:underline">
|
||
tiktok.com
|
||
</a>
|
||
,点插件「复制 JSON」或「填入控制台」
|
||
</li>
|
||
<li>本页打开「粘贴 Cookie」→「从剪贴板导入」→ 保存</li>
|
||
</ol>
|
||
</section>
|
||
)}
|
||
</div>
|
||
</Shell>
|
||
);
|
||
}
|