SystemRouteCateServices.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * +----------------------------------------------------------------------
  4. * | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  5. * +----------------------------------------------------------------------
  6. * | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  7. * +----------------------------------------------------------------------
  8. * | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  9. * +----------------------------------------------------------------------
  10. * | Author: CRMEB Team <admin@crmeb.com>
  11. * +----------------------------------------------------------------------
  12. */
  13. namespace app\services\system;
  14. use app\dao\system\SystemRouteCateDao;
  15. use app\services\BaseServices;
  16. use crmeb\services\FormBuilder;
  17. /**
  18. * Class SystemRouteCateServices
  19. * @author 等风来
  20. * @email 136327134@qq.com
  21. * @date 2023/4/6
  22. * @package app\services\system
  23. */
  24. class SystemRouteCateServices extends BaseServices
  25. {
  26. /**
  27. * SystemRouteCateServices constructor.
  28. * @param SystemRouteCateDao $dao
  29. */
  30. public function __construct(SystemRouteCateDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. public function getPathValue(int $pid)
  35. {
  36. if (!$pid) {
  37. return [];
  38. }
  39. $path = $this->dao->value($pid, 'path');
  40. $pathAttr = explode('/', $path);
  41. $pathData = [];
  42. foreach ($pathAttr as $item) {
  43. if (!$item) {
  44. $pathData[] = $item;
  45. }
  46. }
  47. return $pathAttr;
  48. }
  49. /**
  50. * @param array $path
  51. * @param int $id
  52. * @return string
  53. * @author 等风来
  54. * @email 136327134@qq.com
  55. * @date 2023/4/6
  56. */
  57. public function setPathValue(array $path, int $id)
  58. {
  59. return ($path ? '/' . implode('/', $path) : '') . '/' . $id . '/';
  60. }
  61. /**
  62. * @param string $appName
  63. * @return array
  64. * @author 等风来
  65. * @email 136327134@qq.com
  66. * @date 2023/4/6
  67. */
  68. public function getAllList(string $appName = 'outapi', string $field = '*')
  69. {
  70. $list = $this->dao->selectList(['app_name' => $appName], $field)->toArray();
  71. return get_tree_children($list);
  72. }
  73. /**
  74. * @param int $id
  75. * @param string $appName
  76. * @return array
  77. * @author 等风来
  78. * @email 136327134@qq.com
  79. * @date 2023/4/6
  80. */
  81. public function getFrom(int $id = 0, string $appName = 'outapi')
  82. {
  83. $url = '/system/route_cate';
  84. $cateInfo = [];
  85. if ($id) {
  86. $cateInfo = $this->dao->get($id);
  87. $cateInfo = $cateInfo ? $cateInfo->toArray() : [];
  88. $url .= '/' . $id;
  89. }
  90. $options = $this->dao->selectList(['app_name' => $appName], 'name as label,id as value')->toArray();
  91. $rule = [
  92. FormBuilder::select('pid', '上级分类', (int)($cateInfo['pid'] ?? 0))->options($options),
  93. FormBuilder::input('name', '分类名称', $cateInfo['name'] ?? '')->required(),
  94. FormBuilder::number('sort', '排序', (int)($cateInfo['sort'] ?? 0)),
  95. FormBuilder::hidden('app_name', $appName)
  96. ];
  97. return create_form($id ? '修改分类' : '添加分类', $rule, $url, $id ? 'PUT' : 'POST');
  98. }
  99. }