Vote.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace app\mobile\controller;
  3. use app\common\model\VoteApplyModel;
  4. use app\common\model\VoteModel;
  5. use app\mobile\MobileBaseController;
  6. class Vote extends MobileBaseController
  7. {
  8. public function listVote()
  9. {
  10. $list = VoteModel::limit(input('limit', 10))
  11. ->page(input('page', 1))
  12. ->select();
  13. ajax_success($list);
  14. }
  15. public function detail()
  16. {
  17. $id = input('id/d', 0);
  18. empty($id) && jump('该活动不存在');
  19. $info = VoteModel::find($id);
  20. empty($info) && jump('该活动不存在');
  21. $user_id = get_user_id();
  22. $apply = VoteApplyModel::where('vote_id', $id)->where('user_id', $user_id)->find();
  23. $is_apply = 'false';
  24. $option_name = '';
  25. if (!empty($apply)) {
  26. $is_apply = 'true';
  27. $option_name = $apply['option_name'];
  28. }
  29. return view('', [
  30. 'info' => $info,
  31. 'is_apply' => $is_apply,
  32. 'option_name' => $option_name,
  33. ]);
  34. }
  35. public function apply()
  36. {
  37. $id = input('id/d', 0);
  38. empty($id) && ajax_return(1, '该活动不存在');
  39. $vote = VoteModel::find($id);
  40. empty($vote) && ajax_return(1, '该活动不存在');
  41. $option_name = input('option_name', '');
  42. if (!in_array($option_name, $vote['option'])) {
  43. ajax_return(1, '该选项不存在,请重新选择');
  44. }
  45. $user_id = get_user_id();
  46. $check = VoteApplyModel::where('vote_id', $id)->where('user_id', $user_id)->find();
  47. if (!empty($check)) {
  48. ajax_return(1, '请勿重复提交!');
  49. }
  50. VoteApplyModel::create([
  51. 'vote_id' => $id,
  52. 'user_id' => $user_id,
  53. 'option_name' => $option_name,
  54. ]);
  55. ajax_return();
  56. }
  57. }