Compare commits
9 Commits
9c37141399
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc81e0daf1 | ||
|
|
ed13262166 | ||
|
|
b882a5c7d7 | ||
|
|
8e4e3de974 | ||
|
|
0815967ca7 | ||
|
|
dfc7dc217d | ||
|
|
d8a09dcb8b | ||
|
|
5614a13ebb | ||
| 1567021d57 |
@@ -54,5 +54,6 @@ WECOM_NOTIFY_ENABLED=true
|
||||
# ── 调度与入口 ────────────────────────────────────────────
|
||||
# 每天北京时间 09:00 自动执行采集计划,生产保持 true。
|
||||
ENABLE_SCHEDULER=true
|
||||
# 是否在空库时写入演示数据,生产保持 false。
|
||||
SEED_DEMO_DATA=false
|
||||
HTTP_PORT=80
|
||||
|
||||
@@ -74,6 +74,28 @@ type Account = {
|
||||
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 = {
|
||||
summary: { total: number; create: number; update: number; error: number };
|
||||
rows: Array<{
|
||||
@@ -3160,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({
|
||||
accounts,
|
||||
distributions,
|
||||
@@ -3177,6 +3215,7 @@ function ResourcesPage({
|
||||
const [platformFilter, setPlatformFilter] = useState("全部平台");
|
||||
const [ipFilter, setIpFilter] = useState("");
|
||||
const [sourceFilter, setSourceFilter] = useState("");
|
||||
const [page, setPage] = useState(1);
|
||||
const [importOpen, setImportOpen] = useState(false);
|
||||
const [importMode, setImportMode] = useState<"bulk" | "single">("bulk");
|
||||
const [importFile, setImportFile] = useState<File | null>(null);
|
||||
@@ -3196,6 +3235,24 @@ function ResourcesPage({
|
||||
});
|
||||
const [singleWorking, setSingleWorking] = useState(false);
|
||||
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 deliveriesByAccount = new Map<string, Distribution[]>();
|
||||
distributions.forEach((distribution) => {
|
||||
@@ -3288,11 +3345,21 @@ function ResourcesPage({
|
||||
platformFilter !== "全部平台" ||
|
||||
Boolean(ipFilter.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 = () => {
|
||||
setQuery("");
|
||||
setPlatformFilter("全部平台");
|
||||
setIpFilter("");
|
||||
setSourceFilter("");
|
||||
setPage(1);
|
||||
};
|
||||
const closeImport = () => {
|
||||
if (importWorking || singleWorking) return;
|
||||
@@ -3403,6 +3470,126 @@ function ResourcesPage({
|
||||
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 (
|
||||
<div className="stack">
|
||||
<section className="resource-intro">
|
||||
@@ -3432,7 +3619,10 @@ function ResourcesPage({
|
||||
<div className="filter-chips">
|
||||
<button
|
||||
className={platformFilter === "全部平台" ? "active" : ""}
|
||||
onClick={() => setPlatformFilter("全部平台")}
|
||||
onClick={() => {
|
||||
setPlatformFilter("全部平台");
|
||||
setPage(1);
|
||||
}}
|
||||
>
|
||||
全部平台
|
||||
</button>
|
||||
@@ -3440,7 +3630,10 @@ function ResourcesPage({
|
||||
<button
|
||||
className={platformFilter === platform ? "active" : ""}
|
||||
key={platform}
|
||||
onClick={() => setPlatformFilter(platform)}
|
||||
onClick={() => {
|
||||
setPlatformFilter(platform);
|
||||
setPage(1);
|
||||
}}
|
||||
>
|
||||
<PlatformBadge platform={platform} compact />
|
||||
</button>
|
||||
@@ -3453,7 +3646,10 @@ function ResourcesPage({
|
||||
<span aria-hidden="true">⌕</span>
|
||||
<input
|
||||
aria-label="搜索账号名称、账号ID或当前联系人"
|
||||
onChange={(event) => setQuery(event.target.value)}
|
||||
onChange={(event) => {
|
||||
setQuery(event.target.value);
|
||||
setPage(1);
|
||||
}}
|
||||
placeholder="搜索账号名称 / 账号ID / 当前联系人 / 标签"
|
||||
type="search"
|
||||
value={query}
|
||||
@@ -3464,7 +3660,10 @@ function ResourcesPage({
|
||||
<input
|
||||
aria-label="模糊搜索IP地区"
|
||||
list="resource-ip-options"
|
||||
onChange={(event) => setIpFilter(event.target.value)}
|
||||
onChange={(event) => {
|
||||
setIpFilter(event.target.value);
|
||||
setPage(1);
|
||||
}}
|
||||
placeholder="搜索IP地区"
|
||||
type="search"
|
||||
value={ipFilter}
|
||||
@@ -3478,7 +3677,10 @@ function ResourcesPage({
|
||||
<input
|
||||
aria-label="模糊搜索合作来源"
|
||||
list="resource-source-options"
|
||||
onChange={(event) => setSourceFilter(event.target.value)}
|
||||
onChange={(event) => {
|
||||
setSourceFilter(event.target.value);
|
||||
setPage(1);
|
||||
}}
|
||||
placeholder="搜索合作来源"
|
||||
type="search"
|
||||
value={sourceFilter}
|
||||
@@ -3497,7 +3699,19 @@ function ResourcesPage({
|
||||
清空筛选
|
||||
</button>
|
||||
<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
|
||||
className="export-data-button"
|
||||
disabled={exporting || filteredAccounts.length === 0}
|
||||
@@ -3508,9 +3722,10 @@ function ResourcesPage({
|
||||
{exporting ? "正在导出…" : "导出筛选结果"}
|
||||
</button>
|
||||
</div>
|
||||
{idRefreshError && <div className="form-error">{idRefreshError}</div>}
|
||||
</div>
|
||||
<div className="resource-grid">
|
||||
{filteredAccounts.map(({ account, sources, partnerManagedOnly }) => {
|
||||
{pagedAccounts.map(({ account, sources, partnerManagedOnly }) => {
|
||||
const profileLink = resourceProfileLink(account);
|
||||
const accountTags = (account.tags || "")
|
||||
.split(/[,,、;;|]/)
|
||||
@@ -3548,6 +3763,18 @@ function ResourcesPage({
|
||||
<div className="resource-account-number">
|
||||
<span>{accountIdLabel}</span>
|
||||
<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 className="resource-platform-line">
|
||||
<PlatformBadge platform={account.platform} compact />
|
||||
@@ -3600,7 +3827,149 @@ function ResourcesPage({
|
||||
</button>
|
||||
</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>
|
||||
{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 && (
|
||||
<div className="modal-backdrop" role="presentation" onMouseDown={closeImport}>
|
||||
<div
|
||||
|
||||
@@ -54,6 +54,10 @@ import {
|
||||
} from "../../../lib/wecom-group-push-service";
|
||||
import { isManagerRequest } from "../../../lib/user-auth";
|
||||
import { extractPublishUrl } from "../../../lib/publish-url";
|
||||
import {
|
||||
getAccountFieldRefreshStatus,
|
||||
startAccountFieldRefresh,
|
||||
} from "../../../lib/account-field-refresh-service";
|
||||
|
||||
type ActionBody = {
|
||||
action?: string;
|
||||
@@ -683,6 +687,15 @@ export async function POST(request: Request) {
|
||||
const taskId = String(body.taskId ?? "").trim();
|
||||
const pushes = await listGroupPushes(db, taskId || undefined);
|
||||
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") {
|
||||
const groups = await listGroupChatRows(db);
|
||||
return Response.json({ groups });
|
||||
|
||||
113
app/globals.css
113
app/globals.css
@@ -2577,6 +2577,49 @@ a {
|
||||
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 {
|
||||
width: min(780px, calc(100vw - 32px));
|
||||
max-height: min(86vh, 820px);
|
||||
@@ -2972,6 +3015,76 @@ a {
|
||||
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 {
|
||||
display: flex;
|
||||
min-width: 0;
|
||||
|
||||
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;
|
||||
}
|
||||
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"]);
|
||||
});
|
||||
Reference in New Issue
Block a user