LuckLotteryController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, $lottery_id = 0)
  27. {
  28. if (!$factor) return app('json')->fail(100100);
  29. if ($lottery_id) {
  30. $lottery = $this->services->getLottery($lottery_id, '*', ['prize'], true);
  31. } else {
  32. $lottery = $this->services->getFactorLottery((int)$factor, '*', ['prize'], true);
  33. }
  34. if (!$lottery) {
  35. return app('json')->fail(410318);
  36. }
  37. $uid = (int)$request->uid();
  38. $lottery = $lottery->toArray();
  39. $lotteryData = ['lottery' => $lottery];
  40. $this->services->checkoutUserAuth($uid, (int)$lottery['id'], [], $lottery);
  41. $lotteryData['lottery_num'] = $this->services->getLotteryNum($uid, (int)$lottery['id'], [], $lottery);
  42. $all_record = $user_record = [];
  43. if ($lottery['is_all_record'] || $lottery['is_personal_record']) {
  44. /** @var LuckLotteryRecordServices $lotteryRecordServices */
  45. $lotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
  46. if ($lottery['is_all_record']) {
  47. $all_record = $lotteryRecordServices->getWinList(['lottery_id' => $lottery['id']]);
  48. }
  49. if ($lottery['is_personal_record']) {
  50. $user_record = $lotteryRecordServices->getWinList(['lottery_id' => $lottery['id'], 'uid' => $uid]);
  51. }
  52. }
  53. $lotteryData['all_record'] = $all_record;
  54. $lotteryData['user_record'] = $user_record;
  55. return app('json')->success($lotteryData);
  56. }
  57. /**
  58. * 参与抽奖
  59. * @param Request $request
  60. * @return mixed
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. public function luckLottery(Request $request)
  66. {
  67. [$id, $type] = $request->postMore([
  68. ['id', 0],
  69. ['type', 0]
  70. ], true);
  71. $uid = (int)$request->uid();
  72. $channel_type = $request->getFromType();
  73. $key = 'lucklotter_limit_' . $uid;
  74. if (CacheService::get($key)) {
  75. return app('json')->fail('您求的频率太过频繁,请稍后请求!');
  76. }
  77. CacheService::set('lucklotter_limit_' . $uid, $uid, 1);
  78. if ($type == 5 && request()->isWechat()) {
  79. /** @var WechatServices $wechat */
  80. $wechat = app()->make(WechatServices::class);
  81. $subscribe = $wechat->get(['user_type' => 'wechat', 'uid' => $request->uid(), 'subscribe' => 1]);
  82. if (!$subscribe) {
  83. $url = '';
  84. /** @var QrcodeServices $qrcodeService */
  85. $qrcodeService = app()->make(QrcodeServices::class);
  86. $url = $qrcodeService->getTemporaryQrcode('luckLottery-5', $request->uid())->url;
  87. return app('json')->success(410024, ['code' => 'subscribe', 'url' => $url]);
  88. }
  89. }
  90. if (!$id) {
  91. return app('json')->fail(100100);
  92. }
  93. return app('json')->success($this->services->luckLottery($uid, $id, $channel_type));
  94. }
  95. /**
  96. * 领取奖品
  97. * @param Request $request
  98. * @param LuckLotteryRecordServices $lotteryRecordServices
  99. * @return mixed
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. */
  104. public function lotteryReceive(Request $request, LuckLotteryRecordServices $lotteryRecordServices)
  105. {
  106. [$id, $name, $phone, $address, $detail, $mark] = $request->postMore([
  107. ['id', 0],
  108. ['name', ''],
  109. ['phone', ''],
  110. ['address', ''],
  111. ['detail', ''],
  112. ['mark', '']
  113. ], true);
  114. if (!$id) {
  115. return app('json')->fail(100100);
  116. }
  117. $uid = (int)$request->uid();
  118. return app('json')->success($lotteryRecordServices->receivePrize($uid, $id, compact('name', 'phone', 'address', 'detail', 'mark')) ? 410319 : 410320);
  119. }
  120. /**
  121. * 获取中奖记录
  122. * @param Request $request
  123. * @param LuckLotteryRecordServices $lotteryRecordServices
  124. * @return mixed
  125. * @throws \think\db\exception\DataNotFoundException
  126. * @throws \think\db\exception\DbException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. */
  129. public function lotteryRecord(Request $request, LuckLotteryRecordServices $lotteryRecordServices)
  130. {
  131. $uid = (int)$request->uid();
  132. return app('json')->success($lotteryRecordServices->getRecord($uid));
  133. }
  134. }