TreeService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace Ycbl\AdminAuth\Service;
  3. use Hyperf\Utils\Context;
  4. class TreeService
  5. {
  6. protected $pidName = 'pid';
  7. /**
  8. * @var array
  9. */
  10. private $tree;
  11. public function init(array $arr)
  12. {
  13. $this->tree = $arr;
  14. return $this;
  15. }
  16. public function getArr()
  17. {
  18. return $this->tree;
  19. }
  20. /**
  21. * 根据ID获取子集
  22. * @param $my_id
  23. * @return array
  24. */
  25. public function getChild($my_id)
  26. {
  27. $newArr = [];
  28. foreach ($this->getArr() as $value) {
  29. if (!isset($value['id'])) {
  30. continue;
  31. }
  32. if ($value[$this->pidName] == $my_id) {
  33. $newArr[$value['id']] = $value;
  34. }
  35. }
  36. return $newArr;
  37. }
  38. /**
  39. * 读取指定节点的所有子节点
  40. * @param $my_id
  41. * @param false $withSelf
  42. * @return array
  43. */
  44. public function getChildren($my_id, $withSelf = false)
  45. {
  46. $newArr = [];
  47. foreach ($this->getArr() as $value) {
  48. //数组不包含ID就不会有子级
  49. if (!isset($value['id'])) {
  50. continue;
  51. }
  52. if ($value[$this->pidName] == $my_id) {
  53. $newArr[] = $value;
  54. //递归获取子级数组并合并
  55. $newArr = array_merge($newArr, $this->getChildren($value['id']));
  56. } elseif ($withSelf && $value['id'] == $my_id) {
  57. $newArr[] = $value;
  58. }
  59. }
  60. return $newArr;
  61. }
  62. /**
  63. * 读取指定节点的所有孩子节点ID
  64. * @param $my_id
  65. * @param boolean $withSelf 是否包含自身
  66. * @return array
  67. */
  68. public function getChildrenIds($my_id, $withSelf = false)
  69. {
  70. $childrenList = $this->getChildren($my_id, $withSelf);
  71. $childrenIds = [];
  72. foreach ($childrenList as $k => $v) {
  73. $childrenIds[] = $v['id'];
  74. }
  75. return $childrenIds;
  76. }
  77. /**
  78. * 得到当前位置父辈数组
  79. * @param int
  80. * @return array
  81. */
  82. public function getParent($my_id)
  83. {
  84. $pid = 0;
  85. $newArr = [];
  86. foreach ($this->getArr() as $value) {
  87. //没有id 不会是上级节点
  88. if (!isset($value['id'])) {
  89. continue;
  90. }
  91. //查找到自己的位置
  92. if ($value['id'] == $my_id) {
  93. //获取到PID
  94. $pid = $value[$this->pidName];
  95. break;
  96. }
  97. }
  98. //如果pid存在
  99. if ($pid) {
  100. foreach ($this->getArr() as $value) {
  101. //获取上级数组
  102. if ($value['id'] == $pid) {
  103. $newArr[] = $value;
  104. break;
  105. }
  106. }
  107. }
  108. return $newArr;
  109. }
  110. /**
  111. * 得到当前位置所有父辈数组
  112. * @param $my_id
  113. * @param bool $withSelf 是否包含自己
  114. * @return array
  115. */
  116. public function getParents($my_id, $withSelf = false)
  117. {
  118. $pid = 0;
  119. $newArr = [];
  120. foreach ($this->getArr() as $value) {
  121. //没有id 不会是上级节点
  122. if (!isset($value['id'])) {
  123. continue;
  124. }
  125. //查找到自己的位置
  126. if ($value['id'] == $my_id) {
  127. //如果包含自己则添加自身
  128. if ($withSelf) {
  129. $newArr[] = $value;
  130. }
  131. $pid = $value[$this->pidName];
  132. break;
  133. }
  134. }
  135. //如果PID存在
  136. if ($pid) {
  137. //递归获取上级数组
  138. $arr = $this->getParents($pid, true);
  139. $newArr = array_merge($arr, $newArr);
  140. }
  141. return $newArr;
  142. }
  143. /**
  144. * 读取指定节点所有父类节点ID
  145. * @param $my_id
  146. * @param boolean $withSelf
  147. * @return array
  148. */
  149. public function getParentsIds($my_id, $withSelf = false)
  150. {
  151. $parentList = $this->getParents($my_id, $withSelf);
  152. $parentsIds = [];
  153. foreach ($parentList as $k => $v) {
  154. $parentsIds[] = $v['id'];
  155. }
  156. return $parentsIds;
  157. }
  158. /**
  159. * 获取树状列表
  160. * @param $my_id
  161. * @return array
  162. */
  163. public function getTreeArray($my_id)
  164. {
  165. $childes = $this->getChild($my_id);
  166. $num = 0;
  167. $data = [];
  168. if ($childes) {
  169. foreach ($childes as $id => $value) {
  170. $data[$num] = $value;
  171. $data[$num]['childList'] = $this->getTreeArray($id);
  172. $num++;
  173. }
  174. }
  175. return $data;
  176. }
  177. /**
  178. * 转换树状列表为一维数组
  179. * @param array $data
  180. * @param string $field
  181. * @return array
  182. */
  183. public function getTreeList($data = [], $field = 'name')
  184. {
  185. $arr = [];
  186. foreach ($data as $key => $value) {
  187. $child_list = isset($value['childList']) ? $value['childList'] : [];
  188. unset($value['childList']);
  189. $value['hasChild'] = $child_list ? 1 : 0;
  190. if ($value['id']) {
  191. $arr[] = $value;
  192. }
  193. if ($child_list) {
  194. $arr = array_merge($arr, $this->getTreeList($child_list, $field));
  195. }
  196. }
  197. return $arr;
  198. }
  199. }