Player.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 = [];
  11. protected $noNeedRight = ['*'];
  12. //给选手投票
  13. public function record(){
  14. $player_id = input('player_id','');
  15. if(!$player_id){
  16. $this->error();
  17. }
  18. Db::startTrans();
  19. //检查选手
  20. $player_info = Db::name('vote_player')->field('id,subject_id,votes,score')->where(['id'=>$player_id,'status'=>1])->lock(true)->find();
  21. if(!$player_info){
  22. Db::rollback();
  23. $this->error('不存在的选手');
  24. }
  25. $subject_id = $player_info['subject_id'];
  26. //大检查
  27. $check_rs = $this->record_check($this->auth->id);
  28. if($check_rs['status'] === false){
  29. Db::rollback();
  30. $this->error($check_rs['msg'],null,$check_rs['code']);//给不同的code,0报错,2跳到答题
  31. }
  32. $update_data = [
  33. 'votes' => $player_info['votes'] + 1,
  34. ];
  35. //入库
  36. $update_rs = Db::name('vote_player')->where('id',$player_id)->update($update_data);
  37. if($update_rs === false){
  38. Db::rollback();
  39. $this->error('投票失败');
  40. }
  41. //日志
  42. $data = [
  43. 'user_id' => $this->auth->id,
  44. 'subject_id' => $subject_id,
  45. 'player_id' => $player_id,
  46. 'createdate' => strtotime(date('Y-m-d')),
  47. 'createtime' => time(),
  48. ];
  49. $log_id = Db::name('vote_record')->insertGetId($data);
  50. if(!$log_id){
  51. Db::rollback();
  52. $this->error('投票失败');
  53. }
  54. Db::commit();
  55. $this->success('投票成功');
  56. }
  57. //投票检查
  58. private function record_check($uid){
  59. $result = array(
  60. 'status'=>true,
  61. 'code'=>1,
  62. 'msg'=>'',
  63. );
  64. //今天,投了几票
  65. $today_record = Db::name('vote_record')->where('createdate',strtotime(date('Y-m-d')))->where('user_id',$uid)->count();
  66. //今天,免费的的票
  67. $gift_votes = config('site.gift_votes_user_eday');
  68. //今天,免费的答题次数
  69. $exam_times = config('site.exam_times_user_eday');
  70. //投票次数 >= 所有的来源
  71. if($today_record >= $gift_votes + $exam_times){
  72. $result['status'] = false;
  73. $result['code'] = 0;
  74. $result['msg'] = '今天的票已经用光了,明天再来吧';
  75. return $result;
  76. }
  77. //今天,用户答对的次数,也就是答题获得的票数
  78. $question_vote = Db::name('user_question_log')->where('createdate',strtotime(date('Y-m-d')))->where('user_id',$uid)->where('is_right',1)->count();
  79. //投票次数 >= 免费票 + 答对次数
  80. if($today_record >= $gift_votes + $question_vote){
  81. $result['status'] = false;
  82. $result['code'] = 0;
  83. $result['msg'] = '今天的票已经用光了,明天再来吧';
  84. //今天,用户答题的次数
  85. $today_question = Db::name('user_question_log')->where('createdate',strtotime(date('Y-m-d')))->where('user_id',$uid)->count();
  86. //还有答题机会
  87. if($exam_times > $today_question){
  88. $result['code'] = 2;
  89. $result['msg'] = '票已经用光了,去答题获得票?';
  90. }
  91. return $result;
  92. }
  93. return $result;
  94. }
  95. }