本系统是一个基于消费行为的抽奖营销活动系统,支持多种奖品类型、灵活的参与条件和开奖方式。通过用户购买商品、充值等行为自动获得抽奖机会,提高用户参与度和复购率。
系统包含以下核心数据表:
请先执行数据库建表脚本创建相关表结构。
/api/lottery/
接口地址: GET /api/lottery/activityList
请求参数:
{
"page": 1, // 页码,默认1
"limit": 10, // 每页数量,默认10
"status": 1 // 活动状态,默认1(进行中)
}
返回示例:
{
"code": 1,
"msg": "获取成功",
"data": {
"list": [
{
"id": 1,
"name": "新用户专享抽奖",
"description": "新用户购买任意商品即可参与抽奖",
"cover_image": "/uploads/lottery/cover1.jpg",
"status": 1,
"lottery_type": 1,
"prizes": [
{
"id": 1,
"name": "iPhone 15",
"type": 2,
"image": "/uploads/prize/iphone15.jpg",
"probability": 0.01
}
],
"total_participants": 1580
}
],
"total": 5
}
}
接口地址: GET /api/lottery/activityDetail
请求参数:
{
"activity_id": 1 // 活动ID,必填
}
返回示例:
{
"code": 1,
"msg": "获取成功",
"data": {
"id": 1,
"name": "新用户专享抽奖",
"description": "活动描述",
"start_time": 1703980800,
"end_time": 1704067200,
"lottery_type": 1,
"person_limit_num": 3,
"prizes": [
{
"id": 1,
"name": "iPhone 15",
"type": 2,
"probability": 0.01,
"remain_stock": 10,
"is_unlocked": true
}
],
"user_chances": {
"total_chances": 5,
"used_chances": 2,
"remain_chances": 3
},
"user_draw_count": 2,
"user_win_count": 1
}
}
接口地址: POST /api/lottery/draw
请求参数:
{
"activity_id": 1 // 活动ID,必填
}
返回示例:
{
"code": 1,
"msg": "抽奖成功",
"data": {
"draw_id": 12345,
"is_win": 1,
"prize": {
"id": 2,
"name": "10元优惠券",
"type": 3,
"type_text": "优惠券",
"image": "/uploads/coupon.jpg",
"win_prompt": "恭喜获得10元优惠券!"
},
"win_record_id": 6789,
"deliver_status": 1,
"draw_time": 1703980800
}
}
接口地址: GET /api/lottery/getUserChances
请求参数:
{
"activity_id": 1 // 活动ID,必填
}
返回示例:
{
"code": 1,
"msg": "获取成功",
"data": {
"total_chances": 5,
"used_chances": 2,
"remain_chances": 3,
"last_get_time": 1703980800,
"last_use_time": 1703981000,
"get_detail": [
{
"trigger_type": "order",
"order_id": 1001,
"order_amount": 299.00,
"granted_time": 1703980800
}
]
}
}
接口地址: GET /api/lottery/getDrawRecords
请求参数:
{
"activity_id": 1, // 活动ID,可选
"page": 1, // 页码,默认1
"limit": 20 // 每页数量,默认20
}
返回示例:
{
"code": 1,
"msg": "获取成功",
"data": {
"list": [
{
"id": 12345,
"activity_id": 1,
"activity_name": "新用户专享抽奖",
"prize_name": "10元优惠券",
"is_win": 1,
"draw_time": 1703980800,
"win_record": {
"id": 6789,
"deliver_status": 1,
"deliver_status_text": "已发放"
}
}
],
"total": 10
}
}
接口地址: GET /api/lottery/getWinRecords
请求参数:
{
"page": 1, // 页码,默认1
"limit": 20 // 每页数量,默认20
}
接口地址: POST /api/lottery/setWinRecordAddress
请求参数:
{
"win_record_id": 6789,
"receiver_name": "张三",
"receiver_mobile": "13800138000",
"receiver_address": "北京市朝阳区某某小区1号楼101室"
}
接口地址: GET /api/lottery/getRanking
请求参数:
{
"activity_id": 1, // 活动ID,必填
"page": 1, // 页码,默认1
"limit": 50 // 每页数量,默认50
}
use app\common\Service\LotteryService;
// 执行抽奖
$result = LotteryService::drawLottery($activityId, $userId, $triggerType, $triggerOrderId, $triggerAmount);
// 获取用户抽奖机会
$chances = LotteryService::getUserChances($activityId, $userId);
use app\common\Service\LotteryChanceService;
// 订单完成后分发抽奖机会
$orderInfo = [
'id' => $orderId,
'user_id' => $userId,
'total_amount' => 299.00,
'goods' => [
['goods_id' => 1, 'goods_sku_id' => 1, 'nums' => 2]
]
];
$grantedChances = LotteryChanceService::checkAndGrantChanceForOrder($orderInfo, $userId);
// 充值完成后分发抽奖机会
$rechargeInfo = [
'id' => $rechargeId,
'user_id' => $userId,
'amount' => 100.00
];
$grantedChances = LotteryChanceService::checkAndGrantChanceForRecharge($rechargeInfo, $userId);
// 手动给用户增加抽奖机会(管理员操作)
LotteryChanceService::manualGrantChance($activityId, $userId, 3, '客服补偿', $adminId);
系统提供了订单完成钩子,可以在订单支付完成或充值完成时自动检查并分发抽奖机会。
// application/tags.php 或相应的钩子配置文件
return [
// 订单支付完成钩子
'order_paid' => [
'app\\common\\behavior\\LotteryOrderHook'
],
// 充值完成钩子
'recharge_paid' => [
'app\\common\\behavior\\LotteryOrderHook'
]
];
// 在订单支付完成的地方调用
\think\Hook::listen('order_paid', ['order' => $orderInfo]);
// 在充值完成的地方调用
\think\Hook::listen('recharge_paid', ['recharge' => $rechargeInfo]);
系统采用基于概率权重的抽奖算法:
// 购买指定商品
$condition = [
'type' => 1, // 购买指定商品
'goods_ids' => [1, 2, 3], // 商品ID列表
'goods_rule' => 1, // 1=指定商品参与,2=指定商品不可参与
'reward_times' => 1, // 奖励抽奖次数
'is_repeatable' => 0 // 是否可重复获得
];
// 单笔订单消费满额
$condition = [
'type' => 2, // 单笔订单消费满额
'condition_value' => 199.00, // 满额金额
'reward_times' => 1, // 奖励抽奖次数
'is_repeatable' => 1 // 可重复获得
];
可以扩展分享中奖结果到社交媒体的功能,增加活动传播度。
对于按时间开奖的活动,可以使用定时任务来处理开奖逻辑。
基于抽奖数据进行用户行为分析,优化营销策略。
支持微信小程序、APP、H5 等多个渠道的抽奖功能。