ListController.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. $category_child = [];
  35. foreach ($category as $v) {
  36. if ($v['id'] == $id) {
  37. $category_self = $v;
  38. } else {
  39. $category_child[] = $v;
  40. }
  41. }
  42. $this->assign('category_self', $category_self);
  43. $this->assign('category_child', $category_child);
  44. //导航
  45. $path = explode('-', $category_self['path']);
  46. array_shift($path);
  47. $path_list = $portalCategoryModel->where('id', 'in', $path)->select();
  48. $this->assign('path_list', $path_list);
  49. //文章列表
  50. $keyword = $this->request->param('keyword', '');
  51. $condition = [];
  52. if (!empty($keyword)) {
  53. $condition[] = ['post_title', 'like', "%{$keyword}%"];
  54. }
  55. $portalModel = new PortalPostModel();
  56. $list = $portalModel->getListByCategory($id, 10, $condition);
  57. $this->assign('keyword',$keyword);
  58. $this->assign('list', $list);
  59. $this->assign('page', $list->render());
  60. $listTpl = empty($category_self['list_tpl']) ? 'list' : $category_self['list_tpl'];
  61. return $this->fetch('/' . $listTpl);
  62. }
  63. }