## 海报功能 1. 10条随机朋友圈文案(基于书内容) 2. 二维码已带用户ID ## 找伙伴 1. 新增 /api/match/users API 2. 只匹配数据库中的真实用户 3. 排除自己,筛选有头像和联系方式的用户
87 lines
2.5 KiB
TypeScript
87 lines
2.5 KiB
TypeScript
/**
|
|
* 匹配用户API
|
|
* 从数据库中查询用户进行匹配
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { query } from '@/lib/db'
|
|
|
|
/**
|
|
* POST - 获取匹配的用户
|
|
*/
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
const { userId, matchType } = body
|
|
|
|
if (!userId) {
|
|
return NextResponse.json({ success: false, message: '缺少用户ID' }, { status: 400 })
|
|
}
|
|
|
|
// 从数据库查询其他用户(排除自己)
|
|
// 筛选条件:有头像、有昵称、有微信号或手机号
|
|
const users = await query(`
|
|
SELECT
|
|
id,
|
|
nickname,
|
|
avatar,
|
|
wechat as wechatId,
|
|
phone,
|
|
introduction,
|
|
created_at
|
|
FROM users
|
|
WHERE id != ?
|
|
AND nickname IS NOT NULL
|
|
AND nickname != ''
|
|
AND (wechat IS NOT NULL OR phone IS NOT NULL)
|
|
AND (avatar IS NOT NULL AND avatar != '')
|
|
ORDER BY RAND()
|
|
LIMIT 10
|
|
`, [userId]) as any[]
|
|
|
|
if (!users || users.length === 0) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '暂无匹配用户',
|
|
data: null
|
|
})
|
|
}
|
|
|
|
// 随机选择一个用户
|
|
const randomUser = users[Math.floor(Math.random() * users.length)]
|
|
|
|
// 构建匹配结果
|
|
const matchResult = {
|
|
id: randomUser.id,
|
|
nickname: randomUser.nickname || '创业者',
|
|
avatar: randomUser.avatar || 'https://picsum.photos/200/200?random=' + Date.now(),
|
|
wechat: randomUser.wechatId || '',
|
|
phone: randomUser.phone ? randomUser.phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') : '',
|
|
introduction: randomUser.introduction || '来自Soul创业派对的伙伴',
|
|
tags: ['创业者', '私域运营', matchType === 'partner' ? '创业合伙' :
|
|
matchType === 'investor' ? '资源对接' :
|
|
matchType === 'mentor' ? '导师顾问' : '团队招募'],
|
|
matchScore: Math.floor(Math.random() * 20) + 80,
|
|
commonInterests: [
|
|
{ icon: '📚', text: '都在读《创业派对》' },
|
|
{ icon: '💼', text: '对创业感兴趣' },
|
|
{ icon: '🎯', text: '相似的发展方向' }
|
|
]
|
|
}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
data: matchResult,
|
|
totalUsers: users.length
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error('[Match Users] Error:', error)
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '匹配失败',
|
|
error: String(error)
|
|
}, { status: 500 })
|
|
}
|
|
}
|