public function getQuestions() { $request = Request::capture(); $token = $request->input('token'); $targetUserId = $request->input('userId'); // 指定某个用户提出的问题 如果为空 显示token用户关注人提出的问题 $page = $request->input('page'); $size = $request->input('size'); $tagId = $request->input('tagId'); $userId = AuthController::getUserIdByToken($token); if ($userId == null) { return Utility::response_format(Utility::RESPONSE_CODE_AUTH_ERROR, '', '认证失败'); } if ($size == null || $size > 50) { $size = 20; } $questions = null; if ($targetUserId != null) { $questions = Question::select('id', 'title', 'brief', 'publish_time', 'user_id')->where('user_id', $targetUserId)->orderBy('publish_time', 'desc'); } else { $followUser = UserFollows::where('user_id', $userId)->lists('follow_user_id')->toArray(); array_push($followUser, $userId); $questions = Question::select('id', 'title', 'brief', 'publish_time', 'user_id')->whereIn('user_id', $followUser)->orderBy('publish_time', 'desc'); } if ($tagId != null) { $questionsTag = QuestionTags::where('tag_id', $tagId)->lists('question_id'); $questions = $questions->whereIn('id', $questionsTag); } $questions_query = $questions->paginate($size, '*', 'page', $page)->toArray(); $questions_result = $questions_query['data']; $result = array(); foreach ($questions_result as $question) { // 个人信息 $userInfo = UserInfo::select('user_name', 'head_pic')->where('user_id', $question['user_id'])->first()->toArray(); $question = array_merge($question, $userInfo); // 图片 $picIds = QuestionPictures::select('pic_id')->where('question_id', $question['id'])->get()->toArray(); $pics = Picture::whereIn('id', $picIds)->get()->toArray(); $question = array_merge($question, ['image' => $pics]); // print_r($question); // 标签 $tagIds = QuestionTags::select('tag_id')->where('question_id', $question['id'])->get()->toArray(); $tags = Tags::whereIn('id', $tagIds)->get()->toArray(); $question = array_merge($question, ['tags' => $tags]); array_push($result, $question); } // print_r($questions_result); return Utility::response_format(Utility::RESPONSE_CODE_SUCCESS, $result, '请求成功'); }
public function deleteFollowUser() { $request = Request::capture(); $token = $request->input('token'); // 被关注人id $followUserId = $request->input('userId'); if ($followUserId == null) { return Utility::response_format(Utility::RESPONSE_CODE_Error, '', 'userId不能为空'); } // 用户id $userId = AuthController::getUserIdByToken($token); if ($userId == null) { return Utility::response_format(Utility::RESPONSE_CODE_AUTH_ERROR, '', '认证失败'); } DB::beginTransaction(); try { $follow = UserFollows::where('user_id', $userId)->where('follow_user_id', $followUserId)->delete(); $fans = UserFans::where('user_id', $followUserId)->where('fans_user_id', $userId)->delete(); DB::commit(); if ($follow == null || $fans == null) { return Utility::response_format(Utility::RESPONSE_CODE_Error, '', '取消关注失败'); } else { return Utility::response_format(Utility::RESPONSE_CODE_SUCCESS, '', '取消关注成功'); } } catch (Exception $e) { DB::rollBack(); return Utility::response_format(Utility::RESPONSE_CODE_DB_ERROR, '', $e->getMessage()); } }