6.8 KiB
6.8 KiB
拼团 API 测试示例
实际数据结构(已验证)
1. 获取队伍列表
接口: GET /api/v1/users/activity/group_buy/list
返回数据:
{
"code": 200,
"message": "Success",
"data": {
"group_buy_list": ["17670726110-5"], // 所有队伍ID
"lack_group_buy_list": ["17670726110-5"], // 未满员队伍ID
"success_group_buy_list": [] // 已满员队伍ID
}
}
数据说明:
group_buy_list: 所有队伍的 ID 列表lack_group_buy_list: 人数不足的队伍 ID(进行中)success_group_buy_list: 已满员的队伍 ID(成功)- 队伍 ID 格式:
时间戳-人数(如17670726110-5表示5人队)
2. 创建队伍
接口: POST /api/v1/admin/activity/group_buy/add_random_group
请求参数:
{
name: "发士大夫",
group_buy_type: 0 // 0=5人队, 1=10人队
}
返回数据:
{
"code": 200,
"message": "Success",
"data": {
"group_buy_id": "17670733070-5",
"name": "发士大夫",
"maxPerson": 5,
"createTime": "2025-12-30T13:41:47.216888773+08:00",
"users": [{
"user_id": 0,
"user_name": "",
"team_leader": true,
"cover": "https://oss.hostidc.net/api-server/static/files/...",
"idc_phone": "",
"idc_uid": ""
}]
}
}
数据说明:
- 创建成功后会自动添加一个团长(伪人)
team_leader: true表示是团长- 返回完整的队伍信息,包括初始成员
3. 获取队伍详情
接口: GET /api/v1/users/activity/group_buy/detail/:id
返回数据: 与创建队伍返回的数据结构相同
在代码中使用
完整流程示例
<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import {
getGroupBuyList,
getGroupBuyDetail,
addRandomGroup,
addRandomUser,
setOrder
} from '@/api/admin/activity.js'
// 1. 获取队伍列表
const fetchList = async () => {
try {
const res = await getGroupBuyList()
console.log('队伍列表:', res)
if (res.code === 200) {
const allIds = res.data.group_buy_list || []
const lackIds = res.data.lack_group_buy_list || []
const successIds = res.data.success_group_buy_list || []
console.log('所有队伍:', allIds)
console.log('未满员:', lackIds)
console.log('已满员:', successIds)
ElMessage.success(`共 ${allIds.length} 个队伍`)
}
} catch (error) {
console.error('获取失败:', error)
ElMessage.error('获取失败')
}
}
// 2. 创建队伍
const createTeam = async () => {
try {
const res = await addRandomGroup('测试队伍', 0) // 0=5人队
console.log('创建结果:', res)
if (res.code === 200) {
const teamId = res.data.group_buy_id
const teamName = res.data.name
const memberCount = res.data.users?.length || 0
ElMessage.success(`创建成功!队伍ID: ${teamId},当前 ${memberCount} 人`)
// 刷新列表
await fetchList()
}
} catch (error) {
console.error('创建失败:', error)
ElMessage.error('创建失败')
}
}
// 3. 获取队伍详情
const getDetail = async (teamId) => {
try {
const res = await getGroupBuyDetail(teamId)
console.log('队伍详情:', res)
if (res.code === 200) {
const team = res.data
console.log('队伍名称:', team.name)
console.log('最大人数:', team.maxPerson)
console.log('当前成员:', team.users)
}
} catch (error) {
console.error('获取详情失败:', error)
}
}
// 4. 添加伪人
const addFakeUser = async (teamId) => {
try {
const res = await addRandomUser(teamId)
console.log('添加伪人结果:', res)
if (res.code === 200) {
ElMessage.success('添加伪人成功')
// 刷新详情
await getDetail(teamId)
}
} catch (error) {
console.error('添加失败:', error)
ElMessage.error('添加失败')
}
}
// 5. 下发订单
const sendOrder = async (teamId) => {
try {
const res = await setOrder(teamId)
console.log('下发订单结果:', res)
if (res.code === 200) {
ElMessage.success('订单下发成功')
}
} catch (error) {
console.error('下发失败:', error)
ElMessage.error('下发失败')
}
}
</script>
测试步骤
步骤 1: 创建队伍
await addRandomGroup('测试5人队', 0)
// 返回: { group_buy_id: "xxx-5", name: "测试5人队", maxPerson: 5, users: [1个团长] }
步骤 2: 查看列表
await getGroupBuyList()
// 返回: { group_buy_list: ["xxx-5"], lack_group_buy_list: ["xxx-5"], success_group_buy_list: [] }
步骤 3: 添加伪人(重复4次,凑满5人)
await addRandomUser("xxx-5") // 第2人
await addRandomUser("xxx-5") // 第3人
await addRandomUser("xxx-5") // 第4人
await addRandomUser("xxx-5") // 第5人
步骤 4: 再次查看列表
await getGroupBuyList()
// 返回: { group_buy_list: ["xxx-5"], lack_group_buy_list: [], success_group_buy_list: ["xxx-5"] }
// 注意: 队伍从 lack_group_buy_list 移到了 success_group_buy_list
步骤 5: 下发订单
await setOrder("xxx-5")
// 为满员队伍下发订单
队伍状态判断逻辑
const getTeamStatus = (teamId, lackList, successList) => {
if (successList.includes(teamId)) {
return 'success' // 已满员
} else if (lackList.includes(teamId)) {
return 'pending' // 进行中(未满员)
} else {
return 'empty' // 空队伍(理论上不会出现)
}
}
队伍类型判断
根据队伍 ID 判断类型:
const getTeamType = (teamId) => {
if (teamId.includes('-5')) {
return { type: 0, maxPerson: 5, typeName: '5人队' }
} else if (teamId.includes('-10')) {
return { type: 1, maxPerson: 10, typeName: '10人队' }
}
return { type: 0, maxPerson: 5, typeName: '未知' }
}
注意事项
- 列表接口只返回 ID:需要调用详情接口获取完整信息
- 队伍 ID 格式:
时间戳-人数,可以从 ID 判断队伍类型 - 状态判断:通过
lack_group_buy_list和success_group_buy_list判断队伍状态 - 创建即有团长:创建队伍时会自动添加一个团长(伪人)
- 满员条件:5人队需要5人,10人队需要10人
- 下发订单:只能对已满员的队伍下发订单
当前页面功能
src/views/activity/GroupBuyActivity.vue 已实现:
✅ 获取队伍列表(显示所有队伍及状态) ✅ 创建随机伪人队伍 ✅ 添加随机伪人到队伍 ✅ 查看队伍成员详情 ✅ 为满员队伍下发订单 ✅ 导出成功队伍信息 ✅ 实时状态更新
所有功能都已根据实际 API 数据结构进行了适配!