LuckLotteryController.php 6.1 KB

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