interestCircle = $interestCircle; $this->interestCircleUser = $interestCircleUser; } /** * 圈子列表 */ public function circleLists($request, $limit = 3) { $where[] = ['is_open', 1]; if (isset($request['is_recommend'])) { $where[] = ['is_recommend', 1]; } return $this->interestCircle ->where($where) ->orderBy('id', 'desc') ->take($limit) ->get(); } /** * 圈子详情 */ public function detail($request, $isTrashed = false) { $model = $this->interestCircle; if ($isTrashed) { $model->withTrashed(); } $this->interestCircle->where('id', $request['id'])->increment('view_count'); 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['id']) ->first(); if ($row) { return jsonError('您已经加入该圈子了', ['answer_count' => 0]); } $circle = $this->interestCircle->where('id', $request['id'])->first(); $limitCount = 0; if ($circle->join_limit) { if($circle->limit_condition){ $key = 'circle_error_count_' . $userInfo['uid']; $errorCount = Redis::get($key); if ($errorCount >= $circle->limit_condition) { return jsonError('今日次数已用完,明天再来吧', ['answer_count' => 0]); } if (empty($request['answer'])) { return jsonError('请输入答案~', ['answer_count' => intval($circle->limit_condition)-$errorCount]); } }else{ $limitCount = -1; if (empty($request['answer'])) { return jsonError('请输入答案~', ['answer_count' => $limitCount]); } } $question = json_decode($circle->join_question, true); $answer = json_decode($request['answer'], true); $checkRow = $this->checkQuestion($question, $answer); if (!$checkRow) { if($circle->limit_condition){ if (!$errorCount) { Redis::set($key, 1); $expire = Carbon::today()->endOfDay()->timestamp - Carbon::now()->timestamp; Redis::expire($key, $expire); } else { Redis::incr($key); } $answerCount = intval($circle->limit_condition - Redis::get($key)); }else{ $answerCount = -1; } 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'); DB::commit(); //加入圈子成功后,清除错误回答次数 $key = 'circle_error_count_' . $userInfo['uid']; Redis::del($key); return jsonSuccess(); } catch (QueryException $exception) { DB::rollBack(); return jsonError('加入圈子失败,再试试吧', ['answer_count' => $limitCount]); } } /** * 检测答案是否正确 * @param $question 问题 * @param $answer 答案 * @return bool */ private function checkQuestion($question, $answer) { $result = true; $rightAnswer = []; foreach ($question as $key => $value) { $temp = []; foreach ($value['answer'] as $k => $v) { if ($v['right']) { $temp['answer_id'][] = $k + 1; } } $rightAnswer[$key + 1] = $temp; } // 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; } } return $result; } /** * 退出圈子 * @param $request * @param $userInfo * @return array */ public function exitCircle($request, $userInfo) { $info = $this->interestCircleUser ->where('uid', $userInfo['uid']) ->where('circle_id', $request['id']) ->first(); if (!$info) { return jsonError('您未加入该圈子'); } if($info->is_black){ return jsonError('您在本圈子内的权限受限'); } DB::beginTransaction(); try { $info->delete(); $this->interestCircle->where('id', $request['id'])->decrement('join_count'); DB::commit(); //退出圈子成功后,清除错误回答次数 $key = 'circle_error_count_' . $userInfo['uid']; Redis::del($key); return jsonSuccess(); } catch (QueryException $exception) { DB::rollBack(); return jsonError('退出圈子失败,再试试吧'); } } }