Player.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 选手
  7. */
  8. class Player extends Api
  9. {
  10. protected $noNeedLogin = ['playerlist'];
  11. protected $noNeedRight = ['*'];
  12. //选手列表
  13. public function playerlist(){
  14. $subject_id = 1;
  15. $map = [
  16. 'subject_id' => $subject_id,
  17. 'status' => 1,
  18. ];
  19. //搜索
  20. $keyword = input('keyword','');
  21. if($keyword){
  22. $map['title|suozaidanwei|tuijiangonghui'] = ['LIKE','%'.$keyword.'%'];
  23. }
  24. $lists = Db::name('vote_player')->field('id,title')
  25. ->where($map)->order('votes desc,id desc')->autopage()->select();
  26. $this->success(1,$lists);
  27. }
  28. //用户绑定选手
  29. public function bind_player(){
  30. $player_id = input('player_id',0);
  31. $data = [
  32. 'bind_player_id' => $player_id,
  33. 'bind_player_date' => strtotime(date('Y-m-d')),
  34. ];
  35. Db::name('user')->where('id',$this->auth->id)->update($data);
  36. $this->success('绑定成功');
  37. }
  38. //给选手投票
  39. public function record(){
  40. $player_id = input('player_id','');
  41. if(!$player_id){
  42. $this->error();
  43. }
  44. Db::startTrans();
  45. //检查选手
  46. $player_info = Db::name('vote_player')->field('id,subject_id,votes,score')->where(['id'=>$player_id,'status'=>1])->lock(true)->find();
  47. if(!$player_info){
  48. Db::rollback();
  49. $this->error('不存在的选手');
  50. }
  51. $subject_id = $player_info['subject_id'];
  52. //大检查
  53. $check_rs = $this->record_check($this->auth->id);
  54. if($check_rs['status'] === false){
  55. Db::rollback();
  56. $this->error($check_rs['msg'],null,$check_rs['code']);//给不同的code,0报错,2跳到答题
  57. }
  58. $update_data = [
  59. 'votes' => $player_info['votes'] + 1,
  60. ];
  61. //入库
  62. $update_rs = Db::name('vote_player')->where('id',$player_id)->update($update_data);
  63. if($update_rs === false){
  64. Db::rollback();
  65. $this->error('投票失败');
  66. }
  67. //日志
  68. $data = [
  69. 'user_id' => $this->auth->id,
  70. 'subject_id' => $subject_id,
  71. 'player_id' => $player_id,
  72. 'createdate' => strtotime(date('Y-m-d')),
  73. 'createtime' => time(),
  74. ];
  75. $log_id = Db::name('vote_record')->insertGetId($data);
  76. if(!$log_id){
  77. Db::rollback();
  78. $this->error('投票失败');
  79. }
  80. Db::commit();
  81. $this->success('投票成功');
  82. }
  83. //投票检查
  84. private function record_check($uid){
  85. $result = array(
  86. 'status'=>true,
  87. 'code'=>1,
  88. 'msg'=>'',
  89. );
  90. //今天,投了几票
  91. $today_record = Db::name('vote_record')->where('createdate',strtotime(date('Y-m-d')))->where('user_id',$uid)->count();
  92. //今天,免费的的票
  93. $gift_votes = config('site.gift_votes_user_eday');
  94. //今天,免费的答题次数
  95. $exam_times = config('site.exam_times_user_eday');
  96. //投票次数 >= 所有的来源
  97. if($today_record >= $gift_votes + $exam_times){
  98. $result['status'] = false;
  99. $result['code'] = 0;
  100. $result['msg'] = '今天的票已经用光了,明天再来吧';
  101. return $result;
  102. }
  103. //今天,用户答对的次数,也就是答题获得的票数
  104. $question_vote = Db::name('user_question_log')->where('createdate',strtotime(date('Y-m-d')))->where('user_id',$uid)->where('is_right',1)->count();
  105. //投票次数 >= 免费票 + 答对次数
  106. if($today_record >= $gift_votes + $question_vote){
  107. $result['status'] = false;
  108. $result['code'] = 0;
  109. $result['msg'] = '今天的票已经用光了,明天再来吧';
  110. //今天,用户答题的次数
  111. $today_question = Db::name('user_question_log')->where('createdate',strtotime(date('Y-m-d')))->where('user_id',$uid)->count();
  112. //还有答题机会
  113. if($exam_times > $today_question){
  114. $result['code'] = 2;
  115. $result['msg'] = '票已经用光了,去答题获得票?';
  116. }
  117. return $result;
  118. }
  119. return $result;
  120. }
  121. }