[BD]树形用户列表权限

66 阅读1分钟

原本的树形用户将所有用户都显示出来, 这是错误的, 应该只显示下级用户

1.调用的地方(后续可以将方法放在父级控制器中) app\admin\controller\Api.php

    // 获取树形用户列表
    public function getUserTree()
    {

        $model = new AdminUser();
        $data = $model->getUserTree($this->auth->isSuperAdmin(), $this->auth->getAdmin()->id);
        
        return $this->success('', [
            'list'   => $data,
            'total'  => count($data),
        ]);
    }

2.方法定义 app\admin\model\AdminUser.php

    // 获取树形用户
    public function getUserTree($isSuper, $uid)
    {
        if ($isSuper) {
            $res = $this->field(['id','username label','pid'])->where("pid", 0)->select()->toArray();
        } else {
            $res = $this->field(['id','username label','pid'])->where("id", $uid)->select()->toArray();
        }
        $list = [];
        foreach ($res as $item) {
            $list[] = [
                'id'    => $item['id'],
                'pid'   => $item['pid'],
                'label' => $item['label'],
            ];
        }
        foreach ($list as &$user) {
            $child = $this->getChildren($user['id']);
            $user['children'] = $child;
        }
        return $list;
    }

    // 无限极方法
    public function getChildren($id)
    {
        $children = $this->hidden(['group_arr'])->field(['id','username label','pid'])->where('pid', $id)->select()->toArray();
        $list = [];
        foreach ($children as $item) {
            $list[] = [
                'id'    => $item['id'],
                'pid'   => $item['pid'],
                'label' => $item['label'],
            ];
        }
        foreach ($list as &$child) {
            $child['children'] = $this->getChildren($child['id']);
        }
        return $list;
    }

待完成 需要修改获取列表数据的权限 ygv1 userAdmin 模型中

 // 无限极方法,获取所有下级用户
    public function getChildArr($id)
    {
        $subordinateIds = [];
        $subordinates = $this->where('pid', $id)->select();
        foreach ($subordinates as $subordinate) {
            $subordinateIds[] = $subordinate->id;
            $subordinateIds = array_merge($subordinateIds, $this->getChildArr($subordinate->id));
        }
        return $subordinateIds;
    }

    // 获取树形用户
    public function getUserTree($isSuper, $uid)
    {
        $field = ['id','nickname label','pid'];
        if ($isSuper) {
            $res = $this->field($field)->where("pid", 0)->select()->toArray();
        } else {
            $res = $this->field($field)->where("id", $uid)->select()->toArray();
        }
        $list = [];
        foreach ($res as $item) {
            $list[] = [
                'id'    => $item['id'],
                'pid'   => $item['pid'],
                'label' => $item['label'],
            ];
        }
        foreach ($list as &$user) {
            $child = $this->getChildren($user['id']);
            $user['children'] = $child;
        }
        return $list;
    }

    // 无限极方法
    public function getChildren($id)
    {
        $children = $this->hidden(['group_arr'])->field(['id','nickname label','pid'])->where('pid', $id)->select()->toArray();
        $list = [];
        foreach ($children as $item) {
            $list[] = [
                'id'    => $item['id'],
                'pid'   => $item['pid'],
                'label' => $item['label'],
            ];
        }
        foreach ($list as &$child) {
            $child['children'] = $this->getChildren($child['id']);
        }
        return $list;
    }