Player.php 5.0 KB

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