Lottery.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Cache;
  5. use think\Request;
  6. use app\services\lottery\LuckLotteryRecordServices;
  7. use app\services\lottery\LuckLotteryServices;
  8. class LuckLottery extends Api
  9. {
  10. protected $noNeedRight = ['*'];
  11. protected $services;
  12. public function __construct(LuckLotteryServices $services)
  13. {
  14. parent::__construct();
  15. $this->services = $services;
  16. }
  17. /**
  18. * 抽奖活动信息
  19. * @param Request $request
  20. */
  21. public function LotteryInfo(Request $request)
  22. {
  23. $id = $request->get('id','1');
  24. if (intval($id) <= 0) return $this->error('请上传抽奖ID');
  25. $lottery = $this->services->getLottery($id, '*', ['prize'], true);
  26. if (!$lottery) {
  27. $this->error('未获取到抽奖活动');
  28. }
  29. $lottery = $lottery->toArray();
  30. $lotteryData = ['lottery' => $lottery];
  31. $lotteryData['lottery_num'] = $this->auth->lottery_num ?? 0;
  32. $this->success('获取成功', $lotteryData);
  33. }
  34. /**
  35. * 参与抽奖
  36. * @param Request $request
  37. * @return mixed
  38. */
  39. public function luckLottery(Request $request)
  40. {
  41. $id = $request->get('id','1');
  42. if (!$id) {
  43. $this->error('请上传抽奖ID');
  44. }
  45. if ($this->auth->lottery_num <=0){
  46. $this->error('抽奖次数不足');
  47. }
  48. $uid =$this->auth->id;
  49. $key = 'lucklotter_limit_' . $uid;
  50. if (Cache::get($key)) {
  51. $this->error('您求的频率太过频繁,请稍后请求!');
  52. }
  53. Cache::set('lucklotter_limit_' . $uid, $uid, 1);
  54. $arrUserinfo = $this->auth->getUser()->toArray();
  55. list($nError,$arrData) = $this->services->luckLottery($arrUserinfo, $id);
  56. if ($nError === 1 ) {
  57. $this->error($arrData);
  58. }
  59. $this->success('', $arrData);
  60. }
  61. /**
  62. * 获取抽奖记录
  63. * @param Request $request
  64. * @return mixed
  65. */
  66. public function lotteryRecord(Request $request)
  67. {
  68. $uid = $this->auth->id;
  69. $pageSize = (int)$this->request->param('pageSize', 10);
  70. $page = (int)$this->request->param('page', 1);
  71. $lotteryRecordServices = new LuckLotteryRecordServices();
  72. $arrData = $lotteryRecordServices->getRecord($uid,$page,$pageSize);
  73. $this->success('', $arrData);
  74. }
  75. }