LuckLotteryController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. class LuckLotteryController
  9. {
  10. protected $services;
  11. public function __construct(LuckLotteryServices $services)
  12. {
  13. $this->services = $services;
  14. }
  15. /**
  16. * 抽奖活动信息
  17. * @param Request $request
  18. * @param $factor
  19. * @return mixed
  20. * @throws \Psr\SimpleCache\InvalidArgumentException
  21. * @throws \think\db\exception\DataNotFoundException
  22. * @throws \think\db\exception\DbException
  23. * @throws \think\db\exception\ModelNotFoundException
  24. */
  25. public function LotteryInfo(Request $request, $factor)
  26. {
  27. if (!$factor) return app('json')->fail('参数错误!');
  28. $lottery = $this->services->getFactorLottery((int)$factor);
  29. if (!$lottery) {
  30. return app('json')->fail('抽奖活动不存在');
  31. }
  32. $uid = (int)$request->uid();
  33. $lottery = $lottery->toArray();
  34. $lotteryData = ['lottery' => $lottery];
  35. $this->services->checkoutUserAuth($uid, (int)$lottery['id'], [], $lottery);
  36. $lotteryData['lottery_num'] = $this->services->getLotteryNum($uid, (int)$lottery['id'], [], $lottery);
  37. $all_record = $user_record = [];
  38. if ($lottery['is_all_record'] || $lottery['is_personal_record']) {
  39. /** @var LuckLotteryRecordServices $lotteryRecordServices */
  40. $lotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
  41. if ($lottery['is_all_record']) {
  42. $all_record = $lotteryRecordServices->getWinList(['lottery_id' => $lottery['id']]);
  43. }
  44. if ($lottery['is_personal_record']) {
  45. $user_record = $lotteryRecordServices->getWinList(['lottery_id' => $lottery['id'], 'uid' => $uid]);
  46. }
  47. }
  48. $lotteryData['all_record'] = $all_record;
  49. $lotteryData['user_record'] = $user_record;
  50. return app('json')->successful('ok', $lotteryData);
  51. }
  52. /**
  53. * 参与抽奖
  54. * @param Request $request
  55. * @return mixed
  56. */
  57. public function luckLottery(Request $request)
  58. {
  59. [$id, $type] = $request->postMore([
  60. ['id', 0],
  61. ['type', 0]
  62. ], true);
  63. if ($type == 5 && request()->isWechat()) {
  64. /** @var WechatServices $wechat */
  65. $wechat = app()->make(WechatServices::class);
  66. $subscribe = $wechat->get(['uid' => $request->uid(), 'subscribe' => 1]);
  67. if (!$subscribe) {
  68. $url = '';
  69. /** @var QrcodeServices $qrcodeService */
  70. $qrcodeService = app()->make(QrcodeServices::class);
  71. $url = $qrcodeService->getTemporaryQrcode('luckLottery-5', $request->uid())->url;
  72. return app('json')->successful('请先关注公众号', ['code' => 'subscribe', 'url' => $url]);
  73. }
  74. }
  75. if (!$id) {
  76. return app('json')->fail('参数错误');
  77. }
  78. $uid = (int)$request->uid();
  79. return app('json')->successful($this->services->luckLottery($uid, $id));
  80. }
  81. /**
  82. * 领取奖品
  83. * @param Request $request
  84. * @return mixed
  85. */
  86. public function lotteryReceive(Request $request, LuckLotteryRecordServices $lotteryRecordServices)
  87. {
  88. [$id, $name, $phone, $address, $mark] = $request->postMore([
  89. ['id', 0],
  90. ['name', ''],
  91. ['phone', ''],
  92. ['address', ''],
  93. ['mark', '']
  94. ], true);
  95. if (!$id) {
  96. return app('json')->fail('参数错误');
  97. }
  98. $uid = (int)$request->uid();
  99. return app('json')->successful($lotteryRecordServices->receivePrize($uid, $id, compact('name', 'phone', 'address', 'mark')) ? '领取成功' : '领取失败');
  100. }
  101. /**
  102. * 获取中奖记录
  103. * @param Request $request
  104. * @param LuckLotteryRecordServices $lotteryRecordServices
  105. * @return mixed
  106. */
  107. public function lotteryRecord(Request $request, LuckLotteryRecordServices $lotteryRecordServices)
  108. {
  109. $uid = (int)$request->uid();
  110. return app('json')->successful($lotteryRecordServices->getRecord($uid));
  111. }
  112. }