Bladeren bron

一次答多个题

lizhen_gitee 4 maanden geleden
bovenliggende
commit
2ba5d535ed
1 gewijzigde bestanden met toevoegingen van 42 en 32 verwijderingen
  1. 42 32
      application/api/controller/Question.php

+ 42 - 32
application/api/controller/Question.php

@@ -35,11 +35,10 @@ class Question extends Api
         }
 
         if($today_my_question == 0){
-            $this->error('今日答题机会用光了');
+            $this->error('今日答题次数用完了,明天再来吧');
         }
 
         //获取10个随机题目
-        $count = config('site.exam_times_user_eday');
         $lists = Db::name('exam_question')
         ->field('id,kind,title,options_json')
         ->where('is_material_child', 0)// 材料题子题不显示
@@ -75,7 +74,7 @@ class Question extends Api
      */
     public function submit()
     {
-        $this->error('接口作废');
+//        $this->error('接口作废');
         if(!$this->apiLimit('操作太快了,休息一下吧'));
 
         //上次绑定选手的时间不是今天
@@ -85,41 +84,53 @@ class Question extends Api
 
         //检查今日答题次数
         $exam_times_user_eday = config('site.exam_times_user_eday');
-//        $count = Db::name('user_question_log')->where('createdate',strtotime(date('Y-m-d')))->where('user_id',$this->auth->id)->count();
         $count = RedisUtil::getInstance(RedisKeyEnum::EAXM_TIMES.date('Y-m-d').':'.$this->auth->id)->get();
         if($count >= $exam_times_user_eday){
             $this->error('今日答题次数用完了,明天再来吧');
         }
 
-        $question_id  = input('question_id');
-        $answer       = input('answer');
-
-        if (!$question_id || !$answer) {
+        //答题
+        $user_questions = input('questions','','trim');
+        $user_questions = json_decode($user_questions,true);
+        if (!$user_questions) {
             $this->error('提交数据有误');
         }
+        $questions_ids  = array_column($user_questions, 'id');         // 答题id
+        $answers        = array_column($user_questions, 'answer');     // 用户答案
+
+        $is_right_number = 0;
+        $log_data        = [];
+        $questions = Db::name('exam_question')->field('id,kind,answer')->where('id','IN', $questions_ids)->orderRaw('field(id,'. implode(',', $questions_ids) .')')->select();
+        foreach($questions as $key => $question){
+            $is_right = $this->paperExam($question,$answers[$key]);
+
+            if($is_right){
+                $is_right_number ++;
+            }
+
+            $log_data[] = [
+                'user_id'     => $this->auth->id,
+                'question_id' => $question['id'],
+                'is_right'    => $is_right ? 1 : 0,
+                'player_id'   => $this->auth->bind_player_id,
+                'createtime'  => time(),
+                'createdate'  => strtotime(date('Y-m-d')),
+            ];
+        }
 
-        $is_right = $this->paperExam($question_id,$answer);
 
         Db::startTrans();
 
         //答题日志
-        $log = [
-            'user_id'     => $this->auth->id,
-            'question_id' => $question_id,
-            'is_right'    => $is_right ? 1 : 0,
-            'player_id'   => $this->auth->bind_player_id,
-            'createtime'  => time(),
-            'createdate'  => strtotime(date('Y-m-d')),
-        ];
-        $log_id = Db::name('user_question_log')->insertGetId($log);
+        $log_id = Db::name('user_question_log')->insertAll($log_data);
         if(!$log_id){
             Db::rollback();
             $this->error('答题失败');
         }
 
-        if($is_right){
+        if($is_right_number > 0){
             //给选手加分
-            $rs = Db::name('vote_player')->where('id',$this->auth->bind_player_id)->setInc('score');
+            $rs = Db::name('vote_player')->where('id',$this->auth->bind_player_id)->setInc('score',$is_right_number);
             if($rs === false){
                 Db::rollback();
                 $this->error('答题失败');
@@ -129,20 +140,20 @@ class Question extends Api
         Db::commit();
 
         //今日答题次数,自增一次
-        RedisUtil::getInstance(RedisKeyEnum::EAXM_TIMES.date('Y-m-d').':'.$this->auth->id)->incr_expire(86400);
+        $count = RedisUtil::getInstance(RedisKeyEnum::EAXM_TIMES.date('Y-m-d').':'.$this->auth->id)->incrby_expire(count($user_questions),86400);
 
-        if($is_right){
+        if($is_right_number > 0){
             //今日答对次数,自增一次
-            RedisUtil::getInstance(RedisKeyEnum::EAXM_RIGHT.date('Y-m-d').':'.$this->auth->id)->incr_expire(86400);
-
-            $msg = '回答正确';
-        }else{
-            //
-
-            $msg = '回答错误';
+            RedisUtil::getInstance(RedisKeyEnum::EAXM_RIGHT.date('Y-m-d').':'.$this->auth->id)->incrby_expire($is_right_number,86400);
         }
 
-        $this->success($msg);
+        //返回正确了几道题,剩余答题次数
+        $result = [
+            'submit_number'=> count($user_questions),
+            'right_number' => $is_right_number,
+            'remain'       => $exam_times_user_eday - $count,
+        ];
+        $this->success(1,$result);
     }
 
     /**
@@ -151,11 +162,10 @@ class Question extends Api
      * @param $answer
      * @return bool
      */
-    private function paperExam($question_id,$answer)
+    private function paperExam($question,$answer)
     {
         $is_right = false;
 
-        $question = Db::name('exam_question')->where('id', $question_id)->find();
         if(empty($question)){
             return false;
         }