Vote.php 2.3 KB

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