Lottery.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\Service\lottery\LotteryService;
  5. use app\common\Service\lottery\LotteryChanceService;
  6. use app\common\Service\lottery\LotteryRecordService;
  7. use app\common\Service\lottery\LotteryActivityService;
  8. use app\common\model\lottery\LotteryActivity;
  9. use app\common\model\lottery\LotteryPrize;
  10. use app\common\model\lottery\LotteryDrawRecord;
  11. use app\common\model\lottery\LotteryWinRecord;
  12. use app\common\library\Auth;
  13. use app\api\validate\Lottery as LotteryValidate;
  14. use app\common\Enum\ActivityEnum;
  15. use app\common\Enum\LotteryEnum;
  16. use think\Exception;
  17. /**
  18. * 抽奖API控制器
  19. */
  20. class Lottery extends Api
  21. {
  22. protected $noNeedLogin = ['activityList', 'activityDetail', 'getPrizes'];
  23. protected $noNeedRight = ['*'];
  24. /**
  25. * 获取抽奖活动列表
  26. */
  27. public function activityList()
  28. {
  29. // 验证参数
  30. $validate = new LotteryValidate();
  31. $params = $this->request->param();
  32. if (!$validate->scene('activityList')->check($params)) {
  33. $this->error($validate->getError());
  34. }
  35. $page = $params['page'] ?? 1;
  36. $limit = $params['pageSize'] ?? 10;
  37. $status = $params['status'] ?? ActivityEnum::ACTIVITY_STATUS_ONGOING; // 默认只返回进行中的活动
  38. $where = [];
  39. if ($status !== '') {
  40. $where['status'] = $status;
  41. }
  42. // 只返回进行中且在时间范围内的活动
  43. $now = time();
  44. $where['start_time'] = ['<=', $now];
  45. $where['end_time'] = ['>=', $now];
  46. $activities = LotteryActivity::where($where)
  47. ->field('id,name,description,cover_image,type,status,start_time,end_time,lottery_type,guide_image,guide_text,intro_content')
  48. ->order('createtime desc')
  49. ->page($page, $limit)
  50. ->select();
  51. $list = [];
  52. foreach ($activities as $activity) {
  53. $item = $activity->toArray();
  54. // 获取奖品信息
  55. $prizes = LotteryPrize::where('activity_id', $activity->id)
  56. ->where('status', 1)
  57. ->where('type', '>', 1) // 排除未中奖类型
  58. ->field('id,name,type,image,probability')
  59. ->order('sort_order asc')
  60. ->select();
  61. $item['prizes'] = $prizes;
  62. // 统计信息
  63. $item['total_participants'] = LotteryChanceService::getActivityParticipants($activity->id);
  64. $list[] = $item;
  65. }
  66. $this->success('获取成功', [
  67. 'list' => $list,
  68. 'total' => LotteryActivity::where($where)->count()
  69. ]);
  70. }
  71. /**
  72. * 获取活动详情
  73. */
  74. public function activityDetail()
  75. {
  76. // 验证参数
  77. $validate = new LotteryValidate();
  78. if (!$validate->scene('activityDetail')->check($this->request->param())) {
  79. $this->error($validate->getError());
  80. }
  81. $activityId = $this->request->param('activity_id/d');
  82. $activity = LotteryActivity::find($activityId);
  83. if (!$activity) {
  84. $this->error('活动不存在');
  85. }
  86. $detail = $activity->toArray();
  87. // 获取奖品列表
  88. $prizes = LotteryPrize::where('activity_id', $activityId)
  89. ->where('status', 1)
  90. ->field('id,name,type,image,description,probability,total_stock,remain_stock,win_prompt,sort_order,unlock_people_num')
  91. ->order('sort_order asc')
  92. ->select();
  93. // 检查奖品是否已解锁(如果开启按人数解锁)
  94. if ($activity->unlock_by_people) {
  95. $currentPeopleCount = LotteryChanceService::getActivityParticipants($activityId);
  96. foreach ($prizes as &$prize) {
  97. $prize['is_unlocked'] = $prize->isUnlocked($currentPeopleCount);
  98. }
  99. } else {
  100. foreach ($prizes as &$prize) {
  101. $prize['is_unlocked'] = true;
  102. }
  103. }
  104. $detail['prizes'] = $prizes;
  105. // 统计信息
  106. $detail['total_participants'] = LotteryChanceService::getActivityParticipants($activityId);
  107. // 如果用户已登录,返回用户相关信息
  108. if (Auth::instance()->isLogin()) {
  109. $userId = Auth::instance()->id;
  110. $detail['user_chances'] = LotteryService::getUserChances($activityId, $userId);
  111. $detail['user_draw_count'] = LotteryRecordService::getUserDrawCount($activityId, $userId);
  112. $detail['user_win_count'] = LotteryRecordService::getUserWinCount($activityId, $userId);
  113. }
  114. $this->success('获取成功', $detail);
  115. }
  116. /**
  117. * 执行抽奖
  118. */
  119. public function draw()
  120. {
  121. // 验证参数
  122. $validate = new LotteryValidate();
  123. if (!$validate->scene('draw')->check($this->request->param())) {
  124. $this->error($validate->getError());
  125. }
  126. $activityId = $this->request->param('activity_id/d');
  127. $userId = Auth::instance()->id;
  128. if (!$userId) {
  129. $this->error('请先登录');
  130. }
  131. try {
  132. $result = LotteryService::drawLottery($activityId, $userId);
  133. $this->success('抽奖成功', $result);
  134. } catch (Exception $e) {
  135. $this->error($e->getMessage());
  136. }
  137. }
  138. /**
  139. * 获取用户抽奖机会
  140. */
  141. public function getUserChances()
  142. {
  143. // 验证参数
  144. $validate = new LotteryValidate();
  145. if (!$validate->scene('getUserChances')->check($this->request->param())) {
  146. $this->error($validate->getError());
  147. }
  148. $activityId = $this->request->param('activity_id/d');
  149. $userId = Auth::instance()->id;
  150. if (!$userId) {
  151. $this->error('请先登录');
  152. }
  153. $chances = LotteryChanceService::getUserChanceDetail($activityId, $userId);
  154. $this->success('获取成功', $chances);
  155. }
  156. /**
  157. * 获取用户抽奖记录
  158. */
  159. public function getDrawRecords()
  160. {
  161. // 验证参数(activity_id可选)
  162. $params = $this->request->param();
  163. $validate = new LotteryValidate();
  164. // 如果有activity_id参数,则把它映射为activity_id_optional进行验证
  165. $validateParams = $params;
  166. if (isset($params['activity_id'])) {
  167. $validateParams['activity_id_optional'] = $params['activity_id'];
  168. unset($validateParams['activity_id']);
  169. }
  170. if (!$validate->scene('getDrawRecords')->check($validateParams)) {
  171. $this->error($validate->getError());
  172. }
  173. $activityId = $this->request->param('activity_id/d');
  174. $page = $this->request->param('page/d', 1);
  175. $limit = $this->request->param('limit/d', 20);
  176. $userId = $this->auth->id;
  177. $where = ['user_id' => $userId];
  178. if ($activityId) {
  179. $where['activity_id'] = $activityId;
  180. }
  181. $records = LotteryRecordService::getUserDrawRecords($userId, $activityId, $page, $limit);
  182. $list = [];
  183. foreach ($records as $record) {
  184. $item = [
  185. 'id' => $record->id,
  186. 'activity_id' => $record->activity_id,
  187. 'activity_name' => $record->activity->name ?? '',
  188. 'prize_id' => $record->prize_id,
  189. 'prize_name' => $record->prize->name ?? '',
  190. 'prize_type' => $record->prize->type ?? 0,
  191. 'prize_image' => $record->prize->image ?? '',
  192. 'is_win' => $record->is_win,
  193. 'draw_time' => $record->draw_time,
  194. 'trigger_type_text' => $record->trigger_type_text
  195. ];
  196. // 如果中奖,获取中奖记录详情
  197. if ($record->is_win && $record->winRecord) {
  198. $item['win_record'] = [
  199. 'id' => $record->winRecord->id,
  200. 'deliver_status' => $record->winRecord->deliver_status,
  201. 'deliver_status_text' => $record->winRecord->deliver_status_text,
  202. 'deliver_time' => $record->winRecord->deliver_time,
  203. 'exchange_code' => $record->winRecord->exchange_code
  204. ];
  205. }
  206. $list[] = $item;
  207. }
  208. $this->success('获取成功', [
  209. 'list' => $list,
  210. 'total' => LotteryRecordService::getUserDrawCount($activityId, $userId)
  211. ]);
  212. }
  213. /**
  214. * 获取用户中奖记录
  215. */
  216. public function getWinRecords()
  217. {
  218. // 验证参数
  219. $validate = new LotteryValidate();
  220. if (!$validate->scene('getWinRecords')->check($this->request->param())) {
  221. $this->error($validate->getError());
  222. }
  223. $page = $this->request->param('page/d', 1);
  224. $limit = $this->request->param('limit/d', 20);
  225. $userId = $this->auth->id;
  226. $records = LotteryRecordService::getUserWinRecords($userId, $page, $limit);
  227. $list = [];
  228. foreach ($records as $record) {
  229. $item = [
  230. 'id' => $record->id,
  231. 'activity_id' => $record->activity_id,
  232. 'activity_name' => $record->activity->name ?? '',
  233. 'prize_id' => $record->prize_id,
  234. 'prize_name' => $record->prize_name,
  235. 'prize_type' => $record->prize_type,
  236. 'deliver_status' => $record->deliver_status,
  237. 'deliver_status_text' => $record->deliver_status_text,
  238. 'deliver_time' => $record->deliver_time,
  239. 'exchange_code' => $record->exchange_code,
  240. 'createtime' => $record->createtime
  241. ];
  242. // 根据奖品类型返回特定信息
  243. $prizeValue = $record->prize_value_data;
  244. switch ($record->prize_type) {
  245. case LotteryEnum::PRIZE_TYPE_REDPACK:
  246. $item['amount'] = $prizeValue['amount'] ?? 0;
  247. break;
  248. case LotteryEnum::PRIZE_TYPE_COUPON:
  249. $item['coupon_id'] = $prizeValue['coupon_id'] ?? 0;
  250. break;
  251. case LotteryEnum::PRIZE_TYPE_GOODS:
  252. $item['goods_id'] = $prizeValue['goods_id'] ?? 0;
  253. $item['goods_sku_id'] = $prizeValue['goods_sku_id'] ?? 0;
  254. break;
  255. }
  256. $list[] = $item;
  257. }
  258. $this->success('获取成功', [
  259. 'list' => $list,
  260. 'total' => LotteryRecordService::getUserWinRecords($userId, 1, 1)->count()
  261. ]);
  262. }
  263. /**
  264. * 设置中奖记录收货地址
  265. */
  266. public function setWinRecordAddress()
  267. {
  268. // 验证参数
  269. $validate = new LotteryValidate();
  270. if (!$validate->scene('setWinRecordAddress')->check($this->request->param())) {
  271. $this->error($validate->getError());
  272. }
  273. $winRecordId = $this->request->param('win_record_id/d');
  274. $receiverName = $this->request->param('receiver_name');
  275. $receiverMobile = $this->request->param('receiver_mobile');
  276. $receiverAddress = $this->request->param('receiver_address');
  277. $userId = $this->auth->id;
  278. $winRecord = LotteryWinRecord::where('id', $winRecordId)
  279. ->where('user_id', $userId)
  280. ->find();
  281. if (!$winRecord) {
  282. $this->error('中奖记录不存在');
  283. }
  284. if ($winRecord->deliver_status != LotteryEnum::DELIVER_STATUS_PENDING) {
  285. $this->error('该奖品已处理,无法修改地址');
  286. }
  287. try {
  288. $winRecord->setDeliveryAddress($receiverName, $receiverMobile, $receiverAddress);
  289. $this->success('设置成功');
  290. } catch (Exception $e) {
  291. $this->error('设置失败:' . $e->getMessage());
  292. }
  293. }
  294. /**
  295. * 获取活动排行榜(中奖次数)
  296. */
  297. public function getRanking()
  298. {
  299. // 验证参数
  300. $validate = new LotteryValidate();
  301. if (!$validate->scene('getRanking')->check($this->request->param())) {
  302. $this->error($validate->getError());
  303. }
  304. $activityId = $this->request->param('activity_id/d');
  305. $page = $this->request->param('page/d', 1);
  306. $limit = $this->request->param('limit/d', 50);
  307. // 统计用户中奖次数
  308. $ranking = LotteryRecordService::getActivityDrawRanking($activityId, $limit);
  309. $list = [];
  310. $rank = ($page - 1) * $limit + 1;
  311. foreach ($ranking as $item) {
  312. $list[] = [
  313. 'rank' => $rank++,
  314. 'user_id' => $item->user_id,
  315. 'nickname' => $item->user->nickname ?? '匿名用户',
  316. 'avatar' => $item->user->avatar ?? '',
  317. 'win_count' => $item->win_count ?? 0
  318. ];
  319. }
  320. $this->success('获取成功', $list);
  321. }
  322. /**
  323. * 获取奖品列表
  324. */
  325. public function getPrizes()
  326. {
  327. // 验证参数
  328. $validate = new LotteryValidate();
  329. $params = $this->request->param();
  330. if (!$validate->scene('getPrizes')->check($params)) {
  331. $this->error($validate->getError());
  332. }
  333. $activityId = $params['lottery_id'] ?? 0;
  334. $page = $params['page'] ?? 1;
  335. $pageSize = $params['pageSize'] ?? 10;
  336. $type = $params['type'] ?? null; // 奖品类型筛选,支持逗号分隔字符串
  337. // 调用服务层方法
  338. $paginate = LotteryActivityService::getPrizes($activityId, $page, $pageSize, $type);
  339. // 处理图片 地址
  340. foreach ($paginate as $item) {
  341. $item['image'] = $item['image'] ? cdnurl($item['image']) : '';
  342. }
  343. $this->success('获取成功', $paginate);
  344. }
  345. }