AdverPage.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\admin;
  4. use think\Model;
  5. /**
  6. * @mixin \think\Model
  7. */
  8. class AdverPage extends Model
  9. {
  10. /**
  11. * 获取分类数据
  12. * @return array
  13. * @throws \think\db\exception\DataNotFoundException
  14. * @throws \think\db\exception\DbException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. */
  17. public function getArMenu($cate_name = '',$pid = '',$show = 0){
  18. $menus = $this
  19. ->when(!empty($cate_name),function ($query) use($cate_name){
  20. $query->whereLike('cate_name',"%{$cate_name}%");
  21. })
  22. ->when(!empty($pid),function ($query) use($pid){
  23. $query->where('pid',$pid);
  24. })
  25. ->order("sort","desc")
  26. ->select();
  27. $data = [];
  28. foreach ($menus as $item) {
  29. $data[] = $item->getData();
  30. }
  31. $menuAr = self::getTree($data);
  32. return $menuAr;
  33. }
  34. /**
  35. * 获取树型菜单
  36. * @param $data
  37. * @param int $pid
  38. * @param int $level
  39. * @return array
  40. */
  41. public static function getTree($data, $pid = 0, $level = 0)
  42. {
  43. $childs = self::getChild($data, $pid, $level);
  44. array_multisort(array_column($childs, 'sort'), SORT_DESC, $childs);
  45. foreach ($childs as $key => $navItem) {
  46. $resChild = self::getTree($data, $navItem['id']);
  47. if (null != $resChild) {
  48. $childs[$key]['children'] = $resChild;
  49. }
  50. }
  51. return $childs;
  52. }
  53. /**
  54. * 获取子菜单
  55. * @param $arr
  56. * @param $id
  57. * @param $lev
  58. * @return array
  59. */
  60. private static function getChild(&$arr, $id, $lev)
  61. {
  62. $child = [];
  63. foreach ($arr as $k => $value) {
  64. if ($value['pid'] == $id) {
  65. $value['level'] = $lev;
  66. $child[] = $value;
  67. }
  68. }
  69. return $child;
  70. }
  71. }