CategoryApi.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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\api;
  12. use app\portal\model\PortalCategoryModel;
  13. use think\db\Query;
  14. class CategoryApi
  15. {
  16. /**
  17. * 分类列表 用于模板设计
  18. * @param array $param
  19. * @return false|\PDOStatement|string|\think\Collection
  20. * @throws \think\db\exception\DataNotFoundException
  21. * @throws \think\db\exception\ModelNotFoundException
  22. * @throws \think\exception\DbException
  23. */
  24. public function index($param = [])
  25. {
  26. $portalCategoryModel = new PortalCategoryModel();
  27. $where = ['delete_time' => 0];
  28. //返回的数据必须是数据集或数组,item里必须包括id,name,如果想表示层级关系请加上 parent_id
  29. return $portalCategoryModel->where($where)
  30. ->where(function (Query $query) use ($param) {
  31. if (!empty($param['keyword'])) {
  32. $query->where('name', 'like', "%{$param['keyword']}%");
  33. }
  34. })->select();
  35. }
  36. /**
  37. * 分类列表 用于导航选择
  38. * @return array
  39. */
  40. public function nav()
  41. {
  42. $portalCategoryModel = new PortalCategoryModel();
  43. $where = ['delete_time' => 0];
  44. $categories = $portalCategoryModel->where($where)->select();
  45. $return = [
  46. //'name' => '文章分类',
  47. 'rule' => [
  48. 'action' => 'portal/List/index',
  49. 'param' => [
  50. 'id' => 'id'
  51. ]
  52. ],//url规则
  53. 'items' => $categories //每个子项item里必须包括id,name,如果想表示层级关系请加上 parent_id
  54. ];
  55. return $return;
  56. }
  57. }