Compare commits
13 Commits
b79117df9a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc81e0daf1 | ||
|
|
ed13262166 | ||
|
|
b882a5c7d7 | ||
|
|
8e4e3de974 | ||
|
|
0815967ca7 | ||
|
|
dfc7dc217d | ||
|
|
d8a09dcb8b | ||
|
|
5614a13ebb | ||
| 1567021d57 | |||
|
|
9c37141399 | ||
| 7bb4e0c493 | |||
|
|
08bde54d74 | ||
|
|
ab3a544eec |
@@ -54,5 +54,6 @@ WECOM_NOTIFY_ENABLED=true
|
|||||||
# ── 调度与入口 ────────────────────────────────────────────
|
# ── 调度与入口 ────────────────────────────────────────────
|
||||||
# 每天北京时间 09:00 自动执行采集计划,生产保持 true。
|
# 每天北京时间 09:00 自动执行采集计划,生产保持 true。
|
||||||
ENABLE_SCHEDULER=true
|
ENABLE_SCHEDULER=true
|
||||||
|
# 是否在空库时写入演示数据,生产保持 false。
|
||||||
SEED_DEMO_DATA=false
|
SEED_DEMO_DATA=false
|
||||||
HTTP_PORT=80
|
HTTP_PORT=80
|
||||||
|
|||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -24,6 +24,9 @@
|
|||||||
*.pem
|
*.pem
|
||||||
.codegraph/
|
.codegraph/
|
||||||
.ipynb_checkpoints/
|
.ipynb_checkpoints/
|
||||||
|
/.idea/
|
||||||
|
AGENTS.md
|
||||||
|
CLAUDE.md
|
||||||
|
|
||||||
# debug
|
# debug
|
||||||
npm-debug.log*
|
npm-debug.log*
|
||||||
|
|||||||
@@ -74,6 +74,28 @@ type Account = {
|
|||||||
last_seen_at: string;
|
last_seen_at: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type FieldRefreshStatus = {
|
||||||
|
status: "running" | "done";
|
||||||
|
fields: string[];
|
||||||
|
total: number;
|
||||||
|
processed: number;
|
||||||
|
updated: number;
|
||||||
|
failed: number;
|
||||||
|
skipped: number;
|
||||||
|
startedAt: string;
|
||||||
|
finishedAt: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
const REFRESH_FIELD_OPTIONS = [
|
||||||
|
{ key: "publicAccountId", label: "账号ID(小红书号/抖音号)" },
|
||||||
|
{ key: "nickname", label: "昵称" },
|
||||||
|
{ key: "followers", label: "粉丝数" },
|
||||||
|
{ key: "ipLocation", label: "IP属地" },
|
||||||
|
{ key: "gender", label: "性别" },
|
||||||
|
{ key: "bio", label: "简介" },
|
||||||
|
{ key: "tags", label: "标签(AI 重新打标,覆盖已有标签)" },
|
||||||
|
] as const;
|
||||||
|
|
||||||
type ResourceImportPreview = {
|
type ResourceImportPreview = {
|
||||||
summary: { total: number; create: number; update: number; error: number };
|
summary: { total: number; create: number; update: number; error: number };
|
||||||
rows: Array<{
|
rows: Array<{
|
||||||
@@ -498,6 +520,9 @@ export default function Home({ currentUser }: { currentUser: AuthUser }) {
|
|||||||
const [activeNav, setActiveNav] = useState<NavKey>("overview");
|
const [activeNav, setActiveNav] = useState<NavKey>("overview");
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [working, setWorking] = useState(false);
|
const [working, setWorking] = useState(false);
|
||||||
|
const [collectingDistributionIds, setCollectingDistributionIds] = useState<Set<string>>(
|
||||||
|
new Set(),
|
||||||
|
);
|
||||||
const [error, setError] = useState("");
|
const [error, setError] = useState("");
|
||||||
const [toast, setToast] = useState("");
|
const [toast, setToast] = useState("");
|
||||||
const [menuOpen, setMenuOpen] = useState(false);
|
const [menuOpen, setMenuOpen] = useState(false);
|
||||||
@@ -767,10 +792,23 @@ export default function Home({ currentUser }: { currentUser: AuthUser }) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const collectMetrics = async (distribution: Distribution) => {
|
const collectMetrics = async (distribution: Distribution) => {
|
||||||
await runAction(
|
setCollectingDistributionIds((current) => {
|
||||||
{ action: "collect_now", distributionId: distribution.id },
|
const next = new Set(current);
|
||||||
"公开数据已更新",
|
next.add(distribution.id);
|
||||||
);
|
return next;
|
||||||
|
});
|
||||||
|
try {
|
||||||
|
await runAction(
|
||||||
|
{ action: "collect_now", distributionId: distribution.id },
|
||||||
|
"公开数据已更新",
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
setCollectingDistributionIds((current) => {
|
||||||
|
const next = new Set(current);
|
||||||
|
next.delete(distribution.id);
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const updateDistributionPublishUrl = async (
|
const updateDistributionPublishUrl = async (
|
||||||
@@ -1141,6 +1179,7 @@ export default function Home({ currentUser }: { currentUser: AuthUser }) {
|
|||||||
tasks={data.tasks}
|
tasks={data.tasks}
|
||||||
distributions={data.distributions}
|
distributions={data.distributions}
|
||||||
working={working}
|
working={working}
|
||||||
|
collectingDistributionIds={collectingDistributionIds}
|
||||||
canUpdatePublishUrl={isManager}
|
canUpdatePublishUrl={isManager}
|
||||||
onCollect={collectMetrics}
|
onCollect={collectMetrics}
|
||||||
onUpdatePublishUrl={updateDistributionPublishUrl}
|
onUpdatePublishUrl={updateDistributionPublishUrl}
|
||||||
@@ -3143,6 +3182,22 @@ function ScreenshotTaskTable({ distributions }: { distributions: Distribution[]
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const RESOURCE_PAGE_SIZE = 15;
|
||||||
|
|
||||||
|
function pageList(current: number, total: number): Array<number | "gap"> {
|
||||||
|
if (total <= 7) {
|
||||||
|
return Array.from({ length: total }, (_, index) => index + 1);
|
||||||
|
}
|
||||||
|
const items: Array<number | "gap"> = [1];
|
||||||
|
const start = Math.max(2, current - 1);
|
||||||
|
const end = Math.min(total - 1, current + 1);
|
||||||
|
if (start > 2) items.push("gap");
|
||||||
|
for (let page = start; page <= end; page += 1) items.push(page);
|
||||||
|
if (end < total - 1) items.push("gap");
|
||||||
|
items.push(total);
|
||||||
|
return items;
|
||||||
|
}
|
||||||
|
|
||||||
function ResourcesPage({
|
function ResourcesPage({
|
||||||
accounts,
|
accounts,
|
||||||
distributions,
|
distributions,
|
||||||
@@ -3160,6 +3215,7 @@ function ResourcesPage({
|
|||||||
const [platformFilter, setPlatformFilter] = useState("全部平台");
|
const [platformFilter, setPlatformFilter] = useState("全部平台");
|
||||||
const [ipFilter, setIpFilter] = useState("");
|
const [ipFilter, setIpFilter] = useState("");
|
||||||
const [sourceFilter, setSourceFilter] = useState("");
|
const [sourceFilter, setSourceFilter] = useState("");
|
||||||
|
const [page, setPage] = useState(1);
|
||||||
const [importOpen, setImportOpen] = useState(false);
|
const [importOpen, setImportOpen] = useState(false);
|
||||||
const [importMode, setImportMode] = useState<"bulk" | "single">("bulk");
|
const [importMode, setImportMode] = useState<"bulk" | "single">("bulk");
|
||||||
const [importFile, setImportFile] = useState<File | null>(null);
|
const [importFile, setImportFile] = useState<File | null>(null);
|
||||||
@@ -3179,6 +3235,24 @@ function ResourcesPage({
|
|||||||
});
|
});
|
||||||
const [singleWorking, setSingleWorking] = useState(false);
|
const [singleWorking, setSingleWorking] = useState(false);
|
||||||
const [singleError, setSingleError] = useState("");
|
const [singleError, setSingleError] = useState("");
|
||||||
|
const [editAccount, setEditAccount] = useState<Account | null>(null);
|
||||||
|
const [editAccountId, setEditAccountId] = useState("");
|
||||||
|
const [editWorking, setEditWorking] = useState(false);
|
||||||
|
const [editError, setEditError] = useState("");
|
||||||
|
const [idRefresh, setIdRefresh] = useState<FieldRefreshStatus | null>(null);
|
||||||
|
const [idRefreshStarting, setIdRefreshStarting] = useState(false);
|
||||||
|
const idRefreshNotifiedRef = useRef(true);
|
||||||
|
const [idRefreshError, setIdRefreshError] = useState("");
|
||||||
|
const [fieldPickOpen, setFieldPickOpen] = useState(false);
|
||||||
|
const [fieldChecks, setFieldChecks] = useState<Record<string, boolean>>(
|
||||||
|
() =>
|
||||||
|
Object.fromEntries(
|
||||||
|
REFRESH_FIELD_OPTIONS.map((option) => [
|
||||||
|
option.key,
|
||||||
|
option.key === "publicAccountId",
|
||||||
|
]),
|
||||||
|
),
|
||||||
|
);
|
||||||
const resourceAccounts = useMemo(() => {
|
const resourceAccounts = useMemo(() => {
|
||||||
const deliveriesByAccount = new Map<string, Distribution[]>();
|
const deliveriesByAccount = new Map<string, Distribution[]>();
|
||||||
distributions.forEach((distribution) => {
|
distributions.forEach((distribution) => {
|
||||||
@@ -3271,11 +3345,21 @@ function ResourcesPage({
|
|||||||
platformFilter !== "全部平台" ||
|
platformFilter !== "全部平台" ||
|
||||||
Boolean(ipFilter.trim()) ||
|
Boolean(ipFilter.trim()) ||
|
||||||
Boolean(sourceFilter.trim());
|
Boolean(sourceFilter.trim());
|
||||||
|
const totalPages = Math.max(
|
||||||
|
1,
|
||||||
|
Math.ceil(filteredAccounts.length / RESOURCE_PAGE_SIZE),
|
||||||
|
);
|
||||||
|
const currentPage = Math.min(page, totalPages);
|
||||||
|
const pagedAccounts = filteredAccounts.slice(
|
||||||
|
(currentPage - 1) * RESOURCE_PAGE_SIZE,
|
||||||
|
currentPage * RESOURCE_PAGE_SIZE,
|
||||||
|
);
|
||||||
const clearFilters = () => {
|
const clearFilters = () => {
|
||||||
setQuery("");
|
setQuery("");
|
||||||
setPlatformFilter("全部平台");
|
setPlatformFilter("全部平台");
|
||||||
setIpFilter("");
|
setIpFilter("");
|
||||||
setSourceFilter("");
|
setSourceFilter("");
|
||||||
|
setPage(1);
|
||||||
};
|
};
|
||||||
const closeImport = () => {
|
const closeImport = () => {
|
||||||
if (importWorking || singleWorking) return;
|
if (importWorking || singleWorking) return;
|
||||||
@@ -3386,6 +3470,126 @@ function ResourcesPage({
|
|||||||
setImportWorking(false);
|
setImportWorking(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const closeAccountEdit = () => {
|
||||||
|
if (editWorking) return;
|
||||||
|
setEditAccount(null);
|
||||||
|
setEditAccountId("");
|
||||||
|
setEditError("");
|
||||||
|
};
|
||||||
|
const submitAccountEdit = async () => {
|
||||||
|
if (!editAccount) return;
|
||||||
|
const value = editAccountId.trim();
|
||||||
|
if (!value) {
|
||||||
|
setEditError("请填写新的账号ID");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!/^[\p{L}\p{N}._-]{2,80}$/u.test(value)) {
|
||||||
|
setEditError("账号ID需为 2-80 位字母、数字或 . _ - 组合");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
setEditWorking(true);
|
||||||
|
setEditError("");
|
||||||
|
const response = await fetch("/api/action", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
action: "set_public_account_ids",
|
||||||
|
items: [{ accountId: editAccount.id, publicAccountId: value }],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
const result = await readApiResponse<{ error?: string }>(response, "保存失败");
|
||||||
|
if (!response.ok) throw new Error(result.error || "保存失败");
|
||||||
|
const label = editAccount.nickname || editAccount.public_account_id || "该账号";
|
||||||
|
setEditAccount(null);
|
||||||
|
setEditAccountId("");
|
||||||
|
await onImported(`已更新 ${label} 的账号ID`);
|
||||||
|
} catch (reason) {
|
||||||
|
setEditError(reason instanceof Error ? reason.message : "保存失败");
|
||||||
|
} finally {
|
||||||
|
setEditWorking(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
const runningIdRefresh = idRefresh?.status === "running";
|
||||||
|
const fetchIdRefreshStatus = useCallback(async () => {
|
||||||
|
const response = await fetch("/api/action", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ action: "account_field_refresh_status" }),
|
||||||
|
});
|
||||||
|
const result = await readApiResponse<{ refresh: FieldRefreshStatus | null }>(
|
||||||
|
response,
|
||||||
|
"查询失败",
|
||||||
|
);
|
||||||
|
if (!response.ok) throw new Error(result.error || "查询失败");
|
||||||
|
return result.refresh;
|
||||||
|
}, []);
|
||||||
|
const applyIdRefreshStatus = useCallback(
|
||||||
|
(status: FieldRefreshStatus) => {
|
||||||
|
if (status.status === "done" && !idRefreshNotifiedRef.current) {
|
||||||
|
idRefreshNotifiedRef.current = true;
|
||||||
|
const summary = `账号资料更新完成:成功 ${status.updated} 个${
|
||||||
|
status.skipped ? `,未识别 ${status.skipped} 个` : ""
|
||||||
|
}${status.failed ? `,失败 ${status.failed} 个` : ""}`;
|
||||||
|
void onImported(summary);
|
||||||
|
}
|
||||||
|
setIdRefresh(status);
|
||||||
|
},
|
||||||
|
[onImported],
|
||||||
|
);
|
||||||
|
useEffect(() => {
|
||||||
|
void fetchIdRefreshStatus()
|
||||||
|
.then((status) => {
|
||||||
|
if (status?.status === "running") {
|
||||||
|
idRefreshNotifiedRef.current = false;
|
||||||
|
applyIdRefreshStatus(status);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => undefined);
|
||||||
|
}, [applyIdRefreshStatus, fetchIdRefreshStatus]);
|
||||||
|
useEffect(() => {
|
||||||
|
if (!runningIdRefresh) return;
|
||||||
|
const timer = window.setInterval(() => {
|
||||||
|
void fetchIdRefreshStatus()
|
||||||
|
.then((status) => {
|
||||||
|
if (status) applyIdRefreshStatus(status);
|
||||||
|
})
|
||||||
|
.catch(() => undefined);
|
||||||
|
}, 3000);
|
||||||
|
return () => window.clearInterval(timer);
|
||||||
|
}, [applyIdRefreshStatus, fetchIdRefreshStatus, runningIdRefresh]);
|
||||||
|
const closeFieldPick = () => {
|
||||||
|
if (idRefreshStarting) return;
|
||||||
|
setFieldPickOpen(false);
|
||||||
|
};
|
||||||
|
const startIdRefresh = async () => {
|
||||||
|
if (runningIdRefresh || idRefreshStarting) return;
|
||||||
|
const fields = REFRESH_FIELD_OPTIONS.filter(
|
||||||
|
(option) => fieldChecks[option.key],
|
||||||
|
).map((option) => option.key);
|
||||||
|
if (fields.length === 0) return;
|
||||||
|
try {
|
||||||
|
setIdRefreshStarting(true);
|
||||||
|
setIdRefreshError("");
|
||||||
|
const response = await fetch("/api/action", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ action: "refresh_account_fields", fields }),
|
||||||
|
});
|
||||||
|
const result = await readApiResponse<{ refresh: FieldRefreshStatus }>(
|
||||||
|
response,
|
||||||
|
"启动失败",
|
||||||
|
);
|
||||||
|
if (!response.ok) throw new Error(result.error || "启动失败");
|
||||||
|
idRefreshNotifiedRef.current = false;
|
||||||
|
setIdRefresh(result.refresh);
|
||||||
|
setFieldPickOpen(false);
|
||||||
|
} catch (reason) {
|
||||||
|
setIdRefreshError(reason instanceof Error ? reason.message : "启动失败");
|
||||||
|
} finally {
|
||||||
|
setIdRefreshStarting(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
return (
|
return (
|
||||||
<div className="stack">
|
<div className="stack">
|
||||||
<section className="resource-intro">
|
<section className="resource-intro">
|
||||||
@@ -3415,7 +3619,10 @@ function ResourcesPage({
|
|||||||
<div className="filter-chips">
|
<div className="filter-chips">
|
||||||
<button
|
<button
|
||||||
className={platformFilter === "全部平台" ? "active" : ""}
|
className={platformFilter === "全部平台" ? "active" : ""}
|
||||||
onClick={() => setPlatformFilter("全部平台")}
|
onClick={() => {
|
||||||
|
setPlatformFilter("全部平台");
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
全部平台
|
全部平台
|
||||||
</button>
|
</button>
|
||||||
@@ -3423,7 +3630,10 @@ function ResourcesPage({
|
|||||||
<button
|
<button
|
||||||
className={platformFilter === platform ? "active" : ""}
|
className={platformFilter === platform ? "active" : ""}
|
||||||
key={platform}
|
key={platform}
|
||||||
onClick={() => setPlatformFilter(platform)}
|
onClick={() => {
|
||||||
|
setPlatformFilter(platform);
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<PlatformBadge platform={platform} compact />
|
<PlatformBadge platform={platform} compact />
|
||||||
</button>
|
</button>
|
||||||
@@ -3436,7 +3646,10 @@ function ResourcesPage({
|
|||||||
<span aria-hidden="true">⌕</span>
|
<span aria-hidden="true">⌕</span>
|
||||||
<input
|
<input
|
||||||
aria-label="搜索账号名称、账号ID或当前联系人"
|
aria-label="搜索账号名称、账号ID或当前联系人"
|
||||||
onChange={(event) => setQuery(event.target.value)}
|
onChange={(event) => {
|
||||||
|
setQuery(event.target.value);
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
placeholder="搜索账号名称 / 账号ID / 当前联系人 / 标签"
|
placeholder="搜索账号名称 / 账号ID / 当前联系人 / 标签"
|
||||||
type="search"
|
type="search"
|
||||||
value={query}
|
value={query}
|
||||||
@@ -3447,7 +3660,10 @@ function ResourcesPage({
|
|||||||
<input
|
<input
|
||||||
aria-label="模糊搜索IP地区"
|
aria-label="模糊搜索IP地区"
|
||||||
list="resource-ip-options"
|
list="resource-ip-options"
|
||||||
onChange={(event) => setIpFilter(event.target.value)}
|
onChange={(event) => {
|
||||||
|
setIpFilter(event.target.value);
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
placeholder="搜索IP地区"
|
placeholder="搜索IP地区"
|
||||||
type="search"
|
type="search"
|
||||||
value={ipFilter}
|
value={ipFilter}
|
||||||
@@ -3461,7 +3677,10 @@ function ResourcesPage({
|
|||||||
<input
|
<input
|
||||||
aria-label="模糊搜索合作来源"
|
aria-label="模糊搜索合作来源"
|
||||||
list="resource-source-options"
|
list="resource-source-options"
|
||||||
onChange={(event) => setSourceFilter(event.target.value)}
|
onChange={(event) => {
|
||||||
|
setSourceFilter(event.target.value);
|
||||||
|
setPage(1);
|
||||||
|
}}
|
||||||
placeholder="搜索合作来源"
|
placeholder="搜索合作来源"
|
||||||
type="search"
|
type="search"
|
||||||
value={sourceFilter}
|
value={sourceFilter}
|
||||||
@@ -3480,7 +3699,19 @@ function ResourcesPage({
|
|||||||
清空筛选
|
清空筛选
|
||||||
</button>
|
</button>
|
||||||
<div className="resource-toolbar-summary">
|
<div className="resource-toolbar-summary">
|
||||||
<span>共 <b>{filteredAccounts.length}</b> 个结果</span>
|
<span>共 <b>{filteredAccounts.length}</b> 个结果{totalPages > 1 ? ` · 第 ${currentPage}/${totalPages} 页` : ""}</span>
|
||||||
|
<button
|
||||||
|
className="export-data-button"
|
||||||
|
disabled={runningIdRefresh || idRefreshStarting}
|
||||||
|
title="爬取全部账号主页,按选择的字段批量更新账号资料"
|
||||||
|
onClick={() => setFieldPickOpen(true)}
|
||||||
|
>
|
||||||
|
{idRefreshStarting
|
||||||
|
? "正在启动…"
|
||||||
|
: runningIdRefresh
|
||||||
|
? `更新中 ${idRefresh?.processed ?? 0}/${idRefresh?.total ?? 0}`
|
||||||
|
: "批量更新账号资料"}
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
className="export-data-button"
|
className="export-data-button"
|
||||||
disabled={exporting || filteredAccounts.length === 0}
|
disabled={exporting || filteredAccounts.length === 0}
|
||||||
@@ -3491,9 +3722,10 @@ function ResourcesPage({
|
|||||||
{exporting ? "正在导出…" : "导出筛选结果"}
|
{exporting ? "正在导出…" : "导出筛选结果"}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
{idRefreshError && <div className="form-error">{idRefreshError}</div>}
|
||||||
</div>
|
</div>
|
||||||
<div className="resource-grid">
|
<div className="resource-grid">
|
||||||
{filteredAccounts.map(({ account, sources, partnerManagedOnly }) => {
|
{pagedAccounts.map(({ account, sources, partnerManagedOnly }) => {
|
||||||
const profileLink = resourceProfileLink(account);
|
const profileLink = resourceProfileLink(account);
|
||||||
const accountTags = (account.tags || "")
|
const accountTags = (account.tags || "")
|
||||||
.split(/[,,、;;|]/)
|
.split(/[,,、;;|]/)
|
||||||
@@ -3531,6 +3763,18 @@ function ResourcesPage({
|
|||||||
<div className="resource-account-number">
|
<div className="resource-account-number">
|
||||||
<span>{accountIdLabel}</span>
|
<span>{accountIdLabel}</span>
|
||||||
<strong>{account.public_account_id || "待识别"}</strong>
|
<strong>{account.public_account_id || "待识别"}</strong>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="account-id-edit-button"
|
||||||
|
title="手动修改账号ID"
|
||||||
|
onClick={() => {
|
||||||
|
setEditAccount(account);
|
||||||
|
setEditAccountId(account.public_account_id || "");
|
||||||
|
setEditError("");
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
修改
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<div className="resource-platform-line">
|
<div className="resource-platform-line">
|
||||||
<PlatformBadge platform={account.platform} compact />
|
<PlatformBadge platform={account.platform} compact />
|
||||||
@@ -3583,7 +3827,149 @@ function ResourcesPage({
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
|
{totalPages > 1 && (
|
||||||
|
<div className="resource-pagination">
|
||||||
|
<button
|
||||||
|
disabled={currentPage === 1}
|
||||||
|
onClick={() => setPage(currentPage - 1)}
|
||||||
|
>
|
||||||
|
上一页
|
||||||
|
</button>
|
||||||
|
{pageList(currentPage, totalPages).map((item, index) =>
|
||||||
|
item === "gap" ? (
|
||||||
|
<span className="gap" key={`gap-${index}`}>…</span>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
className={item === currentPage ? "active" : ""}
|
||||||
|
key={item}
|
||||||
|
onClick={() => setPage(item)}
|
||||||
|
>
|
||||||
|
{item}
|
||||||
|
</button>
|
||||||
|
),
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
disabled={currentPage === totalPages}
|
||||||
|
onClick={() => setPage(currentPage + 1)}
|
||||||
|
>
|
||||||
|
下一页
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</section>
|
</section>
|
||||||
|
{fieldPickOpen && (
|
||||||
|
<div className="modal-backdrop" role="presentation" onMouseDown={closeFieldPick}>
|
||||||
|
<div
|
||||||
|
className="modal-card compact"
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
aria-labelledby="field-pick-title"
|
||||||
|
onMouseDown={(event) => event.stopPropagation()}
|
||||||
|
>
|
||||||
|
<div className="modal-heading">
|
||||||
|
<div>
|
||||||
|
<p className="eyebrow">批量更新</p>
|
||||||
|
<h2 id="field-pick-title">选择要更新的字段</h2>
|
||||||
|
</div>
|
||||||
|
<button type="button" onClick={closeFieldPick} aria-label="关闭">×</button>
|
||||||
|
</div>
|
||||||
|
<p className="field-pick-note">
|
||||||
|
将逐个爬取全部账号主页(小红书 + 抖音),只写入选中的字段;
|
||||||
|
爬取为空的字段自动跳过,不会清空已有数据。账号较多时需要几分钟。
|
||||||
|
</p>
|
||||||
|
<div className="field-pick-list">
|
||||||
|
{REFRESH_FIELD_OPTIONS.map((option) => (
|
||||||
|
<label className="field-pick-item" key={option.key}>
|
||||||
|
<input
|
||||||
|
checked={Boolean(fieldChecks[option.key])}
|
||||||
|
onChange={(event) =>
|
||||||
|
setFieldChecks((checks) => ({
|
||||||
|
...checks,
|
||||||
|
[option.key]: event.target.checked,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
type="checkbox"
|
||||||
|
/>
|
||||||
|
<span>{option.label}</span>
|
||||||
|
</label>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{idRefreshError && <div className="form-error">{idRefreshError}</div>}
|
||||||
|
<div className="modal-actions">
|
||||||
|
<button type="button" className="ghost-button" onClick={closeFieldPick}>取消</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="primary-button"
|
||||||
|
disabled={
|
||||||
|
idRefreshStarting ||
|
||||||
|
runningIdRefresh ||
|
||||||
|
!REFRESH_FIELD_OPTIONS.some((option) => fieldChecks[option.key])
|
||||||
|
}
|
||||||
|
onClick={() => void startIdRefresh()}
|
||||||
|
>
|
||||||
|
{idRefreshStarting
|
||||||
|
? "正在启动…"
|
||||||
|
: runningIdRefresh
|
||||||
|
? `更新中 ${idRefresh?.processed ?? 0}/${idRefresh?.total ?? 0}`
|
||||||
|
: "开始更新"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{editAccount && (
|
||||||
|
<div className="modal-backdrop" role="presentation" onMouseDown={closeAccountEdit}>
|
||||||
|
<div
|
||||||
|
className="modal-card compact"
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
aria-labelledby="account-id-edit-title"
|
||||||
|
onMouseDown={(event) => event.stopPropagation()}
|
||||||
|
>
|
||||||
|
<div className="modal-heading">
|
||||||
|
<div>
|
||||||
|
<p className="eyebrow">{editAccount.nickname || "账号"}</p>
|
||||||
|
<h2 id="account-id-edit-title">修改账号ID</h2>
|
||||||
|
</div>
|
||||||
|
<button type="button" onClick={closeAccountEdit} aria-label="关闭">×</button>
|
||||||
|
</div>
|
||||||
|
<div className="account-id-edit-form">
|
||||||
|
<label>
|
||||||
|
<span>
|
||||||
|
{editAccount.platform === "小红书"
|
||||||
|
? "小红书号"
|
||||||
|
: editAccount.platform === "抖音"
|
||||||
|
? "抖音号"
|
||||||
|
: "账号ID"}
|
||||||
|
</span>
|
||||||
|
<input
|
||||||
|
autoFocus
|
||||||
|
type="text"
|
||||||
|
value={editAccountId}
|
||||||
|
placeholder="2-80 位字母、数字或 . _ -"
|
||||||
|
onChange={(event) => setEditAccountId(event.target.value)}
|
||||||
|
onKeyDown={(event) => {
|
||||||
|
if (event.key === "Enter") void submitAccountEdit();
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
<small>只更新账号ID,昵称、粉丝数等其他资料保持不变</small>
|
||||||
|
</label>
|
||||||
|
{editError && <div className="form-error">{editError}</div>}
|
||||||
|
</div>
|
||||||
|
<div className="modal-actions">
|
||||||
|
<button type="button" className="ghost-button" onClick={closeAccountEdit}>取消</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="primary-button"
|
||||||
|
disabled={editWorking || !editAccountId.trim()}
|
||||||
|
onClick={() => void submitAccountEdit()}
|
||||||
|
>
|
||||||
|
{editWorking ? "保存中…" : "保存"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
{importOpen && (
|
{importOpen && (
|
||||||
<div className="modal-backdrop" role="presentation" onMouseDown={closeImport}>
|
<div className="modal-backdrop" role="presentation" onMouseDown={closeImport}>
|
||||||
<div
|
<div
|
||||||
@@ -3882,6 +4268,7 @@ function RecoveryPage({
|
|||||||
tasks,
|
tasks,
|
||||||
distributions,
|
distributions,
|
||||||
working,
|
working,
|
||||||
|
collectingDistributionIds,
|
||||||
canUpdatePublishUrl,
|
canUpdatePublishUrl,
|
||||||
onCollect,
|
onCollect,
|
||||||
onUpdatePublishUrl,
|
onUpdatePublishUrl,
|
||||||
@@ -3896,6 +4283,7 @@ function RecoveryPage({
|
|||||||
tasks: Task[];
|
tasks: Task[];
|
||||||
distributions: Distribution[];
|
distributions: Distribution[];
|
||||||
working: boolean;
|
working: boolean;
|
||||||
|
collectingDistributionIds: ReadonlySet<string>;
|
||||||
canUpdatePublishUrl: boolean;
|
canUpdatePublishUrl: boolean;
|
||||||
onCollect: (distribution: Distribution) => void;
|
onCollect: (distribution: Distribution) => void;
|
||||||
onUpdatePublishUrl: (distribution: Distribution) => void;
|
onUpdatePublishUrl: (distribution: Distribution) => void;
|
||||||
@@ -4183,7 +4571,9 @@ function RecoveryPage({
|
|||||||
</span>
|
</span>
|
||||||
) : item.screenshot_key ? (
|
) : item.screenshot_key ? (
|
||||||
<span className="creator-ocr-failed">
|
<span className="creator-ocr-failed">
|
||||||
待KOC填写数据
|
{item.ocr_status === "processing"
|
||||||
|
? "正在识别数据"
|
||||||
|
: "识别失败,请手动填写"}
|
||||||
</span>
|
</span>
|
||||||
) : null}
|
) : null}
|
||||||
{item.screenshot_key &&
|
{item.screenshot_key &&
|
||||||
@@ -4206,7 +4596,12 @@ function RecoveryPage({
|
|||||||
) : (
|
) : (
|
||||||
<button
|
<button
|
||||||
className="collect-button"
|
className="collect-button"
|
||||||
disabled={working || !noteUrl}
|
disabled={
|
||||||
|
!noteUrl ||
|
||||||
|
(working &&
|
||||||
|
(collectingDistributionIds.size === 0 ||
|
||||||
|
collectingDistributionIds.has(item.id)))
|
||||||
|
}
|
||||||
onClick={() => onCollect(item)}
|
onClick={() => onCollect(item)}
|
||||||
>
|
>
|
||||||
{noteUrl ? "立即采集" : "待填链接"}
|
{noteUrl ? "立即采集" : "待填链接"}
|
||||||
|
|||||||
@@ -54,6 +54,10 @@ import {
|
|||||||
} from "../../../lib/wecom-group-push-service";
|
} from "../../../lib/wecom-group-push-service";
|
||||||
import { isManagerRequest } from "../../../lib/user-auth";
|
import { isManagerRequest } from "../../../lib/user-auth";
|
||||||
import { extractPublishUrl } from "../../../lib/publish-url";
|
import { extractPublishUrl } from "../../../lib/publish-url";
|
||||||
|
import {
|
||||||
|
getAccountFieldRefreshStatus,
|
||||||
|
startAccountFieldRefresh,
|
||||||
|
} from "../../../lib/account-field-refresh-service";
|
||||||
|
|
||||||
type ActionBody = {
|
type ActionBody = {
|
||||||
action?: string;
|
action?: string;
|
||||||
@@ -683,6 +687,15 @@ export async function POST(request: Request) {
|
|||||||
const taskId = String(body.taskId ?? "").trim();
|
const taskId = String(body.taskId ?? "").trim();
|
||||||
const pushes = await listGroupPushes(db, taskId || undefined);
|
const pushes = await listGroupPushes(db, taskId || undefined);
|
||||||
return Response.json({ pushes });
|
return Response.json({ pushes });
|
||||||
|
} else if (body.action === "refresh_account_fields") {
|
||||||
|
const status = startAccountFieldRefresh(
|
||||||
|
db,
|
||||||
|
resolveCollectionMcpConfig(env as unknown as CollectionMcpBindings),
|
||||||
|
body.fields,
|
||||||
|
);
|
||||||
|
return Response.json({ refresh: status });
|
||||||
|
} else if (body.action === "account_field_refresh_status") {
|
||||||
|
return Response.json({ refresh: getAccountFieldRefreshStatus() });
|
||||||
} else if (body.action === "wecom_group_chats") {
|
} else if (body.action === "wecom_group_chats") {
|
||||||
const groups = await listGroupChatRows(db);
|
const groups = await listGroupChatRows(db);
|
||||||
return Response.json({ groups });
|
return Response.json({ groups });
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ import {
|
|||||||
getUploadBucket,
|
getUploadBucket,
|
||||||
} from "../../../lib/mvp-db";
|
} from "../../../lib/mvp-db";
|
||||||
import { adminForbidden, isAdminRequest } from "../../../lib/admin-auth";
|
import { adminForbidden, isAdminRequest } from "../../../lib/admin-auth";
|
||||||
|
import { verifyCreatorScreenshotAccessToken } from "../../../lib/creator-screenshot-access";
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
if (!(await isAdminRequest(request))) return adminForbidden();
|
const isAdmin = await isAdminRequest(request);
|
||||||
try {
|
try {
|
||||||
await ensureSchema();
|
await ensureSchema();
|
||||||
const distributionId = new URL(request.url).searchParams
|
const distributionId = new URL(request.url).searchParams
|
||||||
@@ -22,6 +23,18 @@ export async function GET(request: Request) {
|
|||||||
)
|
)
|
||||||
.bind(distributionId)
|
.bind(distributionId)
|
||||||
.first<{ screenshot_key: string | null }>();
|
.first<{ screenshot_key: string | null }>();
|
||||||
|
const mcpToken = new URL(request.url).searchParams.get("mcp_token") || "";
|
||||||
|
if (
|
||||||
|
!isAdmin &&
|
||||||
|
(!row?.screenshot_key ||
|
||||||
|
!verifyCreatorScreenshotAccessToken(
|
||||||
|
distributionId,
|
||||||
|
row.screenshot_key,
|
||||||
|
mcpToken,
|
||||||
|
))
|
||||||
|
) {
|
||||||
|
return adminForbidden();
|
||||||
|
}
|
||||||
if (
|
if (
|
||||||
!row?.screenshot_key ||
|
!row?.screenshot_key ||
|
||||||
!row.screenshot_key.startsWith("creator-center/")
|
!row.screenshot_key.startsWith("creator-center/")
|
||||||
|
|||||||
@@ -13,6 +13,13 @@ import {
|
|||||||
parseResultScreenshotKeys,
|
parseResultScreenshotKeys,
|
||||||
serializeResultScreenshotKeys,
|
serializeResultScreenshotKeys,
|
||||||
} from "../../../lib/result-screenshots";
|
} from "../../../lib/result-screenshots";
|
||||||
|
import { creatorScreenshotMcpUrl } from "../../../lib/creator-screenshot-access";
|
||||||
|
import {
|
||||||
|
extractCreatorMetricsFromMcp,
|
||||||
|
resolveCollectionMcpConfig,
|
||||||
|
type CollectionMcpBindings,
|
||||||
|
} from "../../../lib/mcp-collection-client";
|
||||||
|
import { getRuntimeEnv } from "../../../lib/runtime-env";
|
||||||
|
|
||||||
async function readUpload(request: Request) {
|
async function readUpload(request: Request) {
|
||||||
const contentType = request.headers.get("content-type") ?? "";
|
const contentType = request.headers.get("content-type") ?? "";
|
||||||
@@ -163,13 +170,47 @@ async function handlePost(request: Request) {
|
|||||||
screenshot_key = ?,
|
screenshot_key = ?,
|
||||||
ocr_status = CASE
|
ocr_status = CASE
|
||||||
WHEN exposure IS NOT NULL AND views IS NOT NULL THEN ocr_status
|
WHEN exposure IS NOT NULL AND views IS NOT NULL THEN ocr_status
|
||||||
ELSE 'uploaded'
|
ELSE 'processing'
|
||||||
END,
|
END,
|
||||||
updated_at = CURRENT_TIMESTAMP
|
updated_at = CURRENT_TIMESTAMP
|
||||||
WHERE id = ?`,
|
WHERE id = ?`,
|
||||||
)
|
)
|
||||||
.bind(key, upload.distributionId)
|
.bind(key, upload.distributionId)
|
||||||
.run();
|
.run();
|
||||||
|
try {
|
||||||
|
const metrics = await extractCreatorMetricsFromMcp(
|
||||||
|
creatorScreenshotMcpUrl(request, upload.distributionId, key),
|
||||||
|
resolveCollectionMcpConfig(
|
||||||
|
getRuntimeEnv() as unknown as CollectionMcpBindings,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await getRawDb()
|
||||||
|
.prepare(
|
||||||
|
`UPDATE distributions SET exposure = ?, views = ?,
|
||||||
|
ocr_status = 'success', updated_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE id = ?`,
|
||||||
|
)
|
||||||
|
.bind(metrics.exposure, metrics.views, upload.distributionId)
|
||||||
|
.run();
|
||||||
|
return Response.json({
|
||||||
|
uploaded: true,
|
||||||
|
exposure: metrics.exposure,
|
||||||
|
views: metrics.views,
|
||||||
|
ocrStatus: "success",
|
||||||
|
kind: "creator-center",
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[KOC LOOP] creator screenshot OCR failed", {
|
||||||
|
distributionId: upload.distributionId,
|
||||||
|
detail: error instanceof Error ? error.message : String(error),
|
||||||
|
});
|
||||||
|
await getRawDb()
|
||||||
|
.prepare(
|
||||||
|
`UPDATE distributions SET ocr_status = 'failed', updated_at = CURRENT_TIMESTAMP WHERE id = ?`,
|
||||||
|
)
|
||||||
|
.bind(upload.distributionId)
|
||||||
|
.run();
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
await getRawDb()
|
await getRawDb()
|
||||||
.prepare(
|
.prepare(
|
||||||
@@ -189,6 +230,7 @@ async function handlePost(request: Request) {
|
|||||||
: isCreatorCenter
|
: isCreatorCenter
|
||||||
? "creator-center"
|
? "creator-center"
|
||||||
: "publish",
|
: "publish",
|
||||||
|
ocrStatus: isCreatorCenter ? "failed" : undefined,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return Response.json(
|
return Response.json(
|
||||||
|
|||||||
12
app/api/resources/route.ts
Normal file
12
app/api/resources/route.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { resourceSearch } from "../../../lib/mcp-operations";
|
||||||
|
|
||||||
|
export async function GET(request: Request) {
|
||||||
|
const searchParams = new URL(request.url).searchParams;
|
||||||
|
return Response.json(
|
||||||
|
await resourceSearch({
|
||||||
|
query: searchParams.get("q") ?? "",
|
||||||
|
limit: Number(searchParams.get("limit")) || 50,
|
||||||
|
offset: Number(searchParams.get("offset")) || 0,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -6,6 +6,13 @@ import {
|
|||||||
uid,
|
uid,
|
||||||
} from "../../../lib/mvp-db";
|
} from "../../../lib/mvp-db";
|
||||||
import { adminForbidden, isAdminRequest } from "../../../lib/admin-auth";
|
import { adminForbidden, isAdminRequest } from "../../../lib/admin-auth";
|
||||||
|
import { creatorScreenshotMcpUrl } from "../../../lib/creator-screenshot-access";
|
||||||
|
import {
|
||||||
|
extractCreatorMetricsFromMcp,
|
||||||
|
resolveCollectionMcpConfig,
|
||||||
|
type CollectionMcpBindings,
|
||||||
|
} from "../../../lib/mcp-collection-client";
|
||||||
|
import { getRuntimeEnv } from "../../../lib/runtime-env";
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
if (!(await isAdminRequest(request))) return adminForbidden();
|
if (!(await isAdminRequest(request))) return adminForbidden();
|
||||||
@@ -33,12 +40,37 @@ export async function POST(request: Request) {
|
|||||||
screenshot_key = ?,
|
screenshot_key = ?,
|
||||||
exposure = NULL,
|
exposure = NULL,
|
||||||
views = NULL,
|
views = NULL,
|
||||||
ocr_status = 'failed',
|
ocr_status = 'processing',
|
||||||
updated_at = CURRENT_TIMESTAMP
|
updated_at = CURRENT_TIMESTAMP
|
||||||
WHERE id = ?`,
|
WHERE id = ?`,
|
||||||
)
|
)
|
||||||
.bind(key, distributionId)
|
.bind(key, distributionId)
|
||||||
.run();
|
.run();
|
||||||
|
try {
|
||||||
|
const metrics = await extractCreatorMetricsFromMcp(
|
||||||
|
creatorScreenshotMcpUrl(request, distributionId, key),
|
||||||
|
resolveCollectionMcpConfig(getRuntimeEnv() as unknown as CollectionMcpBindings),
|
||||||
|
);
|
||||||
|
await db
|
||||||
|
.prepare(
|
||||||
|
`UPDATE distributions SET exposure = ?, views = ?,
|
||||||
|
ocr_status = 'success', updated_at = CURRENT_TIMESTAMP
|
||||||
|
WHERE id = ?`,
|
||||||
|
)
|
||||||
|
.bind(metrics.exposure, metrics.views, distributionId)
|
||||||
|
.run();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("[KOC LOOP] creator screenshot OCR failed", {
|
||||||
|
distributionId,
|
||||||
|
detail: error instanceof Error ? error.message : String(error),
|
||||||
|
});
|
||||||
|
await db
|
||||||
|
.prepare(
|
||||||
|
`UPDATE distributions SET ocr_status = 'failed', updated_at = CURRENT_TIMESTAMP WHERE id = ?`,
|
||||||
|
)
|
||||||
|
.bind(distributionId)
|
||||||
|
.run();
|
||||||
|
}
|
||||||
return Response.json(await getDashboardData());
|
return Response.json(await getDashboardData());
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return Response.json(
|
return Response.json(
|
||||||
|
|||||||
113
app/globals.css
113
app/globals.css
@@ -2577,6 +2577,49 @@ a {
|
|||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.resource-pagination {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding-top: 18px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.resource-pagination button {
|
||||||
|
min-width: 30px;
|
||||||
|
height: 30px;
|
||||||
|
padding: 0 10px;
|
||||||
|
border: 1px solid transparent;
|
||||||
|
border-radius: 8px;
|
||||||
|
color: #66736f;
|
||||||
|
background: transparent;
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: 600;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.resource-pagination button:hover:not(:disabled) {
|
||||||
|
background: #f0f4f2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.resource-pagination button.active {
|
||||||
|
color: #34443f;
|
||||||
|
background: white;
|
||||||
|
border-color: #dce2de;
|
||||||
|
box-shadow: 0 1px 3px rgb(18 31 28 / 0.08);
|
||||||
|
}
|
||||||
|
|
||||||
|
.resource-pagination button:disabled {
|
||||||
|
color: #b7c1bd;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
|
.resource-pagination .gap {
|
||||||
|
color: #9aa4a0;
|
||||||
|
font-size: 11px;
|
||||||
|
padding: 0 2px;
|
||||||
|
}
|
||||||
|
|
||||||
.resource-import-modal {
|
.resource-import-modal {
|
||||||
width: min(780px, calc(100vw - 32px));
|
width: min(780px, calc(100vw - 32px));
|
||||||
max-height: min(86vh, 820px);
|
max-height: min(86vh, 820px);
|
||||||
@@ -2972,6 +3015,76 @@ a {
|
|||||||
white-space: nowrap;
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.account-id-edit-button {
|
||||||
|
flex: 0 0 auto;
|
||||||
|
padding: 1px 6px;
|
||||||
|
border: 1px solid #dce2de;
|
||||||
|
border-radius: 999px;
|
||||||
|
color: #8a978f;
|
||||||
|
background: transparent;
|
||||||
|
font-size: 9px;
|
||||||
|
line-height: 1.5;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.account-id-edit-button:hover {
|
||||||
|
border-color: #70ad95;
|
||||||
|
color: #40806a;
|
||||||
|
}
|
||||||
|
|
||||||
|
.account-id-edit-form {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.account-id-edit-form small {
|
||||||
|
color: #9aa4a0;
|
||||||
|
font-size: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field-pick-note {
|
||||||
|
margin: 0 0 14px;
|
||||||
|
color: #8a978f;
|
||||||
|
font-size: 11px;
|
||||||
|
line-height: 1.7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field-pick-list {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 6px;
|
||||||
|
margin-bottom: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field-pick-item {
|
||||||
|
flex-direction: row;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
margin-bottom: 0;
|
||||||
|
padding: 9px 11px;
|
||||||
|
border: 1px solid #eef1ef;
|
||||||
|
border-radius: 9px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field-pick-item:hover {
|
||||||
|
border-color: #d8e2dd;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field-pick-item input {
|
||||||
|
width: 15px;
|
||||||
|
height: 15px;
|
||||||
|
padding: 0;
|
||||||
|
accent-color: #70ad95;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.field-pick-item span {
|
||||||
|
color: #4a5854;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
||||||
.resource-platform-line {
|
.resource-platform-line {
|
||||||
display: flex;
|
display: flex;
|
||||||
min-width: 0;
|
min-width: 0;
|
||||||
|
|||||||
@@ -954,7 +954,7 @@ export default function Home() {
|
|||||||
selectedItem: Assignment,
|
selectedItem: Assignment,
|
||||||
file: File,
|
file: File,
|
||||||
kind: "publish" | "creator-center" | "task-result",
|
kind: "publish" | "creator-center" | "task-result",
|
||||||
) => {
|
): Promise<{ exposure?: number | null; views?: number | null; ocrStatus?: string }> => {
|
||||||
const compressed = await compressScreenshot(file);
|
const compressed = await compressScreenshot(file);
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
"Content-Type": compressed.type || "image/jpeg",
|
"Content-Type": compressed.type || "image/jpeg",
|
||||||
@@ -973,7 +973,12 @@ export default function Home() {
|
|||||||
headers,
|
headers,
|
||||||
body: compressed,
|
body: compressed,
|
||||||
});
|
});
|
||||||
const uploadResult = (await uploadResponse.json()) as { error?: string };
|
const uploadResult = (await uploadResponse.json()) as {
|
||||||
|
error?: string;
|
||||||
|
exposure?: number | null;
|
||||||
|
views?: number | null;
|
||||||
|
ocrStatus?: string;
|
||||||
|
};
|
||||||
if (!uploadResponse.ok) {
|
if (!uploadResponse.ok) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
uploadResult.error ||
|
uploadResult.error ||
|
||||||
@@ -984,6 +989,7 @@ export default function Home() {
|
|||||||
: "发布截图上传失败"),
|
: "发布截图上传失败"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
return uploadResult;
|
||||||
};
|
};
|
||||||
|
|
||||||
const submitNote = async (event: FormEvent) => {
|
const submitNote = async (event: FormEvent) => {
|
||||||
@@ -1082,22 +1088,31 @@ export default function Home() {
|
|||||||
setToast("请选择创作者中心截图");
|
setToast("请选择创作者中心截图");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (
|
|
||||||
!/^\d{1,12}$/.test(creatorExposure) ||
|
|
||||||
!/^\d{1,12}$/.test(creatorViews)
|
|
||||||
) {
|
|
||||||
setToast("请填写正确的曝光量和阅读量");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
setCreatorWorking(true);
|
setCreatorWorking(true);
|
||||||
|
let exposureValue = creatorExposure;
|
||||||
|
let viewsValue = creatorViews;
|
||||||
if (creatorScreenshot) {
|
if (creatorScreenshot) {
|
||||||
setCreatorStage("正在上传截图…");
|
setCreatorStage("正在上传截图…");
|
||||||
await uploadEvidence(
|
const ocrResult = await uploadEvidence(
|
||||||
selected,
|
selected,
|
||||||
creatorScreenshot,
|
creatorScreenshot,
|
||||||
"creator-center",
|
"creator-center",
|
||||||
);
|
);
|
||||||
|
if (typeof ocrResult.exposure === "number") {
|
||||||
|
exposureValue = String(ocrResult.exposure);
|
||||||
|
setCreatorExposure(exposureValue);
|
||||||
|
}
|
||||||
|
if (typeof ocrResult.views === "number") {
|
||||||
|
viewsValue = String(ocrResult.views);
|
||||||
|
setCreatorViews(viewsValue);
|
||||||
|
}
|
||||||
|
if (ocrResult.ocrStatus === "success") {
|
||||||
|
setCreatorStage("已识别截图数据,正在保存…");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!/^\d{1,12}$/.test(exposureValue) || !/^\d{1,12}$/.test(viewsValue)) {
|
||||||
|
throw new Error("未识别到完整数据,请填写曝光量和阅读量");
|
||||||
}
|
}
|
||||||
setCreatorStage("正在保存数据…");
|
setCreatorStage("正在保存数据…");
|
||||||
const response = await fetch(partnerApi("/api/partner"), {
|
const response = await fetch(partnerApi("/api/partner"), {
|
||||||
@@ -1109,8 +1124,8 @@ export default function Home() {
|
|||||||
claimToken,
|
claimToken,
|
||||||
delegationToken,
|
delegationToken,
|
||||||
distributionId: selected.id,
|
distributionId: selected.id,
|
||||||
exposure: creatorExposure,
|
exposure: exposureValue,
|
||||||
views: creatorViews,
|
views: viewsValue,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
const result = (await response.json()) as { error?: string };
|
const result = (await response.json()) as { error?: string };
|
||||||
@@ -1593,7 +1608,7 @@ export default function Home() {
|
|||||||
<div className="evidence-empty-state">
|
<div className="evidence-empty-state">
|
||||||
<b>+</b>
|
<b>+</b>
|
||||||
<strong>点击选择第7天创作者中心截图</strong>
|
<strong>点击选择第7天创作者中心截图</strong>
|
||||||
<small>截图仅用于运营核对,不再自动OCR</small>
|
<small>上传后自动识别曝光量和阅读量,识别失败可手动填写</small>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<label className="evidence-upload-action">
|
<label className="evidence-upload-action">
|
||||||
|
|||||||
@@ -92,7 +92,7 @@ test("keeps claiming minimal and backfill one-to-one", async () => {
|
|||||||
assert.match(page, /submit_creator_metrics/);
|
assert.match(page, /submit_creator_metrics/);
|
||||||
assert.match(page, /creatorExposure/);
|
assert.match(page, /creatorExposure/);
|
||||||
assert.match(page, /creatorViews/);
|
assert.match(page, /creatorViews/);
|
||||||
assert.match(page, /截图仅用于运营核对,不再自动OCR/);
|
assert.match(page, /上传后自动识别曝光量和阅读量,识别失败可手动填写/);
|
||||||
assert.match(page, /evidenceImageUrl/);
|
assert.match(page, /evidenceImageUrl/);
|
||||||
assert.match(page, /params\.set\("v", evidenceKey\)/);
|
assert.match(page, /params\.set\("v", evidenceKey\)/);
|
||||||
assert.match(page, /evidenceImageUrl\(selected, "publish"\)/);
|
assert.match(page, /evidenceImageUrl\(selected, "publish"\)/);
|
||||||
@@ -140,7 +140,7 @@ test("renders screenshot-only tasks with anonymous delegation and multi-image up
|
|||||||
assert.match(styles, /\.evidence-preview-button/);
|
assert.match(styles, /\.evidence-preview-button/);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("shows D1 timestamps in Beijing time", () => {
|
test("shows D1 and MySQL UTC timestamps in Beijing time", () => {
|
||||||
const stored = "2026-07-29 05:36:00";
|
const stored = "2026-07-29 05:36:00";
|
||||||
assert.equal(parseStoredDate(stored).toISOString(), "2026-07-29T05:36:00.000Z");
|
assert.equal(parseStoredDate(stored).toISOString(), "2026-07-29T05:36:00.000Z");
|
||||||
assert.match(formatShanghaiDate(stored, true), /07\/29.*13:36/);
|
assert.match(formatShanghaiDate(stored, true), /07\/29.*13:36/);
|
||||||
|
|||||||
253
lib/account-field-refresh-service.ts
Normal file
253
lib/account-field-refresh-service.ts
Normal file
@@ -0,0 +1,253 @@
|
|||||||
|
import { runInBackground } from "./background";
|
||||||
|
import {
|
||||||
|
generateAccountTagsFromMcp,
|
||||||
|
resolveProfileDetailsFromMcp,
|
||||||
|
type AccountTaggingEnv,
|
||||||
|
type CollectionMcpConfig,
|
||||||
|
} from "./mcp-collection-client";
|
||||||
|
import { getRuntimeEnv } from "./runtime-env";
|
||||||
|
import type { DatabaseClient } from "./database";
|
||||||
|
|
||||||
|
export const ACCOUNT_REFRESH_FIELDS = [
|
||||||
|
"publicAccountId",
|
||||||
|
"nickname",
|
||||||
|
"followers",
|
||||||
|
"ipLocation",
|
||||||
|
"gender",
|
||||||
|
"bio",
|
||||||
|
"tags",
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
export type AccountRefreshField = (typeof ACCOUNT_REFRESH_FIELDS)[number];
|
||||||
|
|
||||||
|
export const ACCOUNT_REFRESH_FIELD_LABELS: Record<AccountRefreshField, string> = {
|
||||||
|
publicAccountId: "账号ID(小红书号/抖音号)",
|
||||||
|
nickname: "昵称",
|
||||||
|
followers: "粉丝数",
|
||||||
|
ipLocation: "IP属地",
|
||||||
|
gender: "性别",
|
||||||
|
bio: "简介",
|
||||||
|
tags: "标签(AI 重新打标,覆盖已有标签)",
|
||||||
|
};
|
||||||
|
|
||||||
|
const FIELD_COLUMNS: Record<AccountRefreshField, string> = {
|
||||||
|
publicAccountId: "public_account_id",
|
||||||
|
nickname: "nickname",
|
||||||
|
followers: "followers",
|
||||||
|
ipLocation: "ip_location",
|
||||||
|
gender: "gender",
|
||||||
|
bio: "bio",
|
||||||
|
tags: "tags",
|
||||||
|
};
|
||||||
|
|
||||||
|
export function sanitizeRefreshFields(value: unknown): AccountRefreshField[] {
|
||||||
|
const requested = Array.isArray(value) ? value : [];
|
||||||
|
const fields = ACCOUNT_REFRESH_FIELDS.filter((field) =>
|
||||||
|
requested.includes(field),
|
||||||
|
);
|
||||||
|
return fields.length > 0 ? fields : ["publicAccountId"];
|
||||||
|
}
|
||||||
|
|
||||||
|
export type AccountFieldRefreshStatus = {
|
||||||
|
status: "running" | "done";
|
||||||
|
fields: AccountRefreshField[];
|
||||||
|
total: number;
|
||||||
|
processed: number;
|
||||||
|
updated: number;
|
||||||
|
failed: number;
|
||||||
|
skipped: number;
|
||||||
|
startedAt: string;
|
||||||
|
finishedAt: string | null;
|
||||||
|
};
|
||||||
|
|
||||||
|
type RefreshTargetRow = {
|
||||||
|
id: string;
|
||||||
|
platform: string;
|
||||||
|
platform_uid: string;
|
||||||
|
profile_url: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ScrapedProfile = {
|
||||||
|
nickname: string | null;
|
||||||
|
redId: string | null;
|
||||||
|
followers: number | null;
|
||||||
|
ipLocation: string | null;
|
||||||
|
gender: string;
|
||||||
|
bio: string;
|
||||||
|
recentNoteTitles: string[];
|
||||||
|
providerTags: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ProfileScraper = (
|
||||||
|
profileUrl: string,
|
||||||
|
platform: "小红书" | "抖音",
|
||||||
|
) => Promise<ScrapedProfile>;
|
||||||
|
|
||||||
|
export type TagGenerator = (input: {
|
||||||
|
platform: string;
|
||||||
|
nickname: string;
|
||||||
|
bio: string;
|
||||||
|
gender: string;
|
||||||
|
ipLocation: string;
|
||||||
|
followers: number | null;
|
||||||
|
providerTags: string[];
|
||||||
|
recentNoteTitles: string[];
|
||||||
|
}) => Promise<string[]>;
|
||||||
|
|
||||||
|
function scrapedFieldValues(
|
||||||
|
details: ScrapedProfile,
|
||||||
|
): Partial<Record<AccountRefreshField, string | number>> {
|
||||||
|
const values: Partial<Record<AccountRefreshField, string | number>> = {};
|
||||||
|
const redId = details.redId?.trim();
|
||||||
|
if (redId) values.publicAccountId = redId;
|
||||||
|
const nickname = details.nickname?.trim();
|
||||||
|
if (nickname) values.nickname = nickname;
|
||||||
|
if (details.followers !== null && Number.isFinite(details.followers)) {
|
||||||
|
values.followers = details.followers;
|
||||||
|
}
|
||||||
|
const ipLocation = details.ipLocation?.trim();
|
||||||
|
if (ipLocation) values.ipLocation = ipLocation;
|
||||||
|
if (details.gender === "男" || details.gender === "女") {
|
||||||
|
values.gender = details.gender;
|
||||||
|
}
|
||||||
|
const bio = details.bio?.trim();
|
||||||
|
if (bio) values.bio = bio;
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
let currentJob: AccountFieldRefreshStatus | null = null;
|
||||||
|
|
||||||
|
export function getAccountFieldRefreshStatus(): AccountFieldRefreshStatus | null {
|
||||||
|
return currentJob ? { ...currentJob } : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function startAccountFieldRefresh(
|
||||||
|
db: DatabaseClient,
|
||||||
|
config: CollectionMcpConfig,
|
||||||
|
requestedFields: unknown,
|
||||||
|
): AccountFieldRefreshStatus {
|
||||||
|
if (currentJob?.status === "running") {
|
||||||
|
return { ...currentJob };
|
||||||
|
}
|
||||||
|
const fields = sanitizeRefreshFields(requestedFields);
|
||||||
|
const job: AccountFieldRefreshStatus = {
|
||||||
|
status: "running",
|
||||||
|
fields,
|
||||||
|
total: 0,
|
||||||
|
processed: 0,
|
||||||
|
updated: 0,
|
||||||
|
failed: 0,
|
||||||
|
skipped: 0,
|
||||||
|
startedAt: new Date().toISOString(),
|
||||||
|
finishedAt: null,
|
||||||
|
};
|
||||||
|
currentJob = job;
|
||||||
|
const scraper: ProfileScraper = (profileUrl, platform) =>
|
||||||
|
resolveProfileDetailsFromMcp(profileUrl, platform, config);
|
||||||
|
const tagger: TagGenerator = (input) =>
|
||||||
|
generateAccountTagsFromMcp(
|
||||||
|
input,
|
||||||
|
config,
|
||||||
|
getRuntimeEnv() as AccountTaggingEnv,
|
||||||
|
);
|
||||||
|
runInBackground(
|
||||||
|
executeAccountFieldRefresh(job, db, scraper, tagger),
|
||||||
|
"account field refresh",
|
||||||
|
);
|
||||||
|
return { ...job };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function executeAccountFieldRefresh(
|
||||||
|
job: AccountFieldRefreshStatus,
|
||||||
|
db: DatabaseClient,
|
||||||
|
scraper: ProfileScraper,
|
||||||
|
tagger: TagGenerator,
|
||||||
|
) {
|
||||||
|
const rows = await db
|
||||||
|
.prepare(
|
||||||
|
`SELECT id, platform, platform_uid, profile_url
|
||||||
|
FROM accounts
|
||||||
|
WHERE profile_url IS NOT NULL AND profile_url != ''
|
||||||
|
AND platform IN ('小红书', '抖音')`,
|
||||||
|
)
|
||||||
|
.all<RefreshTargetRow>();
|
||||||
|
const targets = rows.results.filter(
|
||||||
|
(row) => !row.platform_uid.startsWith("xhs-"),
|
||||||
|
);
|
||||||
|
job.total = targets.length;
|
||||||
|
const fields = job.fields;
|
||||||
|
const wantTags = fields.includes("tags");
|
||||||
|
|
||||||
|
let cursor = 0;
|
||||||
|
const worker = async () => {
|
||||||
|
while (cursor < targets.length) {
|
||||||
|
const row = targets[cursor];
|
||||||
|
cursor += 1;
|
||||||
|
try {
|
||||||
|
const details = await scraper(
|
||||||
|
row.profile_url,
|
||||||
|
row.platform === "抖音" ? "抖音" : "小红书",
|
||||||
|
);
|
||||||
|
const values = scrapedFieldValues(details);
|
||||||
|
if (wantTags) {
|
||||||
|
const tags = await tagger({
|
||||||
|
platform: row.platform,
|
||||||
|
nickname: details.nickname?.trim() || "",
|
||||||
|
bio: details.bio,
|
||||||
|
gender: details.gender,
|
||||||
|
ipLocation: details.ipLocation ?? "",
|
||||||
|
followers: details.followers,
|
||||||
|
providerTags: details.providerTags,
|
||||||
|
recentNoteTitles: details.recentNoteTitles,
|
||||||
|
}).catch(() => []);
|
||||||
|
if (tags.length > 0) values.tags = tags.join(",");
|
||||||
|
}
|
||||||
|
const updates = fields.filter(
|
||||||
|
(field) => values[field] !== undefined,
|
||||||
|
);
|
||||||
|
if (updates.length === 0) {
|
||||||
|
job.skipped += 1;
|
||||||
|
} else {
|
||||||
|
const assignments = updates
|
||||||
|
.map((field) => `${FIELD_COLUMNS[field]} = ?`)
|
||||||
|
.join(", ");
|
||||||
|
await db
|
||||||
|
.prepare(`UPDATE accounts SET ${assignments} WHERE id = ?`)
|
||||||
|
.bind(
|
||||||
|
...updates.map((field) => values[field]),
|
||||||
|
row.id,
|
||||||
|
)
|
||||||
|
.run();
|
||||||
|
job.updated += 1;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
job.failed += 1;
|
||||||
|
}
|
||||||
|
job.processed += 1;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
await Promise.all(Array.from({ length: Math.min(4, targets.length) }, worker));
|
||||||
|
job.status = "done";
|
||||||
|
job.finishedAt = new Date().toISOString();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function runAccountFieldRefresh(
|
||||||
|
db: DatabaseClient,
|
||||||
|
scraper: ProfileScraper,
|
||||||
|
tagger: TagGenerator,
|
||||||
|
fields: AccountRefreshField[],
|
||||||
|
): Promise<AccountFieldRefreshStatus> {
|
||||||
|
const job: AccountFieldRefreshStatus = {
|
||||||
|
status: "running",
|
||||||
|
fields: sanitizeRefreshFields(fields),
|
||||||
|
total: 0,
|
||||||
|
processed: 0,
|
||||||
|
updated: 0,
|
||||||
|
failed: 0,
|
||||||
|
skipped: 0,
|
||||||
|
startedAt: new Date().toISOString(),
|
||||||
|
finishedAt: null,
|
||||||
|
};
|
||||||
|
await executeAccountFieldRefresh(job, db, scraper, tagger);
|
||||||
|
return job;
|
||||||
|
}
|
||||||
@@ -21,6 +21,33 @@ type ScheduledTask = {
|
|||||||
|
|
||||||
type CollectionSource = "automatic" | "catchup" | "manual";
|
type CollectionSource = "automatic" | "catchup" | "manual";
|
||||||
|
|
||||||
|
function userFacingCollectionError(error: unknown) {
|
||||||
|
const message = error instanceof Error ? error.message : String(error ?? "");
|
||||||
|
const normalized = message.toLowerCase();
|
||||||
|
if (
|
||||||
|
/cookie|登录|登陆|授权|access.?token|未登录|未授权|401|403/.test(
|
||||||
|
normalized,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return "采集 Cookie 已过期或无权限";
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
/链接|link|url|404|not found|不存在|删除|失效|无法识别.*笔记/.test(
|
||||||
|
normalized,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return "笔记链接失效或不可访问";
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
/超时|timeout|fetch failed|network|502|503|暂时不可用|响应空/.test(
|
||||||
|
normalized,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return "采集服务暂时不可用";
|
||||||
|
}
|
||||||
|
return "笔记链接失效或采集 Cookie 过期";
|
||||||
|
}
|
||||||
|
|
||||||
function utcDay(value: string) {
|
function utcDay(value: string) {
|
||||||
const match = value.match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
const match = value.match(/^(\d{4})-(\d{2})-(\d{2})$/);
|
||||||
if (!match) return null;
|
if (!match) return null;
|
||||||
@@ -311,8 +338,9 @@ export async function collectDistributionMetrics(
|
|||||||
]);
|
]);
|
||||||
return { skipped: false, likes, comments, collects, shares };
|
return { skipped: false, likes, comments, collects, shares };
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const message =
|
const detail = error instanceof Error ? error.message : String(error);
|
||||||
error instanceof Error ? error.message : "公开数据采集失败";
|
const message = userFacingCollectionError(error);
|
||||||
|
console.error("[KOC LOOP] collection failed", { detail, distributionId });
|
||||||
await db.batch([
|
await db.batch([
|
||||||
db
|
db
|
||||||
.prepare(
|
.prepare(
|
||||||
|
|||||||
60
lib/creator-screenshot-access.ts
Normal file
60
lib/creator-screenshot-access.ts
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import { createHmac, timingSafeEqual } from "node:crypto";
|
||||||
|
import { getRuntimeEnv } from "./runtime-env";
|
||||||
|
|
||||||
|
function accessSecret() {
|
||||||
|
const env = getRuntimeEnv();
|
||||||
|
return (
|
||||||
|
env.KOC_MCP_API_KEY ||
|
||||||
|
env.KOC_LOOP_MCP_API_KEY ||
|
||||||
|
env.ADMIN_INTERNAL_TOKEN ||
|
||||||
|
env.AI_TOOL_CENTER_MCP_KEY ||
|
||||||
|
""
|
||||||
|
).trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
function signature(distributionId: string, screenshotKey: string, expiresAt: number) {
|
||||||
|
return createHmac("sha256", accessSecret())
|
||||||
|
.update(`${distributionId}:${screenshotKey}:${expiresAt}`)
|
||||||
|
.digest("hex");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function creatorScreenshotAccessToken(
|
||||||
|
distributionId: string,
|
||||||
|
screenshotKey: string,
|
||||||
|
expiresAt = Math.floor(Date.now() / 1000) + 300,
|
||||||
|
) {
|
||||||
|
const secret = accessSecret();
|
||||||
|
if (!secret) throw new Error("图片识别访问密钥未配置");
|
||||||
|
return `${expiresAt}.${signature(distributionId, screenshotKey, expiresAt)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function verifyCreatorScreenshotAccessToken(
|
||||||
|
distributionId: string,
|
||||||
|
screenshotKey: string,
|
||||||
|
token: string,
|
||||||
|
) {
|
||||||
|
const [expiresText, received] = token.split(".");
|
||||||
|
const expiresAt = Number(expiresText);
|
||||||
|
if (!Number.isInteger(expiresAt) || expiresAt < Math.floor(Date.now() / 1000)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
const expected = signature(distributionId, screenshotKey, expiresAt);
|
||||||
|
if (!received || received.length !== expected.length) return false;
|
||||||
|
return timingSafeEqual(Buffer.from(received), Buffer.from(expected));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function creatorScreenshotMcpUrl(
|
||||||
|
request: Request,
|
||||||
|
distributionId: string,
|
||||||
|
screenshotKey: string,
|
||||||
|
) {
|
||||||
|
const env = getRuntimeEnv();
|
||||||
|
const origin = (env.APP_ORIGIN || new URL(request.url).origin).replace(/\/$/, "");
|
||||||
|
const url = new URL(`${origin}/api/creator-screenshot`);
|
||||||
|
url.searchParams.set("distribution", distributionId);
|
||||||
|
url.searchParams.set(
|
||||||
|
"mcp_token",
|
||||||
|
creatorScreenshotAccessToken(distributionId, screenshotKey),
|
||||||
|
);
|
||||||
|
return url.toString();
|
||||||
|
}
|
||||||
@@ -254,6 +254,7 @@ async function invokeMcpTool(
|
|||||||
timeoutMs: number,
|
timeoutMs: number,
|
||||||
name: string,
|
name: string,
|
||||||
args: Record<string, unknown>,
|
args: Record<string, unknown>,
|
||||||
|
allowText = false,
|
||||||
): Promise<ToolResult> {
|
): Promise<ToolResult> {
|
||||||
const result = await postMcp(
|
const result = await postMcp(
|
||||||
fetchImpl,
|
fetchImpl,
|
||||||
@@ -277,6 +278,12 @@ async function invokeMcpTool(
|
|||||||
try {
|
try {
|
||||||
payload = JSON.parse(text);
|
payload = JSON.parse(text);
|
||||||
} catch {
|
} catch {
|
||||||
|
if (allowText) {
|
||||||
|
return {
|
||||||
|
isError: result.envelope?.result?.isError === true,
|
||||||
|
payload: { text },
|
||||||
|
};
|
||||||
|
}
|
||||||
if (result.envelope?.result?.isError === true) {
|
if (result.envelope?.result?.isError === true) {
|
||||||
return {
|
return {
|
||||||
isError: true,
|
isError: true,
|
||||||
@@ -305,6 +312,7 @@ async function callMcpTool(
|
|||||||
timeoutMs: number,
|
timeoutMs: number,
|
||||||
name: string,
|
name: string,
|
||||||
args: Record<string, unknown>,
|
args: Record<string, unknown>,
|
||||||
|
allowText = false,
|
||||||
): Promise<ToolResult> {
|
): Promise<ToolResult> {
|
||||||
const nested = await invokeMcpTool(
|
const nested = await invokeMcpTool(
|
||||||
fetchImpl,
|
fetchImpl,
|
||||||
@@ -313,6 +321,7 @@ async function callMcpTool(
|
|||||||
timeoutMs,
|
timeoutMs,
|
||||||
name,
|
name,
|
||||||
{ request: args },
|
{ request: args },
|
||||||
|
allowText,
|
||||||
);
|
);
|
||||||
if (!isToolArgumentShapeError(nested)) return nested;
|
if (!isToolArgumentShapeError(nested)) return nested;
|
||||||
return invokeMcpTool(
|
return invokeMcpTool(
|
||||||
@@ -322,10 +331,86 @@ async function callMcpTool(
|
|||||||
timeoutMs,
|
timeoutMs,
|
||||||
name,
|
name,
|
||||||
args,
|
args,
|
||||||
|
allowText,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function metricsFromToolResult(result: ToolResult): XhsPublicMetrics {
|
function imageToolText(value: unknown) {
|
||||||
|
if (typeof value === "string") return value;
|
||||||
|
const root = asRecord(value);
|
||||||
|
if (!root) return "";
|
||||||
|
return [root.text, root.description, root.content, root.message, root.error]
|
||||||
|
.flatMap((item) => (Array.isArray(item) ? item : [item]))
|
||||||
|
.map((item) => {
|
||||||
|
if (typeof item === "string") return item;
|
||||||
|
const record = asRecord(item);
|
||||||
|
return String(record?.text ?? record?.description ?? "");
|
||||||
|
})
|
||||||
|
.filter(Boolean)
|
||||||
|
.join("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
function metricFromImageText(text: string, labels: string[], label: string) {
|
||||||
|
const pattern = labels.join("|");
|
||||||
|
const match = text.match(
|
||||||
|
new RegExp(`(?:${pattern})\\s*[::]?\\s*([\\d,.]+(?:万|w|千|k)?)`, "i"),
|
||||||
|
);
|
||||||
|
if (!match) return null;
|
||||||
|
try {
|
||||||
|
return metricValue(match[1], label);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function creatorMetricsFromImageResult(result: ToolResult) {
|
||||||
|
if (result.isError) {
|
||||||
|
const detail = imageToolText(result.payload);
|
||||||
|
throw new Error(
|
||||||
|
`图片识别 MCP 调用失败${detail ? `:${safeMessage(detail, "")}` : ""}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const text = imageToolText(result.payload);
|
||||||
|
const exposure = metricFromImageText(
|
||||||
|
text,
|
||||||
|
["曝光量", "曝光", "impressions", "exposure"],
|
||||||
|
"曝光量",
|
||||||
|
);
|
||||||
|
const views = metricFromImageText(
|
||||||
|
text,
|
||||||
|
["阅读量", "阅读", "views", "view_count", "view count"],
|
||||||
|
"阅读量",
|
||||||
|
);
|
||||||
|
if (exposure === null || views === null) {
|
||||||
|
throw new Error("图片识别未找到曝光量和阅读量");
|
||||||
|
}
|
||||||
|
return { exposure, views };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function extractCreatorMetricsFromMcp(
|
||||||
|
imageUrl: string,
|
||||||
|
config: CollectionMcpConfig,
|
||||||
|
fetchImpl: typeof fetch = fetch,
|
||||||
|
) {
|
||||||
|
const endpoint = buildMcpUrl(config);
|
||||||
|
const timeoutMs = Math.max(5_000, config.timeoutMs ?? 30_000);
|
||||||
|
const sessionId = await createMcpSession(fetchImpl, endpoint, timeoutMs);
|
||||||
|
const result = await callMcpTool(
|
||||||
|
fetchImpl,
|
||||||
|
endpoint,
|
||||||
|
sessionId,
|
||||||
|
timeoutMs,
|
||||||
|
"analyze_image",
|
||||||
|
{ image_url: imageUrl },
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
return creatorMetricsFromImageResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
function metricsFromToolResult(
|
||||||
|
result: ToolResult,
|
||||||
|
toolName = "fetch_content_detail",
|
||||||
|
): XhsPublicMetrics {
|
||||||
const root = asRecord(result.payload);
|
const root = asRecord(result.payload);
|
||||||
const response = asRecord(root?.response) ?? root;
|
const response = asRecord(root?.response) ?? root;
|
||||||
const data = asRecord(response?.data) ?? asRecord(root?.data);
|
const data = asRecord(response?.data) ?? asRecord(root?.data);
|
||||||
@@ -337,14 +422,18 @@ function metricsFromToolResult(result: ToolResult): XhsPublicMetrics {
|
|||||||
success === false ||
|
success === false ||
|
||||||
(Number.isFinite(code) && code >= 400)
|
(Number.isFinite(code) && code >= 400)
|
||||||
) {
|
) {
|
||||||
|
const providerCode = Number(response?.code);
|
||||||
|
const codeLabel = Number.isFinite(providerCode)
|
||||||
|
? `(code ${providerCode})`
|
||||||
|
: "";
|
||||||
throw new Error(
|
throw new Error(
|
||||||
safeMessage(
|
`MCP工具 ${toolName} 返回失败${codeLabel}:${safeMessage(
|
||||||
response?.msg ?? response?.message ?? root?.message,
|
response?.msg ?? response?.message ?? root?.message,
|
||||||
"公开数据采集失败",
|
"公开数据采集失败",
|
||||||
),
|
)}`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if (!data) throw new Error("采集结果缺少互动数据");
|
if (!data) throw new Error(`MCP工具 ${toolName} 未返回互动数据`);
|
||||||
|
|
||||||
const count = (value: unknown, label: string) =>
|
const count = (value: unknown, label: string) =>
|
||||||
value === null || value === undefined || value === ""
|
value === null || value === undefined || value === ""
|
||||||
@@ -1156,7 +1245,7 @@ async function collectInSession(
|
|||||||
auto_cookie: true,
|
auto_cookie: true,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return metricsFromToolResult(primary);
|
return metricsFromToolResult(primary, "fetch_content_detail");
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resolveCollectionMcpConfig(
|
export function resolveCollectionMcpConfig(
|
||||||
|
|||||||
@@ -362,7 +362,7 @@ function resourceWhere(input: ResourceFilters) {
|
|||||||
const bindings: unknown[] = [];
|
const bindings: unknown[] = [];
|
||||||
if (input.query?.trim()) {
|
if (input.query?.trim()) {
|
||||||
conditions.push(
|
conditions.push(
|
||||||
"(a.nickname LIKE ? ESCAPE '\\' OR a.public_account_id LIKE ? ESCAPE '\\' OR a.current_contact LIKE ? ESCAPE '\\' OR a.tags LIKE ? ESCAPE '\\' OR a.bio LIKE ? ESCAPE '\\')",
|
"(a.nickname LIKE ? ESCAPE '\\\\' OR a.public_account_id LIKE ? ESCAPE '\\\\' OR a.current_contact LIKE ? ESCAPE '\\\\' OR a.tags LIKE ? ESCAPE '\\\\' OR a.bio LIKE ? ESCAPE '\\\\')",
|
||||||
);
|
);
|
||||||
const pattern = like(input.query.trim());
|
const pattern = like(input.query.trim());
|
||||||
bindings.push(pattern, pattern, pattern, pattern, pattern);
|
bindings.push(pattern, pattern, pattern, pattern, pattern);
|
||||||
|
|||||||
141
tests/account-field-refresh.test.mjs
Normal file
141
tests/account-field-refresh.test.mjs
Normal file
@@ -0,0 +1,141 @@
|
|||||||
|
import assert from "node:assert/strict";
|
||||||
|
import test from "node:test";
|
||||||
|
import {
|
||||||
|
runAccountFieldRefresh,
|
||||||
|
sanitizeRefreshFields,
|
||||||
|
} from "../lib/account-field-refresh-service.ts";
|
||||||
|
|
||||||
|
function fakeDb(accounts) {
|
||||||
|
const updates = [];
|
||||||
|
return {
|
||||||
|
db: {
|
||||||
|
prepare: (sql) => {
|
||||||
|
if (sql.startsWith("SELECT")) {
|
||||||
|
return {
|
||||||
|
all: async () => ({
|
||||||
|
results: accounts.map((account) => ({
|
||||||
|
id: account.id,
|
||||||
|
platform: account.platform,
|
||||||
|
platform_uid: account.platform_uid,
|
||||||
|
profile_url: account.profile_url,
|
||||||
|
})),
|
||||||
|
success: true,
|
||||||
|
meta: { changes: 0, last_row_id: 0 },
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
assert.match(sql, /^UPDATE accounts SET [a-z_, ?= ]+ WHERE id = \?$/);
|
||||||
|
return {
|
||||||
|
bind: (...args) => ({
|
||||||
|
run: async () => {
|
||||||
|
updates.push({ sql, args });
|
||||||
|
return { success: true, meta: { changes: 1, last_row_id: 0 } };
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
updates,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const profile = (overrides = {}) => ({
|
||||||
|
nickname: "昵称A",
|
||||||
|
redId: "id-a",
|
||||||
|
followers: 123,
|
||||||
|
ipLocation: "上海",
|
||||||
|
gender: "女",
|
||||||
|
bio: "简介A",
|
||||||
|
recentNoteTitles: [],
|
||||||
|
providerTags: [],
|
||||||
|
...overrides,
|
||||||
|
});
|
||||||
|
|
||||||
|
const noTag = async () => [];
|
||||||
|
|
||||||
|
test("writes only the selected fields as dynamic columns", async () => {
|
||||||
|
const accounts = [
|
||||||
|
{ id: "a1", platform: "小红书", platform_uid: "uid-1", profile_url: "https://www.xiaohongshu.com/user/profile/a1" },
|
||||||
|
];
|
||||||
|
const { db, updates } = fakeDb(accounts);
|
||||||
|
const status = await runAccountFieldRefresh(
|
||||||
|
db,
|
||||||
|
async () => profile(),
|
||||||
|
noTag,
|
||||||
|
["nickname", "ipLocation"],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.equal(status.status, "done");
|
||||||
|
assert.equal(status.updated, 1);
|
||||||
|
assert.equal(status.fields.join(","), "nickname,ipLocation");
|
||||||
|
assert.equal(updates.length, 1);
|
||||||
|
assert.match(updates[0].sql, /SET nickname = \?, ip_location = \?/);
|
||||||
|
assert.doesNotMatch(updates[0].sql, /public_account_id|followers|gender|bio|tags/);
|
||||||
|
assert.deepEqual(updates[0].args, ["昵称A", "上海", "a1"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("tags field uses the tagger output and joins with commas", async () => {
|
||||||
|
const accounts = [
|
||||||
|
{ id: "a1", platform: "小红书", platform_uid: "uid-1", profile_url: "https://www.xiaohongshu.com/user/profile/a1" },
|
||||||
|
];
|
||||||
|
const { db, updates } = fakeDb(accounts);
|
||||||
|
const status = await runAccountFieldRefresh(
|
||||||
|
db,
|
||||||
|
async () => profile({ redId: "" }),
|
||||||
|
async () => ["美妆", "学生党"],
|
||||||
|
["tags"],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.equal(status.updated, 1);
|
||||||
|
assert.match(updates[0].sql, /SET tags = \?/);
|
||||||
|
assert.deepEqual(updates[0].args, ["美妆,学生党", "a1"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("empty scrape values and empty tagger result are skipped without writing", async () => {
|
||||||
|
const accounts = [
|
||||||
|
{ id: "a1", platform: "小红书", platform_uid: "uid-1", profile_url: "https://www.xiaohongshu.com/user/profile/a1" },
|
||||||
|
];
|
||||||
|
const { db, updates } = fakeDb(accounts);
|
||||||
|
const status = await runAccountFieldRefresh(
|
||||||
|
db,
|
||||||
|
async () => profile({ redId: " ", nickname: null, followers: null, ipLocation: "", gender: "", bio: "" }),
|
||||||
|
noTag,
|
||||||
|
["publicAccountId", "nickname", "followers", "ipLocation", "gender", "bio", "tags"],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.equal(status.updated, 0);
|
||||||
|
assert.equal(status.skipped, 1);
|
||||||
|
assert.equal(updates.length, 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("scrape failures count as failed and demo accounts are excluded", async () => {
|
||||||
|
const accounts = [
|
||||||
|
{ id: "boom", platform: "小红书", platform_uid: "uid-1", profile_url: "https://www.xiaohongshu.com/user/profile/boom" },
|
||||||
|
{ id: "demo", platform: "小红书", platform_uid: "xhs-8af3", profile_url: "https://www.xiaohongshu.com/user/profile/demo" },
|
||||||
|
{ id: "ok", platform: "抖音", platform_uid: "uid-2", profile_url: "https://www.douyin.com/user/ok" },
|
||||||
|
];
|
||||||
|
const { db, updates } = fakeDb(accounts);
|
||||||
|
const status = await runAccountFieldRefresh(
|
||||||
|
db,
|
||||||
|
async (url) => {
|
||||||
|
if (url.includes("boom")) throw new Error("mcp down");
|
||||||
|
return profile({ redId: "ok-2" });
|
||||||
|
},
|
||||||
|
noTag,
|
||||||
|
["publicAccountId"],
|
||||||
|
);
|
||||||
|
|
||||||
|
assert.equal(status.total, 2);
|
||||||
|
assert.equal(status.updated, 1);
|
||||||
|
assert.equal(status.failed, 1);
|
||||||
|
assert.deepEqual(updates[0].args.slice(-1), ["ok"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("sanitizeRefreshFields filters unknown values and falls back to account id", () => {
|
||||||
|
assert.deepEqual(sanitizeRefreshFields(["nickname", "hacked", "tags"]), [
|
||||||
|
"nickname",
|
||||||
|
"tags",
|
||||||
|
]);
|
||||||
|
assert.deepEqual(sanitizeRefreshFields([]), ["publicAccountId"]);
|
||||||
|
assert.deepEqual(sanitizeRefreshFields("bad"), ["publicAccountId"]);
|
||||||
|
});
|
||||||
@@ -6,7 +6,7 @@ import {
|
|||||||
parseStoredDate,
|
parseStoredDate,
|
||||||
} from "../lib/date-utils.ts";
|
} from "../lib/date-utils.ts";
|
||||||
|
|
||||||
test("treats D1 CURRENT_TIMESTAMP values as UTC and displays Beijing time", () => {
|
test("treats D1 and MySQL UTC timestamps as UTC and displays Beijing time", () => {
|
||||||
const stored = "2026-07-29 05:36:00";
|
const stored = "2026-07-29 05:36:00";
|
||||||
assert.equal(parseStoredDate(stored).toISOString(), "2026-07-29T05:36:00.000Z");
|
assert.equal(parseStoredDate(stored).toISOString(), "2026-07-29T05:36:00.000Z");
|
||||||
assert.match(formatShanghaiDate(stored, true), /07\/29.*13:36/);
|
assert.match(formatShanghaiDate(stored, true), /07\/29.*13:36/);
|
||||||
|
|||||||
@@ -520,7 +520,7 @@ test("surfaces current detail tool failures without calling removed tools", asyn
|
|||||||
},
|
},
|
||||||
fetchImpl,
|
fetchImpl,
|
||||||
),
|
),
|
||||||
/获取内容详情失败/,
|
/MCP工具 fetch_content_detail 返回失败(code 400):获取内容详情失败/,
|
||||||
);
|
);
|
||||||
assert.equal(calls.length, 3);
|
assert.equal(calls.length, 3);
|
||||||
assert.equal(calls[2].body.params.name, "fetch_content_detail");
|
assert.equal(calls[2].body.params.name, "fetch_content_detail");
|
||||||
|
|||||||
@@ -190,8 +190,9 @@ test("issues external task links and supports one-to-one note submissions", asyn
|
|||||||
assert.doesNotMatch(partnerUtils, /user\/profile\/\$\{platformUid\}/);
|
assert.doesNotMatch(partnerUtils, /user\/profile\/\$\{platformUid\}/);
|
||||||
assert.match(uploadRoute, /publish-evidence/);
|
assert.match(uploadRoute, /publish-evidence/);
|
||||||
assert.match(uploadRoute, /creator-center/);
|
assert.match(uploadRoute, /creator-center/);
|
||||||
assert.match(uploadRoute, /ELSE 'uploaded'/);
|
assert.match(uploadRoute, /ELSE 'processing'/);
|
||||||
assert.doesNotMatch(uploadRoute, /ocrMetric/);
|
assert.match(uploadRoute, /extractCreatorMetricsFromMcp/);
|
||||||
|
assert.match(uploadRoute, /creatorScreenshotMcpUrl/);
|
||||||
assert.match(uploadRoute, /x-koc-upload-kind/);
|
assert.match(uploadRoute, /x-koc-upload-kind/);
|
||||||
assert.match(uploadRoute, /x-koc-distribution/);
|
assert.match(uploadRoute, /x-koc-distribution/);
|
||||||
assert.match(uploadRoute, /request\.arrayBuffer/);
|
assert.match(uploadRoute, /request\.arrayBuffer/);
|
||||||
@@ -223,7 +224,7 @@ test("issues external task links and supports one-to-one note submissions", asyn
|
|||||||
assert.match(adminApp, /全部内容类型/);
|
assert.match(adminApp, /全部内容类型/);
|
||||||
assert.match(adminApp, /全部平台/);
|
assert.match(adminApp, /全部平台/);
|
||||||
assert.match(adminApp, /task-scope-subline/);
|
assert.match(adminApp, /task-scope-subline/);
|
||||||
assert.match(adminApp, /待KOC填写数据/);
|
assert.match(adminApp, /识别失败,请手动填写/);
|
||||||
assert.match(adminApp, /AdminImageLightbox/);
|
assert.match(adminApp, /AdminImageLightbox/);
|
||||||
assert.match(adminApp, /CreatorScreenshotPreview/);
|
assert.match(adminApp, /CreatorScreenshotPreview/);
|
||||||
assert.match(adminApp, /admin-image-lightbox/);
|
assert.match(adminApp, /admin-image-lightbox/);
|
||||||
|
|||||||
Reference in New Issue
Block a user