xielin 5 yıl önce
ebeveyn
işleme
f59683dbba

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

@@ -57,6 +57,11 @@ class CircleController extends Controller
         return $this->jsonSuccess($data);
     }
 
+    /**
+     * 查询圈子问题
+     * @param Request $request
+     * @return array
+     */
     public function getQuestion(Request $request){
         $userInfo = $this->getUserInfo();
         if ($userInfo) {
@@ -79,6 +84,45 @@ class CircleController extends Controller
         return $this->jsonSuccess($data);
     }
 
+    /**
+     * 加入圈子
+     * @param Request $request
+     * @return array
+     */
+    public function joinCircle(Request $request){
+        $userInfo = $this->getUserInfo();
+        if($userInfo){
+            return $this->jsonError('获取用户信息失败');
+        }
+
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:interest_circles'
+        ]);
+        if ($validator->fails()) {
+            return $this->jsonError($validator->errors()->first());
+        }
+        return $this->circleRepository->joinCircle($request->all(),$userInfo);
+    }
+
+    /**
+     * 退出圈子
+     * @param Request $request
+     * @return array
+     */
+    public function exitCircle(Request $request){
+        $userInfo = $this->getUserInfo();
+        if($userInfo){
+            return $this->jsonError('获取用户信息失败');
+        }
+
+        $validator = Validator::make($request->all(), [
+            'id' => 'required|exists:interest_circles'
+        ]);
+        if ($validator->fails()) {
+            return $this->jsonError($validator->errors()->first());
+        }
+        return $this->circleRepository->exitCircle($request->all(),$userInfo);
+    }
 
 
 }

+ 89 - 0
app/Repositories/Circle/CircleRepository.php

@@ -57,6 +57,95 @@ class CircleRepository
         return $model->find($request['id']);
     }
 
+    /**
+     * 加入圈子
+     * @param $request
+     * @param $userInfo
+     * @return array
+     */
+    public function joinCircle($request, $userInfo)
+    {
+        $row = $this->interestCircleUser
+            ->where('uid', $userInfo['uid'])
+            ->where('circle_id', $request['circle_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){
+                return jsonError('学习学习,明天再来吧~');
+            }
+        }
+        DB::beginTransaction();
+        try {
+            $this->interestCircleUser->create(['uid' => $userInfo['uid'], 'circle_id' => $request['circle_id']]);
+            DB::commit();
+            //加入圈子成功后,清除错误回答次数
+            $key = 'circle_error_count_' . $userInfo['uid'];
+            Redis::del($key);
+            return jsonSuccess();
+        } catch (QueryException $exception) {
+            DB::rollBack();
+            return jsonError('加入圈子失败,再试试吧');
+        }
+
+    }
+
+    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;
+                }
+            }
+            $rightAnswer[$key+1] = $answer;
+        }
+        var_dump($rightAnswer);
+        foreach ($answer as $ak=>$av){
+            $right = $rightAnswer[$av['question']];
+            if(array_intersect($right,$av['answer'])){
+                $result = false;
+                break;
+            }
+        }
+        return $result;
+    }
+
+    /**
+     * 退出圈子
+     * @param $request
+     * @param $userInfo
+     * @return array
+     */
+    public function exitCircle($request, $userInfo)
+    {
+        $info = $this->interestCircleUser
+            ->where('uid', $userInfo['uid'])
+            ->where('circle_id', $request['circle_id'])
+            ->first();
+        if (!$info) {
+            return jsonError('您未加入该圈子');
+        }
+        DB::beginTransaction();
+        try {
+            $info->delete();
+            DB::commit();
+            //退出圈子成功后,清除错误回答次数
+            $key = 'circle_error_count_' . $userInfo['uid'];
+            Redis::del($key);
+            return jsonSuccess();
+        } catch (QueryException $exception) {
+            DB::rollBack();
+            return jsonError('退出圈子失败,再试试吧');
+        }
+    }
+
 
     /**
      * 相册列表

+ 2 - 1
app/Transformers/Circle/DetailTransformer.php

@@ -24,6 +24,7 @@ class DetailTransformer extends TransformerAbstract
 
     public function transform(InterestCircle $interestCircle)
     {
+        $answer = $interestCircle['limit_condition'] - intval(Redis::get('circle_error_count_' . $this->uid));
         $row = [
             'id' => $interestCircle['id'],
             'name' => $interestCircle['name'],
@@ -31,6 +32,7 @@ class DetailTransformer extends TransformerAbstract
             'image' => $interestCircle['image'],
             'join_limit' => $interestCircle['join_limit'],
             'is_join' => $this->isJoin($this->uid, $interestCircle['id']),
+            'answer_count' => $answer > 0 ? $answer : 0
         ];
         $funs = $this->getFunction($interestCircle);
         return array_merge($row, $funs);
@@ -62,7 +64,6 @@ class DetailTransformer extends TransformerAbstract
                 if (1 == $func['is_open']) {
                     $info['pictures'] = ['is_open' => 1, 'extra' => (string)$interestCircle['picture_count']];
                 }
-
             }
             if ('chatroom' == $func['function']) {
                 if (1 == $func['is_open']) {

+ 19 - 4
app/Transformers/Circle/QuestionTransformer.php

@@ -24,12 +24,27 @@ class QuestionTransformer extends TransformerAbstract
 
     public function transform(InterestCircle $interestCircle)
     {
+        $answer = $interestCircle['limit_condition'] - intval(Redis::get('circle_error_count_' . $this->uid));
         return [
             'id' => $interestCircle['id'],
-            'join_limit' => $interestCircle['join_limit'],
-            'limit_condition' => $interestCircle['limit_condition'],
-            'join_question' => (json_decode($interestCircle['join_question'], true)) ?? [],
-            'answer_error_count' => intval(Redis::get('circle_error_count_'.$this->uid))
+            'join_question' => $this->formatQuestion($interestCircle),
+            'answer_count' => $answer > 0 ? $answer : 0
         ];
     }
+
+    public function formatQuestion(InterestCircle $interestCircle)
+    {
+        $question = [];
+        $ques = json_decode($interestCircle['join_question'], true);
+        if (empty($ques)) {
+            return $question;
+        }
+        foreach ($ques as $key => &$value) {
+            $value['id'] = $key + 1;
+            foreach ($value['answer'] as $k => &$v) {
+                $v['id'] = $k + 1;
+            }
+        }
+        return $ques;
+    }
 }