SystemRouteCateServices.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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(array $path)
  35. {
  36. $pathAttr = explode('/', $path);
  37. $pathData = [];
  38. foreach ($pathAttr as $item) {
  39. if (!$item) {
  40. $pathData[] = $item;
  41. }
  42. }
  43. return $pathAttr;
  44. }
  45. /**
  46. * @param array $path
  47. * @param int $id
  48. * @return string
  49. * @author 等风来
  50. * @email 136327134@qq.com
  51. * @date 2023/4/6
  52. */
  53. public function setPathValue(array $path, int $id)
  54. {
  55. return ($path ? '/' . implode('/', $path) : '') . '/' . $id . '/';
  56. }
  57. /**
  58. * @param string $appName
  59. * @return array
  60. * @author 等风来
  61. * @email 136327134@qq.com
  62. * @date 2023/4/6
  63. */
  64. public function getAllList(string $appName = 'outapi', string $field = '*', string $order = '')
  65. {
  66. $list = $this->dao->selectList(['app_name' => $appName], $field, 0, 0, $order)->toArray();
  67. return get_tree_children($list);
  68. }
  69. /**
  70. * @param int $id
  71. * @param string $appName
  72. * @return array
  73. * @author 等风来
  74. * @email 136327134@qq.com
  75. * @date 2023/4/6
  76. */
  77. public function getFrom(int $id = 0, string $appName = 'outapi')
  78. {
  79. $url = '/system/route_cate';
  80. $cateInfo = [];
  81. $path = [];
  82. if ($id) {
  83. $cateInfo = $this->dao->get($id);
  84. $cateInfo = $cateInfo ? $cateInfo->toArray() : [];
  85. $url .= '/' . $id;
  86. $path = explode('/', $cateInfo['path']);
  87. $newPath = [];
  88. foreach ($path as $item) {
  89. if ($item) {
  90. $newPath[] = $item;
  91. }
  92. }
  93. $path = $newPath;
  94. }
  95. $options = $this->dao->selectList(['app_name' => $appName], 'name as label,id as value,id,pid')->toArray();
  96. $rule = [
  97. // FormBuilder::cascader('path', '上级分类', $path)->data(get_tree_children($options)),
  98. FormBuilder::input('name', '分类名称', $cateInfo['name'] ?? '')->required(),
  99. FormBuilder::number('sort', '排序', (int)($cateInfo['sort'] ?? 0)),
  100. FormBuilder::hidden('app_name', $appName)
  101. ];
  102. return create_form($id ? '修改分类' : '添加分类', $rule, $url, $id ? 'PUT' : 'POST');
  103. }
  104. }