feat: 批量更新账号资料支持勾选字段(含AI打标)
- 更新全部账号ID 升级为 批量更新账号资料:弹窗勾选 账号ID/昵称/粉丝数/IP属地/性别/简介/标签 - 标签勾选时调用 LLM 重新打标(覆盖已有标签),AI_TAGGING_TOOL 未配置自动跳过 - 只写入选中字段,爬取为空的字段跳过不清空;演示账号(xhs- 前缀)不参与 - .env.app.example 为 SEED_DEMO_DATA 补充注释
This commit is contained in:
@@ -74,8 +74,9 @@ type Account = {
|
||||
last_seen_at: string;
|
||||
};
|
||||
|
||||
type IdRefreshStatus = {
|
||||
type FieldRefreshStatus = {
|
||||
status: "running" | "done";
|
||||
fields: string[];
|
||||
total: number;
|
||||
processed: number;
|
||||
updated: number;
|
||||
@@ -85,6 +86,16 @@ type IdRefreshStatus = {
|
||||
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<{
|
||||
@@ -3228,10 +3239,20 @@ function ResourcesPage({
|
||||
const [editAccountId, setEditAccountId] = useState("");
|
||||
const [editWorking, setEditWorking] = useState(false);
|
||||
const [editError, setEditError] = useState("");
|
||||
const [idRefresh, setIdRefresh] = useState<IdRefreshStatus | null>(null);
|
||||
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) => {
|
||||
@@ -3494,9 +3515,9 @@ function ResourcesPage({
|
||||
const response = await fetch("/api/action", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ action: "public_account_id_refresh_status" }),
|
||||
body: JSON.stringify({ action: "account_field_refresh_status" }),
|
||||
});
|
||||
const result = await readApiResponse<{ refresh: IdRefreshStatus | null }>(
|
||||
const result = await readApiResponse<{ refresh: FieldRefreshStatus | null }>(
|
||||
response,
|
||||
"查询失败",
|
||||
);
|
||||
@@ -3504,10 +3525,10 @@ function ResourcesPage({
|
||||
return result.refresh;
|
||||
}, []);
|
||||
const applyIdRefreshStatus = useCallback(
|
||||
(status: IdRefreshStatus) => {
|
||||
(status: FieldRefreshStatus) => {
|
||||
if (status.status === "done" && !idRefreshNotifiedRef.current) {
|
||||
idRefreshNotifiedRef.current = true;
|
||||
const summary = `账号ID更新完成:成功 ${status.updated} 个${
|
||||
const summary = `账号资料更新完成:成功 ${status.updated} 个${
|
||||
status.skipped ? `,未识别 ${status.skipped} 个` : ""
|
||||
}${status.failed ? `,失败 ${status.failed} 个` : ""}`;
|
||||
void onImported(summary);
|
||||
@@ -3537,30 +3558,32 @@ function ResourcesPage({
|
||||
}, 3000);
|
||||
return () => window.clearInterval(timer);
|
||||
}, [applyIdRefreshStatus, fetchIdRefreshStatus, runningIdRefresh]);
|
||||
const closeFieldPick = () => {
|
||||
if (idRefreshStarting) return;
|
||||
setFieldPickOpen(false);
|
||||
};
|
||||
const startIdRefresh = async () => {
|
||||
if (runningIdRefresh || idRefreshStarting) return;
|
||||
if (
|
||||
!window.confirm(
|
||||
"将逐个爬取全部账号主页以更新小红书号/抖音号(其他资料不变),账号较多时需要几分钟。确定开始?",
|
||||
)
|
||||
) {
|
||||
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_public_account_ids" }),
|
||||
body: JSON.stringify({ action: "refresh_account_fields", fields }),
|
||||
});
|
||||
const result = await readApiResponse<{ refresh: IdRefreshStatus }>(
|
||||
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 {
|
||||
@@ -3680,14 +3703,14 @@ function ResourcesPage({
|
||||
<button
|
||||
className="export-data-button"
|
||||
disabled={runningIdRefresh || idRefreshStarting}
|
||||
title="爬取全部账号主页,更新小红书号/抖音号,其他资料不变"
|
||||
onClick={() => void startIdRefresh()}
|
||||
title="爬取全部账号主页,按选择的字段批量更新账号资料"
|
||||
onClick={() => setFieldPickOpen(true)}
|
||||
>
|
||||
{idRefreshStarting
|
||||
? "正在启动…"
|
||||
: runningIdRefresh
|
||||
? `更新中 ${idRefresh?.processed ?? 0}/${idRefresh?.total ?? 0}`
|
||||
: "更新全部账号ID"}
|
||||
: "批量更新账号资料"}
|
||||
</button>
|
||||
<button
|
||||
className="export-data-button"
|
||||
@@ -3834,6 +3857,66 @@ function ResourcesPage({
|
||||
</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
|
||||
|
||||
@@ -55,9 +55,9 @@ import {
|
||||
import { isManagerRequest } from "../../../lib/user-auth";
|
||||
import { extractPublishUrl } from "../../../lib/publish-url";
|
||||
import {
|
||||
getPublicAccountIdRefreshStatus,
|
||||
startPublicAccountIdRefresh,
|
||||
} from "../../../lib/account-id-refresh-service";
|
||||
getAccountFieldRefreshStatus,
|
||||
startAccountFieldRefresh,
|
||||
} from "../../../lib/account-field-refresh-service";
|
||||
|
||||
type ActionBody = {
|
||||
action?: string;
|
||||
@@ -687,14 +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_public_account_ids") {
|
||||
const status = startPublicAccountIdRefresh(
|
||||
} 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 === "public_account_id_refresh_status") {
|
||||
return Response.json({ refresh: getPublicAccountIdRefreshStatus() });
|
||||
} 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 });
|
||||
|
||||
@@ -3043,6 +3043,48 @@ a {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user