2026-01-14 07:50:53 +00:00
|
|
|
|
import { type NextRequest, NextResponse } from "next/server"
|
|
|
|
|
|
import crypto from "crypto"
|
|
|
|
|
|
|
|
|
|
|
|
// 存客宝API配置
|
2026-01-21 15:49:12 +08:00
|
|
|
|
const CKB_API_KEY = process.env.CKB_API_KEY || "fyngh-ecy9h-qkdae-epwd5-rz6kd"
|
2026-01-14 07:50:53 +00:00
|
|
|
|
const CKB_API_URL = "https://ckbapi.quwanzhi.com/v1/api/scenarios"
|
|
|
|
|
|
|
2026-01-21 15:49:12 +08:00
|
|
|
|
// 生成签名 - 根据文档实现
|
|
|
|
|
|
function generateSign(params: Record<string, any>, apiKey: string): string {
|
|
|
|
|
|
// 1. 移除 sign、apiKey、portrait 字段
|
|
|
|
|
|
const filteredParams = { ...params }
|
|
|
|
|
|
delete filteredParams.sign
|
|
|
|
|
|
delete filteredParams.apiKey
|
|
|
|
|
|
delete filteredParams.portrait
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 移除空值字段
|
|
|
|
|
|
const nonEmptyParams: Record<string, any> = {}
|
|
|
|
|
|
for (const [key, value] of Object.entries(filteredParams)) {
|
|
|
|
|
|
if (value !== null && value !== "") {
|
|
|
|
|
|
nonEmptyParams[key] = value
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 按参数名升序排序
|
|
|
|
|
|
const sortedKeys = Object.keys(nonEmptyParams).sort()
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 拼接参数值
|
|
|
|
|
|
const stringToSign = sortedKeys.map(key => nonEmptyParams[key]).join("")
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 第一次 MD5
|
|
|
|
|
|
const firstMd5 = crypto.createHash("md5").update(stringToSign).digest("hex")
|
|
|
|
|
|
|
|
|
|
|
|
// 6. 拼接 apiKey 再次 MD5
|
|
|
|
|
|
const sign = crypto.createHash("md5").update(firstMd5 + apiKey).digest("hex")
|
|
|
|
|
|
|
|
|
|
|
|
return sign
|
2026-01-14 07:50:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 不同类型对应的source标签
|
|
|
|
|
|
const sourceMap: Record<string, string> = {
|
|
|
|
|
|
team: "团队招募",
|
|
|
|
|
|
investor: "资源对接",
|
|
|
|
|
|
mentor: "导师顾问",
|
2026-01-21 15:49:12 +08:00
|
|
|
|
partner: "创业合伙",
|
2026-01-14 07:50:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const tagsMap: Record<string, string> = {
|
|
|
|
|
|
team: "切片团队,团队招募",
|
|
|
|
|
|
investor: "资源对接,资源群",
|
|
|
|
|
|
mentor: "导师顾问,咨询服务",
|
2026-01-21 15:49:12 +08:00
|
|
|
|
partner: "创业合伙,创业伙伴",
|
2026-01-14 07:50:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export async function POST(request: NextRequest) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const body = await request.json()
|
2026-01-21 15:49:12 +08:00
|
|
|
|
const { type, phone, wechat, name, userId, remark } = body
|
2026-01-14 07:50:53 +00:00
|
|
|
|
|
2026-01-21 15:49:12 +08:00
|
|
|
|
// 验证必填参数 - 手机号或微信号至少一个
|
|
|
|
|
|
if (!phone && !wechat) {
|
|
|
|
|
|
return NextResponse.json({ success: false, message: "请提供手机号或微信号" }, { status: 400 })
|
2026-01-14 07:50:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 验证类型
|
2026-01-21 15:49:12 +08:00
|
|
|
|
if (!["team", "investor", "mentor", "partner"].includes(type)) {
|
2026-01-14 07:50:53 +00:00
|
|
|
|
return NextResponse.json({ success: false, message: "无效的加入类型" }, { status: 400 })
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-21 15:49:12 +08:00
|
|
|
|
// 生成时间戳(秒级)
|
2026-01-14 07:50:53 +00:00
|
|
|
|
const timestamp = Math.floor(Date.now() / 1000)
|
|
|
|
|
|
|
2026-01-21 15:49:12 +08:00
|
|
|
|
// 构建请求参数(不包含sign)
|
|
|
|
|
|
const requestParams: Record<string, any> = {
|
|
|
|
|
|
timestamp,
|
|
|
|
|
|
source: `创业实验-${sourceMap[type]}`,
|
|
|
|
|
|
tags: tagsMap[type],
|
|
|
|
|
|
siteTags: "创业实验APP",
|
|
|
|
|
|
remark: remark || `用户通过创业实验APP申请${sourceMap[type]}`,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 添加可选字段
|
|
|
|
|
|
if (phone) requestParams.phone = phone
|
|
|
|
|
|
if (wechat) requestParams.wechatId = wechat
|
|
|
|
|
|
if (name) requestParams.name = name
|
|
|
|
|
|
|
|
|
|
|
|
// 生成签名
|
|
|
|
|
|
const sign = generateSign(requestParams, CKB_API_KEY)
|
|
|
|
|
|
|
|
|
|
|
|
// 构建最终请求体
|
2026-01-14 07:50:53 +00:00
|
|
|
|
const requestBody = {
|
2026-01-21 15:49:12 +08:00
|
|
|
|
...requestParams,
|
2026-01-14 07:50:53 +00:00
|
|
|
|
apiKey: CKB_API_KEY,
|
|
|
|
|
|
sign,
|
2026-01-21 15:49:12 +08:00
|
|
|
|
portrait: {
|
|
|
|
|
|
type: 4, // 互动行为
|
|
|
|
|
|
source: 0, // 本站
|
|
|
|
|
|
sourceData: {
|
|
|
|
|
|
joinType: type,
|
|
|
|
|
|
joinLabel: sourceMap[type],
|
|
|
|
|
|
userId: userId || "",
|
|
|
|
|
|
device: "webapp",
|
|
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
|
|
},
|
|
|
|
|
|
remark: `${sourceMap[type]}申请`,
|
|
|
|
|
|
uniqueId: `soul_${phone || wechat}_${timestamp}`,
|
|
|
|
|
|
},
|
2026-01-14 07:50:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 调用存客宝API
|
|
|
|
|
|
const response = await fetch(CKB_API_URL, {
|
|
|
|
|
|
method: "POST",
|
|
|
|
|
|
headers: {
|
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
|
},
|
|
|
|
|
|
body: JSON.stringify(requestBody),
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
const result = await response.json()
|
|
|
|
|
|
|
2026-01-21 15:49:12 +08:00
|
|
|
|
if (result.code === 200) {
|
2026-01-14 07:50:53 +00:00
|
|
|
|
return NextResponse.json({
|
|
|
|
|
|
success: true,
|
2026-01-21 15:49:12 +08:00
|
|
|
|
message: result.message === "已存在" ? "您已加入,我们会尽快联系您" : `成功加入${sourceMap[type]}`,
|
2026-01-14 07:50:53 +00:00
|
|
|
|
data: result.data,
|
|
|
|
|
|
})
|
|
|
|
|
|
} else {
|
2026-01-21 15:49:12 +08:00
|
|
|
|
console.error("CKB API Error:", result)
|
2026-01-14 07:50:53 +00:00
|
|
|
|
return NextResponse.json({
|
|
|
|
|
|
success: false,
|
|
|
|
|
|
message: result.message || "加入失败,请稍后重试",
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error("存客宝API调用失败:", error)
|
|
|
|
|
|
return NextResponse.json({ success: false, message: "服务器错误,请稍后重试" }, { status: 500 })
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|