ArticleController.php 6.3 KB

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