Player.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 = [];
  13. protected $noNeedRight = ['*'];
  14. //给选手投票
  15. public function record(){
  16. $this->error('接口作废');
  17. if(!$this->apiLimit('操作太快了,休息一下吧'));
  18. $player_id = input('player_id','');
  19. $vote = input('vote',1);
  20. if(!$player_id){
  21. $this->error();
  22. }
  23. //登录用户票数的检查
  24. $user_id = $this->auth->id;
  25. //今天,投了几票
  26. $today_record = RedisUtil::getInstance(RedisKeyEnum::VOTE_RECORD.date('Y-m-d').':'.$user_id)->get();
  27. //今天,用户答对的次数,也就是答题获得的票数
  28. $question_vote = RedisUtil::getInstance(RedisKeyEnum::EAXM_RIGHT.date('Y-m-d').':'.$user_id)->get();
  29. //今天,用户答题的次数
  30. $today_question = RedisUtil::getInstance(RedisKeyEnum::EAXM_TIMES.date('Y-m-d').':'.$user_id)->get();
  31. //今天,免费的的票
  32. $gift_votes = config('site.gift_votes_user_eday');
  33. //今天,免费的答题次数
  34. $exam_times = config('site.exam_times_user_eday');
  35. //免费票 + 答对次数 - 已投票次数 < vote
  36. $times = $gift_votes + $question_vote - $today_record;
  37. if($times < 0){$times = 0;}
  38. //票数不够
  39. if($times < $vote){
  40. $msg = '投票失败,您今日剩余投票次数: '.$times.'次';
  41. //剩余投票次数,剩余答题次数
  42. $result = [
  43. 'uservote' => $times,
  44. 'userquestion' => $exam_times - $today_question,
  45. ];
  46. $this->success($msg,$result);
  47. }
  48. Db::startTrans();
  49. //检查选手
  50. $player_info = Db::name('vote_player')->field('id,subject_id,votes')->where(['id'=>$player_id])->lock(true)->find();
  51. if(!$player_info){
  52. Db::rollback();
  53. $this->error('不存在的选手');
  54. }
  55. //给选手加票
  56. $update_data = [
  57. 'votes' => $player_info['votes'] + $vote,
  58. ];
  59. $update_rs = Db::name('vote_player')->where('id',$player_id)->update($update_data);
  60. if($update_rs === false){
  61. Db::rollback();
  62. $this->error('投票失败');
  63. }
  64. //日志
  65. $data = [
  66. 'user_id' => $this->auth->id,
  67. 'subject_id' => $player_info['subject_id'],
  68. 'player_id' => $player_id,
  69. 'vote' => $vote,
  70. 'createdate' => strtotime(date('Y-m-d')),
  71. 'createtime' => time(),
  72. ];
  73. $log_id = Db::name('vote_record')->insertGetId($data);
  74. if(!$log_id){
  75. Db::rollback();
  76. $this->error('投票失败');
  77. }
  78. Db::commit();
  79. //今日投票次数,自增一次
  80. $today_record = RedisUtil::getInstance(RedisKeyEnum::VOTE_RECORD.date('Y-m-d').':'.$this->auth->id)->incrby_expire($vote,86400);
  81. //免费票 + 答对次数 - 已投票次数
  82. $times = $gift_votes + $question_vote - $today_record;
  83. if($times < 0){$times = 0;}
  84. //
  85. $msg = '投票成功,您今日剩余投票次数: '.$times.'次';
  86. //剩余投票次数,剩余答题次数
  87. $result = [
  88. 'uservote' => $times,
  89. 'userquestion' => $exam_times - $today_question,
  90. ];
  91. $this->success($msg,$result);
  92. }
  93. }