* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Http\Resources\CMS\ArticleResource; use App\Http\Resources\CMS\BannerResource; use App\Http\Resources\CMS\NavigationResource; use App\Repositories\Eloquent\CMS\ArticleRepositoryEloquent; use App\Repositories\Enums\Base\BannerPositionEnum; use App\Repositories\Enums\CMS\CategoryKeyEnum; use App\Repositories\Enums\CMS\IsNavigationEnum; use App\Repositories\Models\Base\Resource; use App\Repositories\Models\CMS\Article; use App\Repositories\Models\CMS\Banner; use App\Repositories\Models\CMS\Category; use App\Repositories\Models\CMS\Setting; use Illuminate\Http\Request; use Jiannei\Response\Laravel\Support\Facades\Response; class PageController extends Controller { /** * 首页 * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource * Author: Mead */ public function home() { $banners = $news = $activities = $miens = []; $banners = Banner::query()->status()->active()->where('position', BannerPositionEnum::HOME)->orderBy('sort')->get(); $banners = BannerResource::collection($banners); $keys = Category::query()->whereIn('key', [CategoryKeyEnum::NOTICES, CategoryKeyEnum::NEWS, CategoryKeyEnum::ACADEMIC, CategoryKeyEnum::LINK, CategoryKeyEnum::MIEN])->pluck('id', 'key')->toArray(); $ks = array_keys($keys); $news = []; if (in_array(CategoryKeyEnum::NEWS, $ks)) { $news = Article::query()->status()->where('category_id', $keys[CategoryKeyEnum::NEWS])->orderByDesc('sort')->orderByDesc('id')->limit(6)->get(); $news = ArticleResource::collection($news); } $notices = []; if (in_array(CategoryKeyEnum::NOTICES, $ks)) { $notices = Article::query()->status()->where('category_id', $keys[CategoryKeyEnum::NOTICES])->orderByDesc('sort')->orderByDesc('id')->limit(6)->get(); $notices = ArticleResource::collection($notices); } $academic = []; if (in_array(CategoryKeyEnum::ACADEMIC, $ks)) { $academic = Article::query()->status()->where('category_id', $keys[CategoryKeyEnum::ACADEMIC])->orderByDesc('sort')->orderByDesc('id')->limit(6)->get(); $academic = ArticleResource::collection($academic); } $mien = []; if (in_array(CategoryKeyEnum::MIEN, $ks)) { $mien = Article::query()->status()->where('category_id', $keys[CategoryKeyEnum::MIEN])->orderByDesc('sort')->orderByDesc('id')->limit(9)->get(); $mien = ArticleResource::collection($mien); } $link = []; if (in_array(CategoryKeyEnum::LINK, $ks)) { $link = Article::query()->status()->where('category_id', $keys[CategoryKeyEnum::LINK])->orderByDesc('sort')->orderByDesc('id')->limit(6)->get(); $link = ArticleResource::collection($link); } $navigations = Category::query()->where('is_nav', IsNavigationEnum::YES)->orderByDesc('sort')->orderByDesc('id')->get(); $navigations = NavigationResource::collection($navigations); return Response::success(compact('banners', 'news', 'notices', 'academic', 'mien', 'link', 'navigations')); } /** * 导航 * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource * Author: Mead */ public function navigations() { $navigations = Category::query()->status()->where('is_nav', IsNavigationEnum::YES)->orderByDesc('sort')->get(); $navigations = NavigationResource::collection($navigations); return Response::success($navigations); } /** * 新闻列表 * @param ArticleRepositoryEloquent $articleRepositoryEloquent * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource * Author: Mead */ public function articles(ArticleRepositoryEloquent $articleRepositoryEloquent) { $lists = $articleRepositoryEloquent->status()->paginate(request('per_page', self::PAGE_NUM)); $lists = ArticleResource::collection($lists); return Response::success($lists); } /** * 内容详情 * @param $id * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource * Author: Mead */ public function article($id) { $model = Article::query()->status()->find($id); if (!$model) { return Response::success(null); } $model->view_count++; $model->save(); $model = ArticleResource::make($model); return Response::success($model); } public function navigationArticle($id) { $model = Article::query()->status()->where('category_id', $id)->orderByDesc('sort')->orderByDesc('id')->first(); if (!$model) { return Response::success(null); } $model->view_count++; $model->save(); $model = ArticleResource::make($model); return Response::success($model); } /** * 配置参数 * @param Request $request * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource * Author: Mead */ public function settings(Request $request) { $keys = str2arr($request->get('keys')); $settings = Setting::query()->whereIn('key', $keys)->get(); $data = []; foreach ($settings as $setting) { switch ($setting->type) { case 0: $data[$setting['key']] = $setting->body; break; case 1: $data[$setting['key']] = Resource::query()->where('id', $setting->body)->select(['url', 'path'])->first(); break; } } return Response::success($data); } }