@@ -1,18 +1,31 @@
|
||||
import { BadRequestException, Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
||||
import {
|
||||
WECOM_BIZ_TODO_PUSH_NAME,
|
||||
WECOM_DEAL_BROADCAST_PUSH_NAME,
|
||||
WECOM_PUSH_CONDITIONS,
|
||||
WECOM_PUSH_DEFAULT_ALERT_CONDITIONS,
|
||||
WECOM_PUSH_DEFAULT_BIZ_TODO_CONDITIONS,
|
||||
WECOM_PUSH_DEFAULT_DEAL_BROADCAST_CONDITIONS,
|
||||
WECOM_PUSH_DEFAULT_DEV_DISPATCH_CONDITIONS,
|
||||
WECOM_PUSH_DEFAULT_STORE_AUDIT_CONDITIONS,
|
||||
WECOM_STORE_AUDIT_PLACEHOLDER_WEBHOOK,
|
||||
WECOM_STORE_AUDIT_PUSH_NAME,
|
||||
WECOM_TEMPLATE_EVENT_KEYS,
|
||||
maskWecomWebhookUrl,
|
||||
parseWecomPushConditions,
|
||||
type WecomMessagePushDto,
|
||||
type WecomPushCondition,
|
||||
type WecomPushTemplateDto,
|
||||
type WecomTemplateEventKey,
|
||||
} from '@dukang/shared-types';
|
||||
import { PrismaService } from '../../common/prisma/prisma.module';
|
||||
import { applyWecomAtMentionInContent } from '../../modules/dev-plan/dev-plan-wecom-mention.util';
|
||||
import {
|
||||
WECOM_PUSH_TEMPLATE_DEFAULTS,
|
||||
buildHqHandleUrl,
|
||||
getDefaultTemplate,
|
||||
renderWecomTemplate,
|
||||
} from './wecom-push-template.defaults';
|
||||
|
||||
type PushRow = {
|
||||
id: bigint;
|
||||
@@ -27,6 +40,16 @@ type PushRow = {
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
type TemplateRow = {
|
||||
id: bigint;
|
||||
eventKey: string;
|
||||
title: string;
|
||||
body: string;
|
||||
handleLabel: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class WecomMessagePushService implements OnModuleInit {
|
||||
private readonly logger = new Logger(WecomMessagePushService.name);
|
||||
@@ -43,7 +66,7 @@ export class WecomMessagePushService implements OnModuleInit {
|
||||
}
|
||||
}
|
||||
|
||||
/** 表空时从 .env / 旧 dev_plan_settings 迁移默认推送;并按名称 upsert「门店审核通知群」(v3.5.3) */
|
||||
/** 表空时从 .env / 旧设置迁移;并确保门店审核 / 业务待办 / 成交播报 / 模板缺行 */
|
||||
async ensureDefaults(): Promise<void> {
|
||||
const count = await this.prisma.wecomMessagePush.count();
|
||||
if (count === 0) {
|
||||
@@ -100,45 +123,81 @@ export class WecomMessagePushService implements OnModuleInit {
|
||||
}
|
||||
}
|
||||
|
||||
await this.ensureStoreAuditPush();
|
||||
await this.ensureNamedPush(
|
||||
WECOM_STORE_AUDIT_PUSH_NAME,
|
||||
WECOM_PUSH_DEFAULT_STORE_AUDIT_CONDITIONS,
|
||||
20,
|
||||
process.env.WECOM_STORE_AUDIT_WEBHOOK_URL,
|
||||
);
|
||||
await this.ensureNamedPush(
|
||||
WECOM_BIZ_TODO_PUSH_NAME,
|
||||
WECOM_PUSH_DEFAULT_BIZ_TODO_CONDITIONS,
|
||||
25,
|
||||
process.env.WECOM_BIZ_TODO_WEBHOOK_URL,
|
||||
);
|
||||
await this.ensureNamedPush(
|
||||
WECOM_DEAL_BROADCAST_PUSH_NAME,
|
||||
WECOM_PUSH_DEFAULT_DEAL_BROADCAST_CONDITIONS,
|
||||
30,
|
||||
process.env.WECOM_DEAL_BROADCAST_WEBHOOK_URL,
|
||||
);
|
||||
await this.ensureTemplates();
|
||||
}
|
||||
|
||||
/** 按名称 upsert「门店审核通知群」:已有行保留 webhook;无行则 env 或占位 URL */
|
||||
private async ensureStoreAuditPush(): Promise<void> {
|
||||
const existing = await this.prisma.wecomMessagePush.findFirst({
|
||||
where: { name: WECOM_STORE_AUDIT_PUSH_NAME },
|
||||
});
|
||||
private async ensureNamedPush(
|
||||
name: string,
|
||||
conditions: WecomPushCondition[],
|
||||
sortOrder: number,
|
||||
envUrl?: string,
|
||||
): Promise<void> {
|
||||
const existing = await this.prisma.wecomMessagePush.findFirst({ where: { name } });
|
||||
if (existing) return;
|
||||
|
||||
const conditionsJson = JSON.stringify(WECOM_PUSH_DEFAULT_STORE_AUDIT_CONDITIONS);
|
||||
const envUrl = (process.env.WECOM_STORE_AUDIT_WEBHOOK_URL || '').trim();
|
||||
|
||||
if (envUrl) {
|
||||
const conditionsJson = JSON.stringify(conditions);
|
||||
const url = (envUrl || '').trim();
|
||||
if (url) {
|
||||
await this.prisma.wecomMessagePush.create({
|
||||
data: {
|
||||
name: WECOM_STORE_AUDIT_PUSH_NAME,
|
||||
webhookUrl: envUrl,
|
||||
name,
|
||||
webhookUrl: url,
|
||||
enabled: true,
|
||||
pushConditions: conditionsJson,
|
||||
sortOrder: 20,
|
||||
sortOrder,
|
||||
},
|
||||
});
|
||||
this.logger.log(`seeded wecom message push: ${WECOM_STORE_AUDIT_PUSH_NAME} (from env)`);
|
||||
this.logger.log(`seeded wecom message push: ${name} (from env)`);
|
||||
return;
|
||||
}
|
||||
|
||||
await this.prisma.wecomMessagePush.create({
|
||||
data: {
|
||||
name: WECOM_STORE_AUDIT_PUSH_NAME,
|
||||
name,
|
||||
webhookUrl: WECOM_STORE_AUDIT_PLACEHOLDER_WEBHOOK,
|
||||
enabled: false,
|
||||
pushConditions: conditionsJson,
|
||||
sortOrder: 20,
|
||||
sortOrder,
|
||||
},
|
||||
});
|
||||
this.logger.log(
|
||||
`seeded wecom message push: ${WECOM_STORE_AUDIT_PUSH_NAME} (placeholder, disabled)`,
|
||||
);
|
||||
this.logger.log(`seeded wecom message push: ${name} (placeholder, disabled)`);
|
||||
}
|
||||
|
||||
/** 仅插入缺失 eventKey,不覆盖已有文案 */
|
||||
async ensureTemplates(): Promise<void> {
|
||||
for (const def of WECOM_PUSH_TEMPLATE_DEFAULTS) {
|
||||
const existing = await this.prisma.wecomPushTemplate.findUnique({
|
||||
where: { eventKey: def.eventKey },
|
||||
});
|
||||
if (existing) continue;
|
||||
await this.prisma.wecomPushTemplate.create({
|
||||
data: {
|
||||
eventKey: def.eventKey,
|
||||
title: def.title,
|
||||
body: def.body,
|
||||
handleLabel: def.handleLabel,
|
||||
},
|
||||
});
|
||||
this.logger.log(`seeded wecom push template: ${def.eventKey}`);
|
||||
}
|
||||
}
|
||||
|
||||
async listMatchingPushes(eventKey: WecomPushCondition): Promise<PushRow[]> {
|
||||
@@ -154,6 +213,58 @@ export class WecomMessagePushService implements OnModuleInit {
|
||||
return pushes.length > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读 HQ 模板 → 插值 → 补快链 → 按条件路由推送。
|
||||
* 失败只打日志,不抛给业务。
|
||||
*/
|
||||
async dispatchEvent(
|
||||
eventKey: WecomTemplateEventKey,
|
||||
vars: Record<string, string | number | null | undefined>,
|
||||
options?: { applyMention?: boolean; handlePath?: string },
|
||||
): Promise<number> {
|
||||
try {
|
||||
const content = await this.renderEventContent(eventKey, vars, options?.handlePath);
|
||||
return await this.dispatchMarkdown(eventKey, content, {
|
||||
applyMention: options?.applyMention ?? false,
|
||||
});
|
||||
} catch (e) {
|
||||
this.logger.warn(
|
||||
`dispatchEvent(${eventKey}) failed: ${e instanceof Error ? e.message : String(e)}`,
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
async renderEventContent(
|
||||
eventKey: WecomTemplateEventKey,
|
||||
vars: Record<string, string | number | null | undefined>,
|
||||
handlePath?: string,
|
||||
): Promise<string> {
|
||||
const row = await this.prisma.wecomPushTemplate.findUnique({ where: { eventKey } });
|
||||
const def = getDefaultTemplate(eventKey);
|
||||
const body = row?.body || def?.body || `**${eventKey}**`;
|
||||
const handleLabel = row?.handleLabel || def?.handleLabel || '去处理';
|
||||
|
||||
const merged: Record<string, string | number | null | undefined> = {
|
||||
...vars,
|
||||
handleLabel: vars.handleLabel ?? handleLabel,
|
||||
time:
|
||||
vars.time ??
|
||||
new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' }),
|
||||
};
|
||||
|
||||
if (handlePath && !merged.handleUrl) {
|
||||
merged.handleUrl = buildHqHandleUrl(handlePath);
|
||||
}
|
||||
|
||||
let content = renderWecomTemplate(body, merged).trim();
|
||||
const url = String(merged.handleUrl || '').trim();
|
||||
if (url && !content.includes(url) && !/\{\{handleUrl\}\}/.test(body)) {
|
||||
content = `${content}\n[${handleLabel}](${url})`;
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
/** 向所有匹配 eventKey 的启用推送发送 markdown;返回成功发送数 */
|
||||
async dispatchMarkdown(
|
||||
eventKey: WecomPushCondition,
|
||||
@@ -239,6 +350,188 @@ export class WecomMessagePushService implements OnModuleInit {
|
||||
: { ok: false, message: 'Webhook 调用失败,请检查 URL 或 API 日志' };
|
||||
}
|
||||
|
||||
// ── templates CRUD ──
|
||||
|
||||
async listTemplates(): Promise<WecomPushTemplateDto[]> {
|
||||
await this.ensureTemplates();
|
||||
const rows = await this.prisma.wecomPushTemplate.findMany({
|
||||
orderBy: { eventKey: 'asc' },
|
||||
});
|
||||
return rows.map((r) => this.templateToDto(r));
|
||||
}
|
||||
|
||||
async getTemplate(eventKey: string): Promise<WecomPushTemplateDto> {
|
||||
this.assertTemplateKey(eventKey);
|
||||
await this.ensureTemplates();
|
||||
const row = await this.prisma.wecomPushTemplate.findUnique({ where: { eventKey } });
|
||||
if (!row) throw new BadRequestException('模板不存在');
|
||||
return this.templateToDto(row);
|
||||
}
|
||||
|
||||
async updateTemplate(
|
||||
eventKey: string,
|
||||
data: { title?: string; body?: string; handleLabel?: string },
|
||||
): Promise<WecomPushTemplateDto> {
|
||||
this.assertTemplateKey(eventKey);
|
||||
await this.ensureTemplates();
|
||||
const existing = await this.prisma.wecomPushTemplate.findUnique({ where: { eventKey } });
|
||||
if (!existing) throw new BadRequestException('模板不存在');
|
||||
|
||||
const title = data.title != null ? String(data.title).trim() : undefined;
|
||||
const body = data.body != null ? String(data.body).trim() : undefined;
|
||||
const handleLabel =
|
||||
data.handleLabel != null ? String(data.handleLabel).trim() || '去处理' : undefined;
|
||||
if (title !== undefined && !title) throw new BadRequestException('标题不能为空');
|
||||
if (body !== undefined && !body) throw new BadRequestException('正文不能为空');
|
||||
|
||||
const row = await this.prisma.wecomPushTemplate.update({
|
||||
where: { eventKey },
|
||||
data: {
|
||||
...(title !== undefined ? { title } : {}),
|
||||
...(body !== undefined ? { body } : {}),
|
||||
...(handleLabel !== undefined ? { handleLabel } : {}),
|
||||
},
|
||||
});
|
||||
return this.templateToDto(row);
|
||||
}
|
||||
|
||||
async resetTemplate(eventKey: string): Promise<WecomPushTemplateDto> {
|
||||
this.assertTemplateKey(eventKey);
|
||||
const def = getDefaultTemplate(eventKey);
|
||||
if (!def) throw new BadRequestException('无默认模板');
|
||||
await this.ensureTemplates();
|
||||
const row = await this.prisma.wecomPushTemplate.upsert({
|
||||
where: { eventKey },
|
||||
create: {
|
||||
eventKey: def.eventKey,
|
||||
title: def.title,
|
||||
body: def.body,
|
||||
handleLabel: def.handleLabel,
|
||||
},
|
||||
update: {
|
||||
title: def.title,
|
||||
body: def.body,
|
||||
handleLabel: def.handleLabel,
|
||||
},
|
||||
});
|
||||
return this.templateToDto(row);
|
||||
}
|
||||
|
||||
/** 用示例变量渲染并推到勾选了该事件的启用群 */
|
||||
async testTemplate(eventKey: string): Promise<{ ok: boolean; message: string; preview: string }> {
|
||||
this.assertTemplateKey(eventKey);
|
||||
const sample = this.sampleVars(eventKey);
|
||||
const preview = await this.renderEventContent(
|
||||
eventKey,
|
||||
sample.vars,
|
||||
sample.handlePath,
|
||||
);
|
||||
const sent = await this.dispatchMarkdown(eventKey, preview, { applyMention: false });
|
||||
if (sent === 0) {
|
||||
return {
|
||||
ok: false,
|
||||
message: '没有已启用且勾选该事件的消息推送,请先配置 webhook',
|
||||
preview,
|
||||
};
|
||||
}
|
||||
return { ok: true, message: `已发送至 ${sent} 个推送`, preview };
|
||||
}
|
||||
|
||||
private sampleVars(eventKey: WecomTemplateEventKey): {
|
||||
vars: Record<string, string>;
|
||||
handlePath: string;
|
||||
} {
|
||||
const samples: Record<WecomTemplateEventKey, { vars: Record<string, string>; handlePath: string }> = {
|
||||
'order.paid': {
|
||||
vars: {
|
||||
orderNo: 'DK202608200001',
|
||||
payAmount: '199.00',
|
||||
cityName: '郑州',
|
||||
skuSummary: '杜康原浆 ×2',
|
||||
phoneMasked: '138****8000',
|
||||
},
|
||||
handlePath: '/orders?orderNo=DK202608200001',
|
||||
},
|
||||
'redeem.success': {
|
||||
vars: {
|
||||
redeemNo: 'RD202608200001',
|
||||
amount: '88.00',
|
||||
storeName: '示例门店',
|
||||
channel: '扫码',
|
||||
},
|
||||
handlePath: '/redeem-records?redeemNo=RD202608200001',
|
||||
},
|
||||
'store.audit_pending': {
|
||||
vars: {
|
||||
storeName: '示例门店',
|
||||
cityName: '郑州',
|
||||
partnerLabel: '示例合伙人',
|
||||
action: '新建',
|
||||
storeId: '1',
|
||||
},
|
||||
handlePath: '/stores?auditStatus=PENDING&storeId=1',
|
||||
},
|
||||
'store.package_audit_pending': {
|
||||
vars: {
|
||||
storeName: '示例门店',
|
||||
cityName: '郑州',
|
||||
submitter: '合伙人',
|
||||
packageCount: '3',
|
||||
requestId: '1',
|
||||
},
|
||||
handlePath: '/store-package-audits?requestId=1',
|
||||
},
|
||||
'store.info_change_pending': {
|
||||
vars: {
|
||||
storeName: '示例门店',
|
||||
cityName: '郑州',
|
||||
submitter: '门店',
|
||||
changedFields: '门店名称、详细地址',
|
||||
requestId: '1',
|
||||
},
|
||||
handlePath: '/store-package-audits?tab=info&infoRequestId=1',
|
||||
},
|
||||
'store.withdraw_pending': {
|
||||
vars: {
|
||||
storeName: '示例门店',
|
||||
withdrawNo: 'SW202608200001',
|
||||
amount: '500.00',
|
||||
payoutCount: '5',
|
||||
storeId: '1',
|
||||
},
|
||||
handlePath: '/finance/store-bills?kind=WITHDRAW&status=PENDING_REVIEW&storeId=1',
|
||||
},
|
||||
'invoice.pending': {
|
||||
vars: {
|
||||
invoiceNo: 'IV202608200001',
|
||||
orderNo: 'DK202608200001',
|
||||
payAmount: '199.00',
|
||||
titleName: '示例公司',
|
||||
phoneMasked: '138****8000',
|
||||
},
|
||||
handlePath: '/invoices?status=PENDING&invoiceNo=IV202608200001',
|
||||
},
|
||||
};
|
||||
return samples[eventKey];
|
||||
}
|
||||
|
||||
private assertTemplateKey(eventKey: string): asserts eventKey is WecomTemplateEventKey {
|
||||
if (!(WECOM_TEMPLATE_EVENT_KEYS as readonly string[]).includes(eventKey)) {
|
||||
throw new BadRequestException(`无效模板事件:${eventKey}`);
|
||||
}
|
||||
}
|
||||
|
||||
templateToDto(row: TemplateRow): WecomPushTemplateDto {
|
||||
return {
|
||||
id: row.id.toString(),
|
||||
eventKey: row.eventKey as WecomTemplateEventKey,
|
||||
title: row.title,
|
||||
body: row.body,
|
||||
handleLabel: row.handleLabel,
|
||||
updatedAt: row.updatedAt.toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
toDto(row: PushRow): WecomMessagePushDto {
|
||||
return {
|
||||
id: row.id.toString(),
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
import type { WecomTemplateEventKey } from '@dukang/shared-types';
|
||||
|
||||
export type WecomTemplateDefault = {
|
||||
eventKey: WecomTemplateEventKey;
|
||||
title: string;
|
||||
body: string;
|
||||
handleLabel: string;
|
||||
};
|
||||
|
||||
/** 代码内默认文案;ensureDefaults 仅在库中无行时写入,不覆盖 HQ 已改 */
|
||||
export const WECOM_PUSH_TEMPLATE_DEFAULTS: WecomTemplateDefault[] = [
|
||||
{
|
||||
eventKey: 'order.paid',
|
||||
title: '订单支付成功',
|
||||
body: [
|
||||
'**订单支付成功**',
|
||||
'订单号:{{orderNo}}',
|
||||
'实付:¥{{payAmount}}',
|
||||
'城市:{{cityName}}',
|
||||
'商品:{{skuSummary}}',
|
||||
'用户:{{phoneMasked}}',
|
||||
'时间:{{time}}',
|
||||
'[{{handleLabel}}]({{handleUrl}})',
|
||||
].join('\n'),
|
||||
handleLabel: '去处理',
|
||||
},
|
||||
{
|
||||
eventKey: 'redeem.success',
|
||||
title: '门店核销成功',
|
||||
body: [
|
||||
'**门店核销成功**',
|
||||
'核销单号:{{redeemNo}}',
|
||||
'金额:¥{{amount}}',
|
||||
'门店:{{storeName}}',
|
||||
'渠道:{{channel}}',
|
||||
'时间:{{time}}',
|
||||
'[{{handleLabel}}]({{handleUrl}})',
|
||||
].join('\n'),
|
||||
handleLabel: '去处理',
|
||||
},
|
||||
{
|
||||
eventKey: 'store.audit_pending',
|
||||
title: '门店审核待处理',
|
||||
body: [
|
||||
'**门店审核待处理 · {{action}}**',
|
||||
'门店:{{storeName}}',
|
||||
'城市:{{cityName}}',
|
||||
'合伙人:{{partnerLabel}}',
|
||||
'时间:{{time}}',
|
||||
'[{{handleLabel}}]({{handleUrl}})',
|
||||
].join('\n'),
|
||||
handleLabel: '去处理',
|
||||
},
|
||||
{
|
||||
eventKey: 'store.package_audit_pending',
|
||||
title: '套餐变更待审核',
|
||||
body: [
|
||||
'**套餐变更待审核**',
|
||||
'门店:{{storeName}}',
|
||||
'城市:{{cityName}}',
|
||||
'提交端:{{submitter}}',
|
||||
'套餐条数:{{packageCount}}',
|
||||
'时间:{{time}}',
|
||||
'[{{handleLabel}}]({{handleUrl}})',
|
||||
].join('\n'),
|
||||
handleLabel: '去处理',
|
||||
},
|
||||
{
|
||||
eventKey: 'store.info_change_pending',
|
||||
title: '门店信息变更待审',
|
||||
body: [
|
||||
'**门店信息变更待审**',
|
||||
'门店:{{storeName}}',
|
||||
'城市:{{cityName}}',
|
||||
'提交端:{{submitter}}',
|
||||
'变更字段:{{changedFields}}',
|
||||
'时间:{{time}}',
|
||||
'[{{handleLabel}}]({{handleUrl}})',
|
||||
].join('\n'),
|
||||
handleLabel: '去处理',
|
||||
},
|
||||
{
|
||||
eventKey: 'store.withdraw_pending',
|
||||
title: '门店提现待审',
|
||||
body: [
|
||||
'**门店提现待审**',
|
||||
'门店:{{storeName}}',
|
||||
'提现单号:{{withdrawNo}}',
|
||||
'金额:¥{{amount}}',
|
||||
'明细笔数:{{payoutCount}}',
|
||||
'时间:{{time}}',
|
||||
'[{{handleLabel}}]({{handleUrl}})',
|
||||
].join('\n'),
|
||||
handleLabel: '去处理',
|
||||
},
|
||||
{
|
||||
eventKey: 'invoice.pending',
|
||||
title: '发票申请待开票',
|
||||
body: [
|
||||
'**发票申请待开票**',
|
||||
'申请单号:{{invoiceNo}}',
|
||||
'订单号:{{orderNo}}',
|
||||
'金额:¥{{payAmount}}',
|
||||
'抬头:{{titleName}}',
|
||||
'用户:{{phoneMasked}}',
|
||||
'时间:{{time}}',
|
||||
'[{{handleLabel}}]({{handleUrl}})',
|
||||
].join('\n'),
|
||||
handleLabel: '去处理',
|
||||
},
|
||||
];
|
||||
|
||||
export function getDefaultTemplate(eventKey: string): WecomTemplateDefault | undefined {
|
||||
return WECOM_PUSH_TEMPLATE_DEFAULTS.find((t) => t.eventKey === eventKey);
|
||||
}
|
||||
|
||||
/** 将 body 中的 {{key}} 替换为 vars;缺失置空 */
|
||||
export function renderWecomTemplate(
|
||||
body: string,
|
||||
vars: Record<string, string | number | null | undefined>,
|
||||
): string {
|
||||
return body.replace(/\{\{(\w+)\}\}/g, (_m, key: string) => {
|
||||
const v = vars[key];
|
||||
if (v == null) return '';
|
||||
return String(v);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* HQ 后台公网根地址(无尾斜杠)。
|
||||
* 优先 HQ_ADMIN_PUBLIC_URL;未配时按 WECOM_ALERT_ENV_LABEL / NODE_ENV 回退,
|
||||
* 避免相对路径被企微解析成 http://orders/... 这类无效链接。
|
||||
*/
|
||||
export function resolveHqAdminPublicBase(): string {
|
||||
const configured = (process.env.HQ_ADMIN_PUBLIC_URL || '').trim().replace(/\/$/, '');
|
||||
if (configured) return configured;
|
||||
|
||||
const label = (process.env.WECOM_ALERT_ENV_LABEL || process.env.NODE_ENV || '')
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
if (label === 'production' || label === 'prod') {
|
||||
return 'https://admin.dukanghaoke.com';
|
||||
}
|
||||
if (label === 'staging' || label === 'test' || label === 'testing') {
|
||||
return 'https://admin-test.dukanghaoke.com';
|
||||
}
|
||||
// local / development / 未识别:本机 admin-web 默认端口
|
||||
return 'http://localhost:5175';
|
||||
}
|
||||
|
||||
export function buildHqHandleUrl(pathWithQuery: string): string {
|
||||
const base = resolveHqAdminPublicBase();
|
||||
let path = (pathWithQuery || '').trim();
|
||||
if (!path) return base;
|
||||
if (!path.startsWith('/')) path = `/${path}`;
|
||||
return `${base}${path}`;
|
||||
}
|
||||
@@ -19,11 +19,13 @@ export class AdminInvoicesController {
|
||||
@Get()
|
||||
list(
|
||||
@Query('status') status?: string,
|
||||
@Query('invoiceNo') invoiceNo?: string,
|
||||
@Query('page') page = '1',
|
||||
@Query('pageSize') pageSize = '20',
|
||||
) {
|
||||
return this.tradeService.adminListInvoices({
|
||||
status,
|
||||
invoiceNo,
|
||||
page: Number(page),
|
||||
pageSize: Number(pageSize),
|
||||
});
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
Post,
|
||||
Put,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import type { UpdateWecomPushTemplateRequest } from '@dukang/shared-types';
|
||||
import { HqAuthGuard } from '../../common/guards/hq-auth.guard';
|
||||
import {
|
||||
HqPermissionGuard,
|
||||
RequireHqPermissions,
|
||||
} from '../../common/guards/hq-permission.guard';
|
||||
import { HqOperation } from '../../common/hq-operation/hq-operation.decorator';
|
||||
import { HqOperationAction } from '../../common/hq-operation/hq-operation.constants';
|
||||
import { WecomMessagePushService } from '../../integrations/wecom/wecom-message-push.service';
|
||||
|
||||
@Controller('admin/wecom-push-templates')
|
||||
@UseGuards(HqAuthGuard, HqPermissionGuard)
|
||||
@RequireHqPermissions('wecom_bots')
|
||||
export class AdminWecomPushTemplatesController {
|
||||
constructor(private readonly wecomPush: WecomMessagePushService) {}
|
||||
|
||||
@Get()
|
||||
list() {
|
||||
return this.wecomPush.listTemplates();
|
||||
}
|
||||
|
||||
@Get(':eventKey')
|
||||
detail(@Param('eventKey') eventKey: string) {
|
||||
return this.wecomPush.getTemplate(eventKey);
|
||||
}
|
||||
|
||||
@Put(':eventKey')
|
||||
@HqOperation({
|
||||
action: HqOperationAction.WECOM_MESSAGE_PUSH_UPDATE,
|
||||
refType: 'WECOM_PUSH_TEMPLATE',
|
||||
refIdField: 'eventKey',
|
||||
includeBody: true,
|
||||
})
|
||||
update(@Param('eventKey') eventKey: string, @Body() body: UpdateWecomPushTemplateRequest) {
|
||||
return this.wecomPush.updateTemplate(eventKey, body);
|
||||
}
|
||||
|
||||
@Post(':eventKey/reset')
|
||||
@HqOperation({
|
||||
action: HqOperationAction.WECOM_MESSAGE_PUSH_UPDATE,
|
||||
refType: 'WECOM_PUSH_TEMPLATE',
|
||||
refIdField: 'eventKey',
|
||||
})
|
||||
reset(@Param('eventKey') eventKey: string) {
|
||||
return this.wecomPush.resetTemplate(eventKey);
|
||||
}
|
||||
|
||||
@Post(':eventKey/test')
|
||||
@HqOperation({
|
||||
action: HqOperationAction.WECOM_MESSAGE_PUSH_TEST,
|
||||
refType: 'WECOM_PUSH_TEMPLATE',
|
||||
refIdField: 'eventKey',
|
||||
})
|
||||
async test(@Param('eventKey') eventKey: string) {
|
||||
const result = await this.wecomPush.testTemplate(eventKey);
|
||||
if (!result.ok) throw new BadRequestException(result.message);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -69,6 +69,7 @@ import { AdminWecomBotsController } from './admin-wecom-bots.controller';
|
||||
import { AdminWecomBotsService } from './admin-wecom-bots.service';
|
||||
import { AdminWecomMessagePushesController } from './admin-wecom-message-pushes.controller';
|
||||
import { AdminWecomMessagePushesService } from './admin-wecom-message-pushes.service';
|
||||
import { AdminWecomPushTemplatesController } from './admin-wecom-push-templates.controller';
|
||||
import { AdminWecomBotLogsController } from './admin-wecom-bot-logs.controller';
|
||||
import { AdminWecomBotLogsService } from './admin-wecom-bot-logs.service';
|
||||
import { AdminLlmConfigsController } from './admin-llm-configs.controller';
|
||||
@@ -124,6 +125,7 @@ import { AdminTestWhitelistController } from './admin-test-whitelist.controller'
|
||||
AdminSystemConfigController,
|
||||
AdminWecomBotsController,
|
||||
AdminWecomMessagePushesController,
|
||||
AdminWecomPushTemplatesController,
|
||||
AdminWecomBotLogsController,
|
||||
AdminLlmConfigsController,
|
||||
AdminKnowledgeBasesController,
|
||||
|
||||
@@ -30,6 +30,7 @@ import { SettlementService } from '../settlement/settlement.service';
|
||||
import { BenefitService } from '../benefit/benefit.service';
|
||||
import { AuthService } from '../iam/auth.service';
|
||||
import { PayRedeemAnomalyService } from '../../common/alert/pay-redeem-anomaly.service';
|
||||
import { WecomMessagePushService } from '../../integrations/wecom/wecom-message-push.service';
|
||||
|
||||
type TokenPayload = {
|
||||
userId: string;
|
||||
@@ -82,6 +83,7 @@ export class RedeemService {
|
||||
private readonly analyticsService: AnalyticsService,
|
||||
private readonly authService: AuthService,
|
||||
private readonly payRedeemAnomaly: PayRedeemAnomalyService,
|
||||
private readonly wecomPush: WecomMessagePushService,
|
||||
) {}
|
||||
|
||||
private maskPhoneForStore(phone: string) {
|
||||
@@ -276,6 +278,22 @@ export class RedeemService {
|
||||
extraJson: redeemExtra,
|
||||
});
|
||||
|
||||
if (!isTest) {
|
||||
const channelLabel = redeemChannel === 'PHONE' ? '手机号' : '扫码';
|
||||
void this.wecomPush.dispatchEvent(
|
||||
'redeem.success',
|
||||
{
|
||||
redeemNo: record.redeemNo,
|
||||
amount: amountNum.toFixed(2),
|
||||
storeName: account.store.name || String(account.storeId),
|
||||
channel: channelLabel,
|
||||
},
|
||||
{
|
||||
handlePath: `/redeem-records?redeemNo=${encodeURIComponent(record.redeemNo)}`,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
...record,
|
||||
amount: amountNum,
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
import { PrismaService } from '../../common/prisma/prisma.module';
|
||||
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
|
||||
import { AlertService } from '../../common/alert/alert.service';
|
||||
import { WecomMessagePushService } from '../../integrations/wecom/wecom-message-push.service';
|
||||
import { AnalyticsService } from '../analytics/analytics.service';
|
||||
import { PartnerCityService } from '../city-scope/partner-city.service';
|
||||
import { FulfillmentProviderService } from '../fulfillment/fulfillment-provider.service';
|
||||
@@ -86,6 +87,7 @@ export class SettlementService {
|
||||
private readonly partnerCityService: PartnerCityService,
|
||||
private readonly fulfillmentProviderService: FulfillmentProviderService,
|
||||
private readonly alert: AlertService,
|
||||
private readonly wecomPush: WecomMessagePushService,
|
||||
) {}
|
||||
|
||||
// ─── Store payout (line) ─────────────────────────────
|
||||
@@ -386,20 +388,19 @@ export class SettlementService {
|
||||
},
|
||||
});
|
||||
|
||||
this.alert.notify({
|
||||
level: 'P1',
|
||||
category: 'finance',
|
||||
title: '门店提现待审',
|
||||
detail: [
|
||||
`门店:${store.name}(${store.cityName || '-'} / ${store.phone || '-'})`,
|
||||
`单号:${created.withdrawNo}`,
|
||||
`金额:¥${Number(created.amount).toFixed(2)}`,
|
||||
`明细:${created.payoutCount} 笔未出账核销(已锁定,不进入次日 T+1 出账)`,
|
||||
'请尽快在 HQ「财务 → 门店账单(手动提现)」处理。',
|
||||
].join('\n'),
|
||||
dedupeKey: `store_withdraw_applied|${created.id.toString()}`,
|
||||
dedupeTtlSec: 3600,
|
||||
});
|
||||
void this.wecomPush.dispatchEvent(
|
||||
'store.withdraw_pending',
|
||||
{
|
||||
storeName: store.name,
|
||||
withdrawNo: created.withdrawNo,
|
||||
amount: Number(created.amount).toFixed(2),
|
||||
payoutCount: String(created.payoutCount),
|
||||
storeId: storeId.toString(),
|
||||
},
|
||||
{
|
||||
handlePath: `/finance/store-bills?kind=WITHDRAW&status=PENDING_REVIEW&storeId=${storeId.toString()}`,
|
||||
},
|
||||
);
|
||||
|
||||
return serializeBigInt(created);
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
} from '@dukang/domain';
|
||||
import {
|
||||
STORE_INFO_CHANGEABLE_FIELDS,
|
||||
formatStoreInfoChangeFieldLabels,
|
||||
type StoreInfoChangeFieldDiff,
|
||||
type StoreInfoChangeRequestDto,
|
||||
type StoreInfoChangeStatus,
|
||||
@@ -19,6 +20,7 @@ import { PrismaService } from '../../common/prisma/prisma.module';
|
||||
import { serializeBigInt } from '../../common/decorators/current-user.decorator';
|
||||
import { StoreService } from './store.service';
|
||||
import { PartnerCityService } from '../city-scope/partner-city.service';
|
||||
import { WecomMessagePushService } from '../../integrations/wecom/wecom-message-push.service';
|
||||
|
||||
type ChangeableField = (typeof STORE_INFO_CHANGEABLE_FIELDS)[number];
|
||||
|
||||
@@ -142,6 +144,7 @@ export class StoreInfoChangeService {
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly storeService: StoreService,
|
||||
private readonly partnerCityService: PartnerCityService,
|
||||
private readonly wecomPush: WecomMessagePushService,
|
||||
) {}
|
||||
|
||||
private async loadLiveMediaFields(storeId: bigint, coverResourceId: bigint | null) {
|
||||
@@ -329,6 +332,22 @@ export class StoreInfoChangeService {
|
||||
this.logger.log(
|
||||
`Store info change submitted storeId=${input.storeId} fields=${changedFields.join(',')}`,
|
||||
);
|
||||
|
||||
const submitterLabel = input.submitterType === 'PARTNER' ? '合伙人' : '门店';
|
||||
void this.wecomPush.dispatchEvent(
|
||||
'store.info_change_pending',
|
||||
{
|
||||
storeName: String(store.name ?? input.storeId),
|
||||
cityName: String(store.cityName ?? '—'),
|
||||
submitter: submitterLabel,
|
||||
changedFields: formatStoreInfoChangeFieldLabels(changedFields),
|
||||
requestId: created.id.toString(),
|
||||
},
|
||||
{
|
||||
handlePath: `/store-package-audits?tab=info&infoRequestId=${created.id.toString()}`,
|
||||
},
|
||||
);
|
||||
|
||||
return serializeBigInt(this.toDto(created as unknown as Record<string, unknown>));
|
||||
}
|
||||
|
||||
|
||||
@@ -191,28 +191,18 @@ export class StorePackageService {
|
||||
where: { id: storeId },
|
||||
select: { name: true, cityName: true },
|
||||
});
|
||||
const now = new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
|
||||
const submitterLabel = submitterType === 'PARTNER' ? '合伙人' : '门店';
|
||||
void this.wecomPush
|
||||
.dispatchMarkdown(
|
||||
'store.package_audit_pending',
|
||||
[
|
||||
'**套餐变更待审核**',
|
||||
`门店:${store?.name ?? storeId}`,
|
||||
store?.cityName ? `城市:${store.cityName}` : null,
|
||||
`提交端:${submitterLabel}`,
|
||||
`套餐条数:${packages.length}`,
|
||||
`时间:${now}`,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join('\n'),
|
||||
{ applyMention: false },
|
||||
)
|
||||
.catch((e) =>
|
||||
this.logger.warn(
|
||||
`store.package_audit_pending wecom push failed: ${e instanceof Error ? e.message : String(e)}`,
|
||||
),
|
||||
);
|
||||
void this.wecomPush.dispatchEvent(
|
||||
'store.package_audit_pending',
|
||||
{
|
||||
storeName: store?.name ?? String(storeId),
|
||||
cityName: store?.cityName || '—',
|
||||
submitter: submitterLabel,
|
||||
packageCount: String(packages.length),
|
||||
requestId: req.id.toString(),
|
||||
},
|
||||
{ handlePath: `/store-package-audits?requestId=${req.id.toString()}` },
|
||||
);
|
||||
|
||||
return serializeBigInt({
|
||||
id: req.id.toString(),
|
||||
|
||||
@@ -78,26 +78,25 @@ export class StoreService {
|
||||
|
||||
/** 门店进入 PENDING 时通知企微(失败不挡业务) */
|
||||
private notifyStoreAuditPending(opts: {
|
||||
storeId: bigint;
|
||||
storeName: string;
|
||||
cityName?: string | null;
|
||||
partnerLabel?: string | null;
|
||||
submitType: '新建' | '重提';
|
||||
}) {
|
||||
const now = new Date().toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai' });
|
||||
const lines = [
|
||||
`**门店审核待处理 · ${opts.submitType}**`,
|
||||
`门店:${opts.storeName}`,
|
||||
opts.cityName ? `城市:${opts.cityName}` : null,
|
||||
opts.partnerLabel ? `合伙人:${opts.partnerLabel}` : null,
|
||||
`时间:${now}`,
|
||||
].filter(Boolean);
|
||||
void this.wecomPush
|
||||
.dispatchMarkdown('store.audit_pending', lines.join('\n'), { applyMention: false })
|
||||
.catch((e) =>
|
||||
this.logger.warn(
|
||||
`store.audit_pending wecom push failed: ${e instanceof Error ? e.message : String(e)}`,
|
||||
),
|
||||
);
|
||||
void this.wecomPush.dispatchEvent(
|
||||
'store.audit_pending',
|
||||
{
|
||||
storeName: opts.storeName,
|
||||
cityName: opts.cityName || '—',
|
||||
partnerLabel: opts.partnerLabel || '—',
|
||||
action: opts.submitType,
|
||||
storeId: opts.storeId.toString(),
|
||||
},
|
||||
{
|
||||
handlePath: `/stores?auditStatus=PENDING&storeId=${opts.storeId.toString()}`,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
private async whitelistPhoneSet(): Promise<Set<string>> {
|
||||
@@ -593,6 +592,7 @@ export class StoreService {
|
||||
select: { name: true, phone: true },
|
||||
});
|
||||
this.notifyStoreAuditPending({
|
||||
storeId: store.id,
|
||||
storeName: store.name,
|
||||
cityName: store.cityName,
|
||||
partnerLabel: partner?.name || partner?.phone || String(primaryId),
|
||||
@@ -762,6 +762,7 @@ export class StoreService {
|
||||
select: { name: true, phone: true },
|
||||
});
|
||||
this.notifyStoreAuditPending({
|
||||
storeId: updated.id,
|
||||
storeName: updated.name,
|
||||
cityName: updated.cityName,
|
||||
partnerLabel: partner?.name || partner?.phone || String(primaryId),
|
||||
@@ -886,6 +887,7 @@ export class StoreService {
|
||||
select: { name: true, phone: true },
|
||||
});
|
||||
this.notifyStoreAuditPending({
|
||||
storeId: store.id,
|
||||
storeName: store.name,
|
||||
cityName: store.cityName,
|
||||
partnerLabel: partner?.name || partner?.phone || String(primaryId),
|
||||
|
||||
@@ -35,6 +35,7 @@ import { FulfillmentService } from '../fulfillment/fulfillment.service';
|
||||
import { WechatOrderShippingService } from '../../integrations/wechat/wechat-order-shipping.service';
|
||||
import { AlertService } from '../../common/alert/alert.service';
|
||||
import { PayRedeemAnomalyService } from '../../common/alert/pay-redeem-anomaly.service';
|
||||
import { WecomMessagePushService } from '../../integrations/wecom/wecom-message-push.service';
|
||||
import type { Request } from 'express';
|
||||
|
||||
@Injectable()
|
||||
@@ -56,6 +57,7 @@ export class TradeService {
|
||||
private readonly wechatOrderShipping: WechatOrderShippingService,
|
||||
private readonly payRedeemAnomaly: PayRedeemAnomalyService,
|
||||
private readonly alert: AlertService,
|
||||
private readonly wecomPush: WecomMessagePushService,
|
||||
) {}
|
||||
|
||||
private readonly logger = new Logger(TradeService.name);
|
||||
@@ -429,9 +431,33 @@ export class TradeService {
|
||||
|
||||
private async afterOrderPaid(orderId: bigint) {
|
||||
await this.benefitService.grantOnOrderPaid(orderId);
|
||||
const order = await this.prisma.order.findUnique({ where: { id: orderId } });
|
||||
const order = await this.prisma.order.findUnique({
|
||||
where: { id: orderId },
|
||||
include: {
|
||||
city: { select: { name: true } },
|
||||
user: { select: { phone: true } },
|
||||
},
|
||||
});
|
||||
if (!order) return;
|
||||
|
||||
if (!order.isTest) {
|
||||
const skuSummary = `${order.productName}×${order.quantity}`;
|
||||
const phone = order.user?.phone || '';
|
||||
const phoneMasked =
|
||||
phone.length >= 7 ? `${phone.slice(0, 3)}****${phone.slice(-4)}` : phone || '—';
|
||||
void this.wecomPush.dispatchEvent(
|
||||
'order.paid',
|
||||
{
|
||||
orderNo: order.orderNo,
|
||||
payAmount: Number(order.payAmount).toFixed(2),
|
||||
cityName: order.city?.name || '—',
|
||||
skuSummary,
|
||||
phoneMasked,
|
||||
},
|
||||
{ handlePath: `/orders?orderNo=${encodeURIComponent(order.orderNo)}` },
|
||||
);
|
||||
}
|
||||
|
||||
const delivery = await this.prisma.orderDelivery.findUnique({ where: { orderId } });
|
||||
if (!delivery) {
|
||||
await this.prisma.orderDelivery.create({
|
||||
@@ -1090,6 +1116,26 @@ export class TradeService {
|
||||
remark: body.remark?.trim() || null,
|
||||
},
|
||||
});
|
||||
|
||||
if (!order.isTest) {
|
||||
const phone = resolved.phone.trim();
|
||||
const phoneMasked =
|
||||
phone.length >= 7 ? `${phone.slice(0, 3)}****${phone.slice(-4)}` : phone || '—';
|
||||
void this.wecomPush.dispatchEvent(
|
||||
'invoice.pending',
|
||||
{
|
||||
invoiceNo: invoice.invoiceNo,
|
||||
orderNo: order.orderNo,
|
||||
payAmount: Number(order.payAmount).toFixed(2),
|
||||
titleName: invoice.titleName,
|
||||
phoneMasked,
|
||||
},
|
||||
{
|
||||
handlePath: `/invoices?status=PENDING&invoiceNo=${encodeURIComponent(invoice.invoiceNo)}`,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
return serializeBigInt({ ...invoice, orderNo: order.orderNo });
|
||||
}
|
||||
|
||||
@@ -1177,11 +1223,17 @@ export class TradeService {
|
||||
return this.createInvoice(order.userId, order.id, body);
|
||||
}
|
||||
|
||||
async adminListInvoices(query: { status?: string; page?: number; pageSize?: number }) {
|
||||
async adminListInvoices(query: {
|
||||
status?: string;
|
||||
invoiceNo?: string;
|
||||
page?: number;
|
||||
pageSize?: number;
|
||||
}) {
|
||||
const page = query.page ?? 1;
|
||||
const pageSize = query.pageSize ?? 20;
|
||||
const where: { status?: never } = {};
|
||||
const where: { status?: never; invoiceNo?: { contains: string } } = {};
|
||||
if (query.status) where.status = query.status as never;
|
||||
if (query.invoiceNo?.trim()) where.invoiceNo = { contains: query.invoiceNo.trim() };
|
||||
const [items, total] = await Promise.all([
|
||||
this.prisma.userInvoice.findMany({
|
||||
where,
|
||||
|
||||
Reference in New Issue
Block a user