PageController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Http\Controllers\Api;
  11. use App\Http\Controllers\Controller;
  12. use App\Http\Resources\CMS\ArticleResource;
  13. use App\Http\Resources\CMS\BannerResource;
  14. use App\Http\Resources\CMS\NavigationResource;
  15. use App\Repositories\Eloquent\CMS\ArticleRepositoryEloquent;
  16. use App\Repositories\Enums\Base\BannerPositionEnum;
  17. use App\Repositories\Enums\CMS\CategoryKeyEnum;
  18. use App\Repositories\Enums\CMS\IsNavigationEnum;
  19. use App\Repositories\Models\Base\Resource;
  20. use App\Repositories\Models\CMS\Article;
  21. use App\Repositories\Models\CMS\Banner;
  22. use App\Repositories\Models\CMS\Category;
  23. use App\Repositories\Models\CMS\Setting;
  24. use Illuminate\Http\Request;
  25. use Jiannei\Response\Laravel\Support\Facades\Response;
  26. class PageController extends Controller
  27. {
  28. /**
  29. * 首页
  30. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  31. * Author: Mead
  32. */
  33. public function home()
  34. {
  35. $banners = $news = $activities = $miens = [];
  36. $banners = Banner::query()->status()->active()->where('position', BannerPositionEnum::HOME)->orderBy('sort')->get();
  37. $banners = BannerResource::collection($banners);
  38. $keys = Category::query()->whereIn('key', [CategoryKeyEnum::NOTICES, CategoryKeyEnum::NEWS, CategoryKeyEnum::ACADEMIC, CategoryKeyEnum::LINK, CategoryKeyEnum::MIEN])->pluck('id', 'key')->toArray();
  39. $ks = array_keys($keys);
  40. $news = [];
  41. if (in_array(CategoryKeyEnum::NEWS, $ks)) {
  42. $news = Article::query()->status()->where('category_id', $keys[CategoryKeyEnum::NEWS])->orderByDesc('sort')->orderByDesc('id')->limit(6)->get();
  43. $news = ArticleResource::collection($news);
  44. }
  45. $notices = [];
  46. if (in_array(CategoryKeyEnum::NOTICES, $ks)) {
  47. $notices = Article::query()->status()->where('category_id', $keys[CategoryKeyEnum::NOTICES])->orderByDesc('sort')->orderByDesc('id')->limit(6)->get();
  48. $notices = ArticleResource::collection($notices);
  49. }
  50. $academic = [];
  51. if (in_array(CategoryKeyEnum::ACADEMIC, $ks)) {
  52. $academic = Article::query()->status()->where('category_id', $keys[CategoryKeyEnum::ACADEMIC])->orderByDesc('sort')->orderByDesc('id')->limit(6)->get();
  53. $academic = ArticleResource::collection($academic);
  54. }
  55. $mien = [];
  56. if (in_array(CategoryKeyEnum::MIEN, $ks)) {
  57. $mien = Article::query()->status()->where('category_id', $keys[CategoryKeyEnum::MIEN])->orderByDesc('sort')->orderByDesc('id')->limit(9)->get();
  58. $mien = ArticleResource::collection($mien);
  59. }
  60. $link = [];
  61. if (in_array(CategoryKeyEnum::LINK, $ks)) {
  62. $link = Article::query()->status()->where('category_id', $keys[CategoryKeyEnum::LINK])->orderByDesc('sort')->orderByDesc('id')->limit(6)->get();
  63. $link = ArticleResource::collection($link);
  64. }
  65. $navigations = Category::query()->where('is_nav', IsNavigationEnum::YES)->orderByDesc('sort')->orderByDesc('id')->get();
  66. $navigations = NavigationResource::collection($navigations);
  67. return Response::success(compact('banners', 'news', 'notices', 'academic', 'mien', 'link', 'navigations'));
  68. }
  69. /**
  70. * 导航
  71. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  72. * Author: Mead
  73. */
  74. public function navigations()
  75. {
  76. $navigations = Category::query()->status()->where('is_nav', IsNavigationEnum::YES)->orderByDesc('sort')->get();
  77. $navigations = NavigationResource::collection($navigations);
  78. return Response::success($navigations);
  79. }
  80. /**
  81. * 新闻列表
  82. * @param ArticleRepositoryEloquent $articleRepositoryEloquent
  83. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  84. * Author: Mead
  85. */
  86. public function articles(ArticleRepositoryEloquent $articleRepositoryEloquent)
  87. {
  88. $lists = $articleRepositoryEloquent->status()->paginate(request('per_page', self::PAGE_NUM));
  89. $lists = ArticleResource::collection($lists);
  90. return Response::success($lists);
  91. }
  92. /**
  93. * 内容详情
  94. * @param $id
  95. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  96. * Author: Mead
  97. */
  98. public function article($id)
  99. {
  100. $model = Article::query()->status()->find($id);
  101. if (!$model) {
  102. return Response::success(null);
  103. }
  104. $model->view_count++;
  105. $model->save();
  106. $model = ArticleResource::make($model);
  107. return Response::success($model);
  108. }
  109. public function navigationArticle($id)
  110. {
  111. $model = Article::query()->status()->where('category_id', $id)->orderByDesc('sort')->orderByDesc('id')->first();
  112. if (!$model) {
  113. return Response::success(null);
  114. }
  115. $model->view_count++;
  116. $model->save();
  117. $model = ArticleResource::make($model);
  118. return Response::success($model);
  119. }
  120. /**
  121. * 配置参数
  122. * @param Request $request
  123. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\JsonResource
  124. * Author: Mead
  125. */
  126. public function settings(Request $request)
  127. {
  128. $keys = str2arr($request->get('keys'));
  129. $settings = Setting::query()->whereIn('key', $keys)->get();
  130. $data = [];
  131. foreach ($settings as $setting) {
  132. switch ($setting->type) {
  133. case 0:
  134. $data[$setting['key']] = $setting->body;
  135. break;
  136. case 1:
  137. $data[$setting['key']] = Resource::query()->where('id', $setting->body)->select(['url', 'path'])->first();
  138. break;
  139. }
  140. }
  141. return Response::success($data);
  142. }
  143. }