VoteController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace App\Http\Controllers\Appearance;
  3. use App\Exceptions\AlertException;
  4. use App\Http\Controllers\Core\Auth;
  5. use App\Models\Appearance\AppearanceVoteModel;
  6. use App\Models\User\UserModel;
  7. use App\Services\Appearance\RankService;
  8. use App\Services\Appearance\VoteService;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\Controller;
  11. class VoteController extends Controller
  12. {
  13. /**
  14. * 投票
  15. * @param Request $request
  16. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  17. */
  18. public function vote(Request $request)
  19. {
  20. $uid = Auth::auth();
  21. $this->validate($request, [
  22. 'type' => 'required|in:partner,school',
  23. 'value' => 'required',
  24. ]);
  25. $type = $request->input('type', 'partner');
  26. $value = $request->input('value');
  27. $mediaId = $request->input('media_id', '');
  28. // 第二票以后需要榜单手机号
  29. if (AppearanceVoteModel::where('uid', $uid)->first()) {
  30. if (!UserModel::where('uid', $uid)->value('phone')) {
  31. return response([
  32. 'code' => 201,
  33. 'message' => '请先绑定手机号再投票'
  34. ]);
  35. }
  36. }
  37. // 今天是否为本渠道投过票
  38. if (AppearanceVoteModel::where('uid', $uid)->where('media_id', $mediaId)->today()->first()) {
  39. $today = true;
  40. } else {
  41. $today = false;
  42. }
  43. // 投票
  44. $voteService = new VoteService();
  45. switch ($type) {
  46. case "partner":
  47. $voteService->votePartner($uid, $value, $mediaId);
  48. break;
  49. case "school":
  50. $voteService->voteSchool($uid, $value, $mediaId);
  51. break;
  52. }
  53. // 渠道+1
  54. if ($mediaId && !$today && ($voteService->pick_partner || $voteService->pick_school)) {
  55. \DB::table('pocket.kdgx_activity_enrolls')
  56. ->where('media_id', $mediaId)
  57. ->where('activity_id', 3)
  58. ->increment('pick');
  59. }
  60. return response([
  61. 'code' => 200,
  62. 'message' => 'OK',
  63. 'data' => [
  64. 'pick_partner' => $voteService->pick_partner,
  65. 'pick_school' => $voteService->pick_school,
  66. ]
  67. ]);
  68. }
  69. /**
  70. * 投票明细(最近100)
  71. * @param Request $request
  72. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  73. */
  74. public function votes(Request $request)
  75. {
  76. $votes = AppearanceVoteModel::where('type', 1)
  77. ->when($request->filled('school'), function ($query) use ($request) {
  78. return $query->where('school', $request->school);
  79. })->orderBy('id', 'desc')
  80. ->limit(100)
  81. ->get();
  82. foreach ($votes as $vote) {
  83. $vote->user;
  84. }
  85. return response([
  86. 'code' => 200,
  87. 'message' => 'OK',
  88. 'data' => $votes
  89. ]);
  90. }
  91. /**
  92. * 申请上架
  93. * @param Request $request
  94. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  95. * @throws AlertException
  96. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  97. */
  98. public function apply(Request $request)
  99. {
  100. $uid = Auth::auth();
  101. if ((new RankService())->passive($uid, true)) {
  102. return response([
  103. 'code' => 200,
  104. 'message' => 'Success'
  105. ]);
  106. } else {
  107. return response([
  108. 'code' => 400,
  109. 'message' => '未满足上榜资格'
  110. ]);
  111. }
  112. }
  113. /**
  114. * 设置下榜
  115. * @param Request $request
  116. * @return mixed
  117. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  118. */
  119. public function updateApply(Request $request)
  120. {
  121. $uid = Auth::auth();
  122. $voteService = new VoteService();
  123. $voteService->unApply($uid);
  124. return response([
  125. 'code' => 200,
  126. 'message' => 'OK'
  127. ]);
  128. }
  129. }