SystemRouteCateServices.php 3.4 KB

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