Player.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. if(!$player_id){
  46. $this->error();
  47. }
  48. //登录用户票数的检查
  49. $check_rs = $this->record_check($this->auth->id);
  50. if($check_rs['status'] === false){
  51. $this->error($check_rs['msg'],null,$check_rs['code']);//给不同的code,0报错,2跳到答题
  52. }
  53. Db::startTrans();
  54. //检查选手
  55. $player_info = Db::name('vote_player')->field('id,subject_id,votes')->where(['id'=>$player_id])->lock(true)->find();
  56. if(!$player_info){
  57. Db::rollback();
  58. $this->error('不存在的选手');
  59. }
  60. //给选手加票
  61. $update_data = [
  62. 'votes' => $player_info['votes'] + 1,
  63. ];
  64. $update_rs = Db::name('vote_player')->where('id',$player_id)->update($update_data);
  65. if($update_rs === false){
  66. Db::rollback();
  67. $this->error('投票失败');
  68. }
  69. //日志
  70. $data = [
  71. 'user_id' => $this->auth->id,
  72. 'subject_id' => $player_info['subject_id'],
  73. 'player_id' => $player_id,
  74. 'createdate' => strtotime(date('Y-m-d')),
  75. 'createtime' => time(),
  76. ];
  77. $log_id = Db::name('vote_record')->insertGetId($data);
  78. if(!$log_id){
  79. Db::rollback();
  80. $this->error('投票失败');
  81. }
  82. Db::commit();
  83. //今日投票次数,自增一次
  84. RedisUtil::getInstance(RedisKeyEnum::VOTE_RECORD.date('Y-m-d').':'.$this->auth->id)->incr_expire(86400);
  85. //剩余票数,
  86. $check_rs['uservote'] - 1;
  87. $this->success('投票成功');
  88. }
  89. //投票检查
  90. private function record_check($uid){
  91. $result = array(
  92. 'status'=>true,
  93. 'code'=>1,
  94. 'msg'=>'',
  95. 'uservote' => 0,
  96. );
  97. //今天,投了几票
  98. // $today_record = Db::name('vote_record')->where('createdate',strtotime(date('Y-m-d')))->where('user_id',$uid)->count();
  99. $today_record = RedisUtil::getInstance(RedisKeyEnum::VOTE_RECORD.date('Y-m-d').':'.$this->auth->id)->get();
  100. //今天,免费的的票
  101. $gift_votes = config('site.gift_votes_user_eday');
  102. //今天,免费的答题次数
  103. $exam_times = config('site.exam_times_user_eday');
  104. //投票次数 >= 所有的来源
  105. if($today_record >= $gift_votes + $exam_times){
  106. $result['status'] = false;
  107. $result['code'] = 0;
  108. $result['msg'] = '投票失败,您今日剩余投票次数: 0次,明天再来吧';
  109. $result['uservote'] = 0;
  110. return $result;
  111. }
  112. //今天,用户答对的次数,也就是答题获得的票数
  113. // $question_vote = Db::name('user_question_log')->where('createdate',strtotime(date('Y-m-d')))->where('user_id',$uid)->where('is_right',1)->count();
  114. $question_vote = RedisUtil::getInstance(RedisKeyEnum::EAXM_RIGHT.date('Y-m-d').':'.$this->auth->id)->get();
  115. //投票次数 >= 免费票 + 答对次数
  116. if($today_record >= $gift_votes + $question_vote){
  117. $result['status'] = false;
  118. $result['code'] = 0;
  119. $result['msg'] = '投票失败,您今日剩余投票次数: 0次,明天再来吧';
  120. $result['uservote'] = 0;
  121. //今天,用户答题的次数
  122. // $today_question = Db::name('user_question_log')->where('createdate',strtotime(date('Y-m-d')))->where('user_id',$uid)->count();
  123. $today_question = RedisUtil::getInstance(RedisKeyEnum::EAXM_TIMES.date('Y-m-d').':'.$this->auth->id)->get();
  124. //还有答题机会
  125. if($exam_times > $today_question){
  126. $result['status'] = false;
  127. $result['code'] = 2;
  128. $result['msg'] = '投票失败,您今日剩余投票次数: 0次,可通过答题增加票数';
  129. $result['uservote'] = 0;
  130. }
  131. return $result;
  132. }
  133. //剩余票数
  134. $result['uservote'] = $gift_votes + $question_vote - $today_record;
  135. return $result;
  136. }
  137. }