feat: 负责人下钻视图+项目负责人编辑管理+企微授权自助绑定

- 任务/项目负责人 union scope:任务中心分组视图、工作台项目进度卡、收录验证列
- 项目负责人可管理自己项目:编辑/删除项目、编辑任务、分配负责人、推送、项目内建任务;堵死转移归属/越权新建/任务迁移
- 只读依赖放开给已登录:inspect_feishu、wecom_list_internal_contacts
- 企微 OAuth 自助绑定:/api/wecom-bind(state 签名校验+登录态一致性+归属冲突检查),未绑定 banner 增加入口
- 工作台项目进度卡与未绑定 banner 排版修复
- 修 user-auth 判空分支被 Turbopack dev 折叠删除导致匿名请求 500
This commit is contained in:
ABAPPLO
2026-09-17 15:21:24 +08:00
parent dd5b603149
commit 8f1ebf7697
24 changed files with 1812 additions and 213 deletions

View File

@@ -19,11 +19,9 @@ export type CreateDistributionTaskInput = {
export type CreateScreenshotTaskInput = {
name: string;
brand: string;
relatedTaskId: string;
dueAt: string;
keyword: string;
instructions: string;
quantity: number;
exampleImageKey?: string;
projectId?: string;
ownerUserid?: string;
@@ -52,6 +50,7 @@ export type ScreenshotTaskCreation = {
brand: string;
dueAt: string;
quantity: number;
relatedTaskId: string;
};
type TaskRow = {
@@ -147,8 +146,8 @@ async function insertTaskFromSource(
(id, name, brand, quantity, claimed_quantity, due_at, status,
platform, content_format, project_id, owner_userid,
source_url, source_sheet_id, source_sheet_name, source_synced_at,
share_token)
VALUES (?, ?, ?, ?, 0, ?, 'importing', ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
share_token, join_qr_code)
VALUES (?, ?, ?, ?, 0, ?, 'importing', ?, ?, ?, ?, ?, ?, ?, ?, ?, '')`,
)
.bind(
taskId,
@@ -278,32 +277,70 @@ export async function createDistributionTask(
};
}
type ScreenshotSourceTaskRow = {
id: string;
name: string;
brand: string;
quantity: number;
project_id: string;
owner_userid: string;
task_type: "content_publish" | "screenshot_collect";
};
type ScreenshotSourceContentRow = {
id: string;
title: string;
source_row: number | null;
};
export async function createScreenshotTask(
rawInput: CreateScreenshotTaskInput,
): Promise<ScreenshotTaskCreation> {
await ensureSchema();
const input = {
name: normalizedValue(rawInput.name),
brand: normalizedValue(rawInput.brand),
relatedTaskId: normalizedValue(rawInput.relatedTaskId),
dueAt: normalizedDueDate(rawInput.dueAt),
keyword: normalizedValue(rawInput.keyword),
instructions: normalizedValue(rawInput.instructions),
quantity: Math.floor(Number(rawInput.quantity)),
exampleImageKey: normalizedValue(rawInput.exampleImageKey ?? ""),
projectId: normalizedValue(rawInput.projectId ?? ""),
ownerUserid: normalizedValue(rawInput.ownerUserid ?? "").slice(0, 64),
};
if (!input.name || !input.brand || !input.keyword || !input.instructions) {
throw new Error("请补全任务名称、品牌/项目、搜索关键词和任务说明");
}
if (!Number.isInteger(input.quantity) || input.quantity < 1 || input.quantity > 500) {
throw new Error("任务数量需为 1—500 份");
if (!input.name || !input.relatedTaskId || !input.instructions) {
throw new Error("请补全任务名称、关联分发任务和任务说明");
}
if (input.exampleImageKey && !input.exampleImageKey.startsWith("task-assets/")) {
throw new Error("示例截图无效,请重新上传");
}
const db = getRawDb();
const source = await db
.prepare(
`SELECT id, name, brand, quantity, project_id, owner_userid, task_type
FROM tasks WHERE id = ?`,
)
.bind(input.relatedTaskId)
.first<ScreenshotSourceTaskRow>();
if (!source) throw new Error("关联的分发任务不存在");
if (source.task_type !== "content_publish") {
throw new Error("请选择内容发布类型的分发任务");
}
const existing = await db
.prepare("SELECT id FROM tasks WHERE related_task_id = ?")
.bind(input.relatedTaskId)
.first<{ id: string }>();
if (existing) throw new Error("该分发任务已有关联回收任务");
const sourceContents = await db
.prepare(
"SELECT id, title, source_row FROM contents WHERE task_id = ? ORDER BY source_row, id",
)
.bind(input.relatedTaskId)
.all<ScreenshotSourceContentRow>();
if (sourceContents.results.length === 0) {
throw new Error("分发任务还没有可回收的笔记");
}
const taskId = uid("task");
const shareToken = crypto.randomUUID().replaceAll("-", "");
const imageAssets = input.exampleImageKey
@@ -311,41 +348,45 @@ export async function createScreenshotTask(
{ index: 1, key: input.exampleImageKey, width: null, height: null },
])
: "[]";
const projectId = input.projectId || source.project_id;
const ownerUserid = input.ownerUserid || source.owner_userid;
await db
.prepare(
`INSERT INTO tasks
(id, name, brand, quantity, claimed_quantity, due_at, status,
task_type, project_id, owner_userid, source_url, source_sheet_id, source_sheet_name,
share_token, collection_days)
VALUES (?, ?, ?, ?, 0, ?, 'active', 'screenshot_collect', ?, ?, '', '', '', ?, '[]')`,
share_token, collection_days, join_qr_code, related_task_id)
VALUES (?, ?, ?, ?, 0, ?, 'active', 'screenshot_collect', ?, ?, '', '', '', ?, '[]', '', ?)`,
)
.bind(
taskId,
input.name,
input.brand,
input.quantity,
source.brand,
sourceContents.results.length,
input.dueAt,
input.projectId,
input.ownerUserid,
projectId,
ownerUserid,
shareToken,
input.relatedTaskId,
)
.run();
try {
const statements = Array.from({ length: input.quantity }, (_, index) =>
const statements = sourceContents.results.map((row) =>
db
.prepare(
`INSERT INTO contents
(id, task_id, title, body, image_assets, video_assets, status, source, source_row)
VALUES (?, ?, ?, ?, ?, '[]', 'available', '截图回收任务', ?)`,
(id, task_id, title, body, image_assets, video_assets, status, source, source_row, related_content_id)
VALUES (?, ?, ?, ?, ?, '[]', 'available', '截图回收任务', ?, ?)`,
)
.bind(
uid("content"),
taskId,
input.keyword,
row.title,
input.instructions,
imageAssets,
index + 1,
row.source_row,
row.id,
),
);
for (let index = 0; index < statements.length; index += 100) {
@@ -364,9 +405,10 @@ export async function createScreenshotTask(
taskId,
shareToken,
name: input.name,
brand: input.brand,
brand: source.brand,
dueAt: input.dueAt,
quantity: input.quantity,
quantity: sourceContents.results.length,
relatedTaskId: input.relatedTaskId,
};
}
@@ -397,6 +439,7 @@ type UpdatableTaskRow = {
claimed_quantity: number;
platform: "小红书" | "抖音";
content_format: "image_text" | "video";
related_task_id: string;
};
// 截图任务的 keyword/instructions 存在 contents 每行(title/body),数量与行数一一对应
@@ -492,7 +535,7 @@ export async function updateTask(
const row = await db
.prepare(
"SELECT id, task_type, quantity, claimed_quantity, platform, content_format FROM tasks WHERE id = ?",
"SELECT id, task_type, quantity, claimed_quantity, platform, content_format, related_task_id FROM tasks WHERE id = ?",
)
.bind(taskId)
.first<UpdatableTaskRow>();
@@ -501,27 +544,65 @@ export async function updateTask(
const claimed = Number(row.claimed_quantity);
let quantity = Number(row.quantity);
if (row.task_type === "screenshot_collect") {
if (!input.keyword || !input.instructions) {
throw new Error("请补全搜索关键词和任务说明");
const related = Boolean(row.related_task_id);
if (!input.instructions) {
throw new Error(related ? "请补全任务说明" : "请补全搜索关键词和任务说明");
}
if (
!Number.isInteger(input.quantity) ||
input.quantity < 1 ||
input.quantity > 500
) {
throw new Error("任务数量需为 1—500 份");
if (related) {
if (
rawInput.quantity !== undefined &&
rawInput.quantity !== null &&
input.quantity !== quantity
) {
throw new Error("关联任务的份数由分发任务决定,不可修改");
}
if (input.keyword) {
const firstTitle = await db
.prepare(
"SELECT title FROM contents WHERE task_id = ? ORDER BY source_row, id LIMIT 1",
)
.bind(taskId)
.first<{ title: string }>();
if (firstTitle && input.keyword !== firstTitle.title) {
throw new Error("关联任务的搜索关键词由分发任务笔记决定,不可修改");
}
}
await db
.prepare("UPDATE contents SET body = ? WHERE task_id = ?")
.bind(input.instructions, taskId)
.run();
if (input.exampleImageKey.startsWith("task-assets/")) {
const imageAssets = JSON.stringify([
{ index: 1, key: input.exampleImageKey, width: null, height: null },
]);
await db
.prepare("UPDATE contents SET image_assets = ? WHERE task_id = ?")
.bind(imageAssets, taskId)
.run();
}
} else {
if (!input.keyword) {
throw new Error("请补全搜索关键词和任务说明");
}
if (
!Number.isInteger(input.quantity) ||
input.quantity < 1 ||
input.quantity > 500
) {
throw new Error("任务数量需为 1—500 份");
}
if (input.quantity < claimed) {
throw new Error(`任务数量不能低于已领取的 ${claimed} 份`);
}
quantity = input.quantity;
await syncScreenshotContents(db, taskId, {
oldQuantity: Number(row.quantity),
quantity,
keyword: input.keyword,
instructions: input.instructions,
exampleImageKey: input.exampleImageKey,
});
}
if (input.quantity < claimed) {
throw new Error(`任务数量不能低于已领取的 ${claimed} 份`);
}
quantity = input.quantity;
await syncScreenshotContents(db, taskId, {
oldQuantity: Number(row.quantity),
quantity,
keyword: input.keyword,
instructions: input.instructions,
exampleImageKey: input.exampleImageKey,
});
}
await db
@@ -542,6 +623,7 @@ export async function updateTask(
taskId,
)
.run();
await refreshTaskStatus(taskId, db).catch(() => undefined);
return { taskId, quantity, claimedQuantity: claimed };
}
@@ -553,3 +635,60 @@ export function buildClaimUrl(portalOrigin: string, shareToken: string) {
url.searchParams.set("task", shareToken);
return url.toString();
}
type TaskStatusRow = {
id: string;
task_type: "content_publish" | "screenshot_collect";
status: string;
};
type DistributionProgressRow = {
total: number;
complete_count: number;
submitted_count: number;
};
// 纯重算任务状态:条件满足置 completed,不满足回 active(释放重开等场景);
// importing 是导入中间态,不参与推导
export async function refreshTaskStatus(
taskId: string,
db: DatabaseClient = getRawDb(),
): Promise<string> {
const row = await db
.prepare("SELECT id, task_type, status FROM tasks WHERE id = ?")
.bind(taskId)
.first<TaskStatusRow>();
if (!row || row.status === "importing") return row?.status ?? "";
const progress = await db
.prepare(
`SELECT COUNT(*) AS total,
COALESCE(SUM(CASE WHEN status = 'complete' THEN 1 ELSE 0 END), 0) AS complete_count,
COALESCE(SUM(CASE WHEN result_submitted_at IS NOT NULL THEN 1 ELSE 0 END), 0) AS submitted_count
FROM distributions WHERE task_id = ?`,
)
.bind(taskId)
.first<DistributionProgressRow>();
const available = await db
.prepare(
"SELECT COUNT(*) AS available_count FROM contents WHERE task_id = ? AND status = 'available'",
)
.bind(taskId)
.first<{ available_count: number }>();
const total = Number(progress?.total ?? 0);
const availableCount = Number(available?.available_count ?? 0);
let completed = false;
if (total > 0 && availableCount === 0) {
completed =
row.task_type === "screenshot_collect"
? Number(progress?.submitted_count ?? 0) === total
: Number(progress?.complete_count ?? 0) === total;
}
const next = completed ? "completed" : "active";
if (next !== row.status) {
await db
.prepare("UPDATE tasks SET status = ? WHERE id = ?")
.bind(next, taskId)
.run();
}
return next;
}