微信企业机器人创建

This commit is contained in:
2026-07-26 08:16:56 +08:00
parent f053908acb
commit 34532dc9bf
26 changed files with 1963 additions and 4 deletions
@@ -49,6 +49,16 @@ import { DELIVERY_QUEUE } from '../jobs/jobs.constants';
TencentLbsProvider,
{ provide: MAP_PROVIDER, useExisting: TencentLbsProvider },
],
exports: [SMS_PROVIDER, SmsCodeStore, PAY_PROVIDER, DELIVERY_PROVIDER, WECHAT_PROVIDER, OSS_PROVIDER, MAP_PROVIDER, TencentLbsProvider, CourierModule],
exports: [
SMS_PROVIDER,
SmsCodeStore,
PAY_PROVIDER,
DELIVERY_PROVIDER,
WECHAT_PROVIDER,
OSS_PROVIDER,
MAP_PROVIDER,
TencentLbsProvider,
CourierModule,
],
})
export class IntegrationsModule {}
@@ -0,0 +1,313 @@
import {
Injectable,
Logger,
OnModuleDestroy,
OnModuleInit,
} from '@nestjs/common';
import { WSClient, generateReqId, type WsFrame } from '@wecom/aibot-node-sdk';
import {
WECOM_BOT_ROLES,
resolveWecomBotPermissions,
type WecomBotRole,
} from '@dukang/shared-types';
import { PrismaService } from '../../common/prisma/prisma.module';
import { WecomBotActionsService } from './wecom-bot-actions.service';
import type { WecomBotRuntimeConfig } from './wecom-bot.types';
const DEFAULT_WELCOMES: Record<WecomBotRole, string> = {
CUSTOMER_SERVICE:
'您好,我是杜康好客【客服】助手。发送「帮助」查看:创建售后工单、查用户(需短信验证)、查快递。',
TECH_SUPPORT:
'您好,我是杜康好客【技术支持】助手。发送「帮助」查看:创建技术支持工单、查看开发进度。',
TEAM_ASSISTANT:
'您好,我是杜康好客【团队助手】。发送「帮助」或「手册 关键词」查询系统使用说明。',
CUSTOM: '您好!发送「帮助」查看可用指令。',
};
export type WecomAibotSlotStatus = {
id: string;
role: string;
key: string;
name: string;
enabled: boolean;
configured: boolean;
connected: boolean;
botIdMasked: string | null;
avatarUrl: string | null;
permissions: string[];
lastError: string | null;
};
export type WecomAibotStatus = {
masterEnabled: boolean;
bots: WecomAibotSlotStatus[];
};
type BotRuntime = {
config: WecomBotRuntimeConfig;
client: WSClient | null;
lastError: string | null;
};
function isWecomRole(v: string): v is WecomBotRole {
return (WECOM_BOT_ROLES as readonly string[]).includes(v);
}
@Injectable()
export class WecomAibotService implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(WecomAibotService.name);
private runtimes = new Map<string, BotRuntime>();
private starting = false;
constructor(
private readonly prisma: PrismaService,
private readonly actions: WecomBotActionsService,
) {}
async onModuleInit() {
await this.reload('boot');
}
async onModuleDestroy() {
this.stopAll('shutdown');
}
getStatus(): WecomAibotStatus {
const masterEnabled = process.env.WECOM_AIBOT_ENABLED === 'true';
const bots: WecomAibotSlotStatus[] = [];
for (const rt of this.runtimes.values()) {
const cfg = rt.config;
bots.push({
id: cfg.id,
role: cfg.role,
key: cfg.key,
name: cfg.name,
enabled: cfg.enabled,
configured: !!(cfg.botId && cfg.secret),
connected: !!rt.client?.isConnected,
botIdMasked: cfg.botId ? maskId(cfg.botId) : null,
avatarUrl: cfg.avatarUrl,
permissions: cfg.permissions,
lastError: rt.lastError,
});
}
return { masterEnabled, bots };
}
async reload(reason = 'config'): Promise<WecomAibotStatus> {
if (this.starting) {
this.logger.warn(`wecom aibot reload skipped (busy): ${reason}`);
return this.getStatus();
}
this.starting = true;
try {
this.stopAll(reason);
const masterEnabled = process.env.WECOM_AIBOT_ENABLED === 'true';
const rows = await this.prisma.wecomBot.findMany({
orderBy: [{ sortOrder: 'asc' }, { id: 'asc' }],
});
if (!masterEnabled) {
this.logger.log(`wecom aibot master disabled (${reason})`);
for (const row of rows) {
const cfg = this.rowToConfig(row);
this.runtimes.set(cfg.key, { config: cfg, client: null, lastError: null });
}
return this.getStatus();
}
for (const row of rows) {
const cfg = this.rowToConfig(row);
if (!cfg.enabled || !cfg.botId || !cfg.secret) {
this.runtimes.set(cfg.key, { config: cfg, client: null, lastError: null });
continue;
}
try {
await this.startBot(cfg);
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
this.logger.error(`wecom bot ${cfg.key} start failed: ${msg}`);
this.runtimes.set(cfg.key, { config: cfg, client: null, lastError: msg });
}
}
return this.getStatus();
} catch (e) {
// 表未创建时不阻断启动
const msg = e instanceof Error ? e.message : String(e);
this.logger.warn(`wecom aibot reload failed (${reason}): ${msg}`);
return this.getStatus();
} finally {
this.starting = false;
}
}
private rowToConfig(row: {
id: bigint;
name: string;
role: string;
botId: string;
secret: string;
avatarUrl: string | null;
welcome: string | null;
permissions: string;
enabled: boolean;
}): WecomBotRuntimeConfig {
const role = (isWecomRole(row.role) ? row.role : 'CUSTOM') as WecomBotRole;
return {
id: row.id.toString(),
key: `db_${row.id.toString()}`,
role,
name: row.name,
enabled: row.enabled,
botId: row.botId,
secret: row.secret,
avatarUrl: row.avatarUrl,
welcome: row.welcome?.trim() || DEFAULT_WELCOMES[role],
permissions: resolveWecomBotPermissions(role, row.permissions),
};
}
private stopAll(reason: string) {
for (const [key, rt] of this.runtimes) {
if (!rt.client) continue;
try {
rt.client.removeAllListeners();
rt.client.disconnect();
this.logger.log(`wecom bot ${key} disconnected (${reason})`);
} catch (e) {
this.logger.warn(`wecom bot ${key} disconnect error: ${String(e)}`);
}
}
this.runtimes.clear();
}
private async startBot(cfg: WecomBotRuntimeConfig) {
const client = new WSClient({
botId: cfg.botId,
secret: cfg.secret,
maxReconnectAttempts: -1,
maxAuthFailureAttempts: 5,
heartbeatInterval: 30_000,
logger: {
debug: (msg, ...args) => this.logger.debug(`[${cfg.key}] ${formatSdkLog(msg, args)}`),
info: (msg, ...args) => this.logger.log(`[${cfg.key}] ${formatSdkLog(msg, args)}`),
warn: (msg, ...args) => this.logger.warn(`[${cfg.key}] ${formatSdkLog(msg, args)}`),
error: (msg, ...args) => this.logger.error(`[${cfg.key}] ${formatSdkLog(msg, args)}`),
},
});
const rt: BotRuntime = { config: cfg, client, lastError: null };
this.runtimes.set(cfg.key, rt);
client.on('authenticated', () => {
rt.lastError = null;
this.logger.log(`wecom bot ${cfg.key} authenticated bot=${maskId(cfg.botId)}`);
});
client.on('disconnected', (reason) => {
this.logger.warn(`wecom bot ${cfg.key} disconnected: ${reason || 'unknown'}`);
});
client.on('error', (err) => {
rt.lastError = err instanceof Error ? err.message : String(err);
this.logger.error(`wecom bot ${cfg.key} error: ${rt.lastError}`);
});
client.on('event.enter_chat', (frame: WsFrame) => {
void this.handleEnterChat(cfg, client, frame);
});
client.on('message.text', (frame: WsFrame) => {
void this.handleText(cfg, client, frame);
});
for (const evt of [
'message.image',
'message.file',
'message.voice',
'message.video',
'message.mixed',
] as const) {
client.on(evt, (frame: WsFrame) => {
void this.replyText(client, frame, '暂仅支持文本消息,请发送「帮助」。');
});
}
client.connect();
this.logger.log(`wecom bot ${cfg.key} connecting bot=${maskId(cfg.botId)}`);
}
private async handleEnterChat(cfg: WecomBotRuntimeConfig, client: WSClient, frame: WsFrame) {
try {
await client.replyWelcome(frame, {
msgtype: 'text',
text: { content: cfg.welcome },
});
} catch (e) {
this.logger.error(`[${cfg.key}] welcome failed: ${String(e)}`);
}
}
private async handleText(cfg: WecomBotRuntimeConfig, client: WSClient, frame: WsFrame) {
const raw = String(frame.body?.text?.content ?? '').trim();
const content = raw.replace(/@[^\s]+\s*/g, '').trim();
const wecomUserId = String(frame.body?.from?.userid ?? 'unknown');
const lower = content.toLowerCase();
try {
if (!content || lower === '帮助' || lower === 'help' || content === '?' || content === '') {
await this.replyText(client, frame, this.actions.buildHelp(cfg));
return;
}
if (lower === '状态' || lower === 'status' || lower === 'ping') {
await this.replyText(client, frame, this.formatStatusMarkdown());
return;
}
const reply = await this.actions.handleCommand(cfg, wecomUserId, content);
await this.replyText(client, frame, reply);
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
this.logger.error(`[${cfg.key}] handle text failed: ${msg}`);
await this.replyText(client, frame, `处理失败:${msg}`);
}
}
private formatStatusMarkdown(): string {
const st = this.getStatus();
const lines = [
'**企微机器人状态**',
`- 总开关:${st.masterEnabled ? '开' : '关'}(系统设置 → 功能开关)`,
'',
];
for (const b of st.bots) {
lines.push(
`**${b.name}**`,
`- 角色:${b.role}`,
`- 启用/配置/连接:${b.enabled ? '是' : '否'} / ${b.configured ? '是' : '否'} / ${b.connected ? '是' : '否'}`,
`- BotID${b.botIdMasked ?? '—'}`,
`- 权限:${b.permissions.join(', ') || '—'}`,
b.lastError ? `- 错误:${b.lastError}` : '',
'',
);
}
return lines.filter((l, i, arr) => l !== '' || arr[i - 1] !== '').join('\n');
}
private async replyText(client: WSClient, frame: WsFrame, content: string) {
const streamId = generateReqId('stream');
try {
await client.replyStream(frame, streamId, content, true);
} catch (e) {
this.logger.error(`reply failed: ${String(e)}`);
}
}
}
function maskId(id: string): string {
if (id.length <= 8) return `${id.slice(0, 2)}***`;
return `${id.slice(0, 4)}${id.slice(-4)}`;
}
function formatSdkLog(message: string, args: unknown[]): string {
if (!args.length) return message;
try {
return `${message} ${args.map((a) => (typeof a === 'string' ? a : JSON.stringify(a))).join(' ')}`;
} catch {
return message;
}
}
@@ -0,0 +1,459 @@
import { Inject, Injectable, Logger } from '@nestjs/common';
import {
SUPPORT_TICKET_STATUS_LABELS,
SUPPORT_TICKET_TYPE_LABELS,
TICKET_TYPE_LABELS,
type AfterSaleTicketType,
type SupportTicketTypeDto,
} from '@dukang/shared-types';
import { PrismaService } from '../../common/prisma/prisma.module';
import { TicketService } from '../../modules/common/ticket.service';
import { SupportTicketService } from '../../modules/common/support-ticket.service';
import { SMS_PROVIDER } from '../integrations.constants';
import type { ISmsProvider } from '../sms/sms.interface';
import type { WecomBotRuntimeConfig } from './wecom-bot.types';
import { wecomBotHasPermission } from './wecom-bot.types';
import { WecomBotSessionService } from './wecom-bot-session.service';
import { searchHandbook } from './wecom-handbook';
const SMS_SCENE = 'WECOM_USER_VIEW';
const PHONE_RE = /^1\d{10}$/;
const TICKET_TYPE_ALIASES: Record<string, AfterSaleTicketType> = {
退: 'REFUND',
退: 'REFUND',
refund: 'REFUND',
: 'RESHIPMENT',
: 'RESHIPMENT',
reshipment: 'RESHIPMENT',
退: 'DAMAGE_RETURN',
退: 'DAMAGE_RETURN',
damage_return: 'DAMAGE_RETURN',
退退: 'RETURN_REFUND',
return_refund: 'RETURN_REFUND',
};
const SUPPORT_TYPE_ALIASES: Record<string, SupportTicketTypeDto> = {
bug: 'BUG',
BUG: 'BUG',
: 'BUG',
: 'SUGGESTION',
suggestion: 'SUGGESTION',
: 'OTHER',
other: 'OTHER',
};
@Injectable()
export class WecomBotActionsService {
private readonly logger = new Logger(WecomBotActionsService.name);
constructor(
private readonly prisma: PrismaService,
private readonly ticketService: TicketService,
private readonly supportTicketService: SupportTicketService,
private readonly session: WecomBotSessionService,
@Inject(SMS_PROVIDER) private readonly sms: ISmsProvider,
) {}
buildHelp(bot: WecomBotRuntimeConfig): string {
const lines = [`**${bot.name}**`, '', '通用:`帮助` · `状态`', ''];
if (wecomBotHasPermission(bot, 'ticket.create')) {
lines.push(
'**售后工单**',
'`工单 <订单号> <类型> [备注]`',
'类型:仅退款 / 破损补发 / 破损退货 / 退货退款',
'例:`工单 DK123 仅退款 用户要求退款`',
'',
);
}
if (wecomBotHasPermission(bot, 'user.view_sms')) {
lines.push(
'**查用户(需短信验证)**',
'`查用户 <手机号>` → 向该手机发验证码',
'`验证 <验证码>` → 验证通过后展示用户摘要',
'',
);
}
if (wecomBotHasPermission(bot, 'delivery.view')) {
lines.push('**查快递**', '`快递 <订单号|运单号>`', '');
}
if (wecomBotHasPermission(bot, 'support_ticket.create')) {
lines.push(
'**技术支持提单**',
'`提单 <BUG|建议|其他> <标题> [| 详情]`',
'例:`提单 BUG 支付回调偶发失败 | 订单号xxx`',
'',
);
}
if (wecomBotHasPermission(bot, 'support_ticket.progress')) {
lines.push('**开发进度**', '`进度` 最近工单 · `进度 <工单号>` 详情', '');
}
if (wecomBotHasPermission(bot, 'handbook.query')) {
lines.push(
'**使用手册**',
'`手册` 目录 · `手册 <关键词>` 如:开城、核销、订单、财务',
'',
);
}
lines.push(`当前权限:${bot.permissions.join(', ') || '无'}`);
return lines.join('\n');
}
async handleCommand(
bot: WecomBotRuntimeConfig,
wecomUserId: string,
text: string,
): Promise<string> {
const content = text.trim();
if (!content) return this.buildHelp(bot);
// 查用户 / 验证
if (/^(查用户|用户)\s+/i.test(content)) {
this.requirePerm(bot, 'user.view_sms');
const phone = content.replace(/^(查用户|用户)\s+/i, '').trim();
return this.startUserView(bot, wecomUserId, phone);
}
if (/^(验证|verify)\s+/i.test(content)) {
this.requirePerm(bot, 'user.view_sms');
const code = content.replace(/^(验证|verify)\s+/i, '').trim();
return this.verifyUserView(bot, wecomUserId, code);
}
// 快递
if (/^(快递|配送|物流)\s+/i.test(content)) {
this.requirePerm(bot, 'delivery.view');
const q = content.replace(/^(快递|配送|物流)\s+/i, '').trim();
return this.lookupDelivery(q);
}
// 售后工单
if (/^(工单|创建工单)\s+/i.test(content)) {
this.requirePerm(bot, 'ticket.create');
return this.createAfterSaleTicket(content.replace(/^(工单|创建工单)\s+/i, '').trim(), wecomUserId);
}
// 技术支持提单
if (/^(提单|技术支持)\s+/i.test(content)) {
this.requirePerm(bot, 'support_ticket.create');
return this.createSupportTicket(content.replace(/^(提单|技术支持)\s+/i, '').trim(), wecomUserId);
}
// 进度
if (/^(进度|开发进度)/i.test(content)) {
this.requirePerm(bot, 'support_ticket.progress');
const rest = content.replace(/^(进度|开发进度)\s*/i, '').trim();
return this.supportProgress(rest);
}
// 手册
if (/^(手册|帮助文档|文档)/i.test(content)) {
this.requirePerm(bot, 'handbook.query');
const q = content.replace(/^(手册|帮助文档|文档)\s*/i, '').trim();
return this.queryHandbook(q);
}
// 自然语言手册(仅团队助手有 handbook 权限时)
if (wecomBotHasPermission(bot, 'handbook.query') && content.length >= 2) {
const hit = searchHandbook(content, 1);
if (hit.length) {
return formatHandbook(hit);
}
}
return `未识别指令。\n\n${this.buildHelp(bot)}`;
}
private requirePerm(bot: WecomBotRuntimeConfig, perm: Parameters<typeof wecomBotHasPermission>[1]) {
if (!wecomBotHasPermission(bot, perm)) {
throw new Error(`当前机器人无权限:${perm}`);
}
}
private async startUserView(bot: WecomBotRuntimeConfig, wecomUserId: string, phone: string) {
if (!PHONE_RE.test(phone)) return '请输入 11 位手机号,例如:`查用户 13800138000`';
const user = await this.prisma.user.findFirst({
where: { phone },
select: { id: true, userNo: true, phone: true },
});
if (!user) return `未找到手机号 ${phone} 对应的用户`;
await this.session.setPendingPhone(bot.key, wecomUserId, phone);
await this.sms.send(phone, SMS_SCENE);
return [
`已向 **${maskPhone(phone)}** 发送验证码(用户 ${user.userNo})。`,
'请回复:`验证 123456`',
'验证码约 3 分钟有效。',
].join('\n');
}
private async verifyUserView(bot: WecomBotRuntimeConfig, wecomUserId: string, code: string) {
const sess = await this.session.get(bot.key, wecomUserId);
if (!sess?.phone) return '请先发送:`查用户 <手机号>`';
if (!code) return '请提供验证码,例如:`验证 123456`';
try {
await this.sms.verify(sess.phone, code, SMS_SCENE);
} catch {
return '验证码错误或已过期,请重新 `查用户`';
}
const user = await this.prisma.user.findFirst({
where: { phone: sess.phone },
select: {
id: true,
userNo: true,
phone: true,
nickname: true,
status: true,
phoneVerifiedAt: true,
createdAt: true,
_count: { select: { orders: true } },
},
});
if (!user) return '用户不存在';
await this.session.markVerified(bot.key, wecomUserId, sess.phone, user.id.toString());
const coupons = await this.prisma.benefitCoupon.aggregate({
where: { userId: user.id, status: 'ACTIVE' },
_sum: { balance: true },
});
return [
'**用户摘要**(短信验证已通过)',
`- 用户号:${user.userNo}`,
`- 昵称:${user.nickname || '—'}`,
`- 手机:${user.phone}`,
`- 手机已验:${user.phoneVerifiedAt ? '是' : '否'}`,
`- 状态:${user.status}`,
`- 订单数:${user._count.orders}`,
`- 权益余额:¥${Number(coupons._sum.balance ?? 0).toFixed(2)}`,
`- 注册:${user.createdAt.toISOString().slice(0, 19).replace('T', ' ')}`,
].join('\n');
}
private async lookupDelivery(q: string) {
if (!q) return '请提供订单号或运单号,例如:`快递 DK123`';
const byOrder = await this.prisma.orderDelivery.findMany({
where: { order: { orderNo: { contains: q } } },
take: 5,
orderBy: { updatedAt: 'desc' },
include: {
order: {
select: {
orderNo: true,
status: true,
receiverName: true,
receiverPhone: true,
deliveryType: true,
productName: true,
},
},
},
});
const byTrack =
byOrder.length > 0
? []
: await this.prisma.orderDelivery.findMany({
where: { trackingNo: { contains: q } },
take: 5,
orderBy: { updatedAt: 'desc' },
include: {
order: {
select: {
orderNo: true,
status: true,
receiverName: true,
receiverPhone: true,
deliveryType: true,
productName: true,
},
},
},
});
const rows = byOrder.length ? byOrder : byTrack;
if (!rows.length) return `未找到与「${q}」匹配的配送单`;
return rows
.map((d, i) => {
const o = d.order;
return [
`**配送 ${i + 1}**`,
`- 订单:${o.orderNo}${o.status}`,
`- 商品:${o.productName}`,
`- 类型:${o.deliveryType}`,
`- 承运:${d.provider}`,
`- 运单:${d.trackingNo || '—'}`,
`- 第三方单号:${d.providerOrderNo || '—'}`,
`- 收货:${o.receiverName} ${maskPhone(o.receiverPhone || '')}`,
].join('\n');
})
.join('\n\n');
}
private async createAfterSaleTicket(rest: string, wecomUserId: string) {
// 订单号 类型 备注
const parts = rest.split(/\s+/).filter(Boolean);
if (parts.length < 2) {
return '格式:`工单 <订单号> <类型> [备注]`\n类型:仅退款 / 破损补发 / 破损退货 / 退货退款';
}
const orderNo = parts[0];
const typeRaw = parts[1];
const remark = parts.slice(2).join(' ') || `企微客服创建 by ${wecomUserId}`;
const ticketType = TICKET_TYPE_ALIASES[typeRaw] || TICKET_TYPE_ALIASES[typeRaw.toLowerCase()];
if (!ticketType) {
return `未知类型「${typeRaw}」。可用:仅退款 / 破损补发 / 破损退货 / 退货退款`;
}
const order = await this.prisma.order.findFirst({ where: { orderNo } });
if (!order) return `订单不存在:${orderNo}`;
if (order.status === 'PENDING_PAY' || order.status === 'CANCELLED') {
return '当前订单状态不可创建售后工单';
}
const pending = await this.prisma.commonTicket.findFirst({
where: {
ticketType: ticketType as never,
refType: 'ORDER',
refId: order.id,
status: { in: ['PENDING', 'OPEN'] },
},
});
if (pending) return `该类型工单已在处理中:${pending.ticketNo}`;
const ticket = await this.ticketService.create({
ticketType,
refType: 'ORDER',
refId: order.id.toString(),
remark: `${remark} [wecom:${wecomUserId}]`,
});
return [
'**售后工单已创建**',
`- 工单号:${ticket.ticketNo}`,
`- 类型:${TICKET_TYPE_LABELS[ticketType]}`,
`- 订单:${orderNo}`,
`- 状态:${ticket.status}`,
].join('\n');
}
private async createSupportTicket(rest: string, wecomUserId: string) {
const m = rest.match(/^(\S+)\s+(.+)$/);
if (!m) return '格式:`提单 <BUG|建议|其他> <标题> [| 详情]`';
const typeRaw = m[1];
const restTitle = m[2];
const ticketType =
SUPPORT_TYPE_ALIASES[typeRaw] || SUPPORT_TYPE_ALIASES[typeRaw.toLowerCase()];
if (!ticketType) return '类型请使用:BUG / 建议 / 其他';
const [titlePart, ...contentParts] = restTitle.split('|');
const title = titlePart.trim();
const content = contentParts.join('|').trim();
if (!title) return '请填写标题';
const creator = await this.resolveCreator(wecomUserId);
const ticket = await this.supportTicketService.create(
{
ticketType,
title,
content: content || undefined,
remark: `企微技术支持机器人`,
},
creator,
);
return [
'**技术支持工单已创建**',
`- 工单号:${ticket.ticketNo}`,
`- 类型:${SUPPORT_TICKET_TYPE_LABELS[ticketType]}`,
`- 状态:${SUPPORT_TICKET_STATUS_LABELS[ticket.status as keyof typeof SUPPORT_TICKET_STATUS_LABELS] || ticket.status}`,
`- 标题:${title}`,
'等待最高管理员评审。',
].join('\n');
}
private async supportProgress(ticketNo: string) {
if (ticketNo) {
const ticket = await this.prisma.commonSupportTicket.findFirst({
where: { ticketNo: { contains: ticketNo } },
});
if (!ticket) return `未找到工单:${ticketNo}`;
return [
`**${ticket.ticketNo}**`,
`- 类型:${SUPPORT_TICKET_TYPE_LABELS[ticket.ticketType as SupportTicketTypeDto] || ticket.ticketType}`,
`- 状态:${SUPPORT_TICKET_STATUS_LABELS[ticket.status as keyof typeof SUPPORT_TICKET_STATUS_LABELS] || ticket.status}`,
`- 标题:${ticket.title}`,
`- 创建人:${ticket.creatorName}`,
`- 评审人:${ticket.reviewerName || '—'}`,
ticket.rejectReason ? `- 驳回原因:${ticket.rejectReason}` : '',
`- 更新:${ticket.updatedAt.toISOString().slice(0, 19).replace('T', ' ')}`,
]
.filter(Boolean)
.join('\n');
}
const items = await this.prisma.commonSupportTicket.findMany({
orderBy: { updatedAt: 'desc' },
take: 8,
});
if (!items.length) return '暂无技术支持工单';
const byStatus = await this.prisma.commonSupportTicket.groupBy({
by: ['status'],
_count: { _all: true },
});
const summary = byStatus
.map(
(s) =>
`${SUPPORT_TICKET_STATUS_LABELS[s.status as keyof typeof SUPPORT_TICKET_STATUS_LABELS] || s.status}:${s._count._all}`,
)
.join(' · ');
const list = items
.map(
(t) =>
`- ${t.ticketNo} [${SUPPORT_TICKET_STATUS_LABELS[t.status as keyof typeof SUPPORT_TICKET_STATUS_LABELS] || t.status}] ${t.title}`,
)
.join('\n');
return [`**开发进度概览**`, summary, '', '**最近工单**', list, '', '详情:`进度 <工单号>`'].join(
'\n',
);
}
private queryHandbook(q: string) {
if (!q) {
const catalog = searchHandbook('', 20)
.map((e) => `- ${e.title}(关键词:${e.keywords.slice(0, 4).join('、')}`)
.join('\n');
return `**手册目录**\n${catalog}\n\n查询:\`手册 <关键词>\``;
}
const hits = searchHandbook(q, 3);
if (!hits.length) return `未找到与「${q}」相关的手册内容。可试:开城、核销、订单、财务、工单`;
return formatHandbook(hits);
}
private async resolveCreator(wecomUserId: string) {
const admin = await this.prisma.hqAccount.findFirst({
where: { status: 'ACTIVE' },
orderBy: [{ id: 'asc' }],
select: { id: true, name: true },
});
if (!admin) {
this.logger.warn('no hq account for wecom support ticket creator');
throw new Error('系统未配置 HQ 账号,无法创建技术支持工单');
}
return { id: admin.id, name: `${admin.name || 'HQ'}(企微:${wecomUserId})` };
}
}
function maskPhone(phone: string): string {
if (!phone || phone.length < 7) return phone || '—';
return `${phone.slice(0, 3)}****${phone.slice(-4)}`;
}
function formatHandbook(entries: ReturnType<typeof searchHandbook>): string {
return entries.map((e) => `**${e.title}**\n${e.body}`).join('\n\n---\n\n');
}
@@ -0,0 +1,44 @@
import { Injectable } from '@nestjs/common';
import { RedisService } from '../../common/redis/redis.service';
const TTL_SECONDS = 30 * 60;
export type WecomUserVerifySession = {
phone: string;
/** 验证通过后可查看 */
verified: boolean;
pendingUserId?: string;
};
@Injectable()
export class WecomBotSessionService {
constructor(private readonly redis: RedisService) {}
private key(botKey: string, wecomUserId: string) {
return `dukang:wecom:session:${botKey}:${wecomUserId}`;
}
async get(botKey: string, wecomUserId: string): Promise<WecomUserVerifySession | null> {
return this.redis.getJson<WecomUserVerifySession>(this.key(botKey, wecomUserId));
}
async setPendingPhone(botKey: string, wecomUserId: string, phone: string) {
await this.redis.setJson(
this.key(botKey, wecomUserId),
{ phone, verified: false } satisfies WecomUserVerifySession,
TTL_SECONDS,
);
}
async markVerified(botKey: string, wecomUserId: string, phone: string, userId: string) {
await this.redis.setJson(
this.key(botKey, wecomUserId),
{ phone, verified: true, pendingUserId: userId } satisfies WecomUserVerifySession,
TTL_SECONDS,
);
}
async clear(botKey: string, wecomUserId: string) {
await this.redis.del(this.key(botKey, wecomUserId));
}
}
@@ -0,0 +1,21 @@
import type { WecomBotPermission, WecomBotRole } from '@dukang/shared-types';
export type WecomBotRuntimeConfig = {
id: string;
key: string;
role: WecomBotRole;
name: string;
enabled: boolean;
botId: string;
secret: string;
welcome: string;
avatarUrl: string | null;
permissions: WecomBotPermission[];
};
export function wecomBotHasPermission(
bot: WecomBotRuntimeConfig,
permission: WecomBotPermission,
): boolean {
return bot.permissions.includes(permission);
}
@@ -0,0 +1,125 @@
/** 团队助手手册条目(知识库摘要,供关键词检索) */
export type HandbookEntry = {
id: string;
title: string;
keywords: string[];
body: string;
};
export const WECOM_HANDBOOK_ENTRIES: HandbookEntry[] = [
{
id: 'overview',
title: '系统整体概述',
keywords: ['概述', '四端', '整体', '是什么', '介绍'],
body: [
'杜康好客:购酒 → 1:1 好客权益 → 门店核销。',
'四端:用户小程序 / 门店 H5 / 合伙人 H5 / HQ 后台。',
'核心:门店结算=核销额×60%;权益永久;核销码 3 分钟。',
].join('\n'),
},
{
id: 'order',
title: '订单与履约',
keywords: ['订单', '同城', '跨城', '提货', '履约', '小飞侠'],
body: [
'状态:待付款 → 已付款 → 已完成(30 分钟未付取消)。',
'同城≥2瓶:仓配/小飞侠;跨城≥1箱:总部物流到付。',
'现场提货:支付后直接已完成并发权益。',
'HQ「订单」可查看详情、填运单;「配送单」维护运单号。',
].join('\n'),
},
{
id: 'redeem',
title: '好客权益与核销',
keywords: ['权益', '核销', '出码', '扫码', '余额'],
body: [
'支付成功发放实付 1:1 权益,永久有效。',
'用户出码 3 分钟;门店可扫码或手机号+验证码核销。',
'直接核销:0 < 金额 ≤ 全部 ACTIVE 余额。',
'门店账本按核销额×60% 入账,T+1 出账。',
].join('\n'),
},
{
id: 'city',
title: '开城流程',
keywords: ['开城', '城市', '合伙人', '仓库', '仓配'],
body: [
'HQ「开城」:①新增城市 ②配置合伙人(全城/区域+佣金)③仓库 ④仓配承运商。',
'已开城走同城规则;未开城走跨城到付。',
'订单佣金按收货区县解析区域/全城合伙人;跨城归总部。',
].join('\n'),
},
{
id: 'store',
title: '开店/拓店流程',
keywords: ['开店', '拓店', '入驻', '审核', '试核销'],
body: [
'合伙人三步录入 → 负责人复核 → HQ 审核 → 试核销 100 元 → 正式入驻。',
'营业中门店才对 C 端可见。',
'AUTO_APPROVE_STORE 开启时可自动审核(试点)。',
].join('\n'),
},
{
id: 'finance',
title: '财务结算',
keywords: ['财务', '账单', '提现', '打款', '佣金'],
body: [
'门店账单:核销×60%,T+1 出账;HQ 确认打款。',
'合伙人月账独立确认打款。',
'未出账提现受白名单/单日上限等 FIN 护栏。',
].join('\n'),
},
{
id: 'ticket',
title: '工单与发票',
keywords: ['工单', '售后', '退款', '补发', '发票', '技术支持'],
body: [
'售后四类型:仅退款 / 破损补发 / 破损退货 / 退货退款 → HQ 工单中心。',
'技术支持:BUG/建议/其他,待评审→开发→测试→通过。',
'发票:个人/企业 × 普票/专票;2 工作日 SLA。',
].join('\n'),
},
{
id: 'hq-roles',
title: 'HQ 角色分工',
keywords: ['运营', '财务', '客服', '权限', '角色', 'hq'],
body: [
'运营:商品/开城/门店/订单/配送/权益。',
'财务:门店/合伙人/酒厂账单与打款、发票、酒厂账户。',
'客服:用户/订单、售后工单、发票协助。',
'超管:权限分配、技术支持评审、系统设置。',
].join('\n'),
},
{
id: 'settings',
title: '系统设置',
keywords: ['系统设置', 'mock', '短信', '微信', 'oss', '企微'],
body: [
'HQ「系统设置」:功能开关、短信、微信、企微机器人、OSS、应用链接、部署、酒厂账户。',
'企微可配置客服/技术支持/团队助手三组 BotID+Secret+权限。',
'改完可「同步到 env」;密钥类变更后注意重启标识。',
].join('\n'),
},
];
export function searchHandbook(query: string, limit = 3): HandbookEntry[] {
const q = query.trim().toLowerCase();
if (!q) return WECOM_HANDBOOK_ENTRIES.slice(0, limit);
const scored = WECOM_HANDBOOK_ENTRIES.map((e) => {
let score = 0;
const title = e.title.toLowerCase();
if (title.includes(q)) score += 10;
for (const kw of e.keywords) {
const k = kw.toLowerCase();
if (q.includes(k) || k.includes(q)) score += 5;
}
if (e.body.toLowerCase().includes(q)) score += 1;
return { e, score };
})
.filter((x) => x.score > 0)
.sort((a, b) => b.score - a.score);
if (!scored.length) return [];
return scored.slice(0, limit).map((x) => x.e);
}
@@ -0,0 +1,14 @@
import { Module, forwardRef } from '@nestjs/common';
import { CommonModule } from '../../modules/common/common.module';
import { IntegrationsModule } from '../integrations.module';
import { WecomAibotService } from './wecom-aibot.service';
import { WecomBotActionsService } from './wecom-bot-actions.service';
import { WecomBotSessionService } from './wecom-bot-session.service';
/** 企微多机器人:依赖 Common(工单)+ Integrations(短信),不反向被 Integrations 引用 */
@Module({
imports: [forwardRef(() => CommonModule), forwardRef(() => IntegrationsModule)],
providers: [WecomBotSessionService, WecomBotActionsService, WecomAibotService],
exports: [WecomAibotService],
})
export class WecomModule {}