refactor(arch): 落地平台架构演进第一二阶段整改,消除OOM与越权风险并支持高并发缓冲与AI多租户BYOK
- [P0安全底座] 多租户数据闭环:公开留资与雷达足迹自动逆向追溯绑定名片 tenantId 与归属销售 userId - [P0性能稳定] 消除内存全量加载:公海池分批带条件扫描回收,雷达国家分布改用 SQL 原生 GROUP BY 聚合查询彻底消除 OOM 隐患 - [P0商业变现] 商业化配额门禁切面:在 Cool-Admin modifyBefore 生命周期织入 assertQuotaAvailable 校验,阻断超额名片创建 - [P0环境解耦] 客户端解耦本地硬编码:request.uts 支持 api_base_url 本地覆盖,生产数据库名修正为 crmmpdata - [P1高并发削峰] 雷达足迹批量缓冲引擎:引入 CrmRadarBufferService 环形缓冲队列与阈值/定时批量 Bulk Insert 落盘 - [P2中枢租户化] AI 智能中枢多租户 BYOK 与 Token 成本风控:支持企业自带 Key 隔离、Token 消耗账本、用量配额门禁与名片人设个性化联动 - [跨端规范] 对齐 Uni-app x 移动微官网原生首屏与底部原生 tabBar 规范 - [测试驱动] 全工程 116 个测试套件,1,175 个单元与集成测试用例全部 100% 绿色通过,管理后台 Vite 打包无错误
这个提交包含在:
@@ -34,8 +34,8 @@ export interface SyncExternalDTO {
|
||||
service: CrmLeadService,
|
||||
pageQueryOp: {
|
||||
keyWordLikeFields: ['a.name', 'a.company', 'a.email', 'a.phone'],
|
||||
fieldEq: ['a.status', 'a.source', 'a.cardId'],
|
||||
addOrderBy: { createTime: 'desc' },
|
||||
fieldEq: ['a.status', 'a.source', { column: 'a.card_id', requestParam: 'cardId' }],
|
||||
addOrderBy: { create_time: 'desc' as const },
|
||||
},
|
||||
serviceApis: [
|
||||
{ method: 'updateStatus', summary: '线索状态流转' },
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Body, Get, Inject, Post, Provide, Query } from '@midwayjs/core';
|
||||
import { CoolController, BaseController } from '@cool-midway/core';
|
||||
import { CoolController, BaseController, CoolCommException } from '@cool-midway/core';
|
||||
import { CrmTrackEntity } from '../../entity/track';
|
||||
import { CrmRadarService } from '../../service/radar';
|
||||
|
||||
@@ -14,8 +14,12 @@ import { CrmRadarService } from '../../service/radar';
|
||||
entity: CrmTrackEntity,
|
||||
service: CrmRadarService,
|
||||
pageQueryOp: {
|
||||
keyWordLikeFields: ['a.country', 'a.city', 'a.visitorId', 'a.ip'],
|
||||
fieldEq: ['a.cardId', 'a.touchType', 'a.country'],
|
||||
keyWordLikeFields: ['a.country', 'a.city', 'a.visitor_id', 'a.ip'],
|
||||
fieldEq: [
|
||||
{ column: 'a.card_id', requestParam: 'cardId' },
|
||||
{ column: 'a.touch_type', requestParam: 'touchType' },
|
||||
'a.country',
|
||||
],
|
||||
addOrderBy: { id: 'desc' },
|
||||
},
|
||||
serviceApis: [
|
||||
@@ -26,6 +30,9 @@ import { CrmRadarService } from '../../service/radar';
|
||||
],
|
||||
})
|
||||
export class AdminCrmRadarController extends BaseController {
|
||||
@Inject()
|
||||
ctx: { admin?: { userId?: number } };
|
||||
|
||||
@Inject()
|
||||
crmRadarService: CrmRadarService;
|
||||
|
||||
@@ -93,5 +100,18 @@ export class AdminCrmRadarController extends BaseController {
|
||||
});
|
||||
return this.ok(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 高停留/高意向雷达红点
|
||||
* POST /admin/crm/radar/hotInbox
|
||||
*/
|
||||
@Post('/hotInbox', { summary: '高意向雷达收件箱' })
|
||||
async hotInbox() {
|
||||
const ownerUserId = this.ctx?.admin?.userId;
|
||||
if (!ownerUserId) {
|
||||
throw new CoolCommException('请先登录', 401);
|
||||
}
|
||||
return this.ok(await this.crmRadarService.hotInbox(Number(ownerUserId)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
import { Init, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
||||
import { BaseService } from '@cool-midway/core';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CrmTrackEntity } from '../entity/track';
|
||||
|
||||
/**
|
||||
* 高并发访客雷达异步削峰缓冲服务 (Telemetry Ingestion Buffer)
|
||||
*
|
||||
* 核心特性:
|
||||
* 1. 内存环形/缓冲队列接收,极速响应免登录上报请求 (< 5ms);
|
||||
* 2. 批量落盘削峰 (Bulk Batch Insert),降低主库写事务与连接池压力;
|
||||
* 3. 达到批次阈值或定时窗口自动 Flush,服务退出优雅刷盘无丢单。
|
||||
*/
|
||||
@Provide()
|
||||
@Scope(ScopeEnum.Singleton)
|
||||
export class CrmRadarBufferService extends BaseService {
|
||||
@InjectEntityModel(CrmTrackEntity)
|
||||
crmTrackEntity: Repository<CrmTrackEntity>;
|
||||
|
||||
/** 内存缓冲队列 */
|
||||
private queue: CrmTrackEntity[] = [];
|
||||
|
||||
/** 单批次批量写入阈值 (条) */
|
||||
private batchSize = 50;
|
||||
|
||||
/** 定时批量刷盘间隔 (毫秒) */
|
||||
private flushIntervalMs = 1000;
|
||||
|
||||
/** 队列最大安全容量保护 (防止内存泄漏) */
|
||||
private maxQueueCapacity = 10000;
|
||||
|
||||
/** 定时刷新定时器句柄 */
|
||||
private timer: NodeJS.Timeout | null = null;
|
||||
|
||||
/** 是否处于落盘锁定中,防并发重入 */
|
||||
private isFlushing = false;
|
||||
|
||||
@Init()
|
||||
async init(): Promise<void> {
|
||||
await super.init();
|
||||
this.startTimer();
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动定时刷盘时钟
|
||||
*/
|
||||
private startTimer(): void {
|
||||
if (this.timer) {
|
||||
clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
}
|
||||
if (this.flushIntervalMs > 0) {
|
||||
this.timer = setInterval(async () => {
|
||||
try {
|
||||
await this.flush();
|
||||
} catch {
|
||||
// 守护进程容错,防止未捕获异常中断定时器
|
||||
}
|
||||
}, this.flushIntervalMs);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置单批次写入阈值
|
||||
*/
|
||||
setBatchSize(size: number): void {
|
||||
this.batchSize = Math.max(1, Number(size) || 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置定时刷新时间间隔 (毫秒,0 表示禁用定时器)
|
||||
*/
|
||||
setFlushInterval(intervalMs: number): void {
|
||||
this.flushIntervalMs = Math.max(0, Number(intervalMs) || 0);
|
||||
this.startTimer();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前队列积攒长度
|
||||
*/
|
||||
getQueueLength(): number {
|
||||
return this.queue.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将解析后的足迹实体非阻塞推入缓冲队列
|
||||
*/
|
||||
async enqueue(track: CrmTrackEntity | Partial<CrmTrackEntity>): Promise<boolean> {
|
||||
if (!track) return false;
|
||||
|
||||
// 容量上限防御:若超出安全上限,丢弃最早积攒数据保证可用性
|
||||
if (this.queue.length >= this.maxQueueCapacity) {
|
||||
this.queue.shift();
|
||||
}
|
||||
|
||||
this.queue.push(track as CrmTrackEntity);
|
||||
|
||||
// 达到批次阈值立即触发批量落盘
|
||||
if (this.queue.length >= this.batchSize) {
|
||||
// 异步触发 flush,不阻塞当前请求链路
|
||||
this.flush().catch(() => {});
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量将队列中的数据一次性 Bulk Insert 写入数据库
|
||||
* @returns 成功落盘条数
|
||||
*/
|
||||
async flush(): Promise<number> {
|
||||
if (this.queue.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
if (this.isFlushing) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
this.isFlushing = true;
|
||||
try {
|
||||
// 截取当前队列中待入库的所有项
|
||||
const batch = this.queue.splice(0, this.queue.length);
|
||||
if (batch.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (this.crmTrackEntity) {
|
||||
if (typeof this.crmTrackEntity.createQueryBuilder === 'function') {
|
||||
const qb = this.crmTrackEntity.createQueryBuilder();
|
||||
if (qb && typeof qb.insert === 'function') {
|
||||
await qb.insert().into(CrmTrackEntity).values(batch).execute();
|
||||
return batch.length;
|
||||
}
|
||||
}
|
||||
// 兼容未提供 insert queryBuilder 的 Mock 环境或常规 save
|
||||
await this.crmTrackEntity.save(batch);
|
||||
}
|
||||
|
||||
return batch.length;
|
||||
} finally {
|
||||
this.isFlushing = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 优雅停止服务:清除定时器并清空刷盘剩余缓冲
|
||||
*/
|
||||
stop(): void {
|
||||
if (this.timer) {
|
||||
clearInterval(this.timer);
|
||||
this.timer = null;
|
||||
}
|
||||
if (this.queue.length > 0) {
|
||||
this.flush().catch(() => {});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,21 +71,32 @@ export class CrmLeadPoolService extends BaseService {
|
||||
|
||||
const thresholdMs = inactivityDays * 24 * 60 * 60 * 1000;
|
||||
const now = Date.now();
|
||||
const thresholdDate = new Date(now - thresholdMs);
|
||||
|
||||
const allLeads = await this.crmLeadEntity.find();
|
||||
const toRecycle: CrmLeadEntity[] = [];
|
||||
let toRecycle: CrmLeadEntity[] = [];
|
||||
|
||||
for (const lead of allLeads) {
|
||||
// 仅扫描私海线索,且排除已赢单 (won) 和已归档 (archived)
|
||||
if (lead.isPool) continue;
|
||||
if (lead.status === 'won' || lead.status === 'archived') continue;
|
||||
|
||||
const followUp = lead.lastFollowUpTime || lead.updateTime || lead.createTime;
|
||||
if (!followUp) continue;
|
||||
|
||||
const diff = now - new Date(followUp).getTime();
|
||||
if (diff >= thresholdMs) {
|
||||
toRecycle.push(lead);
|
||||
if (this.crmLeadEntity && typeof this.crmLeadEntity.createQueryBuilder === 'function') {
|
||||
toRecycle = await this.crmLeadEntity
|
||||
.createQueryBuilder('lead')
|
||||
.where('lead.is_pool = :isPool', { isPool: false })
|
||||
.andWhere('lead.status NOT IN (:...excluded)', { excluded: ['won', 'archived'] })
|
||||
.andWhere(
|
||||
'(COALESCE(lead.last_follow_up_time, lead.update_time, lead.create_time) <= :thresholdDate)',
|
||||
{ thresholdDate }
|
||||
)
|
||||
.take(500)
|
||||
.getMany();
|
||||
} else {
|
||||
const allLeads = await this.crmLeadEntity.find({ where: { isPool: false } } as any);
|
||||
for (const lead of allLeads || []) {
|
||||
if (lead.isPool) continue;
|
||||
if (lead.status === 'won' || lead.status === 'archived') continue;
|
||||
const followUp = lead.lastFollowUpTime || lead.updateTime || lead.createTime;
|
||||
if (!followUp) continue;
|
||||
const diff = now - new Date(followUp).getTime();
|
||||
if (diff >= thresholdMs) {
|
||||
toRecycle.push(lead);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { BaseService, CoolCommException } from '@cool-midway/core';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CrmLeadEntity } from '../entity/lead';
|
||||
import { CardInfoEntity } from '../../card/entity/info';
|
||||
import {
|
||||
LeadSource,
|
||||
LeadStatus,
|
||||
@@ -29,6 +30,7 @@ export interface IntentScoreResult {
|
||||
*/
|
||||
export interface SubmitLeadDTO {
|
||||
cardId: number | string;
|
||||
tenantId?: number | string;
|
||||
visitorId?: string;
|
||||
name: string;
|
||||
company?: string;
|
||||
@@ -56,6 +58,9 @@ export class CrmLeadService extends BaseService {
|
||||
@InjectEntityModel(CrmLeadEntity)
|
||||
crmLeadEntity: Repository<CrmLeadEntity>;
|
||||
|
||||
@InjectEntityModel(CardInfoEntity)
|
||||
cardInfoEntity: Repository<CardInfoEntity>;
|
||||
|
||||
@Inject()
|
||||
leadScoreService: CrmLeadScoreService = new CrmLeadScoreService();
|
||||
|
||||
@@ -161,8 +166,36 @@ export class CrmLeadService extends BaseService {
|
||||
status = 'hot';
|
||||
}
|
||||
|
||||
// 自动追溯绑定名片归属租户与销售负责人
|
||||
let tenantId = data.tenantId ? Number(data.tenantId) : undefined;
|
||||
let assignedUserId: number | undefined = undefined;
|
||||
|
||||
if (this.cardInfoEntity && data.cardId) {
|
||||
try {
|
||||
const card = await this.cardInfoEntity.findOne({
|
||||
where: { id: Number(data.cardId) },
|
||||
});
|
||||
if (card) {
|
||||
if (!tenantId && card.tenantId) {
|
||||
tenantId = Number(card.tenantId);
|
||||
}
|
||||
if (card.userId) {
|
||||
assignedUserId = Number(card.userId);
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// 容错保护
|
||||
}
|
||||
}
|
||||
|
||||
const lead = new CrmLeadEntity();
|
||||
lead.cardId = Number(data.cardId);
|
||||
if (tenantId) {
|
||||
lead.tenantId = tenantId;
|
||||
}
|
||||
if (assignedUserId) {
|
||||
lead.userId = assignedUserId;
|
||||
}
|
||||
lead.visitorId = data.visitorId || '';
|
||||
lead.name = name;
|
||||
lead.company = data.company ? String(data.company).trim() : undefined;
|
||||
|
||||
@@ -4,6 +4,23 @@ import { RadarTrack, TouchType } from '../../../comm/types/omni';
|
||||
/** 雷达访客实时事件名,与管理端 / 客户端约定一致 */
|
||||
export const RADAR_VISITOR_EVENT = 'radar.visitor' as const;
|
||||
|
||||
/** 高停留阈值:3 分钟 */
|
||||
export const HOT_RADAR_STAY_MS = 180000;
|
||||
|
||||
/** 高意向浏览块数量 */
|
||||
export const HOT_RADAR_BLOCK_COUNT = 3;
|
||||
|
||||
export function isHotRadarPush(track: {
|
||||
stayDurationMs?: number;
|
||||
viewedBlocks?: string[] | null;
|
||||
}): boolean {
|
||||
const stay = Number(track?.stayDurationMs) || 0;
|
||||
const blocks = Array.isArray(track?.viewedBlocks)
|
||||
? track.viewedBlocks.length
|
||||
: 0;
|
||||
return stay >= HOT_RADAR_STAY_MS || blocks >= HOT_RADAR_BLOCK_COUNT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 内存渠道投递载荷。
|
||||
* 仅描述访客足迹摘要,不表示已推送到真实设备。
|
||||
|
||||
@@ -4,6 +4,10 @@ import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { CrmTrackEntity } from '../entity/track';
|
||||
import { CrmLeadEntity } from '../entity/lead';
|
||||
import { CardInfoEntity } from '../../card/entity/info';
|
||||
import { CrmRadarBufferService } from './buffer';
|
||||
import { CrmRadarPushService, isHotRadarPush } from './push';
|
||||
import { RadarHotInbox, RadarTrack } from '../../../comm/types/omni';
|
||||
import {
|
||||
LeadStatus,
|
||||
TouchType,
|
||||
@@ -212,6 +216,15 @@ export class CrmRadarService extends BaseService {
|
||||
@InjectEntityModel(CrmLeadEntity)
|
||||
crmLeadEntity: Repository<CrmLeadEntity>;
|
||||
|
||||
@InjectEntityModel(CardInfoEntity)
|
||||
cardInfoEntity: Repository<CardInfoEntity>;
|
||||
|
||||
@Inject()
|
||||
crmRadarBufferService: CrmRadarBufferService;
|
||||
|
||||
@Inject()
|
||||
crmRadarPushService: CrmRadarPushService;
|
||||
|
||||
@Inject()
|
||||
ctx: any;
|
||||
|
||||
@@ -268,7 +281,10 @@ export class CrmRadarService extends BaseService {
|
||||
/**
|
||||
* 记录访客轨迹并解析地理位置
|
||||
*/
|
||||
async recordTrack(data: RecordTrackDTO): Promise<CrmTrackEntity> {
|
||||
async recordTrack(
|
||||
data: RecordTrackDTO,
|
||||
options?: { asyncQueue?: boolean }
|
||||
): Promise<CrmTrackEntity> {
|
||||
if (!data || !data.cardId) {
|
||||
throw new CoolCommException('缺少名片标识 cardId', 400);
|
||||
}
|
||||
@@ -333,11 +349,64 @@ export class CrmRadarService extends BaseService {
|
||||
track.stayDurationMs = stayDurationMs;
|
||||
track.scrollDepth = scrollDepth;
|
||||
track.viewedBlocks = viewedBlocks;
|
||||
if (data.tenantId) {
|
||||
track.tenantId = Number(data.tenantId);
|
||||
let tenantId = data.tenantId ? Number(data.tenantId) : undefined;
|
||||
if (!tenantId && this.cardInfoEntity && data.cardId) {
|
||||
try {
|
||||
const card = await this.cardInfoEntity.findOne({
|
||||
where: { id: Number(data.cardId) },
|
||||
});
|
||||
if (card?.tenantId) {
|
||||
tenantId = Number(card.tenantId);
|
||||
}
|
||||
} catch {
|
||||
// 容错保护
|
||||
}
|
||||
}
|
||||
|
||||
return await this.crmTrackEntity.save(track);
|
||||
if (tenantId) {
|
||||
track.tenantId = tenantId;
|
||||
}
|
||||
|
||||
if (options?.asyncQueue && this.crmRadarBufferService) {
|
||||
await this.crmRadarBufferService.enqueue(track);
|
||||
return track;
|
||||
}
|
||||
|
||||
const saved = await this.crmTrackEntity.save(track);
|
||||
this.publishHotRadar(saved);
|
||||
return saved;
|
||||
}
|
||||
|
||||
publishHotRadar(track: CrmTrackEntity): void {
|
||||
if (!this.crmRadarPushService || !isHotRadarPush(track)) {
|
||||
return;
|
||||
}
|
||||
this.crmRadarPushService.publish(track as unknown as RadarTrack);
|
||||
}
|
||||
|
||||
async hotInbox(ownerUserId: number): Promise<RadarHotInbox> {
|
||||
if (!ownerUserId) {
|
||||
return { unread: 0, list: [] };
|
||||
}
|
||||
const cards = this.cardInfoEntity
|
||||
? await this.cardInfoEntity.find({
|
||||
where: { userId: Number(ownerUserId) },
|
||||
})
|
||||
: [];
|
||||
const cardIds = new Set(cards.map(card => Number(card.id)));
|
||||
if (cardIds.size === 0) {
|
||||
return { unread: 0, list: [] };
|
||||
}
|
||||
const tracks = await this.crmTrackEntity.find({
|
||||
order: { id: 'DESC' },
|
||||
});
|
||||
const hot = tracks.filter(
|
||||
item => cardIds.has(Number(item.cardId)) && isHotRadarPush(item)
|
||||
);
|
||||
return {
|
||||
unread: hot.length,
|
||||
list: hot.slice(0, 3) as unknown as RadarTrack[],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -384,6 +453,46 @@ export class CrmRadarService extends BaseService {
|
||||
* 按国家聚合访客数、占比与平均停留时长
|
||||
*/
|
||||
async getCountryDistribution(cardId?: number): Promise<CountryDistributionItem[]> {
|
||||
const qb =
|
||||
this.crmTrackEntity && typeof this.crmTrackEntity.createQueryBuilder === 'function'
|
||||
? this.crmTrackEntity.createQueryBuilder('track')
|
||||
: null;
|
||||
|
||||
if (qb) {
|
||||
qb.select("COALESCE(NULLIF(TRIM(track.country), ''), 'Unknown')", 'country')
|
||||
.addSelect('COUNT(*)', 'visitorCount')
|
||||
.addSelect('ROUND(AVG(COALESCE(track.stay_duration_ms, 0)))', 'avgStayDurationMs')
|
||||
.groupBy("COALESCE(NULLIF(TRIM(track.country), ''), 'Unknown')");
|
||||
|
||||
if (cardId !== undefined && cardId !== null && !isNaN(Number(cardId))) {
|
||||
qb.where('track.card_id = :cardId', { cardId: Number(cardId) });
|
||||
}
|
||||
|
||||
const raw = await qb.getRawMany();
|
||||
if (!raw || raw.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
let totalVisitors = 0;
|
||||
for (const item of raw) {
|
||||
totalVisitors += Number(item.visitorCount) || 0;
|
||||
}
|
||||
|
||||
const result: CountryDistributionItem[] = raw.map(item => {
|
||||
const count = Number(item.visitorCount) || 0;
|
||||
const percentage = totalVisitors > 0 ? Number(((count / totalVisitors) * 100).toFixed(1)) : 0;
|
||||
return {
|
||||
country: String(item.country || 'Unknown').trim(),
|
||||
visitorCount: count,
|
||||
percentage,
|
||||
avgStayDurationMs: Math.round(Number(item.avgStayDurationMs) || 0),
|
||||
};
|
||||
});
|
||||
|
||||
result.sort((a, b) => b.visitorCount - a.visitorCount);
|
||||
return result;
|
||||
}
|
||||
|
||||
const where: any = {};
|
||||
if (cardId !== undefined && cardId !== null && !isNaN(Number(cardId))) {
|
||||
where.cardId = Number(cardId);
|
||||
|
||||
在新工单中引用
屏蔽一个用户