xielin 5 роки тому
батько
коміт
075d7a6989

+ 6 - 0
app/Http/Controllers/V1/CircleController.php

@@ -94,6 +94,9 @@ class CircleController extends Controller
         if($userInfo){
             return $this->jsonError('获取用户信息失败');
         }
+//        else{
+//            $userInfo['uid'] = 268;
+//        }
 
         $validator = Validator::make($request->all(), [
             'id' => 'required|exists:interest_circles'
@@ -114,6 +117,9 @@ class CircleController extends Controller
         if($userInfo){
             return $this->jsonError('获取用户信息失败');
         }
+//        else{
+//            $userInfo['uid'] = 268;
+//        }
 
         $validator = Validator::make($request->all(), [
             'id' => 'required|exists:interest_circles'

+ 45 - 18
app/Repositories/Circle/CircleRepository.php

@@ -67,21 +67,38 @@ class CircleRepository
     {
         $row = $this->interestCircleUser
             ->where('uid', $userInfo['uid'])
-            ->where('circle_id', $request['circle_id'])
+            ->where('circle_id', $request['id'])
             ->first();
         if ($row) {
             return jsonError('您已经加入该圈子了');
         }
-        $circle = $this->interestCircle->where('circle_id',$request['id'])->first();
-        if($circle->join_limit){
-            $checkRow = $this->checkQuestion($circle->join_question,$request['answer']);
-            if(!$checkRow){
+        $circle = $this->interestCircle->where('id', $request['id'])->first();
+        $key = 'circle_error_count_' . $userInfo['uid'];
+        $errorCount = Redis::get($key);
+        if ($errorCount >= $circle->limit_condition) {
+            return jsonError('今日次数已用完,明天再来吧');
+        }
+        if ($circle->join_limit) {
+            if (empty($request['answer'])) {
+                return jsonError('请输入答案~');
+            }
+            $question = json_decode($circle->join_question, true);
+            $answer = json_decode($request['answer'], true);
+            $checkRow = $this->checkQuestion($question, $answer);
+            if (!$checkRow) {
+                if (!$errorCount) {
+                    Redis::set($key, 1);
+                    $expire = Carbon::today()->endOfDay()->timestamp - Carbon::now()->timestamp;
+                    Redis::expire($key, $expire);
+                } else {
+                    Redis::incr($key);
+                }
                 return jsonError('学习学习,明天再来吧~');
             }
         }
         DB::beginTransaction();
         try {
-            $this->interestCircleUser->create(['uid' => $userInfo['uid'], 'circle_id' => $request['circle_id']]);
+            $this->interestCircleUser->create(['uid' => $userInfo['uid'], 'circle_id' => $request['id']]);
             DB::commit();
             //加入圈子成功后,清除错误回答次数
             $key = 'circle_error_count_' . $userInfo['uid'];
@@ -94,22 +111,32 @@ class CircleRepository
 
     }
 
-    private function checkQuestion($question,$answer){
+    /**
+     * 检测答案是否正确
+     * @param $question 问题
+     * @param $answer   答案
+     * @return bool
+     */
+    private function checkQuestion($question, $answer)
+    {
         $result = true;
         $rightAnswer = [];
-        foreach($question as $key=>$value){
-            //$answer['question_id'] = $key+1;
-            foreach ($value['answer'] as $k=>$v){
-                if($v['right']){
-                    $answer['answer_id'][] = $k+1;
+        foreach ($question as $key => $value) {
+            $temp = [];
+            foreach ($value['answer'] as $k => $v) {
+                if ($v['right']) {
+                    $temp['answer_id'][] = $k + 1;
                 }
             }
-            $rightAnswer[$key+1] = $answer;
+            $rightAnswer[$key + 1] = $temp;
         }
-        var_dump($rightAnswer);
-        foreach ($answer as $ak=>$av){
-            $right = $rightAnswer[$av['question']];
-            if(array_intersect($right,$av['answer'])){
+//        var_dump(json_encode($rightAnswer));
+        foreach ($answer as $ak => $av) {
+            $right = $rightAnswer[$av['question']]['answer_id'];
+//            var_dump($right);
+//            var_dump($av['answer']);
+//            var_dump($right != $av['answer']);
+            if ($right != $av['answer']) {
                 $result = false;
                 break;
             }
@@ -127,7 +154,7 @@ class CircleRepository
     {
         $info = $this->interestCircleUser
             ->where('uid', $userInfo['uid'])
-            ->where('circle_id', $request['circle_id'])
+            ->where('circle_id', $request['id'])
             ->first();
         if (!$info) {
             return jsonError('您未加入该圈子');

+ 5 - 0
routes/api.php

@@ -117,6 +117,11 @@ $api->version('v1', [
         $api->get('rankingList', 'BeanDetailController@rankingList');
         //后院首页
         $api->get('starHome', 'BeanDetailController@starHome');
+
+        //加入圈子
+        $api->post('circle/join', 'CircleController@joinCircle');
+        //退出圈子
+        $api->delete('circle/join', 'CircleController@exitCircle');
     });