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 打包无错误
这个提交包含在:
wikiboss
2026-09-21 11:14:10 +08:00
父节点 e73c15bd22
当前提交 6c3d5d323a
共有 637 个文件被更改,包括 4731 次插入 和 56919 次删除

查看文件

@@ -13,8 +13,11 @@ export const adminCardArticleCurdOption = {
service: CardArticleService,
pageQueryOp: {
keyWordLikeFields: ['a.title', 'a.summary'],
fieldEq: ['a.cardId', 'a.isPublished'],
addOrderBy: { updateTime: 'desc' as const },
fieldEq: [
{ column: 'a.card_id', requestParam: 'cardId' },
{ column: 'a.is_published', requestParam: 'isPublished' },
],
addOrderBy: { update_time: 'desc' as const },
},
};

查看文件

@@ -13,8 +13,8 @@ export const adminCardBentoCurdOption = {
service: CardBentoService,
pageQueryOp: {
keyWordLikeFields: ['a.title', 'a.content'],
fieldEq: ['a.cardId', 'a.type', 'a.status'],
addOrderBy: { sortOrder: 'asc' as const },
fieldEq: [{ column: 'a.card_id', requestParam: 'cardId' }, 'a.type', 'a.status'],
addOrderBy: { sort_order: 'asc' as const },
},
serviceApis: [
{ method: 'getBlocksByCardId', summary: '按名片获取有序 Bento' },

查看文件

@@ -13,8 +13,8 @@ export const adminCardCasesCurdOption = {
service: CardCasesService,
pageQueryOp: {
keyWordLikeFields: ['a.title', 'a.client', 'a.metrics'],
fieldEq: ['a.cardId'],
addOrderBy: { sortOrder: 'asc' as const },
fieldEq: [{ column: 'a.card_id', requestParam: 'cardId' }],
addOrderBy: { sort_order: 'asc' as const },
},
serviceApis: [
{ method: 'getCasesByCardId', summary: '按名片获取案例列表' },

查看文件

@@ -0,0 +1,123 @@
import { Body, Inject, Post, Provide } from '@midwayjs/core';
import { CoolController, BaseController, CoolCommException } from '@cool-midway/core';
import { CardContactEntity } from '../../entity/contact';
import { CardContactService } from '../../service/contact';
import { CardInfoEntity } from '../../entity/info';
interface AdminCtx {
admin?: {
userId?: number;
tenantId?: number;
};
}
/**
* 名片大厅人脉
* 路由前缀: /admin/card/contact
*/
@Provide()
@CoolController({
api: ['page'],
entity: CardContactEntity,
service: CardContactService,
pageQueryOp: {
keyWordLikeFields: ['b.name', 'b.title', 'b.company'],
select: [
'a.*',
'b.name AS peerName',
'b.title AS peerTitle',
'b.company AS peerCompany',
'b.avatar_url AS peerAvatarUrl',
'b.slug AS peerSlug',
],
join: [
{
entity: CardInfoEntity,
alias: 'b',
condition: 'a.peer_card_id = b.id',
type: 'leftJoin',
},
],
where: async (ctx: AdminCtx) => [
['a.owner_user_id = :uid', { uid: ctx.admin?.userId || 0 }],
],
addOrderBy: { update_time: 'desc' as const },
},
})
export class AdminCardContactController extends BaseController {
@Inject()
ctx: AdminCtx;
@Inject()
cardContactService: CardContactService;
/**
* 把对方名片收入大厅
* POST /admin/card/contact/collect
*/
@Post('/collect', { summary: '收入大厅人脉' })
async collect(
@Body() body: { peerCardId?: number; ownerCardId?: number }
) {
const ownerUserId = this.ctx.admin?.userId;
if (!ownerUserId) {
throw new CoolCommException('请先登录', 401);
}
if (!body?.peerCardId) {
throw new CoolCommException('缺少对方名片', 400);
}
const data = await this.cardContactService.collect({
ownerUserId: Number(ownerUserId),
peerCardId: Number(body.peerCardId),
ownerCardId: body.ownerCardId != null ? Number(body.ownerCardId) : null,
});
return this.ok(data);
}
/**
* 当前用户人脉分页(带对方名片摘要)
* POST /admin/card/contact/hallPage
*/
@Post('/hallPage', { summary: '大厅人脉分页' })
async hallPage(
@Body() body: { page?: number; size?: number; keyWord?: string }
) {
const ownerUserId = this.ctx.admin?.userId;
if (!ownerUserId) {
throw new CoolCommException('请先登录', 401);
}
const data = await this.cardContactService.pageContacts({
ownerUserId: Number(ownerUserId),
page: body?.page,
size: body?.size,
keyWord: body?.keyWord,
});
return this.ok(data);
}
/**
* 同租户已发布名片墙
* POST /admin/card/contact/teamWall
*/
@Post('/teamWall', { summary: '团队名片墙' })
async teamWall(
@Body() body: { page?: number; size?: number; keyWord?: string }
) {
const ownerUserId = this.ctx.admin?.userId;
if (!ownerUserId) {
throw new CoolCommException('请先登录', 401);
}
const tenantId = await this.cardContactService.resolveTenantId(
Number(ownerUserId),
this.ctx.admin?.tenantId
);
const data = await this.cardContactService.pageTeamWall({
tenantId,
excludeUserId: Number(ownerUserId),
page: body?.page,
size: body?.size,
keyWord: body?.keyWord,
});
return this.ok(data);
}
}

查看文件

@@ -0,0 +1,75 @@
import { Body, Inject, Post, Provide } from '@midwayjs/core';
import { CoolController, BaseController, CoolCommException } from '@cool-midway/core';
import { CardEventService } from '../../service/event';
interface AdminCtx {
admin?: {
userId?: number;
tenantId?: number;
};
}
/**
* 展会现场
* 路由前缀: /admin/card/event
*/
@Provide()
@CoolController()
export class AdminCardEventController extends BaseController {
@Inject()
ctx: AdminCtx;
@Inject()
cardEventService: CardEventService;
private requireUserId(): number {
const ownerUserId = this.ctx.admin?.userId;
if (!ownerUserId) {
throw new CoolCommException('请先登录', 401);
}
return Number(ownerUserId);
}
/**
* POST /admin/card/event/join
*/
@Post('/join', { summary: '活动码加入展会现场' })
async join(
@Body() body: { code?: string; cardId?: number }
) {
if (!body?.code) {
throw new CoolCommException('缺少活动码', 400);
}
const data = await this.cardEventService.joinEvent({
userId: this.requireUserId(),
code: body.code,
cardId: body.cardId != null ? Number(body.cardId) : null,
});
return this.ok(data);
}
/**
* POST /admin/card/event/floorPage
*/
@Post('/floorPage', { summary: '展会现场同场名片' })
async floorPage(
@Body() body: { page?: number; size?: number; keyWord?: string }
) {
const data = await this.cardEventService.pageEventFloor({
userId: this.requireUserId(),
page: body?.page,
size: body?.size,
keyWord: body?.keyWord,
});
return this.ok(data);
}
/**
* POST /admin/card/event/current
*/
@Post('/current', { summary: '当前加入的展会现场' })
async current() {
const data = await this.cardEventService.currentEvent(this.requireUserId());
return this.ok(data);
}
}

查看文件

@@ -0,0 +1,100 @@
import { Body, Inject, Post, Provide } from '@midwayjs/core';
import { CoolController, BaseController, CoolCommException } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { CardMutualExchangeService } from '../../service/mutual-exchange';
import { CardInfoEntity } from '../../entity/info';
interface AdminCtx {
admin?: {
userId?: number;
};
}
/**
* 名片双向互换
* 路由前缀: /admin/card/exchange
*/
@Provide()
@CoolController()
export class AdminCardExchangeController extends BaseController {
@Inject()
ctx: AdminCtx;
@Inject()
cardMutualExchangeService: CardMutualExchangeService;
@InjectEntityModel(CardInfoEntity)
cardInfoEntity: Repository<CardInfoEntity>;
private async resolveActingCardId(explicit?: number): Promise<number> {
if (explicit && Number(explicit) > 0) {
return Number(explicit);
}
const userId = this.ctx.admin?.userId;
if (!userId) {
throw new CoolCommException('请先登录', 401);
}
const card = await this.cardInfoEntity.findOne({
where: { userId: Number(userId), status: 'published' },
});
if (!card) {
throw new CoolCommException('请先发布一张名片再互换', 400);
}
return Number(card.id);
}
/**
* POST /admin/card/exchange/initiate
*/
@Post('/initiate', { summary: '发起名片互换' })
async initiate(
@Body()
body: {
senderCardId?: number;
recipientCardId?: number;
notes?: string;
}
) {
if (!body?.recipientCardId) {
throw new CoolCommException('缺少对方名片', 400);
}
const senderCardId = await this.resolveActingCardId(body.senderCardId);
const data = await this.cardMutualExchangeService.initiateExchange({
senderCardId,
recipientCardId: Number(body.recipientCardId),
notes: body.notes,
});
return this.ok(data);
}
/**
* POST /admin/card/exchange/confirm
*/
@Post('/confirm', { summary: '确认名片互换' })
async confirm(
@Body()
body: {
invitationId?: string;
exchangeCode?: string;
actingCardId?: number;
}
) {
const actingCardId = await this.resolveActingCardId(body?.actingCardId);
if (body?.exchangeCode) {
const data = await this.cardMutualExchangeService.confirmByCode(
body.exchangeCode,
actingCardId
);
return this.ok(data);
}
if (!body?.invitationId) {
throw new CoolCommException('缺少互换单或互换码', 400);
}
const data = await this.cardMutualExchangeService.confirmExchange(
String(body.invitationId),
actingCardId
);
return this.ok(data);
}
}

查看文件

@@ -8,6 +8,7 @@ import { CardInfoService } from '../../service/info';
interface AdminInsertCtx {
admin?: {
userId?: number;
tenantId?: number;
};
}
@@ -22,12 +23,13 @@ export const adminCardInfoCurdOption = {
service: CardInfoService,
insertParam: (ctx: AdminInsertCtx) => ({
userId: ctx.admin?.userId,
tenantId: ctx.admin?.tenantId,
}),
infoIgnoreProperty: ['remark', 'internalConfig'],
pageQueryOp: {
keyWordLikeFields: ['a.name', 'a.title', 'a.company', 'a.slug'],
fieldEq: ['a.status', 'a.themeCode'],
addOrderBy: { updateTime: 'desc' as const },
fieldEq: ['a.status', { column: 'a.theme_code', requestParam: 'themeCode' }],
addOrderBy: { update_time: 'desc' as const },
},
};

查看文件

@@ -12,7 +12,7 @@ export const adminCardLocaleCurdOption = {
entity: CardLocaleEntity,
service: CardLocaleService,
pageQueryOp: {
fieldEq: ['a.cardId', 'a.locale'],
fieldEq: [{ column: 'a.card_id', requestParam: 'cardId' }, 'a.locale'],
},
serviceApis: [
{ method: 'upsert', summary: '按名片语言覆盖写入' },