LuckLotteryController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v2\activity;
  12. use app\Request;
  13. use app\services\activity\lottery\LuckLotteryRecordServices;
  14. use app\services\activity\lottery\LuckLotteryServices;
  15. use app\services\other\QrcodeServices;
  16. use app\services\wechat\WechatServices;
  17. use crmeb\services\CacheService;
  18. class LuckLotteryController
  19. {
  20. protected $services;
  21. public function __construct(LuckLotteryServices $services)
  22. {
  23. $this->services = $services;
  24. }
  25. /**
  26. * 抽奖活动信息
  27. * @param Request $request
  28. * @param $factor
  29. * @return mixed
  30. * @throws \Psr\SimpleCache\InvalidArgumentException
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function LotteryInfo(Request $request, $factor, $lottery_id = 0)
  36. {
  37. if (!$factor) return app('json')->fail('参数错误');
  38. if ($lottery_id) {
  39. $lottery = $this->services->getLottery($lottery_id, '*', ['prize'], true);
  40. } else {
  41. $lottery = $this->services->getFactorLottery((int)$factor, '*', ['prize'], true);
  42. }
  43. if (!$lottery) {
  44. return app('json')->fail('抽奖不存在');
  45. }
  46. $uid = (int)$request->uid();
  47. $lottery = $lottery->toArray();
  48. $lotteryData = ['lottery' => $lottery];
  49. $this->services->checkoutUserAuth($uid, (int)$lottery['id'], [], $lottery);
  50. $lotteryData['lottery_num'] = $this->services->getLotteryNum($uid, (int)$lottery['id'], [], $lottery);
  51. $all_record = $user_record = [];
  52. if ($lottery['is_all_record'] || $lottery['is_personal_record']) {
  53. /** @var LuckLotteryRecordServices $lotteryRecordServices */
  54. $lotteryRecordServices = app()->make(LuckLotteryRecordServices::class);
  55. if ($lottery['is_all_record']) {
  56. $all_record = $lotteryRecordServices->getWinList(['lottery_id' => $lottery['id']]);
  57. }
  58. if ($lottery['is_personal_record']) {
  59. $user_record = $lotteryRecordServices->getWinList(['lottery_id' => $lottery['id'], 'uid' => $uid]);
  60. }
  61. }
  62. $lotteryData['all_record'] = $all_record;
  63. $lotteryData['user_record'] = $user_record;
  64. return app('json')->success($lotteryData);
  65. }
  66. /**
  67. * 参与抽奖
  68. * @param Request $request
  69. * @return mixed
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. */
  74. public function luckLottery(Request $request)
  75. {
  76. [$id, $type] = $request->postMore([
  77. ['id', 0],
  78. ['type', 0]
  79. ], true);
  80. $uid = (int)$request->uid();
  81. $channel_type = $request->getFromType();
  82. $key = 'lucklotter_limit_' . $uid;
  83. if (CacheService::get($key)) {
  84. return app('json')->fail('您求的频率太过频繁,请稍后请求!');
  85. }
  86. CacheService::set('lucklotter_limit_' . $uid, $uid, 1);
  87. if ($type == 5 && request()->isWechat()) {
  88. /** @var WechatServices $wechat */
  89. $wechat = app()->make(WechatServices::class);
  90. $subscribe = $wechat->get(['user_type' => 'wechat', 'uid' => $request->uid(), 'subscribe' => 1]);
  91. if (!$subscribe) {
  92. $url = '';
  93. /** @var QrcodeServices $qrcodeService */
  94. $qrcodeService = app()->make(QrcodeServices::class);
  95. $url = $qrcodeService->getTemporaryQrcode('luckLottery-5', $request->uid())->url;
  96. return app('json')->success('请先关注公众号', ['code' => 'subscribe', 'url' => $url]);
  97. }
  98. }
  99. if (!$id) {
  100. return app('json')->fail('参数错误');
  101. }
  102. return app('json')->success($this->services->luckLottery($uid, $id, $channel_type));
  103. }
  104. /**
  105. * 领取奖品
  106. * @param Request $request
  107. * @param LuckLotteryRecordServices $lotteryRecordServices
  108. * @return mixed
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\DbException
  111. * @throws \think\db\exception\ModelNotFoundException
  112. */
  113. public function lotteryReceive(Request $request, LuckLotteryRecordServices $lotteryRecordServices)
  114. {
  115. [$id, $name, $phone, $address, $detail, $mark] = $request->postMore([
  116. ['id', 0],
  117. ['name', ''],
  118. ['phone', ''],
  119. ['address', ''],
  120. ['detail', ''],
  121. ['mark', '']
  122. ], true);
  123. if (!$id) {
  124. return app('json')->fail('参数错误');
  125. }
  126. $uid = (int)$request->uid();
  127. return app('json')->success($lotteryRecordServices->receivePrize($uid, $id, compact('name', 'phone', 'address', 'detail', 'mark')) ? '领取成功' : '领取失败');
  128. }
  129. /**
  130. * 获取中奖记录
  131. * @param Request $request
  132. * @param LuckLotteryRecordServices $lotteryRecordServices
  133. * @return mixed
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function lotteryRecord(Request $request, LuckLotteryRecordServices $lotteryRecordServices)
  139. {
  140. $uid = (int)$request->uid();
  141. return app('json')->success($lotteryRecordServices->getRecord($uid));
  142. }
  143. }