LuckLotteryController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace app\api\controller\v2\activity;
  3. use app\Request;
  4. use app\services\activity\lottery\LuckLotteryRecordServices;
  5. use app\services\activity\lottery\LuckLotteryServices;
  6. use app\services\other\QrcodeServices;
  7. use app\services\wechat\WechatServices;
  8. use crmeb\services\CacheService;
  9. class LuckLotteryController
  10. {
  11. protected $services;
  12. public function __construct(LuckLotteryServices $services)
  13. {
  14. $this->services = $services;
  15. }
  16. /**
  17. * 抽奖活动信息
  18. * @param Request $request
  19. * @param $factor
  20. * @return mixed
  21. * @throws \Psr\SimpleCache\InvalidArgumentException
  22. * @throws \think\db\exception\DataNotFoundException
  23. * @throws \think\db\exception\DbException
  24. * @throws \think\db\exception\ModelNotFoundException
  25. */
  26. public function LotteryInfo(Request $request, $factor)
  27. {
  28. if (!$factor) return app('json')->fail(100100);
  29. $lottery = $this->services->getFactorLottery((int)$factor);
  30. if (!$lottery) {
  31. return app('json')->fail(410318);
  32. }
  33. $uid = (int)$request->uid();
  34. $lottery = $lottery->toArray();
  35. $lotteryData = ['lottery' => $lottery];
  36. $this->services->checkoutUserAuth($uid, (int)$lottery['id'], [], $lottery);
  37. $lotteryData['lottery_num'] = $this->services->getLotteryNum($uid, (int)$lottery['id'], [], $lottery);
  38. $all_record = $user_record = [];
  39. if ($lottery['is_all_record'] || $lottery['is_personal_record']) {
  40. /** @var LuckLotteryRecordServices $lotteryRecordServices */
  41. $lotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
  42. if ($lottery['is_all_record']) {
  43. $all_record = $lotteryRecordServices->getWinList(['lottery_id' => $lottery['id']]);
  44. }
  45. if ($lottery['is_personal_record']) {
  46. $user_record = $lotteryRecordServices->getWinList(['lottery_id' => $lottery['id'], 'uid' => $uid]);
  47. }
  48. }
  49. $lotteryData['all_record'] = $all_record;
  50. $lotteryData['user_record'] = $user_record;
  51. return app('json')->success($lotteryData);
  52. }
  53. /**
  54. * 参与抽奖
  55. * @param Request $request
  56. * @return mixed
  57. */
  58. public function luckLottery(Request $request)
  59. {
  60. [$id, $type] = $request->postMore([
  61. ['id', 0],
  62. ['type', 0]
  63. ], true);
  64. $uid = (int)$request->uid();
  65. $key = 'lucklotter_limit_' . $uid;
  66. if (CacheService::redisHandler()->get($key)) {
  67. return app('json')->fail('您求的频率太过频繁,请稍后请求!');
  68. }
  69. CacheService::redisHandler()->set('lucklotter_limit_' . $uid, $uid, 1);
  70. if ($type == 5 && request()->isWechat()) {
  71. /** @var WechatServices $wechat */
  72. $wechat = app()->make(WechatServices::class);
  73. $subscribe = $wechat->get(['user_type' => 'wechat', 'uid' => $request->uid(), 'subscribe' => 1]);
  74. if (!$subscribe) {
  75. $url = '';
  76. /** @var QrcodeServices $qrcodeService */
  77. $qrcodeService = app()->make(QrcodeServices::class);
  78. $url = $qrcodeService->getTemporaryQrcode('luckLottery-5', $request->uid())->url;
  79. return app('json')->success(410024, ['code' => 'subscribe', 'url' => $url]);
  80. }
  81. }
  82. if (!$id) {
  83. return app('json')->fail(100100);
  84. }
  85. return app('json')->success($this->services->luckLottery($uid, $id));
  86. }
  87. /**
  88. * 领取奖品
  89. * @param Request $request
  90. * @return mixed
  91. */
  92. public function lotteryReceive(Request $request, LuckLotteryRecordServices $lotteryRecordServices)
  93. {
  94. [$id, $name, $phone, $address, $mark] = $request->postMore([
  95. ['id', 0],
  96. ['name', ''],
  97. ['phone', ''],
  98. ['address', ''],
  99. ['mark', '']
  100. ], true);
  101. if (!$id) {
  102. return app('json')->fail(100100);
  103. }
  104. $uid = (int)$request->uid();
  105. return app('json')->success($lotteryRecordServices->receivePrize($uid, $id, compact('name', 'phone', 'address', 'mark')) ? 410319 : 410320);
  106. }
  107. /**
  108. * 获取中奖记录
  109. * @param Request $request
  110. * @param LuckLotteryRecordServices $lotteryRecordServices
  111. * @return mixed
  112. */
  113. public function lotteryRecord(Request $request, LuckLotteryRecordServices $lotteryRecordServices)
  114. {
  115. $uid = (int)$request->uid();
  116. return app('json')->success($lotteryRecordServices->getRecord($uid));
  117. }
  118. }