LotteryController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace App\Http\Controllers\Welfare;
  3. use App\Models\User\UserModel;
  4. use App\Services\Welfare\NoticeService;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. use App\Http\Controllers\Miniprogram\Auth;
  8. use App\Models\Welfare\LotteryModel;
  9. use App\Models\Welfare\TicketModel;
  10. class LotteryController extends Controller
  11. {
  12. /**
  13. * 抽奖活动列表
  14. * @param Request $request
  15. * @return \Illuminate\Http\JsonResponse
  16. */
  17. public function lotterys(Request $request)
  18. {
  19. $per_page = $request->input('per_page', 20);
  20. $page = $request->input('page', 1);
  21. $offset = ($page - 1) * $per_page;
  22. $uid = Auth::auth();
  23. $builder = LotteryModel::where('drawed_at', '>', time())->where('showed_at', '<=', time())->where(
  24. 'deleted_at',
  25. 0
  26. );
  27. $count = $builder->count();
  28. $lotterys = $builder->orderBy('drawed_at', 'asc')->offset($offset)->limit($per_page)->get();
  29. foreach ($lotterys as $item) {
  30. $item->get_ticket = TicketModel::where('lottery_id', $item->id)->where('uid', $uid)->first() ? true : false;
  31. }
  32. return response()->json([
  33. 'code' => 200,
  34. 'message' => 'OK',
  35. 'data' => [
  36. 'list' => $lotterys,
  37. 'count' => $count,
  38. 'per_page' => $per_page,
  39. 'page' => $page
  40. ]
  41. ]);
  42. }
  43. public function historys(Request $request)
  44. {
  45. $per_page = $request->input('per_page', 14);
  46. $page = $request->input('page', 1);
  47. $offset = ($page - 1) * $per_page;
  48. $builder = LotteryModel::where('drawed_at', '<', time())->where('deleted_at', 0);
  49. $count = $builder->count();
  50. $lotterys = $builder->orderBy('drawed_at', 'desc')->offset($offset)->limit($per_page)->get();
  51. foreach ($lotterys as $item) {
  52. $tickets = TicketModel::where('lucky', '>', 0)->where('lottery_id', $item->id)->get();
  53. foreach ($tickets as $ticket) {
  54. $user = UserModel::find($ticket->uid);
  55. $ticket->nickname = $user->nickname;
  56. $ticket->headimgurl = $user->headimgurl;
  57. }
  58. $item->luckys = $tickets;
  59. }
  60. return response()->json([
  61. 'code' => 200,
  62. 'message' => 'OK',
  63. 'data' => [
  64. 'list' => $lotterys,
  65. 'count' => $count,
  66. 'per_page' => $per_page,
  67. 'page' => $page
  68. ]
  69. ]);
  70. }
  71. /**
  72. * 抽中的列表
  73. * @param Request $request
  74. * @return \Illuminate\Http\JsonResponse
  75. */
  76. public function luckys(Request $request)
  77. {
  78. $lotterys = LotteryModel::where('drawed_at', '<', time())->pluck('id');
  79. $tickets = TicketModel::where('lucky', '>', 0)->whereIn('lottery_id', $lotterys)->orderBy(
  80. 'created_at',
  81. 'desc'
  82. )->limit(50)->get();
  83. foreach ($tickets as $ticket) {
  84. $user = UserModel::find($ticket->uid);
  85. $ticket->nickname = $user->nickname;
  86. $ticket->headimgurl = $user->headimgurl;
  87. $lottery = LotteryModel::find($ticket->lottery_id);
  88. $ticket->title = $lottery->title;
  89. }
  90. return response()->json([
  91. 'code' => 200,
  92. 'message' => 'OK',
  93. 'data' => [
  94. 'list' => $tickets
  95. ]
  96. ]);
  97. }
  98. /**
  99. * 我的抽奖历史
  100. * @param Request $request
  101. * @return \Illuminate\Http\JsonResponse
  102. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  103. */
  104. public function myLotterys(Request $request)
  105. {
  106. $uid = Auth::auth();
  107. $per_page = $request->input('per_page', 20);
  108. $page = $request->input('page', 1);
  109. $offset = ($page - 1) * $per_page;
  110. $builder = LotteryModel::where(function ($query) use ($uid) {
  111. return $query->whereIn('id', TicketModel::where('uid', $uid)->distinct()->pluck('lottery_id'));
  112. })->where('deleted_at', 0);
  113. $count = $builder->count();
  114. $lotterys = $builder->orderBy('drawed_at', 'desc')->offset($offset)->limit($per_page)->get();
  115. foreach ($lotterys as $lottery) {
  116. $lottery->lucky_ticket_count = TicketModel::where('lottery_id', $lottery->id)->where(
  117. 'uid',
  118. $uid
  119. )->where('lucky', '>', 0)->count();
  120. $lottery->unlucky_ticket_count = TicketModel::where('lottery_id', $lottery->id)->where(
  121. 'uid',
  122. $uid
  123. )->where('lucky', 0)->count();
  124. }
  125. return response()->json([
  126. 'code' => 200,
  127. 'message' => 'OK',
  128. 'data' => [
  129. 'list' => $lotterys,
  130. 'count' => $count,
  131. 'per_page' => $per_page,
  132. 'page' => $page
  133. ]
  134. ]);
  135. }
  136. /**
  137. * 抽奖活动详情
  138. * @param Request $request
  139. * @return \Illuminate\Http\JsonResponse
  140. */
  141. public function lottery(Request $request)
  142. {
  143. $lottery = LotteryModel::find($request->id);
  144. if (!$lottery) {
  145. return response()->json([
  146. 'code' => 404,
  147. 'message' => '不存在',
  148. ]);
  149. }
  150. if ($lottery->drawed_at < time()) {
  151. $luckys = TicketModel::where('lottery_id', $lottery->id)->where('lucky', 1)->get();
  152. foreach ($luckys as $lucky) {
  153. $user = UserModel::find($lucky->uid);
  154. $lucky->nickname = $user->nickname;
  155. $lucky->headimgurl = $user->headimgurl;
  156. }
  157. $lottery->luckys = $luckys;
  158. }
  159. $lottery->lucky_ticket_count = 0;
  160. $lottery->unlucky_ticket_count = 0;
  161. $uid = Auth::auth();
  162. $tickets = TicketModel::where('lottery_id', $lottery->id)->where('uid', $uid)->orderBy('id', 'desc')->get();
  163. foreach ($tickets as $ticket) {
  164. switch ($ticket->type) {
  165. case '领取':
  166. case '分享助力':
  167. $userinfo = UserModel::where('uid', $ticket->uid)->first();
  168. $ticket->nickname = $userinfo->nickname;
  169. $ticket->headimgurl = $userinfo->headimgurl;
  170. break;
  171. case '好友助力':
  172. $userinfo = UserModel::where('uid', $ticket->value)->first();
  173. $ticket->nickname = $userinfo->nickname;
  174. $ticket->headimgurl = $userinfo->headimgurl;
  175. break;
  176. }
  177. if ($ticket->lucky) {
  178. $lottery->lucky_ticket_count += 1;
  179. } else {
  180. $lottery->unlucky_ticket_count += 1;
  181. }
  182. }
  183. $lottery->tickets = $tickets;
  184. return response()->json([
  185. 'code' => 200,
  186. 'data' => $lottery
  187. ]);
  188. }
  189. /**
  190. * 领取我的抽奖码
  191. * @param Request $request
  192. * @return \Illuminate\Http\JsonResponse
  193. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  194. */
  195. public function ticket(Request $request)
  196. {
  197. $uid = Auth::auth();
  198. $lottery = LotteryModel::find($request->id);
  199. if (!$lottery) {
  200. return response()->json([
  201. 'code' => 404,
  202. 'message' => '不存在',
  203. ]);
  204. }
  205. if ($lottery->drawed_at < time()) {
  206. return response()->json([
  207. 'code' => 10001,
  208. 'message' => '活动已开奖'
  209. ]);
  210. }
  211. if (TicketModel::where('uid', $uid)->where('type', '领取')->where('lottery_id', $lottery->id)->first()) {
  212. return response()->json([
  213. 'code' => 10002,
  214. 'message' => '已经领取'
  215. ]);
  216. }
  217. $ticket = TicketModel::create([
  218. 'uid' => $uid,
  219. 'type' => '领取',
  220. 'lottery_id' => $lottery->id,
  221. 'ticket_code' => getChars(6),
  222. ]);
  223. return response()->json([
  224. 'code' => 200,
  225. 'message' => 'OK',
  226. 'data' => $ticket
  227. ]);
  228. }
  229. /**
  230. * 邀请
  231. * @param Request $request
  232. * @return \Illuminate\Http\JsonResponse
  233. */
  234. public function invitation(Request $request)
  235. {
  236. $uid = Auth::auth();
  237. $lottery_id = $request->input('id');
  238. $inviter_uid = $request->input('inviter');
  239. if (TicketModel::where(['lottery_id' => $lottery_id, 'uid' => $uid, 'type' => '领取'])->first()) {
  240. return response()->json([
  241. 'code' => 10001,
  242. 'message' => '已经领取',
  243. ]);
  244. }
  245. // 邀请者+3,最多10个
  246. for ($i = 0; $i < 3; $i++) {
  247. if (TicketModel::where(['uid' => $inviter_uid, 'lottery_id' => $lottery_id])->count() < 10) {
  248. TicketModel::create([
  249. 'uid' => $inviter_uid,
  250. 'type' => '好友助力',
  251. 'value' => $uid,
  252. 'lottery_id' => $lottery_id,
  253. 'ticket_code' => getChars(6),
  254. ]);
  255. }
  256. }
  257. try {
  258. $ns = new NoticeService();
  259. $ns->helpToScore($uid, $inviter_uid);
  260. $ns->inviteToScore($inviter_uid, $uid);
  261. } catch (\Exception $e) {
  262. }
  263. return response()->json([
  264. 'code' => 200,
  265. 'message' => 'OK'
  266. ]);
  267. }
  268. /**
  269. * 首次分享
  270. * @param Request $request
  271. * @return array|\Illuminate\Http\JsonResponse
  272. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  273. */
  274. public function firstInvitation(Request $request)
  275. {
  276. $uid = Auth::auth();
  277. $id = $request->id;
  278. $ticket = TicketModel::where('uid', $uid)->where('type', '分享助力')->where('lottery_id', $id)->first();
  279. if ($ticket) {
  280. return ['code' => 10001, 'message' => '已分享'];
  281. }
  282. $ticket = TicketModel::create([
  283. 'uid' => $uid,
  284. 'type' => '分享助力',
  285. 'lottery_id' => $id,
  286. 'ticket_code' => getChars(6),
  287. ]);
  288. return response()->json([
  289. 'code' => 200,
  290. 'message' => 'OK',
  291. 'data' => $ticket
  292. ]);
  293. }
  294. }