Vote.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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::where('status',VoteModel::STATUS_SHOW)
  11. ->limit(input('limit', 10))
  12. ->order('id','desc')
  13. ->page(input('page', 1))
  14. ->select();
  15. ajax_success($list);
  16. }
  17. public function detail()
  18. {
  19. $id = input('id/d', 0);
  20. empty($id) && jump('该活动不存在');
  21. $info = VoteModel::find($id);
  22. empty($info) && jump('该活动不存在');
  23. $info['status'] != VoteModel::STATUS_SHOW && jump('该活动不存在');
  24. $user_id = get_user_id();
  25. $apply = VoteApplyModel::where('vote_id', $id)->where('user_id', $user_id)->find();
  26. $is_apply = 'false';
  27. $option_name = '';
  28. if (!empty($apply)) {
  29. $is_apply = 'true';
  30. $option_name = $apply['option_name'];
  31. }
  32. return view('', [
  33. 'info' => $info,
  34. 'is_apply' => $is_apply,
  35. 'option_name' => $option_name,
  36. ]);
  37. }
  38. public function apply()
  39. {
  40. $id = input('id/d', 0);
  41. empty($id) && ajax_return(1, '该活动不存在');
  42. $vote = VoteModel::find($id);
  43. empty($vote) && ajax_return(1, '该活动不存在');
  44. $vote['status'] != VoteModel::STATUS_SHOW && jump('该活动不存在');
  45. $option_name = input('option_name', '');
  46. if (!in_array($option_name, $vote['option'])) {
  47. ajax_return(1, '该选项不存在,请重新选择');
  48. }
  49. $time = time();
  50. if ($time < strtotime($vote['start_time'])) {
  51. ajax_return(1, '活动未开始');
  52. }
  53. if ($time > strtotime($vote['end_time'])) {
  54. ajax_return(1, '活动已结束');
  55. }
  56. if (request()->ip() != '192.168.1.1') {
  57. ajax_return(1, '请连接RCG的WIFI进行投票');
  58. }
  59. $user_id = get_user_id();
  60. $check = VoteApplyModel::where('vote_id', $id)->where('user_id', $user_id)->find();
  61. if (!empty($check)) {
  62. ajax_return(1, '请勿重复提交!');
  63. }
  64. VoteApplyModel::create([
  65. 'vote_id' => $id,
  66. 'user_id' => $user_id,
  67. 'option_name' => $option_name,
  68. ]);
  69. ajax_return();
  70. }
  71. }