123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\Service\LotteryService;
- use app\common\Service\LotteryChanceService;
- use app\common\model\lottery\LotteryActivity;
- use app\common\model\lottery\LotteryPrize;
- use app\common\model\lottery\LotteryDrawRecord;
- use app\common\model\lottery\LotteryWinRecord;
- use app\common\model\lottery\LotteryUserChance;
- use app\common\library\Auth;
- use think\Exception;
- /**
- * 抽奖API控制器
- */
- class Lottery extends Api
- {
- protected $noNeedLogin = ['activityList', 'activityDetail'];
- protected $noNeedRight = ['*'];
- /**
- * 获取抽奖活动列表
- */
- public function activityList()
- {
- $page = $this->request->param('page/d', 1);
- $limit = $this->request->param('limit/d', 10);
- $status = $this->request->param('status/d', 1); // 默认只返回进行中的活动
- $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'] = LotteryUserChance::where('activity_id', $activity->id)->count();
-
- $list[] = $item;
- }
- $this->success('获取成功', [
- 'list' => $list,
- 'total' => LotteryActivity::where($where)->count()
- ]);
- }
- /**
- * 获取活动详情
- */
- public function activityDetail()
- {
- $activityId = $this->request->param('activity_id/d');
- if (!$activityId) {
- $this->error('参数错误');
- }
- $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 = LotteryUserChance::where('activity_id', $activityId)->count();
- 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'] = LotteryUserChance::where('activity_id', $activityId)->count();
- // 如果用户已登录,返回用户相关信息
- if (Auth::instance()->isLogin()) {
- $userId = Auth::instance()->id;
- $detail['user_chances'] = LotteryService::getUserChances($activityId, $userId);
- $detail['user_draw_count'] = LotteryDrawRecord::getUserDrawCount($activityId, $userId);
- $detail['user_win_count'] = LotteryDrawRecord::getUserWinCount($activityId, $userId);
- }
- $this->success('获取成功', $detail);
- }
- /**
- * 执行抽奖
- */
- public function draw()
- {
- $activityId = $this->request->param('activity_id/d');
- if (!$activityId) {
- $this->error('参数错误');
- }
- $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()
- {
- $activityId = $this->request->param('activity_id/d');
- if (!$activityId) {
- $this->error('参数错误');
- }
- $userId = Auth::instance()->id;
- if (!$userId) {
- $this->error('请先登录');
- }
- $chances = LotteryChanceService::getUserChanceDetail($activityId, $userId);
- $this->success('获取成功', $chances);
- }
- /**
- * 获取用户抽奖记录
- */
- public function getDrawRecords()
- {
- $activityId = $this->request->param('activity_id/d');
- $page = $this->request->param('page/d', 1);
- $limit = $this->request->param('limit/d', 20);
- $userId = Auth::instance()->id;
- if (!$userId) {
- $this->error('请先登录');
- }
- $where = ['user_id' => $userId];
- if ($activityId) {
- $where['activity_id'] = $activityId;
- }
- $records = LotteryDrawRecord::where($where)
- ->with(['activity', 'prize'])
- ->order('createtime desc')
- ->page($page, $limit)
- ->select();
- $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' => LotteryDrawRecord::where($where)->count()
- ]);
- }
- /**
- * 获取用户中奖记录
- */
- public function getWinRecords()
- {
- $page = $this->request->param('page/d', 1);
- $limit = $this->request->param('limit/d', 20);
- $userId = Auth::instance()->id;
- if (!$userId) {
- $this->error('请先登录');
- }
- $records = LotteryWinRecord::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 LotteryPrize::TYPE_RED_PACKET:
- $item['amount'] = $prizeValue['amount'] ?? 0;
- break;
- case LotteryPrize::TYPE_COUPON:
- $item['coupon_id'] = $prizeValue['coupon_id'] ?? 0;
- break;
- case LotteryPrize::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' => LotteryWinRecord::where('user_id', $userId)->count()
- ]);
- }
- /**
- * 设置中奖记录收货地址
- */
- public function setWinRecordAddress()
- {
- $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');
- if (!$winRecordId || !$receiverName || !$receiverMobile || !$receiverAddress) {
- $this->error('参数不完整');
- }
- $userId = Auth::instance()->id;
- if (!$userId) {
- $this->error('请先登录');
- }
- $winRecord = LotteryWinRecord::where('id', $winRecordId)
- ->where('user_id', $userId)
- ->find();
- if (!$winRecord) {
- $this->error('中奖记录不存在');
- }
- if ($winRecord->deliver_status != LotteryWinRecord::DELIVER_STATUS_PENDING) {
- $this->error('该奖品已处理,无法修改地址');
- }
- try {
- $winRecord->setDeliveryAddress($receiverName, $receiverMobile, $receiverAddress);
- $this->success('设置成功');
- } catch (Exception $e) {
- $this->error('设置失败:' . $e->getMessage());
- }
- }
- /**
- * 获取活动排行榜(中奖次数)
- */
- public function getRanking()
- {
- $activityId = $this->request->param('activity_id/d');
- $page = $this->request->param('page/d', 1);
- $limit = $this->request->param('limit/d', 50);
- if (!$activityId) {
- $this->error('参数错误');
- }
- // 统计用户中奖次数
- $ranking = LotteryWinRecord::where('activity_id', $activityId)
- ->field('user_id, count(*) as win_count')
- ->with('user')
- ->group('user_id')
- ->order('win_count desc')
- ->page($page, $limit)
- ->select();
- $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
- ];
- }
- $this->success('获取成功', $list);
- }
- }
|