ListController.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 老猫 <thinkcmf@126.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\portal\controller;
  12. use app\portal\model\PortalPostModel;
  13. use cmf\controller\HomeBaseController;
  14. use app\portal\model\PortalCategoryModel;
  15. class ListController extends HomeBaseController
  16. {
  17. /***
  18. * 文章列表
  19. * @return mixed
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. * @throws \think\exception\DbException
  23. */
  24. public function index()
  25. {
  26. $this->getTopNav();
  27. //获取分类
  28. $id = $this->request->param('category_id', 0, 'intval');
  29. $portalCategoryModel = new PortalCategoryModel();
  30. $category = $portalCategoryModel->where(function ($query) use ($id) {
  31. $query->where('id', $id)->whereOr('parent_id', $id);
  32. })->where('status', 1)->select();
  33. $category_self = [
  34. 'id' => 0,
  35. 'name' => '文章列表',
  36. ];
  37. $category_child = [];
  38. foreach ($category as $v) {
  39. if ($v['id'] == $id) {
  40. $category_self = $v;
  41. } else {
  42. $category_child[] = $v;
  43. }
  44. }
  45. $this->assign('category_self', $category_self);
  46. $this->assign('category_child', $category_child);
  47. //导航
  48. $path_list = [];
  49. if (!empty($id)) {
  50. $path = explode('-', $category_self['path']);
  51. array_shift($path);
  52. $path_list = $portalCategoryModel->where('id', 'in', $path)->select();
  53. }
  54. $this->assign('path_list', $path_list);
  55. //文章列表
  56. $keyword = $this->request->param('keyword', '');
  57. $condition = [];
  58. if (!empty($keyword)) {
  59. $condition[] = ['post_title', 'like', "%{$keyword}%"];
  60. }
  61. $portalModel = new PortalPostModel();
  62. $list = $portalModel->getListByCategory($id, 10, $condition);
  63. foreach ($list as $v) {
  64. $v['post_content'] = strip_tags($v['post_content']);
  65. $v['update_time'] = date('Y-m-d H:i:s',$v['update_time']);
  66. }
  67. $this->assign('keyword',$keyword);
  68. $this->assign('list', $list);
  69. $this->assign('page', $list->render());
  70. $listTpl = empty($category_self['list_tpl']) ? 'list' : $category_self['list_tpl'];
  71. return $this->fetch('/' . $listTpl);
  72. }
  73. }