json([ 'about_us'=>app_settings('app.article_about_us', ''), 'user_promotion_agreement'=> app_settings('app.article_user_promotion_agreement', ''), 'user_hide_agreement' => app_settings('app.article_user_hide_agreement', ''), ]); } /** * * * @param \Illuminate\Http\Request $request * @return void */ public function index(Request $request) { $cate = (string) $request->query('cate'); $key = (string) $request->query('key'); $categoryId = Arr::get([ 'help'=> app_settings('app.article_help'), 'agreement'=> app_settings('app.article_agreement'), 'health'=> app_settings('app.article_health'), ], $cate); $query = Article::query()->with(['likesInfo'=>function ($q) use ($request) { $user_id = $request->user()?->id; return $q->where('user_id', $user_id ?? 0); }])->where('is_show', 1); if ($categoryId) { $query->where('category_id', $categoryId); } if ($key) { $query->where('title', 'like', '%'.$key.'%'); } $query->orderBy('is_recommend', 'desc'); $query->orderBy('sort', 'desc'); $query->orderBy('created_at', 'desc'); $list = $query->simplePaginate(PaginatorHelper::resolvePerPage('per_page', 20, 50)); return ArticleSimpleResource::collection($list); } public function healthCategory(Request $request) { $key = (string) $request->query('key'); $categoryId = app_settings('app.article_health'); $query = ArticleCategory::query()->where('parent_id', $categoryId)->where('is_show', 1); if ($key) { $query->where('name', 'like', '%'.$key.'%'); } $query->orderBy('is_recommend', 'desc'); $query->orderBy('sort', 'desc'); $query->orderBy('created_at', 'desc'); $categories = $query->get(); return ArticleCategoryResource::collection($categories); } public function articleList($id, Request $request) { $key = (string) $request->query('key'); $categoryId = $id; $query = Article::query()->with(['likesInfo'=>function ($q) use ($request) { $user_id = $request->user()?->id; return $q->where('user_id', $user_id ?? 0); }])->where('is_show', 1); if ($categoryId) { $query->where('category_id', $categoryId); } if ($key) { $query->where('title', 'like', '%'.$key.'%'); } $query->orderBy('is_recommend', 'desc'); $query->orderBy('sort', 'desc'); $query->orderBy('created_at', 'desc'); $list = $query->simplePaginate(PaginatorHelper::resolvePerPage('per_page', 20, 50)); return ArticleSimpleResource::collection($list); } /** * 文章详情 * * @param [type] $id * @return void */ public function show($id, Request $request) { $article = Article::with(['likesInfo'=>function ($q) use ($request) { return $q->where('user_id', $request->user()->id); }])->where('is_show', 1)->findOrFail($id); return ArticleResource::make($article); } /** * 文章点赞 * * @param [type] $id * @param Request $request * @return void */ public function like($id, Request $request) { $article = Article::where('is_show', 1)->findOrFail($id); if (ArticleLikesLog::where('user_id', $request->user()->id)->where('article_id', $article->id)->exists()) { throw new BizException('您已点赞过这篇文章'); } try { DB::beginTransaction(); ArticleLikesLog::create([ 'article_id' => $article->id, 'user_id' => $request->user()->id, ]); $article->increment('likes'); DB::commit(); } catch (Throwable $th) { DB::rollBack(); report($th); throw new BizException('系统繁忙,请稍后再试'); } return response()->noContent(); } /** * 阅读加积分接口 * * @param [type] $id * @return void */ public function read($id, Request $request, PointsService $pointsService) { $article = Article::where('is_show', 1)->findOrFail($id); if ($article->points > 0) { if (ArticlePointsLog::where('user_id', $request->user()->id)->whereDate('created_at', now())->exists()) { throw new BizException('您今天已拿过积分了,请明天再来'); } try { DB::beginTransaction(); ArticlePointsLog::create([ 'user_id' => $request->user()->id, 'article_id' => $article->id, ]); $pointsService->sendPoints(PointsLog::TYPE_READ, $request->user(), $article->points, '阅读文章送积分'); DB::commit(); } catch (Throwable $th) { DB::rollBack(); report($th); throw new BizException('系统繁忙,请稍后再试'); } } return response()->json([ 'points'=>$article->points, ]); } }