Files
shensheshou/开发文档/5、接口/API清单与核心逻辑.md

3.1 KiB
Raw Blame History

神射手 - API清单与核心逻辑

📅 从 app/api/ 提取 | 2026-01-31


一、API清单 (25个)

路由 方法 功能 核心逻辑
/api/ai-chat GET/POST AI对话 parseIntent → queryFullProfile
/api/data-sources GET 数据源列表 listDatabases + 统计
/api/tags GET 标签统计 $sample 采样
/api/portrait GET 用户画像 queryFullProfile
/api/traffic-packages GET/POST 流量池/导出 user_evaluation_score 分桶
/api/crowd-pools GET 人群圈选 按项目 getProjectPools
/api/database-structure GET 血缘节点 listDatabases + 生成节点
/api/channels GET/POST 渠道对接 转发到 Gateway
/api/monitoring GET 系统监控 serverStatus + 健康检查
/api/ai-tagging GET/POST AI打标 -
/api/cleaning-rules GET/POST 清洗规则 -
/api/users GET 用户列表 -
/api/search POST 智能搜索 intelligentSearch

二、AI对话意图解析 (核心逻辑)

// app/api/ai-chat/route.ts
function parseIntent(message: string): { type: string; query?: string } {
  // 手机号: 1[3-9]\d{9}
  const phoneMatch = message.match(/(\+?86)?1[3-9]\d{9}/g)
  if (phoneMatch) return { type: "query_phone", query: phoneMatch[0] }
  
  // QQ号
  const qqMatch = message.match(/(?:qq)?[:\s]*(\d{5,11})/i)
  if (qqMatch) return { type: "query_qq", query: qqMatch[1] }
  
  // 系统状态
  if (msg.includes("状态") || msg.includes("统计")) return { type: "system_status" }
  
  // RFM分析
  if (msg.includes("rfm") || msg.includes("估值")) return { type: "rfm_analysis" }
  
  // 高价值用户
  if (msg.includes("高价值") || msg.includes("top")) return { type: "high_value_users" }
  
  return { type: "search", query: message }
}

三、流量池分桶逻辑

// app/api/traffic-packages/route.ts
const scoreRange = {
  diamond: { $gte: 3000 },
  gold: { $gte: 2000, $lt: 3000 },
  silver: { $gte: 1000, $lt: 2000 },
  bronze: { $gte: 500, $lt: 1000 },
  potential: { $lt: 500 }
}

// 使用 $sample 采样
const stats = await collection.aggregate([
  { $sample: { size: 100000 } },
  { $match: { user_evaluation_score: { $exists: true, $gt: 0 } } },
  { $bucket: { groupBy: '$user_evaluation_score', boundaries: [0, 500, 1000, 2000, 3000, 10000] } }
], { maxTimeMS: 15000 })

四、人群圈选逻辑

// app/api/crowd-pools/route.ts
// 项目定义
const PROJECTS = {
  ckb: { db: 'KR_存客宝', coll: '用户资产统一视图', by: 'traffic_pool.pool_name' },
  dlm: { db: 'KR_点了码', coll: '用户资产统一视图', by: '角色标签' },
  weibo: { db: 'KR_微博', coll: '微博uid+手机' },
  qq: { db: 'KR_腾讯', coll: 'QQ+手机', by: '省份' }
}

// 获取池内用户
const users = await collection.find({ [field]: poolId })
  .skip((page - 1) * limit)
  .limit(limit)
  .project({ phone: 1, name: 1, ... })
  .toArray()

五、关联文档