|
@@ -1,85 +1,357 @@
|
|
|
<?php
|
|
|
|
|
|
-
|
|
|
namespace app\api\controller;
|
|
|
|
|
|
-
|
|
|
use app\common\controller\Api;
|
|
|
-use think\Cache;
|
|
|
-use think\Request;
|
|
|
-use app\services\lottery\LuckLotteryRecordServices;
|
|
|
-use app\services\lottery\LuckLotteryServices;
|
|
|
-
|
|
|
+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;
|
|
|
|
|
|
-class LuckLottery extends Api
|
|
|
+/**
|
|
|
+ * 抽奖API控制器
|
|
|
+ */
|
|
|
+class Lottery extends Api
|
|
|
{
|
|
|
+ protected $noNeedLogin = ['activityList', 'activityDetail'];
|
|
|
protected $noNeedRight = ['*'];
|
|
|
- protected $services;
|
|
|
|
|
|
- public function __construct(LuckLotteryServices $services)
|
|
|
+ /**
|
|
|
+ * 获取抽奖活动列表
|
|
|
+ */
|
|
|
+ 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()
|
|
|
{
|
|
|
- parent::__construct();
|
|
|
- $this->services = $services;
|
|
|
+ $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);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
- * 抽奖活动信息
|
|
|
- * @param Request $request
|
|
|
+ * 获取用户抽奖记录
|
|
|
*/
|
|
|
- public function LotteryInfo(Request $request)
|
|
|
+ public function getDrawRecords()
|
|
|
{
|
|
|
- $id = $request->get('id','1');
|
|
|
- if (intval($id) <= 0) return $this->error('请上传抽奖ID');
|
|
|
- $lottery = $this->services->getLottery($id, '*', ['prize'], true);
|
|
|
- if (!$lottery) {
|
|
|
- $this->error('未获取到抽奖活动');
|
|
|
- }
|
|
|
- $lottery = $lottery->toArray();
|
|
|
- $lotteryData = ['lottery' => $lottery];
|
|
|
- $lotteryData['lottery_num'] = $this->auth->lottery_num ?? 0;
|
|
|
- $this->success('获取成功', $lotteryData);
|
|
|
+ $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()
|
|
|
+ ]);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 参与抽奖
|
|
|
- * @param Request $request
|
|
|
- * @return mixed
|
|
|
+ * 获取用户中奖记录
|
|
|
*/
|
|
|
- public function luckLottery(Request $request)
|
|
|
+ public function getWinRecords()
|
|
|
{
|
|
|
- $id = $request->get('id','1');
|
|
|
- if (!$id) {
|
|
|
- $this->error('请上传抽奖ID');
|
|
|
+ $page = $this->request->param('page/d', 1);
|
|
|
+ $limit = $this->request->param('limit/d', 20);
|
|
|
+
|
|
|
+ $userId = Auth::instance()->id;
|
|
|
+ if (!$userId) {
|
|
|
+ $this->error('请先登录');
|
|
|
}
|
|
|
- if ($this->auth->lottery_num <=0){
|
|
|
- $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('请先登录');
|
|
|
}
|
|
|
- $uid =$this->auth->id;
|
|
|
- $key = 'lucklotter_limit_' . $uid;
|
|
|
- if (Cache::get($key)) {
|
|
|
- $this->error('您求的频率太过频繁,请稍后请求!');
|
|
|
+
|
|
|
+ $winRecord = LotteryWinRecord::where('id', $winRecordId)
|
|
|
+ ->where('user_id', $userId)
|
|
|
+ ->find();
|
|
|
+ if (!$winRecord) {
|
|
|
+ $this->error('中奖记录不存在');
|
|
|
}
|
|
|
- Cache::set('lucklotter_limit_' . $uid, $uid, 1);
|
|
|
|
|
|
- $arrUserinfo = $this->auth->getUser()->toArray();
|
|
|
- list($nError,$arrData) = $this->services->luckLottery($arrUserinfo, $id);
|
|
|
- if ($nError === 1 ) {
|
|
|
- $this->error($arrData);
|
|
|
+ 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());
|
|
|
}
|
|
|
- $this->success('', $arrData);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取抽奖记录
|
|
|
- * @param Request $request
|
|
|
- * @return mixed
|
|
|
+ * 获取活动排行榜(中奖次数)
|
|
|
*/
|
|
|
- public function lotteryRecord(Request $request)
|
|
|
+ public function getRanking()
|
|
|
{
|
|
|
- $uid = $this->auth->id;
|
|
|
- $pageSize = (int)$this->request->param('pageSize', 10);
|
|
|
- $page = (int)$this->request->param('page', 1);
|
|
|
- $lotteryRecordServices = new LuckLotteryRecordServices();
|
|
|
- $arrData = $lotteryRecordServices->getRecord($uid,$page,$pageSize);
|
|
|
- $this->success('', $arrData);
|
|
|
+ $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);
|
|
|
}
|
|
|
}
|