field("id,pid")->select()->toArray(); $GetTeamMember = self::get_downline($user, $user_id); $count = 0; if ($GetTeamMember) { foreach ($GetTeamMember as $k => $v) { $count += 1; } } return $count; } public static function father($pid, $id) { $get_fu= self::get_fu($id); if ($get_fu){ $get_id=array_column($get_fu,"id"); $in_array=in_array($pid,$get_id); if ($in_array){ return true; }else{// 没有不存在查看一下下级 $user=User::field("id,pid")->cache(true,600)->select(); $get_xia=self::get_downline($user->toArray(),$id); $get_xia_id=array_column($get_xia,"id"); $in_arrays=in_array($pid,$get_xia_id); if ($in_arrays){ return true; }else{ return false; } } } } // 查找无限上级 public static function get_fu($id) { static $data=[]; if ($id){ $where["id"]=$id; $user=User::where($where)->field("id,pid")->cache(true,600)->find(); if ($user){ $data[]=$user->toArray(); if ($user->pid){ self::get_fu($user->pid); } } } return $data; } //获取用户的所有下级ID public static function get_downline($data, $mid, $level = 0) { $arr = array(); foreach ($data as $key => $v) { if ($v['pid'] == $mid) { //pid为0的是顶级分类 $v['level'] = $level + 1; $arr[] = $v; $arr = array_merge($arr, self::get_downline($data, $v['id'], $level + 1)); } } return $arr; } }