ArticleController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Http\Controllers\Web\Content;
  3. use App\Http\Controllers\Web\WebBaseController;
  4. use App\Services\Content\ArticleService;
  5. use App\Services\Content\ArticleCategoryService;
  6. use App\Services\Content\AdService;
  7. use App\Services\Content\NavigationService;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Collection;
  11. class ArticleController extends WebBaseController
  12. {
  13. protected $articleService;
  14. protected $articleCategoryService;
  15. protected $adService;
  16. protected $navigationService;
  17. /**
  18. * ArticleController constructor.
  19. * @param $articleService
  20. * @param $articleCategoryService
  21. */
  22. public function __construct(ArticleService $articleService, ArticleCategoryService $articleCategoryService, AdService $adService, NavigationService $navigationService)
  23. {
  24. $this->articleService = $articleService;
  25. $this->articleCategoryService = $articleCategoryService;
  26. $this->adService = $adService;
  27. $this->navigationService = $navigationService;
  28. }
  29. public function index(Request $request, $id = '57', $num = '8', $focus = '3', $recommend = '4', $other_num = '5')
  30. {
  31. $key = $request->input('key');
  32. $categorys = $this->articleCategoryService->getChildCategorys($id, $num);
  33. if (!$categorys->isEmpty()) {
  34. $search_id = $categorys[0]->id;
  35. $search_where = [];
  36. $search_where['id'] = $search_id;
  37. if ($id != '1') {
  38. $search_where['parent_id'] = $id;
  39. }
  40. if ($num != '8') {
  41. $search_where['num'] = $num;
  42. }
  43. if ($focus != '3') {
  44. $search_where['focus'] = $focus;
  45. }
  46. if ($recommend != '4') {
  47. $search_where['recommend'] = $recommend;
  48. }
  49. if ($other_num != '5') {
  50. $search_where['other_num'] = $other_num;
  51. }
  52. return redirect()->route('news.list', $search_where);
  53. } else {
  54. $rst = $this->articleService->list($key, '', 10);
  55. }
  56. $focus_articles = $this->articleService->getPropertyArticles($focus, $other_num); //热门资讯
  57. $recommend_articles = $this->articleService->getPropertyArticles($recommend, $other_num); //推荐资讯
  58. //顶部广告位
  59. $ad_data = [
  60. 'theme' => 'default',
  61. 'org' => 'Home',
  62. 'alias' => 'AIX_news_index_top',
  63. 'num' => '1',
  64. ];
  65. $top_ads = $this->adService->getAds($ad_data);
  66. $return_data = [
  67. 'articles' => $rst,
  68. 'categorys' => $categorys,
  69. 'focus_articles' => $focus_articles,
  70. 'recommend_articles' => $recommend_articles,
  71. 'key' => $key,
  72. 'top_ads' => $top_ads,
  73. ];
  74. return view('app.content.article.index', $return_data);
  75. }
  76. public function list(Request $request, $id, $parent_id = '1', $num = '8', $focus = '3', $recommend = '4', $other_num = '5')
  77. {
  78. $key = $request->input('key');
  79. $info = $this->articleCategoryService->getInfo($id);
  80. $lists = $this->articleService->list($key, $id, '10');
  81. $categorys = $this->articleCategoryService->getChildCategorys($info['parent_id'], $num);
  82. $categorys_child = $this->articleCategoryService->getChildCategorys($id, 10);
  83. $focus_articles = $this->articleService->getPropertyArticles($focus, $other_num); //热门资讯
  84. $recommend_articles = $this->articleService->getPropertyArticles($recommend, $other_num); //推荐资讯
  85. $bread = $this->articleCategoryService->getBread($info['id']);
  86. //获取后台职场资讯名称
  87. $where = [
  88. 'alias' => 'AIX_top',
  89. 'pagealias' => 'AIX_news',
  90. 'display' => '1',
  91. ];
  92. $topNav = $this->navigationService->getTopNav($where);
  93. $now_cate = '';
  94. $article_cate = [];
  95. foreach ($categorys as $k => $v) {
  96. if ($v->id == $id) {
  97. $now_cate = $v->category_name;
  98. $article_cate = $v;
  99. }
  100. }
  101. $this->putSeoData('article_cate', $article_cate);
  102. $return_data = [
  103. 'articles' => $lists,
  104. 'categorys' => $categorys,
  105. 'focus_articles' => $focus_articles,
  106. 'recommend_articles' => $recommend_articles,
  107. 'key' => $key,
  108. 'type_id' => $id,
  109. 'top_nav' => $topNav,
  110. 'now_cate' => $now_cate,
  111. 'categorys_child' => $categorys_child,
  112. 'bread' => $bread,
  113. ];
  114. return view('app.content.article.list', $return_data);
  115. }
  116. public function show($id, $parent_id = '1', $num = '8', $focus = '3', $recommend = '4', $other_num = '5')
  117. {
  118. $categorys = $this->articleCategoryService->getChildCategorys($parent_id, $num);
  119. $focus_articles = $this->articleService->getPropertyArticles($focus, $other_num); //热门资讯
  120. $recommend_articles = $this->articleService->getPropertyArticles($recommend, $other_num); //推荐资讯
  121. $article_info = $this->articleService->getArticleInfo($id);
  122. if (!$article_info) {
  123. $back_url = \Illuminate\Support\Facades\URL::previous();
  124. return $this->showMessage('资讯不存在', $back_url, true, '上一页', '3');
  125. }
  126. $this->putSeoData('article', $article_info);
  127. $return_data = [
  128. 'categorys' => $categorys,
  129. 'focus_articles' => $focus_articles,
  130. 'recommend_articles' => $recommend_articles,
  131. 'type_id' => $id,
  132. 'info' => $article_info,
  133. 'parent_id' => $parent_id,
  134. ];
  135. return view('app.content.article.show', $return_data);
  136. }
  137. public function click($id)
  138. {
  139. $rst = $this->articleService->incrementData(['id' => $id], 1, 'click');
  140. $data = ['status' => 0];
  141. if ($rst) {
  142. $data = ['status' => 1];
  143. }
  144. return response()->json($data);
  145. }
  146. }