feat: KOC详情弹窗 + 任务中心单入口改版 + 资源池默认合作方/采集兜底
KOC 详情弹窗(合作方/数据回收/工作台三处入口): - 新增 KocDetailModal:基本信息 / 绑定KOC账号画像 / 参与任务记录 / 履约漏斗 - 工作台最近分发、回收队列账号名、截图回收合作方名、合作方页名称、分发详情头部合作方均可点开 - 分发表行点击仍打开分发详情(stopPropagation 隔离) 任务中心单入口改版(对齐"单任务双阶段"模型): - 任务卡发布/回收面板整块可点,全卡仅保留一个「查看明细」,回收面板重复按钮移除 - 详情页自述身份:动态 label(任务详情·分发与回收一体/仅分发)+ 阶段徽章(含回收·截止/未开通回收) - 内容分发表新增阶段筛选 chips(全部/待发布/已发布待数据/待收截图,未开通回收不显示截图项),筛选落空给专用空态文案 - 数据回收页入口文案改为「选择一个任务进入回收队列」,全站不再区分发布/回收两种任务类型 资源池 KOC 默认加入合作方 + 采集兜底: - partners 新增 released_account_id(mysql/0023):显式解绑/被顶替的账号不再被默认合作方同步回填 - 任务未设置采集计划时,默认按发布时间起连续采集 7 天,等待文案显示「等待D1 · 默认7天」
This commit is contained in:
@@ -16,7 +16,8 @@ type DistributionForCollection = {
|
||||
|
||||
type ScheduledTask = {
|
||||
id: string;
|
||||
collection_start_date: string;
|
||||
task_type: string | null;
|
||||
collection_start_date: string | null;
|
||||
collection_days: string;
|
||||
};
|
||||
|
||||
@@ -63,9 +64,9 @@ function dateForScheduleDay(startDate: string, scheduleDay: number) {
|
||||
.slice(0, 10);
|
||||
}
|
||||
|
||||
function parseDays(value: string) {
|
||||
function parseDays(value: string | null | undefined) {
|
||||
try {
|
||||
const parsed = JSON.parse(value) as unknown;
|
||||
const parsed = JSON.parse(value ?? "[]") as unknown;
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return [...new Set(parsed.map(Number))]
|
||||
.filter((day) => Number.isInteger(day) && day >= 1 && day <= 7)
|
||||
@@ -75,12 +76,35 @@ function parseDays(value: string) {
|
||||
}
|
||||
}
|
||||
|
||||
export function parseCollectionDays(value: string | null | undefined) {
|
||||
return parseDays(value);
|
||||
}
|
||||
|
||||
// 数据回收(自动采集)兜底:任务未设置采集日时默认连续采集 7 天。
|
||||
export const DEFAULT_COLLECTION_DAYS = [1, 2, 3, 4, 5, 6, 7];
|
||||
|
||||
export function effectiveCollectionDays(value: string | null | undefined) {
|
||||
const days = parseDays(value);
|
||||
return days.length > 0 ? days : [...DEFAULT_COLLECTION_DAYS];
|
||||
}
|
||||
|
||||
export function shanghaiDateFromTimestamp(timestamp: number) {
|
||||
return new Date(timestamp + 8 * 60 * 60 * 1000)
|
||||
.toISOString()
|
||||
.slice(0, 10);
|
||||
}
|
||||
|
||||
// publish_time 等列以 UTC 文本存储(CURRENT_TIMESTAMP),统一换算成上海日期。
|
||||
function shanghaiDateFromSqlTimestamp(value: string | null | undefined) {
|
||||
if (!value) return null;
|
||||
const normalized = value.includes("T")
|
||||
? value
|
||||
: `${value.replace(" ", "T")}Z`;
|
||||
const epoch = Date.parse(normalized);
|
||||
if (Number.isNaN(epoch)) return null;
|
||||
return shanghaiDateFromTimestamp(epoch);
|
||||
}
|
||||
|
||||
function shanghaiHourFromTimestamp(timestamp: number) {
|
||||
return Number(
|
||||
new Date(timestamp + 8 * 60 * 60 * 1000)
|
||||
@@ -392,6 +416,42 @@ export async function runScheduledCollections(
|
||||
);
|
||||
}
|
||||
|
||||
async function distributionIdsNeedingCollection(
|
||||
db: DatabaseClient,
|
||||
taskId: string,
|
||||
scheduledDate: string,
|
||||
distributionId?: string,
|
||||
) {
|
||||
const scoped = distributionId ? "AND d.id = ?" : "";
|
||||
const { results } = await db
|
||||
.prepare(
|
||||
`SELECT d.id FROM distributions d
|
||||
WHERE d.task_id = ?
|
||||
AND d.publish_url IS NOT NULL
|
||||
AND d.publish_url != ''
|
||||
${scoped}
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM collection_runs r
|
||||
WHERE r.distribution_id = d.id
|
||||
AND r.scheduled_date = ?
|
||||
AND (
|
||||
r.status = 'success'
|
||||
OR (
|
||||
r.status = 'collecting'
|
||||
AND r.started_at >= datetime('now', '-30 minutes')
|
||||
)
|
||||
OR (
|
||||
r.status = 'failed'
|
||||
AND r.completed_at >= datetime('now', '-15 minutes')
|
||||
)
|
||||
)
|
||||
)`,
|
||||
)
|
||||
.bind(...(distributionId ? [taskId, distributionId, scheduledDate] : [taskId, scheduledDate]))
|
||||
.all<{ id: string }>();
|
||||
return results.map((row) => row.id);
|
||||
}
|
||||
|
||||
export async function runDueScheduledCollections(
|
||||
db: DatabaseClient,
|
||||
timestamp: number,
|
||||
@@ -402,12 +462,9 @@ export async function runDueScheduledCollections(
|
||||
const currentDate = shanghaiDateFromTimestamp(timestamp);
|
||||
const tasks = await db
|
||||
.prepare(
|
||||
`SELECT id, collection_start_date, collection_days
|
||||
`SELECT id, task_type, collection_start_date, collection_days
|
||||
FROM tasks
|
||||
WHERE collection_start_date IS NOT NULL
|
||||
AND collection_start_date != ''
|
||||
AND collection_days IS NOT NULL
|
||||
AND collection_days != '[]'
|
||||
WHERE COALESCE(task_type, 'content_publish') != 'screenshot_collect'
|
||||
AND (? IS NULL OR id = ?)`,
|
||||
)
|
||||
.bind(onlyTaskId ?? null, onlyTaskId ?? null)
|
||||
@@ -417,46 +474,25 @@ export async function runDueScheduledCollections(
|
||||
let succeeded = 0;
|
||||
let failed = 0;
|
||||
|
||||
for (const task of tasks.results) {
|
||||
const schedules = dueSchedules(
|
||||
task.collection_start_date,
|
||||
parseDays(task.collection_days),
|
||||
timestamp,
|
||||
);
|
||||
const collectDue = async (
|
||||
taskId: string,
|
||||
schedules: { scheduleDay: number; scheduledDate: string }[],
|
||||
distributionId?: string,
|
||||
) => {
|
||||
for (const { scheduleDay, scheduledDate } of schedules) {
|
||||
dueTasks += 1;
|
||||
const distributions = await db
|
||||
.prepare(
|
||||
`SELECT d.id FROM distributions d
|
||||
WHERE d.task_id = ?
|
||||
AND d.publish_url IS NOT NULL
|
||||
AND d.publish_url != ''
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM collection_runs r
|
||||
WHERE r.distribution_id = d.id
|
||||
AND r.scheduled_date = ?
|
||||
AND (
|
||||
r.status = 'success'
|
||||
OR (
|
||||
r.status = 'collecting'
|
||||
AND r.started_at >= datetime('now', '-30 minutes')
|
||||
)
|
||||
OR (
|
||||
r.status = 'failed'
|
||||
AND r.completed_at >= datetime('now', '-15 minutes')
|
||||
)
|
||||
)
|
||||
)`,
|
||||
)
|
||||
.bind(task.id, scheduledDate)
|
||||
.all<{ id: string }>();
|
||||
|
||||
for (const distribution of distributions.results) {
|
||||
const ids = await distributionIdsNeedingCollection(
|
||||
db,
|
||||
taskId,
|
||||
scheduledDate,
|
||||
distributionId,
|
||||
);
|
||||
for (const id of ids) {
|
||||
attempted += 1;
|
||||
try {
|
||||
await collectDistributionMetrics(
|
||||
db,
|
||||
distribution.id,
|
||||
id,
|
||||
scheduledDate,
|
||||
scheduleDay,
|
||||
source,
|
||||
@@ -468,6 +504,47 @@ export async function runDueScheduledCollections(
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
for (const task of tasks.results) {
|
||||
const parsedDays = parseDays(task.collection_days);
|
||||
if (task.collection_start_date && parsedDays.length > 0) {
|
||||
await collectDue(
|
||||
task.id,
|
||||
dueSchedules(task.collection_start_date, parsedDays, timestamp),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
// 未设置采集计划:默认按每篇作品的发布时间起连续采集 7 天。
|
||||
const days = effectiveCollectionDays(task.collection_days);
|
||||
const published = await db
|
||||
.prepare(
|
||||
`SELECT d.id, COALESCE(d.publish_time, d.claimed_at) AS publish_anchor
|
||||
FROM distributions d
|
||||
WHERE d.task_id = ?
|
||||
AND d.publish_url IS NOT NULL
|
||||
AND d.publish_url != ''`,
|
||||
)
|
||||
.bind(task.id)
|
||||
.all<{ id: string; publish_anchor: string | null }>();
|
||||
for (const distribution of published.results) {
|
||||
const anchorDate = shanghaiDateFromSqlTimestamp(
|
||||
distribution.publish_anchor,
|
||||
);
|
||||
if (!anchorDate) continue;
|
||||
const windowEndDate = dateForScheduleDay(
|
||||
anchorDate,
|
||||
days[days.length - 1],
|
||||
);
|
||||
// 默认窗口已过(发布超过 7 天且从未配置计划)不再补采历史天,
|
||||
// 避免 D1 快照与真实发布时间脱节;确需补采时由管理员设置明确计划。
|
||||
if (!windowEndDate || currentDate > windowEndDate) continue;
|
||||
await collectDue(
|
||||
task.id,
|
||||
dueSchedules(anchorDate, days, timestamp),
|
||||
distribution.id,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return { currentDate, dueTasks, attempted, succeeded, failed };
|
||||
|
||||
@@ -127,7 +127,7 @@ export function registerMcpOperationTools(server: McpServer, options: Options) {
|
||||
"collection_plan_set",
|
||||
{
|
||||
title: "设置自动采集计划",
|
||||
description: "为任务设置开始日期及第1至第7天的自动采集日,系统在北京时间09:00执行。",
|
||||
description: "为任务设置开始日期及第1至第7天的自动采集日,系统在北京时间09:00执行;未设置时默认自每篇发布时间起连续采集7天。",
|
||||
inputSchema: z.object({
|
||||
task_id: z.string().min(1),
|
||||
start_date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).describe("北京时间开始日期 YYYY-MM-DD"),
|
||||
|
||||
@@ -380,6 +380,11 @@ export async function ensureSchema(database?: DatabaseClient) {
|
||||
"account_id",
|
||||
"account_id TEXT NOT NULL DEFAULT ''",
|
||||
);
|
||||
await ensureColumn(
|
||||
"partners",
|
||||
"released_account_id",
|
||||
"released_account_id TEXT NOT NULL DEFAULT ''",
|
||||
);
|
||||
await ensureColumn(
|
||||
"users",
|
||||
"wecom_userid",
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { ensureSchema, getRawDb } from "./mvp-db";
|
||||
import { type DatabaseClient } from "./database";
|
||||
import { ensureSchema, getRawDb, hashText } from "./mvp-db";
|
||||
import {
|
||||
generateAccountTagsFromMcp,
|
||||
resolveCollectionMcpConfig,
|
||||
@@ -448,3 +449,130 @@ export async function writeAnalyzedRows(rows: AnalyzedRow[]) {
|
||||
await db.batch(statements.slice(index, index + RESOURCE_IMPORT_DB_BATCH_SIZE));
|
||||
}
|
||||
}
|
||||
|
||||
const POOL_PARTNER_PLACEHOLDER_NICKNAME = "待识别账号";
|
||||
export const POOL_PARTNER_SYNC_BATCH_SIZE = 500;
|
||||
const POOL_PARTNER_SYNC_MAX_ITERATIONS = 30;
|
||||
|
||||
type PoolAccountRow = {
|
||||
id: string;
|
||||
platform: string;
|
||||
platform_uid: string;
|
||||
public_account_id: string;
|
||||
nickname: string;
|
||||
};
|
||||
|
||||
type ShellPartnerNameRow = PoolAccountRow & {
|
||||
partner_id: string;
|
||||
partner_name: string;
|
||||
};
|
||||
|
||||
export function poolPartnerDisplayName(account: {
|
||||
nickname?: string | null;
|
||||
public_account_id?: string | null;
|
||||
platform_uid?: string | null;
|
||||
id: string;
|
||||
}) {
|
||||
const nickname = (account.nickname ?? "").trim();
|
||||
if (nickname && nickname !== POOL_PARTNER_PLACEHOLDER_NICKNAME) return nickname;
|
||||
return (
|
||||
(account.public_account_id ?? "").trim() ||
|
||||
(account.platform_uid ?? "").trim() ||
|
||||
account.id
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 资源池中的 KOC 默认加入合作方:为尚未绑定合作方的资源账号自动创建
|
||||
* external_koc 合作方。运营显式解绑或被换绑顶替过的账号
|
||||
* (partners.released_account_id)不回填,保留人工决定。
|
||||
* 合作方 id 由账号身份哈希派生,配合 INSERT OR IGNORE 保证并发下幂等。
|
||||
*/
|
||||
export async function ensurePartnersForPoolAccounts(database?: DatabaseClient) {
|
||||
const db = database ?? getRawDb();
|
||||
for (
|
||||
let iteration = 0;
|
||||
iteration < POOL_PARTNER_SYNC_MAX_ITERATIONS;
|
||||
iteration += 1
|
||||
) {
|
||||
const unbound = await db
|
||||
.prepare(
|
||||
`SELECT a.id, a.platform, a.platform_uid, a.public_account_id, a.nickname
|
||||
FROM accounts a
|
||||
WHERE NOT EXISTS (SELECT 1 FROM partners p WHERE p.account_id = a.id)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM partners r WHERE r.released_account_id = a.id
|
||||
)
|
||||
ORDER BY a.first_seen_at, a.id
|
||||
LIMIT ${POOL_PARTNER_SYNC_BATCH_SIZE}`,
|
||||
)
|
||||
.all<PoolAccountRow>();
|
||||
if (unbound.results.length === 0) break;
|
||||
const statements = unbound.results.map((account) =>
|
||||
db
|
||||
.prepare(
|
||||
`INSERT OR IGNORE INTO partners
|
||||
(id, name, wecom_name, owner, claimed_total, completed_total,
|
||||
partner_type, wecom_userid, account_id, created_at)
|
||||
VALUES (?, ?, '', '运营组', 0, 0, 'external_koc', '', ?, ?)`,
|
||||
)
|
||||
.bind(
|
||||
`partner-koc-${hashText(`${account.platform}:${account.platform_uid}`)}`,
|
||||
poolPartnerDisplayName(account),
|
||||
account.id,
|
||||
new Date().toISOString(),
|
||||
),
|
||||
);
|
||||
for (
|
||||
let index = 0;
|
||||
index < statements.length;
|
||||
index += RESOURCE_IMPORT_DB_BATCH_SIZE
|
||||
) {
|
||||
await db.batch(statements.slice(index, index + RESOURCE_IMPORT_DB_BATCH_SIZE));
|
||||
}
|
||||
if (unbound.results.length < POOL_PARTNER_SYNC_BATCH_SIZE) break;
|
||||
}
|
||||
await refreshPoolPartnerNames(db);
|
||||
}
|
||||
|
||||
/**
|
||||
* 补全默认合作方的展示名:仅处理无企微绑定、无领取/分发记录的空壳合作方,
|
||||
* 昵称补全后把占位名(待识别账号)或账号 ID 兜底名更新为当前昵称,
|
||||
* 不碰运营可能手工维护过含义的名称。
|
||||
*/
|
||||
async function refreshPoolPartnerNames(db: DatabaseClient) {
|
||||
const rows = await db
|
||||
.prepare(
|
||||
`SELECT p.id AS partner_id, p.name AS partner_name,
|
||||
a.id, a.nickname, a.public_account_id, a.platform_uid
|
||||
FROM partners p
|
||||
JOIN accounts a ON a.id = p.account_id
|
||||
WHERE p.partner_type = 'external_koc'
|
||||
AND (p.wecom_external_user_id IS NULL OR p.wecom_external_user_id = '')
|
||||
AND p.wecom_userid = ''
|
||||
AND NOT EXISTS (SELECT 1 FROM claims c WHERE c.partner_id = p.id)
|
||||
AND NOT EXISTS (SELECT 1 FROM distributions d WHERE d.partner_id = p.id)
|
||||
AND (
|
||||
p.name IN ('', '${POOL_PARTNER_PLACEHOLDER_NICKNAME}')
|
||||
OR (p.name IN (a.public_account_id, a.platform_uid)
|
||||
AND a.nickname NOT IN ('', '${POOL_PARTNER_PLACEHOLDER_NICKNAME}'))
|
||||
)`,
|
||||
)
|
||||
.all<ShellPartnerNameRow>();
|
||||
const updates = rows.results
|
||||
.filter(
|
||||
(row) => poolPartnerDisplayName(row) !== row.partner_name.trim(),
|
||||
)
|
||||
.map((row) =>
|
||||
db
|
||||
.prepare(`UPDATE partners SET name = ? WHERE id = ?`)
|
||||
.bind(poolPartnerDisplayName(row), row.partner_id),
|
||||
);
|
||||
for (
|
||||
let index = 0;
|
||||
index < updates.length;
|
||||
index += RESOURCE_IMPORT_DB_BATCH_SIZE
|
||||
) {
|
||||
await db.batch(updates.slice(index, index + RESOURCE_IMPORT_DB_BATCH_SIZE));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user