request->param(); if (!$validate->scene('activityList')->check($params)) { $this->error($validate->getError()); } $page = $params['page'] ?? 1; $limit = $params['pageSize'] ?? 10; $status = $params['status'] ?? ActivityEnum::ACTIVITY_STATUS_ONGOING; // 默认只返回进行中的活动 $where = []; if ($status !== '') { $where['status'] = $status; } // 只返回进行中且在时间范围内的活动 $now = time(); $where['start_time'] = ['<=', $now]; $where['end_time'] = ['>=', $now]; $activities = LotteryActivity::where($where) ->field('id,name,description,cover_image,type,status,start_time,end_time,lottery_type,guide_image,guide_text,intro_content') ->order('createtime desc') ->page($page, $limit) ->select(); $list = []; foreach ($activities as $activity) { $item = $activity->toArray(); // 获取奖品信息 $prizes = LotteryPrize::where('activity_id', $activity->id) ->where('status', 1) ->where('type', '>', 1) // 排除未中奖类型 ->field('id,name,type,image,probability') ->order('sort_order asc') ->select(); $item['prizes'] = $prizes; // 统计信息 $item['total_participants'] = LotteryChanceService::getActivityParticipants($activity->id); $list[] = $item; } $this->success('获取成功', [ 'list' => $list, 'total' => LotteryActivity::where($where)->count() ]); } /** * 获取活动详情 */ public function activityDetail() { // 验证参数 $validate = new LotteryValidate(); if (!$validate->scene('activityDetail')->check($this->request->param())) { $this->error($validate->getError()); } $activityId = $this->request->param('activity_id/d'); $activity = LotteryActivity::find($activityId); if (!$activity) { $this->error('活动不存在'); } $detail = $activity->toArray(); // 获取奖品列表 $prizes = LotteryPrize::where('activity_id', $activityId) ->where('status', 1) ->field('id,name,type,image,description,probability,total_stock,remain_stock,win_prompt,sort_order,unlock_people_num') ->order('sort_order asc') ->select(); // 检查奖品是否已解锁(如果开启按人数解锁) if ($activity->unlock_by_people) { $currentPeopleCount = LotteryChanceService::getActivityParticipants($activityId); foreach ($prizes as &$prize) { $prize['is_unlocked'] = $prize->isUnlocked($currentPeopleCount); } } else { foreach ($prizes as &$prize) { $prize['is_unlocked'] = true; } } $detail['prizes'] = $prizes; // 统计信息 $detail['total_participants'] = LotteryChanceService::getActivityParticipants($activityId); // 如果用户已登录,返回用户相关信息 if (Auth::instance()->isLogin()) { $userId = Auth::instance()->id; $detail['user_chances'] = LotteryService::getUserChances($activityId, $userId); $detail['user_draw_count'] = LotteryRecordService::getUserDrawCount($activityId, $userId); $detail['user_win_count'] = LotteryRecordService::getUserWinCount($activityId, $userId); } $this->success('获取成功', $detail); } /** * 执行抽奖 */ public function draw() { // 验证参数 $validate = new LotteryValidate(); if (!$validate->scene('draw')->check($this->request->param())) { $this->error($validate->getError()); } $activityId = $this->request->param('activity_id/d'); $userId = Auth::instance()->id; if (!$userId) { $this->error('请先登录'); } try { $result = LotteryService::drawLottery($activityId, $userId); $this->success('抽奖成功', $result); } catch (Exception $e) { $this->error($e->getMessage()); } } /** * 获取用户抽奖机会 */ public function getUserChances() { // 验证参数 $validate = new LotteryValidate(); if (!$validate->scene('getUserChances')->check($this->request->param())) { $this->error($validate->getError()); } $activityId = $this->request->param('activity_id/d'); $userId = Auth::instance()->id; if (!$userId) { $this->error('请先登录'); } $chances = LotteryChanceService::getUserChanceDetail($activityId, $userId); $this->success('获取成功', $chances); } /** * 获取用户抽奖记录 */ public function getDrawRecords() { // 验证参数(activity_id可选) $params = $this->request->param(); $validate = new LotteryValidate(); // 如果有activity_id参数,则把它映射为activity_id_optional进行验证 $validateParams = $params; if (isset($params['activity_id'])) { $validateParams['activity_id_optional'] = $params['activity_id']; unset($validateParams['activity_id']); } if (!$validate->scene('getDrawRecords')->check($validateParams)) { $this->error($validate->getError()); } $activityId = $this->request->param('activity_id/d'); $page = $this->request->param('page/d', 1); $limit = $this->request->param('limit/d', 20); $userId = $this->auth->id; $where = ['user_id' => $userId]; if ($activityId) { $where['activity_id'] = $activityId; } $records = LotteryRecordService::getUserDrawRecords($userId, $activityId, $page, $limit); $list = []; foreach ($records as $record) { $item = [ 'id' => $record->id, 'activity_id' => $record->activity_id, 'activity_name' => $record->activity->name ?? '', 'prize_id' => $record->prize_id, 'prize_name' => $record->prize->name ?? '', 'prize_type' => $record->prize->type ?? 0, 'prize_image' => $record->prize->image ?? '', 'is_win' => $record->is_win, 'draw_time' => $record->draw_time, 'trigger_type_text' => $record->trigger_type_text ]; // 如果中奖,获取中奖记录详情 if ($record->is_win && $record->winRecord) { $item['win_record'] = [ 'id' => $record->winRecord->id, 'deliver_status' => $record->winRecord->deliver_status, 'deliver_status_text' => $record->winRecord->deliver_status_text, 'deliver_time' => $record->winRecord->deliver_time, 'exchange_code' => $record->winRecord->exchange_code ]; } $list[] = $item; } $this->success('获取成功', [ 'list' => $list, 'total' => LotteryRecordService::getUserDrawCount($activityId, $userId) ]); } /** * 获取用户中奖记录 */ public function getWinRecords() { // 验证参数 $validate = new LotteryValidate(); if (!$validate->scene('getWinRecords')->check($this->request->param())) { $this->error($validate->getError()); } $page = $this->request->param('page/d', 1); $limit = $this->request->param('limit/d', 20); $userId = $this->auth->id; $records = LotteryRecordService::getUserWinRecords($userId, $page, $limit); $list = []; foreach ($records as $record) { $item = [ 'id' => $record->id, 'activity_id' => $record->activity_id, 'activity_name' => $record->activity->name ?? '', 'prize_id' => $record->prize_id, 'prize_name' => $record->prize_name, 'prize_type' => $record->prize_type, 'deliver_status' => $record->deliver_status, 'deliver_status_text' => $record->deliver_status_text, 'deliver_time' => $record->deliver_time, 'exchange_code' => $record->exchange_code, 'createtime' => $record->createtime ]; // 根据奖品类型返回特定信息 $prizeValue = $record->prize_value_data; switch ($record->prize_type) { case LotteryEnum::PRIZE_TYPE_REDPACK: $item['amount'] = $prizeValue['amount'] ?? 0; break; case LotteryEnum::PRIZE_TYPE_COUPON: $item['coupon_id'] = $prizeValue['coupon_id'] ?? 0; break; case LotteryEnum::PRIZE_TYPE_GOODS: $item['goods_id'] = $prizeValue['goods_id'] ?? 0; $item['goods_sku_id'] = $prizeValue['goods_sku_id'] ?? 0; break; } $list[] = $item; } $this->success('获取成功', [ 'list' => $list, 'total' => LotteryRecordService::getUserWinRecords($userId, 1, 1)->count() ]); } /** * 设置中奖记录收货地址 */ public function setWinRecordAddress() { // 验证参数 $validate = new LotteryValidate(); if (!$validate->scene('setWinRecordAddress')->check($this->request->param())) { $this->error($validate->getError()); } $winRecordId = $this->request->param('win_record_id/d'); $receiverName = $this->request->param('receiver_name'); $receiverMobile = $this->request->param('receiver_mobile'); $receiverAddress = $this->request->param('receiver_address'); $userId = $this->auth->id; $winRecord = LotteryWinRecord::where('id', $winRecordId) ->where('user_id', $userId) ->find(); if (!$winRecord) { $this->error('中奖记录不存在'); } if ($winRecord->deliver_status != LotteryEnum::DELIVER_STATUS_PENDING) { $this->error('该奖品已处理,无法修改地址'); } try { $winRecord->setDeliveryAddress($receiverName, $receiverMobile, $receiverAddress); $this->success('设置成功'); } catch (Exception $e) { $this->error('设置失败:' . $e->getMessage()); } } /** * 获取活动排行榜(中奖次数) */ public function getRanking() { // 验证参数 $validate = new LotteryValidate(); if (!$validate->scene('getRanking')->check($this->request->param())) { $this->error($validate->getError()); } $activityId = $this->request->param('activity_id/d'); $page = $this->request->param('page/d', 1); $limit = $this->request->param('limit/d', 50); // 统计用户中奖次数 $ranking = LotteryRecordService::getActivityDrawRanking($activityId, $limit); $list = []; $rank = ($page - 1) * $limit + 1; foreach ($ranking as $item) { $list[] = [ 'rank' => $rank++, 'user_id' => $item->user_id, 'nickname' => $item->user->nickname ?? '匿名用户', 'avatar' => $item->user->avatar ?? '', 'win_count' => $item->win_count ?? 0 ]; } $this->success('获取成功', $list); } /** * 获取奖品列表 */ public function getPrizes() { // 验证参数 $validate = new LotteryValidate(); $params = $this->request->param(); if (!$validate->scene('getPrizes')->check($params)) { $this->error($validate->getError()); } $activityId = $params['lottery_id'] ?? 0; $page = $params['page'] ?? 1; $pageSize = $params['pageSize'] ?? 10; $type = $params['type'] ?? null; // 奖品类型筛选,支持逗号分隔字符串 // 调用服务层方法 $paginate = LotteryActivityService::getPrizes($activityId, $page, $pageSize, $type); // 处理图片 地址 foreach ($paginate as $item) { $item['image'] = $item['image'] ? cdnurl($item['image']) : ''; } $this->success('获取成功', $paginate); } }