LuckLotteryController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, '*', ['prize'], true);
  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. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public function luckLottery(Request $request)
  62. {
  63. [$id, $type] = $request->postMore([
  64. ['id', 0],
  65. ['type', 0]
  66. ], true);
  67. $uid = (int)$request->uid();
  68. $key = 'lucklotter_limit_' . $uid;
  69. if (CacheService::get($key)) {
  70. return app('json')->fail('您求的频率太过频繁,请稍后请求!');
  71. }
  72. CacheService::set('lucklotter_limit_' . $uid, $uid, 1);
  73. if ($type == 5 && request()->isWechat()) {
  74. /** @var WechatServices $wechat */
  75. $wechat = app()->make(WechatServices::class);
  76. $subscribe = $wechat->get(['user_type' => 'wechat', 'uid' => $request->uid(), 'subscribe' => 1]);
  77. if (!$subscribe) {
  78. $url = '';
  79. /** @var QrcodeServices $qrcodeService */
  80. $qrcodeService = app()->make(QrcodeServices::class);
  81. $url = $qrcodeService->getTemporaryQrcode('luckLottery-5', $request->uid())->url;
  82. return app('json')->success(410024, ['code' => 'subscribe', 'url' => $url]);
  83. }
  84. }
  85. if (!$id) {
  86. return app('json')->fail(100100);
  87. }
  88. return app('json')->success($this->services->luckLottery($uid, $id));
  89. }
  90. /**
  91. * 领取奖品
  92. * @param Request $request
  93. * @param LuckLotteryRecordServices $lotteryRecordServices
  94. * @return mixed
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. public function lotteryReceive(Request $request, LuckLotteryRecordServices $lotteryRecordServices)
  100. {
  101. [$id, $name, $phone, $address, $mark] = $request->postMore([
  102. ['id', 0],
  103. ['name', ''],
  104. ['phone', ''],
  105. ['address', ''],
  106. ['mark', '']
  107. ], true);
  108. if (!$id) {
  109. return app('json')->fail(100100);
  110. }
  111. $uid = (int)$request->uid();
  112. return app('json')->success($lotteryRecordServices->receivePrize($uid, $id, compact('name', 'phone', 'address', 'mark')) ? 410319 : 410320);
  113. }
  114. /**
  115. * 获取中奖记录
  116. * @param Request $request
  117. * @param LuckLotteryRecordServices $lotteryRecordServices
  118. * @return mixed
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. */
  123. public function lotteryRecord(Request $request, LuckLotteryRecordServices $lotteryRecordServices)
  124. {
  125. $uid = (int)$request->uid();
  126. return app('json')->success($lotteryRecordServices->getRecord($uid));
  127. }
  128. }