PageCategoryServices.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\diy;
  13. use app\services\BaseServices;
  14. use app\dao\diy\PageCategoryDao;
  15. use crmeb\services\CacheService;
  16. /**
  17. * 链接分组
  18. * Class PageCategoryServices
  19. * @package app\services\diy
  20. * @mixin PageCategoryDao
  21. */
  22. class PageCategoryServices extends BaseServices
  23. {
  24. protected $tree_page_category_key = 'tree_page_categroy';
  25. /**
  26. * PageCategoryServices constructor.
  27. * @param PageCategoryDao $dao
  28. */
  29. public function __construct(PageCategoryDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取分类列表
  35. * @return bool|mixed|null
  36. */
  37. public function getCategroyList()
  38. {
  39. return CacheService::get($this->tree_page_category_key, function () {
  40. return $this->getSonCategoryList();
  41. }, 86400) ?: [];
  42. }
  43. /**
  44. * tree分类列表
  45. * @param int $pid
  46. * @param string $parent_name
  47. * @return array
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. public function getSonCategoryList($pid = 0)
  53. {
  54. $list = $this->dao->getList(['pid' => $pid], 'id,pid,type,name');
  55. $arr = [];
  56. if ($list) {
  57. foreach ($list as $item) {
  58. $item['title'] = $item['name'];
  59. $item['expand'] = true;
  60. $item['children'] = $this->getSonCategoryList($item['id']);
  61. $arr [] = $item;
  62. }
  63. }
  64. return $arr;
  65. }
  66. }