- 项目总群:≥3 人 appchat/create 自动建群(chatid=pgc+projectId),成员 diff 增删同步;DROP project_group_chats;推送改 appchat/send 群发、无群降级逐人 - 任务负责人:tasks.owner_userid(新建/编辑任务通讯录选人),task_set_owner 分配即预建 tgc 任务群发介绍,群主链接只归属负责人 - 任务编辑 task_update:基础字段+截图任务数量/关键词/说明,contents 调平只删 available 尾行 - 删除「一键推送」:task-one-click-push-service 整体移除(recordCardAck 移入 wecom-callback-events) - 负责人后台视角:users.wecom_userid 绑定,lib/owner-scope 行级过滤(undefined=全量/""=空范围/非空=过滤),~30 action 补 managerGate/taskOwnerGate,owner 登录仅见工作台/任务中心/数据回收/复盘报表 - 任务绑定客户群+进群活码:tasks 增 customer_chat_id/name、join_config_id、join_qr_code 4 列,join_way add/update/get/del(换绑走 update 保 config_id 活码不变),后台「客户群」弹窗,portal 认领后扫码自助进群(满员自动开新群) - 迁移 0013/0014/0015;催办支持按任务;企微可信域名 IP 验证文件 - 288 测试全绿;真机全链路验收通过(列群/活码四端点/portal 渲染/解绑清空)
87 lines
2.5 KiB
TypeScript
87 lines
2.5 KiB
TypeScript
import type { DatabaseClient } from "./database";
|
|
|
|
export type WecomCallbackFields = Record<string, string>;
|
|
|
|
export type WecomCallbackEventResult = {
|
|
recognized: boolean;
|
|
action: string;
|
|
taskId: string;
|
|
btnKey: string;
|
|
operator: string;
|
|
};
|
|
|
|
export function parseWecomCallbackXml(xml: string): WecomCallbackFields {
|
|
const fields: WecomCallbackFields = {};
|
|
const source = String(xml ?? "");
|
|
const pattern =
|
|
/<([A-Za-z][A-Za-z0-9_]*)>(?:<!\[CDATA\[([\s\S]*?)\]\]>|([^<]*))<\/\1>/g;
|
|
let match: RegExpExecArray | null;
|
|
while ((match = pattern.exec(source)) !== null) {
|
|
const tag = match[1];
|
|
const value = match[2] ?? match[3] ?? "";
|
|
if (!(tag in fields)) fields[tag] = value.trim();
|
|
}
|
|
return fields;
|
|
}
|
|
|
|
export function extractWecomEncryptField(body: string): string {
|
|
const fields = parseWecomCallbackXml(body);
|
|
return fields.Encrypt ?? "";
|
|
}
|
|
|
|
export async function recordCardAck(
|
|
db: DatabaseClient,
|
|
params: { cardTaskId: string; btnKey: string; operator: string },
|
|
) {
|
|
const taskId = /^task:([^:]+):/.exec(params.cardTaskId)?.[1] ?? "";
|
|
await db
|
|
.prepare(
|
|
`INSERT INTO wecom_push_logs
|
|
(id, task_id, project_id, push_kind, main_group_count,
|
|
koc_userids, koc_ok_count, koc_fail_list, group_push_id, detail)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
)
|
|
.bind(
|
|
`wpl-${crypto.randomUUID().slice(0, 12)}`,
|
|
taskId || params.cardTaskId,
|
|
"",
|
|
"card_ack",
|
|
0,
|
|
"[]",
|
|
0,
|
|
"[]",
|
|
"",
|
|
JSON.stringify({
|
|
cardTaskId: params.cardTaskId,
|
|
btnKey: params.btnKey,
|
|
operator: params.operator,
|
|
}),
|
|
)
|
|
.run();
|
|
}
|
|
|
|
export async function handleWecomCallbackEvent(
|
|
db: DatabaseClient,
|
|
fields: WecomCallbackFields,
|
|
): Promise<WecomCallbackEventResult> {
|
|
const event = fields.Event ?? "";
|
|
const taskId = fields.TaskId ?? "";
|
|
const btnKey = fields.BtnKey ?? "";
|
|
const operator = fields.FromUserName ?? "";
|
|
|
|
if (fields.MsgType === "event" && event === "template_card_event") {
|
|
if (btnKey === "mark_handled") {
|
|
await recordCardAck(db, { cardTaskId: taskId, btnKey, operator });
|
|
console.info("[KOC LOOP] wecom card ack recorded", { taskId, operator });
|
|
return { recognized: true, action: "card_ack", taskId, btnKey, operator };
|
|
}
|
|
return { recognized: true, action: "ignored", taskId, btnKey, operator };
|
|
}
|
|
|
|
console.info("[KOC LOOP] wecom callback event ignored", {
|
|
msgType: fields.MsgType ?? "",
|
|
event,
|
|
});
|
|
return { recognized: false, action: "ignored", taskId, btnKey, operator };
|
|
}
|