无限子级,适用于上级父级不为0

 1     //获取合伙人下级树
 2     public function getPartnerTree()
 3     {
 4 
 5         //合伙人
 6         $partner = new PartnerUserModel;
 7         
 8         $user_id = input('post.user_id');
 9  
10         $partner_id = $partner->where('user_id',$user_id)->value('user_id');
11         if(!$partner_id){
12             return json(['code'=>-1,'msg'=>'合伙人不存在']);
13         }
14         $list = UserModel::where("user_id",$partner_id)->find();
15 
16         $list["children"] = $this->getTree($partner_id);
17 
18         return $this->renderSuccess(compact('list'));
19     }
20 
21     public  function getTree($partner_id)
22     {
23         //推荐人
24         $referee = new RefereeModel;
25         $list = $referee->where(["dealer_id"=>["=",$partner_id],"level"=>1])->select()->toArray();
26         $tree = [];
27         if(!empty($list)){
28             foreach ($list as $key => $val) {
29                 $tree[$key]= UserModel::where("user_id",$val['user_id'])->find();
30                 $tree[$key]['children'] =$this->getTree($val['user_id']);
31             }
32         }
33         return $tree; 
34     }

 

posted @ 2023-09-11 16:44  糖粿  阅读(17)  评论(0编辑  收藏  举报