NewsList.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Web\Components\Blog;
  3. use App\Exceptions\ApiException;
  4. use App\Web\Facades\BlogRenderFacade;
  5. use App\Web\Facades\WebFacade;
  6. use App\Web\Models\BlogPlateModel;
  7. use App\Web\Services\WebService;
  8. use Illuminate\View\Component;
  9. class NewsList extends Component
  10. {
  11. protected $page; //这是参数
  12. protected $pageSize;
  13. protected $viewFile; //视图地址
  14. protected $typeId;
  15. protected $tagId;
  16. protected $keyword;
  17. protected $router_path;
  18. protected $echo_content;
  19. protected $plate_id;
  20. protected $sort;
  21. /**
  22. * @throws ApiException
  23. */
  24. public function __construct($viewFile = '', $params = [], $keyword = '',$routerPath='', $sort=[], $typeId = 0, $tagId = 0, $page = 1, $pageSize = 10,$plateId=BlogPlateModel::NEWS,$echoContent = false)
  25. {
  26. if (empty($viewFile)) {
  27. throw new ApiException('common.page_none', '您访问的地址不存在~');
  28. }
  29. $this->keyword = $params['keyword'] ?? $keyword;
  30. $this->page = $params['page'] ?? $page;
  31. $this->pageSize = $params['page_size'] ?? $pageSize;
  32. $this->typeId = $params['type_id'] ?? $typeId;
  33. $this->tagId = $params['tag_id'] ?? $tagId;
  34. $this->router_path = $params['router_path'] ?? $routerPath;
  35. $this->plate_id = $params['plate_id'] ?? $plateId;
  36. if (empty($this->plate_id)) {
  37. $this->plate_id = BlogPlateModel::NEWS;
  38. }
  39. if (empty($params['sort'])) {
  40. $this->sort = ['pub_date'];
  41. } else {
  42. $this->sort = $params['sort'];
  43. }
  44. $this->echo_content = $params['echo_content'] ?? $echoContent;
  45. $this->viewFile = $viewFile;
  46. }
  47. /**
  48. * 资讯列表组件
  49. */
  50. public function render()
  51. {
  52. $retData = [
  53. 'recent_news_list' => []
  54. ];
  55. $retData['news_list'] = BlogRenderFacade::getPublishBlogList(
  56. $this->plate_id,
  57. $this->keyword,
  58. $this->page,
  59. $this->pageSize,
  60. $this->sort,
  61. $this->typeId,
  62. $this->tagId,
  63. null,
  64. $this->echo_content
  65. );
  66. if (!empty($this->typeId)) {
  67. $retData['blog_type_info'] = BlogRenderFacade::getBlogTypeInfo($this->typeId);
  68. // dd($retData);
  69. }
  70. $retData['type_items'] = BlogRenderFacade::getPublishedTypeList($this->plate_id);
  71. $retData['tag_items'] = BlogRenderFacade::getPublishedTagList($this->plate_id);
  72. if (!empty($this->router_path)) {
  73. $pageContent = WebFacade::getStaticPageInfo(['route_path' => $this->router_path]);
  74. $layoutContent = WebFacade::getStaticPageInfo(['route_path' => WebService::LAYOUTS_ROUTE]);
  75. if (!empty($layoutContent['page_content'])) {
  76. foreach ($layoutContent['page_content'] as $key => $content_item) {
  77. if (empty($pageContent['page_content'][$key])) {
  78. $pageContent['page_content'][$key] = $content_item;
  79. }
  80. }
  81. }
  82. $retData['page_content'] = $pageContent['page_content'];
  83. }
  84. $retData['menu_data']=WebFacade::getWebMenu();
  85. return view($this->viewFile, $retData);
  86. }
  87. }