interestCircleArticle = $interestCircleArticle; $this->post = $post; } /** * 内容列表 */ public function lists($request) { $perPage = isset($request['per_page']) ? $request['per_page'] : 20; $where = []; if (isset($request['uid'])) { $where[] = ['uid', $request['uid']]; } $articleModel = $this->post; return $articleModel ->join('post_data', 'post_data.post_id', '=', 'post.id') ->join('interest_circle_articles', 'interest_circle_articles.post_id', '=', 'post.id') ->select('post.*', 'interest_circle_articles.is_recommend', 'interest_circle_articles.circle_id') ->with('data') ->where($where) ->where(function ($query) use ($request) { if (isset($request['content'])) { $query->where('title', 'like', "%{$request['content']}%") ->orWhere('content', 'like', "%{$request['content']}%"); } }) ->where(function ($query) use ($request) { if (isset($request['created_at'])) { $time = explode('_', $request['created_at']); $query->whereBetween('post.created_at', $time); } }) ->orderBy('is_recommend', 'desc') ->paginate($perPage); } /** * 推荐精品文章 */ public function articleRecommend($request) { $article = $this->interestCircleArticle ->where('post_id', $request['post_id']) ->where('circle_id', $request['circle_id']) ->first(); if (!$article) { return Response::create([ 'message' => '获取精品文章信息失败', 'status_code' => 500 ]); } if ($article->is_recommend == 1) { $article->is_recommend = 0; } else { $article->is_recommend = 1; } DB::beginTransaction(); try { $article->save(); DB::commit(); return Response::create(); } catch (QueryException $exception) { DB::rollBack(); Log::debug('推荐精品文章:' . $request['post_id'] . '-' . $request['circle_id'] . $exception->getMessage()); return Response::create([ 'message' => '操作失败,请重试', 'error' => $exception->getMessage(), 'status_code' => 500 ]); } } /** * 精品文章从当前圈子移出 */ public function articleRemove($request) { $article = $this->interestCircleArticle ->where('post_id', $request['post_id']) ->where('circle_id', $request['circle_id']) ->first(); if (!$article) { return Response::create([ 'message' => '获取精品文章信息失败', 'status_code' => 500 ]); } DB::beginTransaction(); try { $article->delete(); $isRef = $this->interestCircleArticle->where('post_id', $request['post_id'])->exists(); if (!$isRef) { //todo 移出后需要检测该帖子在其他圈子有没有设为精品,如没有,则删除该帖子的精品标识 //$this->post->where('id',$request['post_id'])->update(); } DB::commit(); return Response::create(); } catch (QueryException $exception) { DB::rollBack(); Log::debug('移出精品文章:' . $request['post_id'] . '-' . $request['circle_id'] . $exception->getMessage()); return Response::create([ 'message' => '操作失败,请重试', 'error' => $exception->getMessage(), 'status_code' => 500 ]); } } }