xielin 5 anos atrás
pai
commit
80468be13f

+ 6 - 3
app/Helper/helper.php

@@ -81,7 +81,7 @@ function jsonSuccess($data = [], $msg = "成功", $extra = [])
     $response = array(
         'code' => 0,
         'msg' => $msg,
-        'data' => []
+        'data' => new \stdClass()
     );
     if ($data) {
         if (is_array($data)) {
@@ -119,13 +119,16 @@ function jsonSuccess($data = [], $msg = "成功", $extra = [])
     return $response;
 }
 
-function jsonError($msg)
+function jsonError($msg, $data = [])
 {
     $response = array(
         'code' => 1,
         'msg' => $msg,
-        'data' => ""
+        'data' => new \stdClass()
     );
+    if ($data) {
+        $response['data'] = $data;
+    }
     return $response;
 }
 

+ 5 - 2
app/Http/Controllers/V1/Controller.php

@@ -43,13 +43,16 @@ class Controller extends BaseController
         return $response;
     }
 
-    public function jsonError($msg)
+    public function jsonError($msg,$data=[])
     {
         $response = array(
             'code' => 1,
             'msg' => $msg,
-            'data' => ""
+            'data' => new \stdClass()
         );
+        if($data){
+            $response['data'] = $data;
+        }
         return $response;
     }
 }

+ 14 - 8
app/Repositories/Circle/CircleRepository.php

@@ -54,7 +54,7 @@ class CircleRepository
         if ($isTrashed) {
             $model->withTrashed();
         }
-        $this->interestCircle->where('id',$request['id'])->increment('view_count');
+        $this->interestCircle->where('id', $request['id'])->increment('view_count');
         return $model->find($request['id']);
     }
 
@@ -71,17 +71,17 @@ class CircleRepository
             ->where('circle_id', $request['id'])
             ->first();
         if ($row) {
-            return jsonError('您已经加入该圈子了');
+            return jsonError('您已经加入该圈子了', ['answer_count' => 0]);
         }
         $circle = $this->interestCircle->where('id', $request['id'])->first();
         if ($circle->join_limit) {
             $key = 'circle_error_count_' . $userInfo['uid'];
             $errorCount = Redis::get($key);
             if ($errorCount >= $circle->limit_condition) {
-                return jsonError('今日次数已用完,明天再来吧');
+                return jsonError('今日次数已用完,明天再来吧', ['answer_count' => 0]);
             }
             if (empty($request['answer'])) {
-                return jsonError('请输入答案~');
+                return jsonError('请输入答案~', ['answer_count' => intval($circle->limit_condition)]);
             }
             $question = json_decode($circle->join_question, true);
             $answer = json_decode($request['answer'], true);
@@ -94,13 +94,19 @@ class CircleRepository
                 } else {
                     Redis::incr($key);
                 }
-                return jsonError('学习学习,明天再来吧~');
+                $answerCount = intval($circle->limit_condition - Redis::get($key));
+                if ($answerCount <= 0) {
+                    return jsonError('学习学习,明天再来吧~', ['answer_count' => $answerCount]);
+                } else {
+                    return jsonError('真遗憾,没有答对哦~', ['answer_count' => $answerCount]);
+                }
+
             }
         }
         DB::beginTransaction();
         try {
             $this->interestCircleUser->create(['uid' => $userInfo['uid'], 'circle_id' => $request['id']]);
-            $this->interestCircle->where('id',$request['id'])->increment('join_count');
+            $this->interestCircle->where('id', $request['id'])->increment('join_count');
             DB::commit();
             //加入圈子成功后,清除错误回答次数
             $key = 'circle_error_count_' . $userInfo['uid'];
@@ -108,7 +114,7 @@ class CircleRepository
             return jsonSuccess();
         } catch (QueryException $exception) {
             DB::rollBack();
-            return jsonError('加入圈子失败,再试试吧');
+            return jsonError('加入圈子失败,再试试吧', ['answer_count' => 0]);
         }
 
     }
@@ -164,7 +170,7 @@ class CircleRepository
         DB::beginTransaction();
         try {
             $info->delete();
-            $this->interestCircle->where('id',$request['id'])->decrement('join_count');
+            $this->interestCircle->where('id', $request['id'])->decrement('join_count');
             DB::commit();
             //退出圈子成功后,清除错误回答次数
             $key = 'circle_error_count_' . $userInfo['uid'];